jammin.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. import sys
  2. from _pi_cffi import ffi, lib
  3. from pprint import pprint
  4. def get_buffer(buffer, bsize):
  5. string = b""
  6. for x in range(bsize):
  7. string += buffer[x]
  8. # return string.decode('cp437').encode('utf-8')
  9. return string.decode("cp437")
  10. # return string.decode('utf-8')
  11. # return string
  12. def get_bytes(buffer, bsize):
  13. string = b""
  14. for x in range(bsize):
  15. string += buffer[x]
  16. # return string.decode('cp437').encode('utf-8')
  17. return string
  18. def get_subfields(subpacket):
  19. done = 0
  20. fields = {}
  21. subfield = lib.JAM_GetSubfield(subpacket)
  22. if subfield == ffi.NULL:
  23. done = 1
  24. else:
  25. fields[int(subfield[0].LoID)] = get_buffer(subfield.Buffer, subfield.DatLen)
  26. # print("SubField ID:", subfield[0].LoID, " ", end='')
  27. # print_message(subfield.Buffer, subfield.DatLen)
  28. while done == 0:
  29. subfield = lib.JAM_GetSubfield(ffi.NULL)
  30. if subfield == ffi.NULL:
  31. done = 1
  32. else:
  33. fields[int(subfield[0].LoID)] = get_buffer(subfield.Buffer, subfield.DatLen)
  34. # print("SubField ID:", subfield[0].LoID, " ", end='')
  35. # print_message(subfield.Buffer, subfield.DatLen)
  36. return fields
  37. def get_messages(messagebase):
  38. jambase = ffi.new("s_JamBase **")
  39. ret = lib.JAM_OpenMB(messagebase.encode("utf-8"), jambase)
  40. if ret != 0:
  41. return None
  42. # Ok, next!
  43. size = ffi.new("uint32_t *")
  44. ret = lib.JAM_GetMBSize(jambase[0], size)
  45. if ret != 0:
  46. return None
  47. basesize = int(ffi.cast("long", size[0]))
  48. msgheader = ffi.new("s_JamMsgHeader *")
  49. subpacket = ffi.new("s_JamSubPacket **")
  50. subfield = ffi.new("s_JamSubfield *")
  51. messages = []
  52. for msgno in range(basesize):
  53. ret = lib.JAM_ReadMsgHeader(jambase[0], msgno, msgheader, subpacket)
  54. # Not sure if I need to delete subpacket on an error...
  55. if ret == 0:
  56. fields = get_subfields(subpacket[0])
  57. # pprint(fields)
  58. msgdata = {
  59. "subject": fields.get(6, ""),
  60. "from": fields.get(2, "<blank>"),
  61. "to": fields.get(3, "<blank>"),
  62. }
  63. msgdata["MsgNum"] = msgheader[0].MsgNum
  64. msgdata["number"] = msgno
  65. msgdata["written"] = msgheader[0].DateWritten
  66. messages.append(msgdata)
  67. lib.JAM_DelSubPacket(subpacket[0])
  68. ret = lib.JAM_CloseMB(jambase[0])
  69. lib.free(jambase[0])
  70. jambase = None
  71. return messages
  72. def read_message(base, number):
  73. jambase = ffi.new("s_JamBase **")
  74. ret = lib.JAM_OpenMB(base.encode("utf-8"), jambase)
  75. if ret != 0:
  76. return None
  77. jambaseheader = ffi.new("s_JamBaseHeader *")
  78. ret = lib.JAM_ReadMBHeader(jambase[0], jambaseheader)
  79. if ret != 0:
  80. return None
  81. actual = number - jambaseheader[0].BaseMsgNum
  82. msgheader = ffi.new("s_JamMsgHeader *")
  83. subpacket = ffi.new("s_JamSubPacket **")
  84. subfield = ffi.new("s_JamSubfield *")
  85. ret = lib.JAM_ReadMsgHeader(jambase[0], actual, msgheader, subpacket)
  86. message = {}
  87. # Not sure if I need to delete subpacket on an error...
  88. if ret == 0:
  89. fields = get_subfields(subpacket[0])
  90. message = {
  91. "subject": fields.get(6, ""),
  92. "from": fields.get(2, "<blank>"),
  93. "to": fields.get(3, "<blank>"),
  94. }
  95. message["fields"] = fields
  96. message["number"] = actual
  97. message["MsgNum"] = msgheader[0].MsgNum
  98. message["written"] = msgheader[0].DateWritten
  99. # message text
  100. buffer = ffi.new("char []", msgheader.TxtLen + 1)
  101. ret = lib.JAM_ReadMsgText(
  102. jambase[0], msgheader.TxtOffset, msgheader.TxtLen, buffer
  103. )
  104. msgtext = get_buffer(buffer, msgheader.TxtLen)
  105. message["text"] = msgtext
  106. message["bytes"] = get_bytes(buffer, msgheader.TxtLen)
  107. buffer = None
  108. lib.JAM_DelSubPacket(subpacket[0])
  109. ret = lib.JAM_CloseMB(jambase[0])
  110. lib.free(jambase[0])
  111. jambase = None
  112. return message