This repository has been archived by the owner on Feb 25, 2018. It is now read-only.
forked from Zren/PyQuassel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
quasselbot.py
76 lines (61 loc) · 2.28 KB
/
quasselbot.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
65
66
67
68
69
70
71
72
73
74
75
76
import glob
import os.path
from importlib.machinery import SourceFileLoader
import traceback
from quassel import *
from quasselclient import QuasselClient
class QuasselBot(QuasselClient):
def __init__(self, config):
super().__init__(config)
self.pushNotification = None
self.plugins = []
self.defaultPlugins = [
'logger',
]
@property
def pluginsToLoad(self):
return getattr(self.config, 'enabledPlugins', self.defaultPlugins)
def loadPlugins(self):
for pluginFilepath in glob.glob('plugins/*.py'):
self.loadPlugin(pluginFilepath)
def loadPlugin(self, pluginFilepath):
pluginName = pluginFilepath[len('plugins/'):-len('.py')]
if pluginName not in self.pluginsToLoad:
return
try:
pluginModuleName = 'plugin_' + pluginName
loader = SourceFileLoader(pluginModuleName, pluginFilepath)
pluginModule = loader.load_module()
self.plugins.append(pluginModule)
print('Plugin "{}" loaded.'.format(pluginFilepath))
except Exception as e:
print('Error loading plugin "{}".'.format(pluginFilepath))
traceback.print_exc()
def pluginCall(self, fnName, *args):
for plugin in self.plugins:
try:
fn = getattr(plugin, fnName, None)
if fn:
fn(self, *args)
except Exception as e:
print('Error while calling {}.{}'.format(plugin.__name__, fnName))
traceback.print_exc()
def onSessionStarted(self):
# self.sendNetworkInits() # Slooooow. Also adds 4-8 Mb to RAM.
# self.sendBufferInits()
self.pluginCall('onSessionStarted')
def onMessageRecieved(self, message):
self.pluginCall('onMessageRecieved', message)
def onSocketClosed(self):
print('\n\nSocket Closed\n\nReconnecting\n')
self.reconnect()
if __name__ == '__main__':
import sys, os
if not os.path.exists('config.py'):
print('Please create a config.py as mentioned in the ReadMe.')
sys.exit(1)
import config
quasselClient = QuasselBot(config)
quasselClient.loadPlugins()
quasselClient.run()
quasselClient.disconnectFromHost()