17 # Defaults, may be overwritten by command line arguments.
18 SERVER = "irc.freenode.net"
21 USERNAME = "plomlombot"
24 DBDIR = os.path.expanduser("~/plomlombot_db")
27 class ExceptionForRestart(Exception):
33 def __init__(self, line):
35 self.tokens = line.split(" ")
37 if self.tokens[0][0] == ":":
38 for rune in self.tokens[0][1:]:
39 if rune in {"!", "@"}:
43 if len(self.tokens) > 2:
44 for rune in self.tokens[2]:
45 if rune in {"!", "@"}:
53 def __init__(self, server, port, timeout):
54 self.timeout = timeout
55 self.socket = socket.socket()
57 self.socket.connect((server, port))
59 raise ExceptionForRestart
60 self.socket.setblocking(0)
63 self.last_pong = time.time()
64 self.servername = self.recv_line(send_ping=False).split(" ")[0][1:]
66 def _pingtest(self, send_ping=True):
67 if self.last_pong + self.timeout < time.time():
68 print("SERVER NOT ANSWERING")
69 raise ExceptionForRestart
71 self.send_line("PING " + self.servername)
73 def send_line(self, msg):
74 msg = msg.replace("\r", " ")
75 msg = msg.replace("\n", " ")
76 if len(msg.encode("utf-8")) > 510:
77 print("NOT SENT LINE TO SERVER (too long): " + msg)
78 print("LINE TO SERVER: "
79 + str(datetime.datetime.now()) + ": " + msg)
83 while total_sent_len < msg_len:
84 sent_len = self.socket.send(bytes(msg[total_sent_len:], "UTF-8"))
86 print("SOCKET CONNECTION BROKEN")
87 raise ExceptionForRestart
88 total_sent_len += sent_len
90 def _recv_line_wrapped(self, send_ping=True):
91 if len(self.line_buffer) > 0:
92 return self.line_buffer.pop(0)
94 ready = select.select([self.socket], [], [], int(self.timeout / 2))
96 self._pingtest(send_ping)
98 self.last_pong = time.time()
99 received_bytes = self.socket.recv(1024)
101 received_runes = received_bytes.decode("UTF-8")
102 except UnicodeDecodeError:
103 received_runes = received_bytes.decode("latin1")
104 if len(received_runes) == 0:
105 print("SOCKET CONNECTION BROKEN")
106 raise ExceptionForRestart
107 self.rune_buffer += received_runes
108 lines_split = str.split(self.rune_buffer, "\r\n")
109 self.line_buffer += lines_split[:-1]
110 self.rune_buffer = lines_split[-1]
111 if len(self.line_buffer) > 0:
112 return self.line_buffer.pop(0)
114 def recv_line(self, send_ping=True):
115 line = self._recv_line_wrapped(send_ping)
117 print("LINE FROM SERVER " + str(datetime.datetime.now()) + ": " +
122 def handle_command(command, argument, notice, target, session):
125 if not os.access(session.quotesfile, os.F_OK):
126 quotesfile = open(session.quotesfile, "w")
127 quotesfile.write("QUOTES FOR " + target + ":\n")
129 quotesfile = open(session.quotesfile, "a")
130 quotesfile.write(argument + "\n")
132 quotesfile = open(session.quotesfile, "r")
133 lines = quotesfile.readlines()
135 notice("ADDED QUOTE #" + str(len(lines) - 1))
140 notice("SYNTAX: !quote [int] OR !quote search QUERY")
141 notice("QUERY may be a boolean grouping of quoted or unquoted " +
142 "search terms, examples:")
143 notice("!quote search foo")
144 notice("!quote search foo AND (bar OR NOT baz)")
145 notice("!quote search \"foo\\\"bar\" AND ('NOT\"' AND \"'foo'\"" +
151 tokens = argument.split(" ")
152 if (len(tokens) > 1 and tokens[0] != "search") or \
153 (len(tokens) == 1 and
154 (tokens[0] == "search" or not tokens[0].isdigit())):
157 if not os.access(session.quotesfile, os.F_OK):
158 notice("NO QUOTES AVAILABLE")
160 quotesfile = open(session.quotesfile, "r")
161 lines = quotesfile.readlines()
166 if i == 0 or i > len(lines):
167 notice("THERE'S NO QUOTE OF THAT INDEX")
170 elif len(tokens) > 1:
171 query = str.join(" ", tokens[1:])
173 results = plomsearch.search(query, lines)
174 except plomsearch.LogicParserError as err:
175 notice("FAILED QUERY PARSING: " + str(err))
177 if len(results) == 0:
178 notice("NO QUOTES MATCHING QUERY")
180 for result in results:
181 notice("QUOTE #" + str(result[0] + 1) + " : "
185 i = random.randrange(len(lines))
186 notice("QUOTE #" + str(i + 1) + ": " + lines[i][:-1])
189 from random import choice, shuffle
194 usable_selections = []
195 for i in range(select_length, 0, -1):
196 for selection in selections:
200 if snippet[-j] != selection[-(j+1)]:
204 usable_selections += [selection]
205 if [] != usable_selections:
207 if [] == usable_selections:
208 usable_selections = selections
209 selection = choice(usable_selections)
210 return selection[select_length]
212 if not os.access(session.markovfile, os.F_OK):
213 notice("NOT ENOUGH TEXT TO MARKOV.")
216 # Lowercase incoming lines, ensure they end in a sentence end mark.
217 file = open(session.markovfile, "r")
218 lines = file.readlines()
221 sentence_end_markers = ".!?)("
223 line = line.lower().replace("\n", "")
224 if line[-1] not in sentence_end_markers:
226 tokens += line.split()
227 if len(tokens) <= select_length:
228 notice("NOT ENOUGH TEXT TO MARKOV.")
231 # Replace URLs with escape string for now, so that the Markov selector
232 # won't see them as different strings. Stash replaced URLs in urls.
235 url_starts = ["http://", "https://", "<http://", "<https://"]
236 for i in range(len(tokens)):
237 for url_start in url_starts:
238 if tokens[i][:len(url_start)] == url_start:
239 length = len(tokens[i])
240 if url_start[0] == "<":
242 length = tokens[i].index(">") + 1
245 urls += [tokens[i][:length]]
246 tokens[i] = url_escape + tokens[i][length:]
249 # For each snippet of select_length, use markov() to find continuation
250 # token from selections. Replace present users' names with malkovich.
251 # Start snippets with the beginning of a sentence, if possible.
252 for i in range(len(tokens) - select_length):
254 for j in range(select_length + 1):
255 token_list += [tokens[i + j]]
256 selections += [token_list]
258 for i in range(select_length):
261 for i in range(len(selections)):
262 if selections[i][0][-1] in sentence_end_markers:
263 for i in range(select_length):
264 snippet[i] = selections[i][i + 1]
267 malkovich = "malkovich"
269 new_end = markov(snippet)
270 for name in session.users_in_chan:
271 if new_end[:len(name)] == name.lower():
272 new_end = malkovich + new_end[len(name):]
274 if len(msg) + len(new_end) > 200:
277 for i in range(select_length - 1):
278 snippet[i] = snippet[i + 1]
279 snippet[select_length - 1] = new_end
281 # Replace occurences of url escape string with random choice from urls.
283 index = msg.find(url_escape)
286 msg = msg.replace(url_escape, choice(urls), 1)
288 # More meaningful ways to randomly end sentences.
289 notice(msg + malkovich + ".")
294 twtfile = open(session.twtfile, mode)
295 except (PermissionError, FileNotFoundError) as err:
296 notice("CAN'T ACCESS OR CREATE TWT FILE: " + str(err))
300 from datetime import datetime
301 if not os.access(session.twtfile, os.F_OK):
302 twtfile = try_open("w")
306 twtfile = try_open("a")
309 twtfile.write(datetime.utcnow().isoformat() + "\t" + argument + "\n")
313 if "addquote" == command:
315 elif "quote" == command:
317 elif "markov" == command:
319 elif "twt" == command:
323 def handle_url(url, notice, show_url=False):
325 def mobile_twitter_hack(url):
326 re1 = 'https?://(mobile.twitter.com/)[^/]+(/status/)'
327 re2 = 'https?://mobile.twitter.com/([^/]+)/status/([^\?/]+)'
328 m = re.search(re1, url)
329 if m and m.group(1) == 'mobile.twitter.com/' \
330 and m.group(2) == '/status/':
331 m = re.search(re2, url)
332 url = 'https://twitter.com/' + m.group(1) + '/status/' + m.group(2)
333 handle_url(url, notice, True)
337 r = requests.get(url, timeout=15)
338 except (requests.exceptions.TooManyRedirects,
339 requests.exceptions.ConnectionError,
340 requests.exceptions.InvalidURL,
342 requests.exceptions.InvalidSchema) as error:
343 notice("TROUBLE FOLLOWING URL: " + str(error))
345 if mobile_twitter_hack(url):
347 title = bs4.BeautifulSoup(r.text, "html5lib").title
348 if title and title.string:
349 prefix = "PAGE TITLE: "
351 prefix = "PAGE TITLE FOR <" + url + ">: "
352 notice(prefix + title.string.strip())
354 notice("PAGE HAS NO TITLE TAG")
359 def __init__(self, io, username, nickname, channel, twtfile, dbdir):
361 self.nickname = nickname
362 self.username = username
363 self.channel = channel
364 self.users_in_chan = []
365 self.twtfile = twtfile
367 self.io.send_line("NICK " + self.nickname)
368 self.io.send_line("USER " + self.username + " 0 * : ")
369 self.io.send_line("JOIN " + self.channel)
370 hash_channel = hashlib.md5(self.channel.encode("utf-8")).hexdigest()
371 self.chandir = self.dbdir + "/" + hash_channel + "/"
372 self.rawlogdir = self.chandir + "raw_logs/"
373 self.logdir = self.chandir + "logs/"
374 if not os.path.exists(self.logdir):
375 os.makedirs(self.logdir)
376 if not os.path.exists(self.rawlogdir):
377 os.makedirs(self.rawlogdir)
378 self.markovfile = self.chandir + "markovfeed"
379 self.quotesfile = self.chandir + "quotes"
384 if type(line) == str:
385 line = Line(":" + self.nickname + "!~" + self.username +
386 "@localhost" + " " + line)
387 now = datetime.datetime.utcnow()
388 logfile = open(self.rawlogdir + now.strftime("%Y-%m-%d") + ".txt", "a")
389 form = "%Y-%m-%d %H:%M:%S UTC\t"
390 logfile.write(now.strftime(form) + " " + line.line + "\n")
392 to_log = irclog.format_logline(line, self.channel)
394 logfile = open(self.logdir + now.strftime("%Y-%m-%d") + ".txt", "a")
395 logfile.write(now.strftime(form) + " " + to_log + "\n")
398 def handle_privmsg(line):
401 line = "NOTICE " + target + " :" + msg
402 self.io.send_line(line)
406 if line.receiver != self.nickname:
407 target = line.receiver
408 msg = str.join(" ", line.tokens[3:])[1:]
409 matches = re.findall("(https?://[^\s>]+)", msg)
410 for i in range(len(matches)):
411 handle_url(matches[i], notice)
413 tokens = msg[1:].split()
414 argument = str.join(" ", tokens[1:])
415 handle_command(tokens[0], argument, notice, target, self)
417 file = open(self.markovfile, "a")
418 file.write(msg + "\n")
422 line = self.io.recv_line()
427 if len(line.tokens) > 1:
428 if line.tokens[0] == "PING":
429 self.io.send_line("PONG " + line.tokens[1])
430 elif line.tokens[1] == "PRIVMSG":
432 elif line.tokens[1] == "353":
433 names = line.tokens[5:]
434 names[0] = names[0][1:]
435 for i in range(len(names)):
436 names[i] = names[i].replace("@", "").replace("+", "")
437 self.users_in_chan += names
438 elif line.tokens[1] == "JOIN" and line.sender != self.nickname:
439 self.users_in_chan += [line.sender]
440 elif line.tokens[1] == "PART":
441 del(self.users_in_chan[self.users_in_chan.index(line.sender)])
442 elif line.tokens[1] == "NICK":
443 del(self.users_in_chan[self.users_in_chan.index(line.sender)])
444 self.users_in_chan += [line.receiver]
447 def parse_command_line_arguments():
448 parser = argparse.ArgumentParser()
449 parser.add_argument("-s, --server", action="store", dest="server",
451 help="server or server net to connect to (default: "
453 parser.add_argument("-p, --port", action="store", dest="port", type=int,
454 default=PORT, help="port to connect to (default : "
456 parser.add_argument("-w, --wait", action="store", dest="timeout",
457 type=int, default=TIMEOUT,
458 help="timeout in seconds after which to attempt " +
459 "reconnect (default: " + str(TIMEOUT) + ")")
460 parser.add_argument("-u, --username", action="store", dest="username",
461 default=USERNAME, help="username to use (default: "
463 parser.add_argument("-n, --nickname", action="store", dest="nickname",
464 default=NICKNAME, help="nickname to use (default: "
466 parser.add_argument("-t, --twtxtfile", action="store", dest="twtfile",
467 default=TWTFILE, help="twtxt file to use (default: "
469 parser.add_argument("-d, --dbdir", action="store", dest="dbdir",
470 default=DBDIR, help="directory to store DB files in")
471 parser.add_argument("CHANNEL", action="store", help="channel to join")
472 opts, unknown = parser.parse_known_args()
476 opts = parse_command_line_arguments()
479 io = IO(opts.server, opts.port, opts.timeout)
480 hash_server = hashlib.md5(opts.server.encode("utf-8")).hexdigest()
481 dbdir = opts.dbdir + "/" + hash_server
482 session = Session(io, opts.username, opts.nickname, opts.CHANNEL,
485 except ExceptionForRestart: