summaryrefslogtreecommitdiff
path: root/client/3rd-party/client.py
diff options
context:
space:
mode:
Diffstat (limited to 'client/3rd-party/client.py')
-rw-r--r--client/3rd-party/client.py69
1 files changed, 68 insertions, 1 deletions
diff --git a/client/3rd-party/client.py b/client/3rd-party/client.py
index 56a8faa..52675ad 100644
--- a/client/3rd-party/client.py
+++ b/client/3rd-party/client.py
@@ -411,6 +411,17 @@ class MoonpasteFFI:
self.lib.mp_generate_keypair.argtypes = []
self.lib.mp_generate_keypair.restype = KeyPair
+ # mp_query_author_packets
+ self.lib.mp_query_author_packets.argtypes = [
+ ctypes.c_char_p, # ip
+ c_int, # port
+ KeyPair, # author_keys
+ c_uint16, # count
+ c_uint64, # offset
+ POINTER(DataPacketList) # out_list
+ ]
+ self.lib.mp_query_author_packets.restype = c_int
+
class MoonpasteClient:
'''High-level Python client for moonpaste'''
@@ -680,4 +691,60 @@ class MoonpasteClient:
# Push to server
success = self.push_packet(ip, port, packet)
- return packet, success \ No newline at end of file
+ return packet, success
+
+ def query_author_packets(self, ip: str, port: int, author_keys: PyKeyPair, count: int = 10, offset: int = 0) -> PyDataPacketList:
+ '''Query packets for a specific author'''
+ c_keys = author_keys.to_c()
+ packet_list = DataPacketList()
+
+ result = self.ffi.lib.mp_query_author_packets(
+ ip.encode('utf-8'),
+ port,
+ c_keys,
+ count,
+ offset,
+ byref(packet_list)
+ )
+
+ if result != ClientError.SUCCESS:
+ raise MoonpasteClientError(ClientError(result))
+
+ packets = []
+ if packet_list.count > 0 and packet_list.packets:
+ for i in range(packet_list.count):
+ packets.append(PyDataPacket.from_c(packet_list.packets[i]))
+
+ # Free the allocated memory safely
+ try:
+ import ctypes.util
+ libc_name = ctypes.util.find_library('c')
+ if libc_name:
+ libc = ctypes.CDLL(libc_name)
+ libc.free.argtypes = [c_void_p]
+ libc.free(cast(packet_list.packets, c_void_p))
+ else:
+ # Platform-specific fallback
+ try:
+ if sys.platform.startswith('linux'):
+ libc = ctypes.CDLL('libc.so.6')
+ elif sys.platform == 'darwin':
+ libc = ctypes.CDLL('libc.dylib')
+ elif sys.platform == 'win32':
+ libc = ctypes.CDLL('msvcrt.dll')
+ else:
+ libc = None
+
+ if libc:
+ libc.free.argtypes = [c_void_p]
+ libc.free(cast(packet_list.packets, c_void_p))
+ except OSError:
+ pass # Can't free, but better than crash
+ except:
+ pass # Memory will be freed when process exits
+
+ return PyDataPacketList(
+ packets=packets,
+ count=packet_list.count,
+ new_offset=packet_list.new_offset
+ ) \ No newline at end of file