-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathirc_worker.py
executable file
·408 lines (361 loc) · 13 KB
/
irc_worker.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
#! /usr/bin/env python
#
# Example program using irc.bot.
#
# Joel Rosdahl <[email protected]>
# slight modifications by Foaad Khosmood
# futher modification by Michael Fekadu to chat with Rasa over REST webhook
#
# Original Source: https://github.com/jaraco/irc
#
# NOTE: The SimpleIRCClient._dispatcher will seek functions named "on_<event_type>"
# * Event Types:
# * https://github.com/jaraco/irc/blob/1331ba85b5d093f06304d316a03a832959eaf4da/irc/events.py#L1-L201
# * Source:
# * https://github.com/jaraco/irc/blob/1331ba85b5d093f06304d316a03a832959eaf4da/irc/client.py#L1119
"""A simple IRC bot.
# Michael:
# This bot/worker serves as Rasa's eyes and ears.
# It handles simple commands, and passes all else to Rasa via REST.
This is an example bot that uses the SingleServerIRCBot class from
irc.bot. The bot enters a channel and listens for commands in
private messages and channel traffic. Commands in channel messages
are given by prefixing the text by the bot name followed by a colon.
It also responds to DCC CHAT invitations and echos data sent in such
sessions.
The known commands are:
stats -- Prints some channel information.
disconnect -- Disconnect the bot. The bot will try to reconnect
after 60 seconds.
die -- Let the bot cease to exist.
dcc -- Let the bot invite you to a DCC CHAT connection.
"""
import logging
import os
import sys
import irc.bot
import irc.strings
from irc.client import ip_numstr_to_quad, ip_quad_to_numstr
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
# create console handler and set level to debug
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
# create formatter
formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
# add formatter to ch
ch.setFormatter(formatter)
# add ch to logger
logger.addHandler(ch)
try:
from rasa.core.constants import DEFAULT_SERVER_URL
except ModuleNotFoundError:
logging.warning("No rasa? Consider: `pip install rasa`")
DEFAULT_SERVER_URL = "http://localhost:5005"
DEFAULT_SERVER_URL = os.environ.get("RASA_SERVER_URL", DEFAULT_SERVER_URL)
IRC_CHANNEL = os.environ.get("RASA_IRC_CHANNEL", "#CPE582")
IRC_SERVER = os.environ.get("RASA_IRC_SERVER", "irc.freenode.net")
IRC_PORT = os.environ.get("RASA_IRC_PORT", 6667)
class RasaIRCBot(irc.bot.SingleServerIRCBot):
def __init__(self, channel, nickname, server, port=6667):
irc.bot.SingleServerIRCBot.__init__(self, [(server, port)], nickname, nickname)
self.channel = channel
def get_version(self):
"""
https://github.com/jaraco/irc/blob/1331ba85b5d093f06304d316a03a832959eaf4da/irc/bot.py#L310-L315
"""
logger.debug("function called: get_version...")
super(RasaIRCBot, self).get_version()
logger.debug("completed: get_version...")
def die(self, msg="Bye, cruel world!"):
"""
https://github.com/jaraco/irc/blob/1331ba85b5d093f06304d316a03a832959eaf4da/irc/bot.py#L288-L297
"""
logger.debug("function called: die...")
super(RasaIRCBot, self).die(msg=msg)
logger.debug("completed: die...")
def disconnect(self, msg="I'll be back!"):
"""
https://github.com/jaraco/irc/blob/1331ba85b5d093f06304d316a03a832959eaf4da/irc/bot.py#L299-L308
"""
logger.debug("function called: disconnect...")
super(RasaIRCBot, self).disconnect(msg=msg)
logger.debug("completed: disconnect...")
def jump_server(self, msg="Changing servers"):
"""
https://github.com/jaraco/irc/blob/1331ba85b5d093f06304d316a03a832959eaf4da/irc/bot.py#L317-L327
"""
logger.debug("function called: jump_server...")
super(RasaIRCBot, self).jump_server(msg=msg)
logger.debug("completed: jump_server...")
def _connect(self):
logger.debug("function called: _connect...")
super(RasaIRCBot, self)._connect()
logger.debug("completed: _connect...")
def _on_disconnect(self, connection, event):
logger.debug(
(
"function called: _on_disconnect...\n"
f"connection: {connection}\n"
f"event: {event}\n"
)
)
super(RasaIRCBot, self)._on_disconnect(connection, event)
logger.debug("completed: _on_disconnect...")
def _on_join(self, connection, event):
logger.debug(
(
"function called: _on_join...\n"
f"connection: {connection}\n"
f"event: {event}\n"
)
)
super(RasaIRCBot, self)._on_join(connection, event)
logger.debug("completed: _on_join...")
def _on_kick(self, connection, event):
logger.debug(
(
"function called: _on_kick...\n"
f"connection: {connection}\n"
f"event: {event}\n"
)
)
super(RasaIRCBot, self)._on_kick(connection, event)
logger.debug("completed: _on_kick...")
def _on_mode(self, connection, event):
logger.debug(
(
"function called: _on_mode...\n"
f"connection: {connection}\n"
f"event: {event}\n"
)
)
super(RasaIRCBot, self)._on_mode(connection, event)
logger.debug("completed: _on_mode...")
def _on_namreply(self, connection, event):
logger.debug(
(
"function called: _on_namreply...\n"
f"connection: {connection}\n"
f"event: {event}\n"
)
)
super(RasaIRCBot, self)._on_namreply(connection, event)
logger.debug("completed: _on_namreply...")
def _on_nick(self, connection, event):
logger.debug(
(
"function called: _on_nick...\n"
f"connection: {connection}\n"
f"event: {event}\n"
)
)
super(RasaIRCBot, self)._on_nick(connection, event)
logger.debug("completed: _on_nick...")
def _on_part(self, connection, event):
logger.debug(
(
"function called: _on_part...\n"
f"connection: {connection}\n"
f"event: {event}\n"
)
)
super(RasaIRCBot, self)._on_part(connection, event)
logger.debug("completed: _on_part...")
def _on_quit(self, connection, event):
logger.debug(
(
"function called: _on_quit...\n"
f"connection: {connection}\n"
f"event: {event}\n"
)
)
super(RasaIRCBot, self)._on_quit(connection, event)
logger.debug("completed: _on_quit...")
def on_ctcp(self, connection, event):
"""
https://github.com/jaraco/irc/blob/1331ba85b5d093f06304d316a03a832959eaf4da/irc/bot.py#L329-L345
"""
logger.debug(
(
"handler called: on_ctcp...\n"
f"connection: {connection}\n"
f"event: {event}\n"
)
)
super(RasaIRCBot, self).on_ctcp(connection, event)
logger.debug("completed: on_ctcp...")
def on_dccchat(self, connection, event):
"""
https://github.com/jaraco/irc/blob/1331ba85b5d093f06304d316a03a832959eaf4da/irc/bot.py#L347-L348
"""
logger.debug(
(
"handler called: on_dccchat...\n"
f"connection: {connection}\n"
f"event: {event}\n"
)
)
super(RasaIRCBot, self).on_dccchat(connection, event)
logger.debug("completed: super on_dccchat...")
logger.debug("doing RasaIRCBot.on_dccchat...")
if len(event.arguments) != 2:
return
args = event.arguments[1].split()
if len(args) == 4:
try:
address = ip_numstr_to_quad(args[2])
port = int(args[3])
except ValueError:
return
self.dcc_connect(address, port)
logger.debug("compelted on_dccchat...")
def start(self):
"""
https://github.com/jaraco/irc/blob/1331ba85b5d093f06304d316a03a832959eaf4da/irc/bot.py#L350-L353
"""
logger.debug("starting bot...")
super(RasaIRCBot, self).start()
logger.debug("started bot!!!")
def on_nicknameinuse(self, connection, event):
logger.debug(
(
"handler called: on_nicknameinuse...\n"
f"connection: {connection}\n"
f"event: {event}\n"
)
)
connection.nick(connection.get_nickname() + "_")
def on_welcome(self, connection, event):
logger.debug(
(
"handler called: on_welcome...\n"
f"connection: {connection}\n"
f"event: {event}\n"
)
)
connection.join(self.channel)
def on_privmsg(self, connection, event):
logger.debug(
(
"handler called: on_privmsg...\n"
f"connection: {connection}\n"
f"event: {event}\n"
)
)
self.do_command(event, event.arguments[0])
def on_pubmsg(self, connection, event):
logger.debug(
(
"handler called: on_pubmsg...\n"
f"connection: {connection}\n"
f"event: {event}\n"
)
)
a = event.arguments[0].split(":", 1)
if len(a) > 1 and irc.strings.lower(a[0]) == irc.strings.lower(
self.connection.get_nickname()
):
self.do_command(event, a[1].strip())
return
def on_dccmsg(self, connection, event):
logger.debug(
(
"handler called: on_dccmsg...\n"
f"connection: {connection}\n"
f"event: {event}\n"
)
)
# non-chat DCC messages are raw bytes; decode as text
text = event.arguments[0].decode("utf-8")
connection.privmsg("You said: " + text)
def do_command(self, event, command):
logger.debug(
(
"function called: do_command...\n"
f"command: {command}\n"
f"event: {event}\n"
)
)
nick = event.source.nick
c = self.connection
if command == "disconnect":
self.disconnect()
elif command == "die":
self.die()
elif command == "stats":
for chname, chobj in self.channels.items():
c.notice(nick, "--- Channel statistics ---")
c.notice(nick, "Channel: " + chname)
users = sorted(chobj.users())
c.notice(nick, "Users: " + ", ".join(users))
opers = sorted(chobj.opers())
c.notice(nick, "Opers: " + ", ".join(opers))
voiced = sorted(chobj.voiced())
c.notice(nick, "Voiced: " + ", ".join(voiced))
elif command == "dcc":
dcc = self.dcc_listen()
c.ctcp(
"DCC",
nick,
"CHAT chat %s %d"
% (ip_quad_to_numstr(dcc.localaddress), dcc.localport),
)
elif command == "hello": # Foaad: change this
c.privmsg(self.channel, "well double hello to you too!")
elif command == "about": # Foaad: add your name
c.privmsg(
self.channel,
"I was made by Dr. Foaad Khosmood for the CPE 466 class in Spring 2016. I was furthere modified by _____",
)
elif command == "usage":
# Foaad: change this
c.privmsg(self.channel, "I can answer questions like this: ....")
else:
c.notice(nick, "Not understood: " + command)
def ensure_hashtag_channel(ch_name):
return f"#{ch_name}" if ch_name[0] != "#" else ch_name
def print_usage():
print("Usage: irc_worker <server[:port]> <channel> <nickname>")
print()
print("Usage: irc_worker <nickname>")
print()
print(f"\tserver default: {IRC_SERVER}")
print(f"\tport default: {IRC_PORT}")
print(f"\tchannel default: {IRC_CHANNEL}")
print()
def exit_usage():
print_usage()
sys.exit(1)
def main():
logger.setLevel(10)
if len(sys.argv) <= 1:
exit_usage()
elif len(sys.argv) == 2:
print_usage()
nickname = sys.argv[1]
channel = IRC_CHANNEL
server = IRC_SERVER
port = int(IRC_PORT)
elif len(sys.argv) == 4:
url = sys.argv[1]
server, port = url.split(":", 1) if ":" in url else (url, IRC_PORT)
port = int(port)
channel = sys.argv[2]
nickname = sys.argv[3]
else:
exit_usage()
channel = ensure_hashtag_channel(channel)
logger.info(
(
"\n\tinitalizing with:\n"
f"\t\tchannel = {channel}\n"
f"\t\tnickname = {nickname}\n"
f"\t\tserver = {server}\n"
f"\t\tport = {port}\n"
)
)
bot = RasaIRCBot(channel=channel, nickname=nickname, server=server, port=port)
bot.start()
if __name__ == "__main__":
main()