-
Notifications
You must be signed in to change notification settings - Fork 0
/
signalbot.py
64 lines (48 loc) · 1.92 KB
/
signalbot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import argparse
import importlib
from pydbus import SessionBus, SystemBus, connect
from gi.repository import GLib
class Message:
def __init__(self, timestamp, sender, group_id, message, attachments):
self.timestamp = timestamp
self.sender = sender
self.group_id = group_id
self.message = message
self.attachments = attachments
class Bot:
def __init__(self, args):
self.args = args
self.plugins = []
def receive(self, timestamp, sender, group_id, message, attachments):
message = Message(timestamp, sender, group_id, message, attachments)
for plugin in self.plugins:
plugin.receive(message)
def start(self):
# Load requested plugins
self.plugins = [
importlib.import_module('plugins.'+plugin).__plugin__(self)
for plugin in self.args.plugins]
# Start listening for messages
if self.args.bus == 'session' or self.args.bus is None:
bus = SessionBus()
elif self.args.bus == 'system':
bus = SystemBus()
else:
bus = connect(self.args.bus)
self.signal = bus.get('org.asamk.Signal')
self.signal.onMessageReceived = self.receive
loop = GLib.MainLoop()
loop.run()
if __name__ == '__main__':
# Parse arguments
parser = argparse.ArgumentParser(description='Signal Bot')
parser.add_argument('--bus', help='DBus bus type (system, session) or bus '
'address')
parser.add_argument('plugins', nargs='+', metavar='plugin')
plugin_group = parser.add_argument_group('plugin arguments')
plugin_group.add_argument('--split-data-dir', help='Data directory for '
'split plugin')
args = parser.parse_args()
# Start bot
bot = Bot(args)
bot.start()