jammin.py 3.8 KB

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