forked from boamaod/skype2irc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
skype2irc.py
executable file
·509 lines (445 loc) · 17.1 KB
/
skype2irc.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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# IRC ⟷ Skype Gateway Bot: Connects Skype Chats to IRC Channels
# Copyright (C) 2014 Märt Põder <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# *** This bot deliberately prefers IRC to Skype! ***
# Snippets from
#
# Feebas Skype Bot (C) duxlol 2011 http://sourceforge.net/projects/feebas/
# IRC on a Higher Level http://www.devshed.com/c/a/Python/IRC-on-a-Higher-Level/
# Time until a date http://stackoverflow.com/questions/1580227/find-time-until-a-date-in-python
# Skype message edit code from Kiantis fork https://github.com/Kiantis/skype2irc
import sys, signal
import time, datetime
import string, textwrap
from ircbot import SingleServerIRCBot
from irclib import ServerNotConnectedError
from threading import Timer
version = "0.3"
if len(sys.argv) >= 2:
# provide path to configuration file as a command line parameter
execfile(sys.argv[1])
else:
# default configuration for testing purposes
servers = [
("irc.freenode.net", 6667),
("hitchcock.freenode.net", 6667),
("leguin.freenode.net", 6667),
("verne.freenode.net", 6667),
("roddenberry.freenode.net", 6667),
]
nick = "skype-}"
botname = "IRC ⟷ Skype".decode('UTF-8')
password = None
vhost = False
mirrors = {
'#test':
'iWwCuTwsjoIglPL3Fbmc_BM95EyK3683btIvrV_B2lQN4agJGCX7-REKzMl7-ruRqvo2RIgcOkQ',
}
colors = True
max_irc_msg_len = 442
ping_interval = 2*60
reconnect_interval = 30
# to avoid flood excess
max_seq_msgs = 2
delay_btw_msgs = 0.35
delay_btw_seqs = 0.15
preferred_encodings = ["UTF-8", "CP1252", "ISO-8859-1"]
name_start = "<".decode('UTF-8') # "◀"
name_end = ">".decode('UTF-8') # "▶"
emote_char = "*".decode('UTF-8') # "✱"
muted_list_filename = nick + '.%s.muted'
topics = ""
usemap = {}
bot = None
mutedl = {}
lastsaid = {}
edmsgs = {}
pinger = None
bot = None
wrapper = textwrap.TextWrapper(width=max_irc_msg_len - 2)
wrapper.break_on_hyphens = False
# Time consts
SECOND = 1
MINUTE = 60 * SECOND
HOUR = 60 * MINUTE
DAY = 24 * HOUR
MONTH = 30 * DAY
def get_relative_time(dt, display_full = True):
"""Returns relative time compared to now from timestamp"""
now = datetime.datetime.now()
delta_time = now - dt
delta = delta_time.days * DAY + delta_time.seconds
minutes = delta / MINUTE
hours = delta / HOUR
days = delta / DAY
if delta <= 0:
return "in the future" if display_full else "!"
if delta < 1 * MINUTE:
if delta == 1:
return "moment ago" if display_full else "1s"
else:
return str(delta) + (" seconds ago" if display_full else "s")
if delta < 2 * MINUTE:
return "a minute ago" if display_full else "1m"
if delta < 45 * MINUTE:
return str(minutes) + (" minutes ago" if display_full else "m")
if delta < 90 * MINUTE:
return "an hour ago" if display_full else "1h"
if delta < 24 * HOUR:
return str(hours) + (" hours ago" if display_full else "h")
if delta < 48 * HOUR:
return "yesterday" if display_full else "1d"
if delta < 30 * DAY:
return str(days) + (" days ago" if display_full else "d")
if delta < 12 * MONTH:
months = delta / MONTH
if months <= 1:
return "one month ago" if display_full else "1m"
else:
return str(months) + (" months ago" if display_full else "m")
else:
years = days / 365.0
if years <= 1:
return "one year ago" if display_full else "1y"
else:
return str(years) + (" years ago" if display_full else "y")
def cut_title(title):
"""Cuts Skype chat title to be ok"""
newtitle = ""
for chunk in title.split():
newtitle += chunk.strip(string.punctuation) + " "
if len(newtitle) > 10:
break
return newtitle.strip()
def get_nick_color(s):
colors = ["\x0305", "\x0304", "\x0303", "\x0309", "\x0302", "\x0312",
"\x0306", "\x0313", "\x0310", "\x0311", "\x0307"]
num = 0
for i in s:
num += ord(i)
num = num % 11
return colors[num]
def get_nick_decorated(nick):
"""Decorate nicks for better visibility in IRC (currently bold or
colors based on nick)"""
if colors:
return get_nick_color(nick) + nick + '\017'
else:
return "\x02" + nick + "\x02"
def load_mutes():
"""Loads people who don't want to be broadcasted from IRC to Skype"""
for channel in mirrors.keys():
mutedl[channel] = []
try:
f = open(muted_list_filename % channel, 'r')
for line in f.readlines():
name = line.rstrip("\n")
mutedl[channel].append(name)
mutedl[channel].sort()
f.close()
print 'Loaded list of ' + str(len(mutedl[channel])) + ' mutes for ' + channel + '!'
except:
pass
def save_mutes(channel):
"""Saves people who don't want to be broadcasted from IRC to Skype"""
try:
f = open(muted_list_filename % channel, 'w')
for name in mutedl[channel]:
f.write(name + '\n')
mutedl[channel].sort()
f.close
print 'Saved ' + str(len(mutedl[channel])) + ' mutes for ' + channel + '!'
except:
pass
def skype_says(chat, msg, edited = False):
"""Translate Skype messages to IRC"""
raw = msg.Body
msgtype = msg.Type
send = chat.SendMessage
senderDisplay = msg.FromDisplayName
senderHandle = msg.FromHandle
if edited:
edit_label = " ✎".decode('UTF-8') + get_relative_time(msg.Datetime, display_full = False)
else:
edit_label = ""
if msgtype == 'EMOTED':
bot.say(usemap[chat], emote_char + " " + get_nick_decorated(senderHandle) + edit_label + " " + raw)
elif msgtype == 'SAID':
bot.say(usemap[chat], name_start + get_nick_decorated(senderHandle) + edit_label + name_end + " " + raw)
def OnMessageStatus(Message, Status):
"""Skype message object listener"""
chat = Message.Chat
# Only react to defined chats
if chat in usemap:
if Status == 'RECEIVED':
skype_says(chat, Message)
def OnNotify(n):
"""Skype notification listener"""
params = n.split()
if len(params) >= 4 and params[0] == "CHATMESSAGE":
if params[2] == "EDITED_TIMESTAMP":
edmsgs[params[1]] = True
elif params[1] in edmsgs and params[2] == "BODY":
msg = skype.Message(params[1])
if msg:
chat = msg.Chat
if chat in usemap:
skype_says(chat, msg, edited = True)
del edmsgs[params[1]]
def decode_irc(raw, preferred_encs = preferred_encodings):
"""Heuristic IRC charset decoder"""
changed = False
for enc in preferred_encs:
try:
res = raw.decode(enc)
changed = True
break
except:
pass
if not changed:
try:
import chardet
enc = chardet.detect(raw)['encoding']
res = raw.decode(enc)
except:
res = raw.decode(enc, 'ignore')
#enc += "+IGNORE"
return res
def signal_handler(signal, frame):
print "Ctrl+C pressed!"
if pinger is not None:
print "Cancelling the pinger..."
pinger.cancel()
if bot is not None:
print "Killing the bot..."
for dh in bot.ircobj.handlers["disconnect"]:
bot.ircobj.remove_global_handler("disconnect", dh[1])
if len(bot.ircobj.handlers["disconnect"]) == 0:
print "Finished."
bot.die()
class MirrorBot(SingleServerIRCBot):
"""Create IRC bot class"""
def __init__(self):
SingleServerIRCBot.__init__(self, servers, nick, (botname + " " + topics).encode("UTF-8"), reconnect_interval)
def start(self):
"""Override default start function to avoid starting/stalling the bot with no connection"""
while not self.connection.is_connected():
self._connect()
if not self.connection.is_connected():
time.sleep(self.reconnection_interval)
self.server_list.append(self.server_list.pop(0))
SingleServerIRCBot.start(self)
def on_nicknameinuse(self, connection, event):
"""Overcome nick collisions"""
newnick = connection.get_nickname() + "_"
print "Nickname in use, adding underscore", newnick
connection.nick(newnick)
def routine_ping(self, first_run = False):
"""Ping server to know when try to reconnect to a new server."""
global pinger
if not first_run and not self.pong_received:
print "Ping reply timeout, disconnecting from", self.connection.get_server_name()
self.disconnect()
return
self.pong_received = False
self.connection.ping(self.connection.get_server_name())
pinger = Timer(ping_interval, self.routine_ping, ())
pinger.start()
def on_pong(self, connection, event):
"""React to pong"""
self.pong_received = True
def say(self, target, msg, do_say = True):
"""Send messages to channels/nicks"""
target = target.lower()
try:
lines = msg.encode("UTF-8").split("\n")
cur = 0
for line in lines:
for irc_msg in wrapper.wrap(line.strip("\r")):
print target, irc_msg
irc_msg += "\r\n"
if target not in lastsaid.keys():
lastsaid[target] = 0
while time.time()-lastsaid[target] < delay_btw_msgs:
time.sleep(0.2)
lastsaid[target]=time.time()
if do_say:
self.connection.privmsg(target, irc_msg)
else:
self.connection.notice(target, irc_msg)
cur += 1
if cur % max_seq_msgs == 0:
time.sleep(delay_btw_seqs) # to avoid flood excess
except ServerNotConnectedError:
print "{" +target + " " + msg+"} SKIPPED!"
def notice(self, target, msg):
"""Send notices to channels/nicks"""
self.say(self, target, msg, False)
def on_welcome(self, connection, event):
"""Do stuff when when welcomed to server"""
print "Connected to", self.connection.get_server_name()
if password is not None:
bot.say("NickServ", "identify " + password)
if vhost:
bot.say("HostServ", "ON")
# ensure handler is present exactly once by removing it before adding
self.connection.remove_global_handler("ctcp", self.handle_ctcp)
self.connection.add_global_handler("ctcp", self.handle_ctcp)
for pair in mirrors:
connection.join(pair)
print "Joined " + pair
self.routine_ping(first_run = True)
def on_pubmsg(self, connection, event):
"""React to channel messages"""
args = event.arguments()
source = event.source().split('!')[0]
target = event.target().lower()
cmds = args[0].split()
if cmds and cmds[0].rstrip(":,") == nick:
if len(cmds)==2:
if cmds[1].upper() == 'ON' and source in mutedl[target]:
mutedl[target].remove(source)
save_mutes(target)
elif cmds[1].upper() == 'OFF' and source not in mutedl[target]:
mutedl[target].append(source)
save_mutes(target)
return
if source in mutedl[target]:
return
msg = name_start + source + name_end + " "
for raw in args:
msg += decode_irc(raw) + "\n"
msg = msg.rstrip("\n")
print cut_title(usemap[target].FriendlyName), msg
usemap[target].SendMessage(msg)
def handle_ctcp(self, connection, event):
"""Handle CTCP events for emoting"""
args = event.arguments()
source = event.source().split('!')[0]
target = event.target().lower()
if target in mirrors.keys():
if source in mutedl[target]:
return
if target in usemap and args[0]=='ACTION' and len(args) == 2:
# An emote/action message has been sent to us
msg = emote_char + " " + source + " " + decode_irc(args[1]) + "\n"
print cut_title(usemap[target].FriendlyName), msg
usemap[target].SendMessage(msg)
def on_privmsg(self, connection, event):
"""React to ON, OF(F), ST(ATUS), IN(FO) etc for switching gateway (from IRC side only)"""
source = event.source().split('!')[0]
raw = event.arguments()[0].decode('utf-8', 'ignore')
args = raw.split()
if not args:
return
two = args[0][:2].upper()
if two == 'ST': # STATUS
muteds = []
brdcsts = []
for channel in mirrors.keys():
if source in mutedl[channel]:
muteds.append(channel)
else:
brdcsts.append(channel)
if len(brdcsts) > 0:
bot.say(source, "You're mirrored to Skype from " + ", ".join(brdcsts))
if len(muteds) > 0:
bot.say(source, "You're silent to Skype on " + ", ".join(muteds))
if two == 'OF': # OFF
for channel in mirrors.keys():
if source not in mutedl[channel]:
mutedl[channel].append(source)
save_mutes(channel)
bot.say(source, "You're silent to Skype now")
elif two == 'ON': # ON
for channel in mirrors.keys():
if source in mutedl[channel]:
mutedl[channel].remove(source)
save_mutes(channel)
bot.say(source, "You're mirrored to Skype now")
elif two == 'IN' and len(args) > 1 and args[1] in mirrors: # INFO
chat = usemap[args[1]]
members = chat.Members
active = chat.ActiveMembers
msg = args[1] + " ⟷ \"".decode("UTF-8") + chat.FriendlyName + "\" (%d/%d)\n" % (len(active), len(members))
# msg += chat.Blob + "\n"
userList = []
for user in members:
if user in active:
desc = " * " + user.Handle + " [" + user.FullName
else:
desc = " - " + user.Handle + " [" + user.FullName
#print user.LastOnlineDatetime
last_online = user.LastOnline
timestr = ""
if last_online > 0:
timestr += " --- " + get_relative_time(datetime.datetime.fromtimestamp(last_online))
mood = user.MoodText
if len(mood) > 0:
desc += ": \"" + mood + "\""
desc += "]" + timestr
userList.append(desc)
userList.sort()
for desc in userList:
msg += desc + '\n'
msg = msg.rstrip("\n")
bot.say(source, msg)
elif two in ('?', 'HE', 'HI', 'WT'): # HELP
bot.say(source, botname + " " + version + " " + topics + "\n * ON/OFF/STATUS --- Trigger mirroring to Skype\n * INFO #channel --- Display list of users from relevant Skype chat\nDetails: https://github.com/boamaod/skype2irc#readme")
# *** Start everything up! ***
signal.signal(signal.SIGINT, signal_handler)
print "Running", botname, "Gateway Bot", version
try:
import Skype4Py
except:
print 'Failed to locate Skype4Py API! Quitting...'
sys.exit()
try:
skype = Skype4Py.Skype();
except:
print 'Cannot open Skype API! Quitting...'
sys.exit()
if skype.Client.IsRunning:
print 'Skype process found!'
elif not skype.Client.IsRunning:
try:
print 'Starting Skype process...'
skype.Client.Start()
except:
print 'Failed to start Skype process! Quitting...'
sys.exit()
try:
skype.Attach();
skype.OnMessageStatus = OnMessageStatus
skype.OnNotify = OnNotify
except:
print 'Failed to connect! You have to log in to your Skype instance and enable access to Skype for Skype4Py! Quitting...'
sys.exit()
print 'Skype API initialised.'
topics = "["
for pair in mirrors:
chat = skype.CreateChatUsingBlob(mirrors[pair])
topic = chat.FriendlyName
print "Joined \"" + topic + "\""
topics += cut_title(topic) + "|"
usemap[pair] = chat
usemap[chat] = pair
topics = topics.rstrip("|") + "]"
load_mutes()
bot = MirrorBot()
print "Starting IRC bot..."
bot.start()