-
Notifications
You must be signed in to change notification settings - Fork 7
/
schongo.py
executable file
·247 lines (183 loc) · 5.87 KB
/
schongo.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#!/usr/bin/env python3
# encoding=utf-8
from __future__ import with_statement
from irc import IrcClient
import logging
import logging.config
import json
import modules
import sys
import getopt
import os.path
from config import Config
global connections
connections = 0
version = "0.1a"
if os.path.isdir(".git"):
# We're running from a git tree, let's pull the revision number, shall we?
with open(".git/HEAD") as f:
ref = f.read().strip()
ref = ref.split(': ',1)[1]
with open(".git/%s" % ref) as f2:
version = f2.read()[:8]
class SchongoClient(IrcClient):
def __init__(self, network, cfg):
server = cfg.get("server")
port = cfg.getint("port")
nicks = cfg.getlist("nicks")
ident = cfg.get("ident")
realname = cfg.get("real name")
def get(opt):
try:
return cfg.get(opt)
except:
return None
self.user_modes = get("user modes")
self._ns_name = get("nickserv name")
self._ns_pass = get("nickserv password")
self._ns_find = get("nickserv find")
if self._ns_find is None:
self._ns_find = "is registered"
if self._ns_name is None:
self._ns_name = "NickServ"
if self._ns_pass is None:
self._ns_authed = True
else:
self._ns_authed = False
channels = cfg.getlist("channels")
IrcClient.__init__(self, server, port, nicks, ident, realname)
if channels is None:
channels = ["#lobby"]
self.channels = channels
if network is not None:
self.network = network
else:
self.network = server
self.logger = logging.getLogger("IrcSocket(%s)" % self.network)
global connections
connections += 1
# Overrides Thread
def getName(self):
return "IrcSocket(%s)" % self.network
# Overrides IrcClient, and calls super
def onConnected(self):
IrcClient.onConnected(self)
if self.user_modes is not None:
self.sendMessage("MODE", self.nick, self.user_modes)
modules.fire_hook("connected", self)
for i in self.channels:
self.join_channel(i)
# Overrides IrcClient, and calls super
def onDisconnected(self):
modules.fire_hook("disconnected", self)
IrcClient.onDisconnected(self)
global connections
connections -= 1
if connections == 0:
self.logger.info("Shutting down.")
modules.shutdown()
# Overrides IrcClient
def onMsg(self, chan, who, what):
modules.fire_hook("message", modules.IrcContext(self, chan, who), what)
# Overrides IrcClient
def onAction(self, chan, who, what):
modules.fire_hook("action", modules.IrcContext(self, chan, who), what)
# Overrides IrcClient
def onCtcp(self, chan, who, cmd, arg):
if cmd == "VERSION":
self.notice(who.nick, "\x01VERSION Schongo Bot %s\x01" % version)
elif cmd == "PING":
self.notice(who.nick, "\x01PING %s\x01" % arg)
else:
modules.fire_hook("ctcp", modules.IrcContext(self, chan, who), cmd, arg)
# Overrides IrcClient
def onJoin(self, who, chan):
modules.fire_hook("join", modules.IrcContext(self, chan, who))
# Overrides IrcClient
def onPart(self, who, chan, msg):
modules.fire_hook("part", modules.IrcContext(self, chan, who))
# Overrides IrcClient
def onQuit(self, who, message):
modules.fire_hook("quit", modules.IrcContext(self, None, who))
# Overrides IrcClient
def onTopic(self, who, chan, topic):
modules.fire_hook("topic", modules.IrcContext(self,chan,who),topic)
# Overrides IrcClient
def onNick(self, old, new):
modules.fire_hook("nick", modules.IrcContext(self, None, old), new)
# Overrides IrcClient
def onMode(self, who, chan, mode, args):
modules.fire_hook("mode", modules.IrcContext(self, chan, who), mode, args)
def onNotice(self, target, who, message):
if not self._ns_authed and who.nick == self._ns_name:
if self._ns_find in message:
self.say(self._ns_name, "IDENTIFY %s" % self._ns_pass)
else: # Intentionally don't allow it to intercept NickServ messages.
modules.fire_hook("notice", modules.IrcContext(self, target, who), message);
# Overrides IrcClient
def onMessage(self, msg):
# Call the parent so we still function.
IrcClient.onMessage(self, msg)
# Fire off an event for modules to eat
modules.fire_hook("irc_%s" % msg.command,
modules.IrcContext(self, None, msg.origin),
msg)
def main(argv):
opts, args = getopt.getopt(argv, "v:c", [ "debug", "config=" ])
print(opts)
debug = False
configFile = None
dn = os.path.dirname(__file__)
if dn is not "":
os.chdir(dn)
del dn
for arg,val in opts:
if arg == "--debug" or arg == "-v":
debug = True
elif arg == "--config" or arg == "-c":
configFile = val
if configFile is None:
configFile = "data/config-debug.cfg" if debug else "data/config.cfg"
if not os.path.isfile(configFile):
if os.path.isfile("data/config.cfg"):
logging.warn("Config file %s missing, reverting to default" % configFile)
configFile = "data/config.cfg"
else:
logging.error("""We are missing a config file, please look at the example.cfg for help on making a new config, and name it config.cfg or pass a config file using --config
The config file should go in the data directory""")
return
logging.basicConfig(level=(logging.DEBUG if debug else logging.WARN))
#logging.config.fileConfig("logging.cfg")
config = Config();
logging.info("Reading config file: %s" % configFile)
try:
config.read(configFile)
except Exception as e:
logging.error("Error reading config file: %s" % e)
return
if debug:
logging.info("Debug mode activated")
basic = config.get_section("Basic")
modules.cfg_basic = basic
modules.config = config
modules.init()
schongos = {}
for i in basic.getlist("networks"):
net = config.get_section("Network/%s" % i)
sch = SchongoClient(i, net)
"""
server=net.get("server"),
port=net.getint("port"),
)icks=net.getlist("nicks"),
ident=net.get("ident"),
realname=net.get("real name"),
network=i,
channels=net.getlist("channels")
)"""
sch.connect()
sch.start()
schongos[i] = sch
modules.connections = schongos
return schongos
if __name__ == "__main__":
main(sys.argv[1:])