-
Notifications
You must be signed in to change notification settings - Fork 0
/
scis.py
executable file
·356 lines (281 loc) · 11.2 KB
/
scis.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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#!/usr/bin/python -u
import silc
import os
import pwd
import socket
import time
import threading
import tempfile
class SupySilcClient(silc.SilcClient):
servername = ""
nickname = ""
username = ""
remoteport = 0
def _to_hex(self, string):
return u''.join(hex(ord(c)).replace('0x', '').zfill(2) for c in string)
def __init__(self):
keybase = tempfile.mkstemp("sciskey")[1]
pubkey = keybase + '.pub'
privkey = keybase + '.prv'
self.keys = None
if os.path.exists(pubkey) and os.path.exists(privkey):
self.keys = silc.load_key_pair(pubkey, privkey)
else:
self.keys = silc.create_key_pair(pubkey, privkey)
self.isconnected = False
self.users = {} # use this to lookup nick to SilcUser objects
self.channels = {} # use this to lookup channels to SilcChannel objects
def start(self):
silc.SilcClient.__init__(self, keys = self.keys, nickname=self.nickname, username=self.username)
def get_nickmask(self, user):
return self.users[user.fingerprint][0]
def get_user_by_nickmask(self,mask): #TODO: my stomach hurts seeing this :-(
for fp in self.users:
if mask==self.users[fp][0]:
return self.users[fp][1]
def _cache_user(self, user):
mask = user.nickname;
i=0
for fp in self.users:
if fp!=user.fingerprint:
if self.users[fp][0]==mask:
mask="%s_%d" % (user.nickname, i)
i+=1
self.users[user.fingerprint] = [mask,user]
def _cache_channel(self, channel):
self.channels[channel.channel_name] = channel
def running(self):
print 'SILC: Running.'
self.connect_to_server(self.servername, self.remoteport)
def connected(self):
print ':scis 001 %s :Connected to server.' % self.nickname
self.isconnected = True
def disconnected(self, msg):
print 'ERROR :Disconnected from server.'
self.isconnected = False
sys.exit()
def failure(self):
print "SILC: Connection failure"
self.isconnected = False
#def command(self, success, code, command, status):
# print 'SILC: Command:', success, code, command, status
def say(self, msg):
pass
def channel_message(self, sender, channel, flags, msg):
print ':%s!%s@%s PRIVMSG %s :%s' % (self.get_nickmask(sender), sender.username, sender.hostname, channel, msg)
self._cache_channel(channel)
self._cache_user(sender)
def private_message(self, sender, flags, msg):
self._cache_user(sender)
print ':%s!%s@%s PRIVMSG %s :%s' % (self.get_nickmask(sender), sender.username, sender.hostname, self.nickname, msg)
def notify_none(self, msg):
print ':scis NOTICE %s :%s' % (thread.c.nickname, msg)
def notify_join(self, joiner, channel):
self._cache_user(joiner)
print ':%s!%s JOIN :%s' % (self.get_nickmask(joiner), joiner.hostname, channel)
thread.c.command_call("USERS %s" % channel)
def notify_invite(self, channel, channel_name, inviter):
print 'SILC: Notify (Invite):', channel, channel_name, inviter
def notify_leave(self, leaver, channel):
self._cache_user(leaver)
self._cache_channel(channel)
print ':%s!%s PART %s' % (self.get_nickmask(leaver), leaver.hostname, channel)
def notify_signoff(self, user, msg, channel):
self._cache_user(user)
print ':%s!%s QUIT' % ( self.get_nickmask(user), user.hostname)
def notify_topic_set(self, type, changedby, channel, topic):
self._cache_user(changedby)
self._cache_channel(channel)
print 'SILC: Notify (Topic Set):', channel, topic
def notify_nick_change(self, user, olduser, newuser):
oldnick=self.get_nickmask(user)
self._cache_user(user)
print ':%s NICK %s' % (oldnick,self.get_nickmask(user))
def notify_cmode_change(self, *args):
pass # TODO: not implemented
def notify_cumode_change(self, *args):
pass # TODO: not implemented
def notify_motd(self, msg):
for line in msg.split('\n'):
print ":scis 372 %s :%s" % (thread.c.nickname, line)
print ":scis 376 %s :End of /MOTD command." % thread.c.nickname
def notify_server_signoff(self):
print 'SILC: Notify (Server Signoff)'
def notify_kicked(self, kicked, reason, kicker, channel):
self._cache_user(kicked)
self._cache_user(kicker)
self._cache_channel(channel)
print 'SILC: Notify (Kick):', kicked, reason, kicker, channel
def notify_killed(self, *args):
pass # TODO: not implemented
def notify_error(self, type, message):
print 'SILC: Notify (Error):', type, message
def notify_watch(self, watched, new_nick, new_user_mode, notification, _):
self._cache_user(watched)
print 'SILC: Notify (Watch):', watched
def command_reply_whois(self, user, nickname, username, realname, mode, idle):
self._cache_user(user)
print 'SILC: Reply (Whois): %s mode: %x idle: %d' % (nickname, mode, idle)
def command_reply_whowas(self, user, nickname, username, realname):
self._cache_user(user)
print 'SILC: Reply (Whowas):', nickname
def command_reply_nick(self, user, nickname, olduserid):
self._cache_user(user)
def command_reply_list(self, channel, channel_name, channel_topic, user_count):
if channel == None:
print 'SILC: Reply (List): END'
else:
self._cache_channel(channel)
print 'SILC: Reply (List):', channel_name, channel_topic
def command_reply_topic(self, channel, topic):
self.cache_channel(channel)
print 'SILC: Reply (Topic):', channel, topic
def command_reply_invite(self, *args):
pass # TODO: not implemented
def command_reply_kill(self, user):
self._cache_user(user)
print 'SILC: Reply (Kill):', user
def command_reply_info(self, *args):
pass # TODO: not implemented
def command_reply_stats(self, *args):
pass # TODO: not implemented
def command_reply_ping(self):
print 'PONG :scis'
def command_reply_oper(self):
print 'SILC: Reply (Oper)'
def command_reply_join(self, channel, channel_name, topic, hmac_name, mode, user_limit, users):
self._cache_channel(channel)
def command_reply_motd(self, msg):
print 'SILC: Reply (MOTD):', msg
def command_reply_cmode(self, channel, mode, user_limit, founder_key, _):
self._cache_channel(channel)
print 'SILC: Reply (Cmode):', channel, mode
def command_reply_cumode(self, mode, channel, user):
self._cache_channel(channel)
self._cache_user(user)
print 'SILC: Reply (CUmode):', channel, user, mode
def command_reply_kick(self, channel, user):
self._cache_channel(channel)
self._cache_user(user)
print 'SILC: Reply (Kick):', channel, user
def command_reply_ban(self, channel, banlist):
self._cache_channel(channel)
print 'SILC: Reply (Ban):', channel
def command_reply_detach(self):
print 'SILC: Reply (Detach)'
def command_reply_watch(self):
print 'SILC: Reply (Watch)'
def command_reply_silcoper(self):
print 'SILC: Reply (SilcOper)'
def command_reply_leave(self, channel):
self._cache_channel(channel)
print 'SILC: Reply (Leave):', channel
def command_reply_users(self, channel, users):
for user in users:
self._cache_user(user)
server=user.server
if not server:
server="scis"
print ":scis 352 %s %s %s %s %s %s H :0 %s" % (thread.c.nickname, channel, user.username, user.hostname, server, self.get_nickmask(user), user.nickname)
print ":scis 315 %s %s :End of /WHO list." % (thread.c.nickname, channel)
namesanswer=":scis 353 %s = %s :" % (thread.c.nickname, channel)
for user in users:
currnick=self.get_nickmask(user)
if len(namesanswer)+len(currnick)<511:
namesanswer+=currnick+" "
else:
print namesanswer.strip()
namesanswer=":scis 353 %s = %s :" % (thread.c.nickname, channel)
print namesanswer.strip()
print ":scis 366 %s %s :End of /NAMES list." % (thread.c.nickname, channel)
def command_reply_service(self, *args):
pass # not implemented
def command_reply_failed(self, command, commandstr, errorcode, errormsg):
# global catching failed commands and their error codes
print 'SILC: Reply (FAILED)!', commandstr, errormsg
class SILCThread(threading.Thread):
c = SupySilcClient()
run=True
def __init__(self):
threading.Thread.__init__(self)
def run(self):
self.c.start()
try:
while self.run:
self.c.run_one()
time.sleep(0.1)
except KeyboardInterrupt:
pass
def privmsg(linea,line):
if ':'==linea[2][0]:
linea[2]=linea[2][1:]
# TODO: silc channels don't need the #. So I should strip it off...
if '#'==linea[1][0] and linea[1] in thread.c.channels:
thread.c.send_channel_message(thread.c.channels[linea[1]], " ".join(linea[2:]))
else:
thread.c.send_private_message(thread.c.get_user_by_nickmask(linea[1]), " ".join(linea[2:]))
def try_to_connect():
if thread.c.servername and thread.c.username and thread.c.nickname:
thread.start()
elif thread.c.username and thread.c.nickname:
print ":scis 001 %s :Yo are %s, now tell me the SERVER" % (thread.c.nickname,thread.c.nickname)
def set_server(linea,line):
if "" != thread.c.servername:
return
thread.c.servername=linea[1]
if len(linea) > 2:
try:
thread.c.remoteport=int(linea[2])
except:
thread.c.remoteport=706
else:
thread.c.remoteport=706
try_to_connect()
def set_nick(linea,line):
thread.c.nickname=linea[1]
try_to_connect()
def set_user(linea,line):
thread.c.username=line.split(":")[1];
try_to_connect()
def quit(linea,line):
thread.run=False
if thread.is_alive():
thread.join()
sys.exit()
def ignore_cmd(linea,line):
pass
def jomat(linea,line):
print "jomat!"+(" ".join(linea[1:]))
thread.c.command_call(" ".join(linea[1:]))
def get_users(linea,line):
thread.c.command_call("USERS "+linea[1])
commands = {"PRIVMSG":privmsg,
"SERVER":set_server,
"NICK":set_nick,
"USER":set_user,
"QUIT":quit,
"MODE":ignore_cmd, # irssi wants to set the mode, just ignore it <- TODO
"WHO":get_users,
"NAMES":get_users,
"JOMAT":jomat
}
if __name__ == "__main__":
import sys
thread = SILCThread()
print ":scis NOTICE AUTH :Hi there..."
while True:
line = sys.stdin.readline()
if line=="":
thread.run=False
if thread.is_alive():
thread.join()
break # EOF
if line == "\n":
continue
line=line.strip()
linea=line.split(' ')
command=linea[0].upper()
if command in commands:
commands[command](linea,line)
else:
thread.c.command_call(line.strip())