-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdorsalfunbot.py
165 lines (141 loc) · 5.2 KB
/
dorsalfunbot.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import lurklib
import imp
import yaml
import sys
import subprocess
import os
import signal
import globals
class DorsalFunBot(lurklib.Client):
def __init__(self, config):
super(DorsalFunBot, self).__init__(**config['irc'])
self.modules = {}
self.load_modules(config['modules'])
globals.init()
def load_modules(self, moduleslist):
try:
modules = {}
for module in moduleslist:
try:
action = module["action"] if "action" in module else ""
if action not in modules:
modules[action] = []
modinfo = imp.find_module(module["name"])
modload = imp.load_module(module["name"], *modinfo)
modules[action].append(
getattr(modload, module["class"])(self))
print("Loaded module " + module['name'])
except ImportError as e:
print(str(e))
self.modules = modules
return True
except Exception as e:
print(str(e))
return False
def unload_modules(self):
for action in self.modules:
for module in self.modules[action]:
try:
module.dispose()
print("Disposed module " + module.__class__.__name__)
except Exception as e:
pass
def on_connect(self):
print("connected")
for chan in config['channels']:
self.join_(chan)
def on_chanmsg(self, from_, chan, msg):
if "" in self.modules:
for module in self.modules[""]:
try:
module.on_chanmsg(from_, chan, msg)
except Exception as e:
print("Error in module: " + str(e))
try:
action = msg.split(maxsplit=1)[0]
helps = {}
if action == "!help":
for action in self.modules:
for module in self.modules[action]:
try:
halp = module.halp()
if not isinstance(halp, list):
halp = [halp]
helps[module.__class__.__name__] = halp
except Exception as e:
print("Error in module help: " + str(e))
for m in helps:
self.privmsg(from_[0], m)
for msg in helps[m]:
self.privmsg(from_[0], " " + str(msg))
elif action[0] == "!" and action[1:] in self.modules:
for module in self.modules[action[1:]]:
try:
module.on_chanmsg(from_, chan, msg)
except Exception as e:
print(
"Error in module " + module.__class__.__name__ + ": " + str(e))
else:
globals.g_buffmsg = msg
except IndexError:
pass
def on_privnotice(self, from_, notice):
if notice == "rehash":
config = load_config()
if not config:
self.notice(
from_[0], "Error loading config file, no rehash has been done")
return
self.unload_modules()
if self.load_modules(config['modules']):
self.notice(from_[0], "rehash has been done")
else:
self.notice(from_[0], "rehash failed dramatically")
def load_config():
try:
with open("config.yaml") as configfile:
config = yaml.load(configfile)
check_config(config)
except Exception as e:
print("Error loading configuration file: " + str(e))
return False
return config
def check_config(config):
if not 'irc' in config.keys():
raise Exception("No IRC configuration found")
elif not 'channels' in config.keys():
raise Exception("No channels configuration found")
elif not 'nick' in config['irc'].keys() \
or not 'user' in config['irc'].keys() \
or not 'server' in config['irc'].keys():
raise Exception(
"You need 'nick', 'user' and 'server' information in the configuration")
if __name__ == '__main__':
config = load_config()
if not config:
sys.exit(1)
pidfile = config['system']['pidfile']
try:
with open(pidfile, "r") as f:
pid = int(f.read().strip())
os.kill(pid, 0)
print("Le bot est deja lance (pid %d)" % (pid))
sys.exit(0)
except ProcessLookupError as e:
print("Le pid dans le fichier pid n'existe pas, go")
except IOError as e:
print("Le fichier pid n'existe pas, go " + str(e))
except ValueError as e:
print("Le fichier pid contient de la cochonnerie, go")
with open(pidfile, "w") as f:
pid = os.getpid()
f.write(str(pid))
bot = DorsalFunBot(config=config)
while True:
try:
bot.mainloop()
except InterruptedError as e:
pass
except:
bot.unload_modules()
break