forked from ltworf/localslackirc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
irc.py
executable file
·501 lines (424 loc) · 17.9 KB
/
irc.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
#!/usr/bin/env python3
# localslackirc
# Copyright (C) 2018 Salvo "LtWorf" Tomaselli
#
# localslackirc 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/>.
#
# author Salvo "LtWorf" Tomaselli <[email protected]>
import datetime
from enum import Enum
import re
import select
import socket
import argparse
from typing import *
from os import environ
from os.path import expanduser
from socket import gethostname
import slack
# How slack expresses mentioning users
_MENTIONS_REGEXP = re.compile(r'<@([0-9A-Za-z]+)>')
_CHANNEL_MENTIONS_REGEXP = re.compile(r'<#[A-Z0-9]+\|([A-Z0-9\-a-z]+)>')
_SUBSTITUTIONS = [
('&', '&'),
('>', '>'),
('<', '<'),
]
class Replies(Enum):
RPL_LUSERCLIENT = 251
RPL_UNAWAY = 305
RPL_NOWAWAY = 306
RPL_ENDOFWHO = 315
RPL_LIST = 322
RPL_LISTEND = 323
RPL_CHANNELMODEIS = 324
RPL_TOPIC = 332
RPL_WHOREPLY = 352
RPL_NAMREPLY = 353
RPL_ENDOFNAMES = 366
ERR_NOSUCHCHANNEL = 403
ERR_UNKNOWNCOMMAND = 421
ERR_FILEERROR = 424
ERR_ERRONEUSNICKNAME = 432
#: Inactivity days to hide a MPIM
MPIM_HIDE_DELAY = datetime.timedelta(days=50)
class Client:
def __init__(self, s, sl_client, *, nouserlist=False, autojoin=False):
self.nick = b''
self.username = b''
self.realname = b''
self.parted_channels = set() # type: Set[bytes]
self.hostname = gethostname().encode('utf8')
self.s = s
self.sl_client = sl_client
self.nouserlist = nouserlist
self.autojoin = autojoin
def _nickhandler(self, cmd: bytes) -> None:
_, nick = cmd.split(b' ', 1)
self.nick = nick.strip()
if self.nick != self.sl_client.login_info.self.name.encode('ascii'):
self._sendreply(Replies.ERR_ERRONEUSNICKNAME, 'Incorrect nickname, use %s' % self.sl_client.login_info.self.name)
def _sendreply(self, code: Union[int,Replies], message: Union[str,bytes], extratokens: List[Union[str,bytes]] = []) -> None:
codeint = code if isinstance(code, int) else code.value
bytemsg = message if isinstance(message, bytes) else message.encode('utf8')
extratokens = list(extratokens)
extratokens.insert(0, self.nick)
self.s.send(b':%s %03d %s :%s\n' % (
self.hostname,
codeint,
b' '.join(i if isinstance(i, bytes) else i.encode('utf8') for i in extratokens),
bytemsg,
))
def _userhandler(self, cmd: bytes) -> None:
#TODO USER salvo 8 * :Salvatore Tomaselli
self._sendreply(1, 'Welcome to localslackirc')
self._sendreply(2, 'Your team name is: %s' % self.sl_client.login_info.team.name)
self._sendreply(2, 'Your team domain is: %s' % self.sl_client.login_info.team.domain)
self._sendreply(2, 'Your nickname must be: %s' % self.sl_client.login_info.self.name)
self._sendreply(Replies.RPL_LUSERCLIENT, 'There are 1 users and 0 services on 1 server')
if self.autojoin and not self.nouserlist:
# We're about to load many users for each chan; instead of requesting each
# profile on its own, batch load the full directory.
self.sl_client.prefetch_users()
if self.autojoin:
mpim_cutoff = datetime.datetime.utcnow() - MPIM_HIDE_DELAY
for sl_chan in self.sl_client.channels():
if not sl_chan.is_member:
continue
if sl_chan.is_mpim and (sl_chan.latest is None or sl_chan.latest.timestamp < mpim_cutoff):
continue
channel_name = '#%s' % sl_chan.name_normalized
self._send_chan_info(channel_name.encode('utf-8'), sl_chan)
else:
for sl_chan in self.sl_client.channels():
channel_name = '#%s' % sl_chan.name_normalized
self.parted_channels.add(channel_name.encode('utf-8'))
def _pinghandler(self, cmd: bytes) -> None:
_, lbl = cmd.split(b' ', 1)
self.s.send(b':%s PONG %s %s\n' % (self.hostname, self.hostname, lbl))
def _joinhandler(self, cmd: bytes) -> None:
_, channel_name = cmd.split(b' ', 1)
if channel_name in self.parted_channels:
self.parted_channels.remove(channel_name)
try:
slchan = self.sl_client.get_channel_by_name(channel_name[1:].decode())
except:
return
self._send_chan_info(channel_name, slchan)
def _send_chan_info(self, channel_name: bytes, slchan: slack.Channel):
if not self.nouserlist:
userlist = [] # type List[bytes]
for i in self.sl_client.get_members(slchan.id):
try:
u = self.sl_client.get_user(i)
except:
continue
if u.deleted:
# Disabled user, skip it
continue
name = u.name.encode('utf8')
prefix = b'@' if u.is_admin else b''
userlist.append(prefix + name)
users = b' '.join(userlist)
self.s.send(b':%[email protected] JOIN %s\n' % (self.nick, channel_name))
self._sendreply(Replies.RPL_TOPIC, slchan.real_topic, [channel_name])
self._sendreply(Replies.RPL_NAMREPLY, b'' if self.nouserlist else users, ['=', channel_name])
self._sendreply(Replies.RPL_ENDOFNAMES, 'End of NAMES list', [channel_name])
def _privmsghandler(self, cmd: bytes) -> None:
_, dest, msg = cmd.split(b' ', 2)
if msg.startswith(b':'):
msg = msg[1:]
message = self._addmagic(msg.decode('utf8'))
if dest.startswith(b'#'):
self.sl_client.send_message(
self.sl_client.get_channel_by_name(dest[1:].decode()).id,
message
)
else:
try:
self.sl_client.send_message_to_user(
self.sl_client.get_user_by_name(dest.decode()).id,
message
)
except:
print('Impossible to find user ', dest)
def _listhandler(self, cmd: bytes) -> None:
for c in self.sl_client.channels():
self._sendreply(Replies.RPL_LIST, c.real_topic, ['#' + c.name, str(c.num_members)])
self._sendreply(Replies.RPL_LISTEND, 'End of LIST')
def _modehandler(self, cmd: bytes) -> None:
params = cmd.split(b' ', 2)
self._sendreply(Replies.RPL_CHANNELMODEIS, '', [params[1], '+'])
def _sendfilehandler(self, cmd: bytes) -> None:
#/sendfile #destination filename
params = cmd.split(b' ', 2)
try:
channel_name = params[1].decode('utf8')
filename = params[2].decode('utf8')
except IndexError:
self._sendreply(Replies.ERR_UNKNOWNCOMMAND, 'Syntax: /sendreply #channel filename')
return
try:
if channel_name.startswith('#'):
dest = self.sl_client.get_channel_by_name(channel_name[1:]).id
else:
dest = self.sl_client.get_user_by_name(channel_name).id
except KeyError:
self._sendreply(Replies.ERR_NOSUCHCHANNEL, f'Unable to find destination: {channel_name}')
return
try:
self.sl_client.send_file(dest, filename)
self._sendreply(0, 'Upload of %s completed' % filename)
except Exception as e:
print(e)
self._sendreply(Replies.ERR_FILEERROR, 'Unable to send file')
def _parthandler(self, cmd: bytes) -> None:
_, name = cmd.split(b' ', 1)
self.parted_channels.add(name)
def _awayhandler(self, cmd: bytes) -> None:
is_away = b' ' in cmd
self.sl_client.away(is_away)
response = Replies.RPL_NOWAWAY if is_away else Replies.RPL_UNAWAY
self._sendreply(response, 'Away status changed')
def _whohandler(self, cmd: bytes) -> None:
_, name = cmd.split(b' ', 1)
if not name.startswith(b'#'):
print('WHO not supported on ', name)
return
try:
channel = self.sl_client.get_channel_by_name(name.decode()[1:])
except KeyError:
return
for i in self.sl_client.get_members(channel.id):
user = self.sl_client.get_user(i)
self._sendreply(Replies.RPL_WHOREPLY, '0 %s' % user.real_name, [name, user.name, '127.0.0.1', self.hostname, user.name, 'H'])
self._sendreply(Replies.RPL_ENDOFWHO, 'End of WHO list', [name])
def sendmsg(self, from_: bytes, to: bytes, message: bytes) -> None:
self.s.send(b':%[email protected] PRIVMSG %s :%s\n' % (
from_,
to, #private message, or a channel
message,
))
def _addmagic(self, msg: str) -> str:
"""
Adds magic codes and various things to
outgoing messages
"""
for i in _SUBSTITUTIONS:
msg = msg.replace(i[1], i[0])
msg = msg.replace('@here', '<!here>')
msg = msg.replace('@channel', '<!channel>')
msg = msg.replace('@yell', '<!channel>')
msg = msg.replace('@shout', '<!channel>')
msg = msg.replace('@attention', '<!channel>')
# Extremely inefficient code to generate mentions
# Just doing them client-side on the receiving end is too mainstream
for username in self.sl_client.get_usernames():
m = re.search(r'\b%s\b' % username, msg)
if m:
msg = msg[0:m.start()] + '<@%s>' % self.sl_client.get_user_by_name(username).id + msg[m.end():]
return msg
def parse_message(self, msg: str) -> Iterator[bytes]:
for i in msg.split('\n'):
if not i:
continue
# Replace all mentions with @user
while True:
mention = _MENTIONS_REGEXP.search(i)
if not mention:
break
i = (
i[0:mention.span()[0]] +
self.sl_client.get_user(mention.groups()[0]).name +
i[mention.span()[1]:]
)
# Replace all channel mentions
while True:
mention = _CHANNEL_MENTIONS_REGEXP.search(i)
if not mention:
break
i = (
i[0:mention.span()[0]] +
'#' +
mention.groups()[0] +
i[mention.span()[1]:]
)
for s in _SUBSTITUTIONS:
i = i.replace(s[0], s[1])
encoded = i.encode('utf8')
encoded = encoded.replace(b'<!here>', b'yelling [%s]' % self.nick)
encoded = encoded.replace(b'<!channel>', b'YELLING LOUDER [%s]' % self.nick)
yield encoded
def _message(self, sl_ev: Union[slack.Message, slack.MessageDelete, slack.MessageBot], prefix: str=''):
"""
Sends a message to the irc client
"""
if hasattr(sl_ev, 'user'):
source = self.sl_client.get_user(sl_ev.user).name.encode('utf8') # type: ignore
if source == self.nick:
return
else:
source = b'bot'
try:
dest = b'#' + self.sl_client.get_channel(sl_ev.channel).name.encode('utf8')
except KeyError:
dest = source
except Exception as e:
print('Error: ', str(e))
return
if dest in self.parted_channels:
# Ignoring messages, channel was left on IRC
return
for msg in self.parse_message(prefix + sl_ev.text):
self.sendmsg(
source,
dest,
msg
)
def _joined(self, sl_ev: slack.Join) -> None:
"""
Handle join events from slack, by sending a JOIN notification
to IRC.
"""
user = self.sl_client.get_user(sl_ev.user)
if user.deleted:
return
channel = self.sl_client.get_channel(sl_ev.channel)
dest = b'#' + channel.name.encode('utf8')
if dest in self.parted_channels:
return
name = user.name.encode('utf8')
rname = user.real_name.replace(' ', '_').encode('utf8')
self.s.send(b':%s!%[email protected] JOIN :%s\n' % (name, rname, dest))
def slack_event(self, sl_ev: slack.SlackEvent) -> None:
if isinstance(sl_ev, slack.MessageDelete):
self._message(sl_ev, '[deleted]')
elif isinstance(sl_ev, slack.Message):
self._message(sl_ev)
elif isinstance(sl_ev, slack.MessageEdit):
if sl_ev.is_changed:
self._message(sl_ev.diffmsg)
elif isinstance(sl_ev, slack.MessageBot):
self._message(sl_ev, '[%s]' % sl_ev.username)
elif isinstance(sl_ev, slack.FileShared):
f = self.sl_client.get_file(sl_ev)
self._message(f.announce())
elif isinstance(sl_ev, slack.Join):
self._joined(sl_ev)
def command(self, cmd: bytes) -> None:
if b' ' in cmd:
cmdid, _ = cmd.split(b' ', 1)
else:
cmdid = cmd
handlers = {
b'NICK': self._nickhandler,
b'USER': self._userhandler,
b'PING': self._pinghandler,
b'JOIN': self._joinhandler,
b'PRIVMSG': self._privmsghandler,
b'LIST': self._listhandler,
b'WHO': self._whohandler,
b'MODE': self._modehandler,
b'PART': self._parthandler,
b'AWAY': self._awayhandler,
b'sendfile': self._sendfilehandler,
#QUIT
#CAP LS
#USERHOST
#Unknown command: b'TOPIC #cama :titolo del canale'
#Unknown command: b'whois TAMARRO'
}
if cmdid in handlers:
handlers[cmdid](cmd)
else:
self._sendreply(Replies.ERR_UNKNOWNCOMMAND, 'Unknown command', [cmdid])
print('Unknown command: ', cmd)
def main() -> None:
parser = argparse.ArgumentParser()
parser.add_argument('-p', '--port', type=int, action='store', dest='port',
default=9007, required=False,
help='set port number')
parser.add_argument('-i', '--ip', type=str, action='store', dest='ip',
default='127.0.0.1', required=False,
help='set ip address')
parser.add_argument('-t', '--tokenfile', type=str, action='store', dest='tokenfile',
default=expanduser('~')+'/.localslackirc',
required=False,
help='set the token file')
parser.add_argument('-u', '--nouserlist', action='store_true',
dest='nouserlist', required=False,
help='don\'t display userlist')
parser.add_argument('-j', '--autojoin', action='store_true',
dest='autojoin', required=False,
help="Automatically join all remote channels")
parser.add_argument('-o', '--override', action='store_true',
dest='overridelocalip', required=False,
help='allow non 127. addresses, this is potentially dangerous')
args = parser.parse_args()
# Exit if their chosden ip isn't local. User can override with -o if they so dare
if not args.ip.startswith('127') and not args.overridelocalip:
exit('supplied ip isn\'t local\nlocalslackirc has no encryption or ' \
'authentication, it\'s recommended to only allow local connections\n' \
'you can override this with -o')
if 'PORT' in environ:
port = int(environ['PORT'])
else:
port = args.port
if 'TOKEN' in environ:
token = environ['TOKEN']
else:
try:
with open(args.tokenfile) as f:
token = f.readline().strip()
except (FileNotFoundError, PermissionError):
exit(f'Unable to open the token file {args.tokenfile}')
sl_client = slack.Slack(token)
sl_events = sl_client.events_iter()
serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
serversocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
serversocket.bind((args.ip, port))
serversocket.listen(1)
poller = select.poll()
while True:
s, _ = serversocket.accept()
ircclient = Client(s, sl_client, nouserlist=args.nouserlist, autojoin=args.autojoin)
poller.register(s.fileno(), select.POLLIN)
if sl_client.fileno is not None:
poller.register(sl_client.fileno, select.POLLIN)
# Main loop
timeout = 7
while True:
s_event = poller.poll(timeout) # type: List[Tuple[int,int]]
sl_event = next(sl_events)
if s_event:
text = s.recv(1024)
if len(text) == 0:
break
#FIXME handle the case when there is more to be read
for i in text.split(b'\n')[:-1]:
i = i.strip()
if i:
ircclient.command(i)
while sl_event:
print(sl_event)
ircclient.slack_event(sl_event)
sl_event = next(sl_events)
if __name__ == '__main__':
while True:
try:
main()
except KeyboardInterrupt:
break
except:
pass