123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- import sys
- from _pi_cffi import ffi, lib
- from pprint import pprint
- def get_buffer(buffer, bsize):
- string = b""
- for x in range(bsize):
- string += buffer[x]
- # return string.decode('cp437').encode('utf-8')
- return string.decode("cp437")
- # return string.decode('utf-8')
- # return string
- def get_bytes(buffer, bsize):
- string = b""
- for x in range(bsize):
- string += buffer[x]
- # return string.decode('cp437').encode('utf-8')
- return string
- def get_subfields(subpacket):
- done = 0
- fields = {}
- subfield = lib.JAM_GetSubfield(subpacket)
- if subfield == ffi.NULL:
- done = 1
- else:
- fields[int(subfield[0].LoID)] = get_buffer(subfield.Buffer, subfield.DatLen)
- # print("SubField ID:", subfield[0].LoID, " ", end='')
- # print_message(subfield.Buffer, subfield.DatLen)
- while done == 0:
- subfield = lib.JAM_GetSubfield(ffi.NULL)
- if subfield == ffi.NULL:
- done = 1
- else:
- fields[int(subfield[0].LoID)] = get_buffer(subfield.Buffer, subfield.DatLen)
- # print("SubField ID:", subfield[0].LoID, " ", end='')
- # print_message(subfield.Buffer, subfield.DatLen)
- return fields
- def get_messages(messagebase):
- jambase = ffi.new("s_JamBase **")
- ret = lib.JAM_OpenMB(messagebase.encode("utf-8"), jambase)
- if ret != 0:
- return None
- # Ok, next!
- size = ffi.new("uint32_t *")
- ret = lib.JAM_GetMBSize(jambase[0], size)
- if ret != 0:
- return None
- basesize = int(ffi.cast("long", size[0]))
- msgheader = ffi.new("s_JamMsgHeader *")
- subpacket = ffi.new("s_JamSubPacket **")
- subfield = ffi.new("s_JamSubfield *")
- messages = []
- for msgno in range(basesize):
- ret = lib.JAM_ReadMsgHeader(jambase[0], msgno, msgheader, subpacket)
- # Not sure if I need to delete subpacket on an error...
- if ret == 0:
- fields = get_subfields(subpacket[0])
- # pprint(fields)
- msgdata = {
- "subject": fields.get(6, ""),
- "from": fields.get(2, "<blank>"),
- "to": fields.get(3, "<blank>"),
- }
- msgdata["MsgNum"] = msgheader[0].MsgNum
- msgdata["number"] = msgno
- msgdata["written"] = msgheader[0].DateWritten
- messages.append(msgdata)
- lib.JAM_DelSubPacket(subpacket[0])
- ret = lib.JAM_CloseMB(jambase[0])
- lib.free(jambase[0])
- jambase = None
- return messages
- def read_message(base, number):
- jambase = ffi.new("s_JamBase **")
- ret = lib.JAM_OpenMB(base.encode("utf-8"), jambase)
- if ret != 0:
- return None
- jambaseheader = ffi.new("s_JamBaseHeader *")
- ret = lib.JAM_ReadMBHeader(jambase[0], jambaseheader)
- if ret != 0:
- return None
- actual = number - jambaseheader[0].BaseMsgNum
- msgheader = ffi.new("s_JamMsgHeader *")
- subpacket = ffi.new("s_JamSubPacket **")
- subfield = ffi.new("s_JamSubfield *")
- ret = lib.JAM_ReadMsgHeader(jambase[0], actual, msgheader, subpacket)
- message = {}
- # Not sure if I need to delete subpacket on an error...
- if ret == 0:
- fields = get_subfields(subpacket[0])
- message = {
- "subject": fields.get(6, ""),
- "from": fields.get(2, "<blank>"),
- "to": fields.get(3, "<blank>"),
- }
- message["fields"] = fields
- message["number"] = actual
- message["MsgNum"] = msgheader[0].MsgNum
- message["written"] = msgheader[0].DateWritten
- # message text
- buffer = ffi.new("char []", msgheader.TxtLen + 1)
- ret = lib.JAM_ReadMsgText(
- jambase[0], msgheader.TxtOffset, msgheader.TxtLen, buffer
- )
- msgtext = get_buffer(buffer, msgheader.TxtLen)
- message["text"] = msgtext
- message["bytes"] = get_bytes(buffer, msgheader.TxtLen)
- buffer = None
- lib.JAM_DelSubPacket(subpacket[0])
- ret = lib.JAM_CloseMB(jambase[0])
- lib.free(jambase[0])
- jambase = None
- return message
|