18 # Defaults, may be overwritten by command line arguments.
19 SERVER = "irc.freenode.net"
22 USERNAME = "plomlombot"
25 DBDIR = os.path.expanduser("~/plomlombot_db")
28 def write_to_file(path, mode, text):
34 class ExceptionForRestart(Exception):
40 def __init__(self, line):
42 self.tokens = line.split(" ")
44 if self.tokens[0][0] == ":":
45 for rune in self.tokens[0][1:]:
46 if rune in {"!", "@"}:
50 if len(self.tokens) > 2:
51 for rune in self.tokens[2]:
52 if rune in {"!", "@"}:
60 def __init__(self, chandir, nickname, username, channel, rmlogs):
61 self.nickname = nickname
62 self.username = username
63 self.channel = channel
64 self.chandir = chandir
65 self.rmlogcycle = rmlogs
66 self.rawlogdir = chandir + "raw_logs/"
67 self.logdir = chandir + "logs/"
68 if not os.path.exists(self.logdir):
69 os.makedirs(self.logdir)
70 if not os.path.exists(self.rawlogdir):
71 os.makedirs(self.rawlogdir)
73 def log(self, line, sent=False):
78 line = Line("< " + line)
79 line.sender = self.nickname
80 identity = self.username + "@localhost"
84 now = datetime.datetime.utcnow()
85 form = "%Y-%m-%d %H:%M:%S UTC"
86 write_to_file(self.rawlogdir + now.strftime("%Y-%m-%d") + ".txt", "a",
87 now.strftime(form) + separator + line.line + "\n")
88 to_log = irclog.format_logline(line, self.channel, identity)
90 write_to_file(self.logdir + now.strftime("%Y-%m-%d") + ".txt", "a",
91 now.strftime(form) + " " + to_log + "\n")
94 if self.rmlogcycle > 0:
95 for f in os.listdir(self.logdir):
96 f = os.path.join(self.logdir, f)
97 if os.path.isfile(f) and \
98 os.stat(f).st_mtime < time.time() - self.rmlogcycle:
101 def separator_line(self):
102 now = datetime.datetime.utcnow()
103 write_to_file(self.logdir + now.strftime("%Y-%m-%d") + ".txt", "a",
104 "-----------------------\n")
109 def __init__(self, server, port, timeout):
111 self.timeout = timeout
112 self.socket = socket.socket()
114 self.socket.connect((server, port))
116 raise ExceptionForRestart
117 self.socket.setblocking(0)
118 self.line_buffer = []
119 self.rune_buffer = ""
120 self.last_pong = time.time()
121 line = self.recv_line(send_ping=False)
122 if not line or len(line) < 1:
123 raise ExceptionForRestart
124 self.servername = line.split(" ")[0][1:]
126 def _pingtest(self, send_ping=True):
127 if self.last_pong + self.timeout < time.time():
128 print("SERVER NOT ANSWERING")
129 raise ExceptionForRestart
131 self.send_line("PING " + self.servername)
133 def send_line(self, msg):
134 msg = msg.replace("\r", " ")
135 msg = msg.replace("\n", " ")
136 if len(msg.encode("utf-8")) > 510:
137 print("NOT SENT LINE TO SERVER (too long): " + msg)
138 print("LINE TO SERVER: "
139 + str(datetime.datetime.now()) + ": " + msg)
141 self.log.log(msg, True)
145 while total_sent_len < msg_len:
146 sent_len = self.socket.send(bytes(msg[total_sent_len:], "UTF-8"))
148 print("SOCKET CONNECTION BROKEN")
149 raise ExceptionForRestart
150 total_sent_len += sent_len
152 def _recv_line_wrapped(self, send_ping=True):
153 if len(self.line_buffer) > 0:
154 return self.line_buffer.pop(0)
156 ready = select.select([self.socket], [], [], int(self.timeout / 2))
158 self._pingtest(send_ping)
160 self.last_pong = time.time()
161 received_bytes = self.socket.recv(1024)
163 received_runes = received_bytes.decode("UTF-8")
164 except UnicodeDecodeError:
165 received_runes = received_bytes.decode("latin1")
166 if len(received_runes) == 0:
167 print("SOCKET CONNECTION BROKEN")
168 raise ExceptionForRestart
169 self.rune_buffer += received_runes
170 lines_split = str.split(self.rune_buffer, "\r\n")
171 self.line_buffer += lines_split[:-1]
172 self.rune_buffer = lines_split[-1]
173 if len(self.line_buffer) > 0:
174 return self.line_buffer.pop(0)
176 def recv_line(self, send_ping=True):
177 line = self._recv_line_wrapped(send_ping)
181 print("LINE FROM SERVER " + str(datetime.datetime.now()) + ": " +
186 def handle_command(command, argument, notice, target, session):
189 if not os.access(session.quotesfile, os.F_OK):
190 write_to_file(session.quotesfile, "w",
191 "QUOTES FOR " + target + ":\n")
192 write_to_file(session.quotesfile, "a", argument + "\n")
193 quotesfile = open(session.quotesfile, "r")
194 lines = quotesfile.readlines()
196 notice("added quote #" + str(len(lines) - 1))
201 notice("syntax: !quote [int] OR !quote search QUERY "
202 "OR !quote offset-search [int] QUERY")
203 notice("QUERY may be a boolean grouping of quoted or unquoted " +
204 "search terms, examples:")
205 notice("!quote search foo")
206 notice("!quote search foo AND (bar OR NOT baz)")
207 notice("!quote search \"foo\\\"bar\" AND ('NOT\"' AND \"'foo'\"" +
209 notice("The offset-search int argument defines how many matches "
210 "to skip (useful if results are above maximum number to "
216 tokens = argument.split(" ")
217 if (len(tokens) == 1 and not tokens[0].isdigit()) or \
219 (tokens[0] not in {"search", "offset-search"} or
220 (tokens[0] == "offset-search" and
221 ((not len(tokens) > 2) or (not tokens[1].isdigit()))))):
224 if not os.access(session.quotesfile, os.F_OK):
225 notice("no quotes available")
227 quotesfile = open(session.quotesfile, "r")
228 lines = quotesfile.readlines()
233 if i == 0 or i > len(lines):
234 notice("there's no quote of that index")
237 elif len(tokens) > 1:
239 if tokens[0] == "search":
240 query = str.join(" ", tokens[1:])
241 elif tokens[0] == "offset-search":
242 to_skip = int(tokens[1])
243 query = str.join(" ", tokens[2:])
245 results = plomsearch.search(query, lines)
246 except plomsearch.LogicParserError as err:
247 notice("failed query parsing: " + str(err))
249 if len(results) == 0:
250 notice("no quotes matching query")
252 if to_skip >= len(results):
253 notice("skipped all quotes matching query")
255 notice("found %s matches, showing max. 3, skipping %s"
256 % (len(results), to_skip))
257 for i in range(len(results)):
258 if i >= to_skip and i < to_skip + 3:
260 notice("quote #" + str(result[0] + 1) + ": "
264 i = random.randrange(len(lines))
265 notice("quote #" + str(i + 1) + ": " + lines[i][:-1])
270 notice("syntax: !markov [integer from 1 to infinite]")
273 usable_selections = []
274 for i in range(select_length, 0, -1):
275 for selection in selections:
279 if snippet[-j] != selection[-(j+1)]:
283 usable_selections += [selection]
284 if [] != usable_selections:
286 if [] == usable_selections:
287 usable_selections = selections
288 selection = choice(usable_selections)
289 return selection[select_length]
294 tokens = argument.split(" ")
295 if (len(tokens) > 1 or (len(tokens) == 1 and not tokens[0].isdigit())):
299 from random import choice, shuffle
306 notice("bad value, using default: " + str(select_length))
309 if not os.access(session.markovfile, os.F_OK):
310 notice("not enough text to markov for selection length")
313 # Lowercase incoming lines, ensure they end in a sentence end mark.
314 file = open(session.markovfile, "r")
315 lines = file.readlines()
318 sentence_end_markers = ".!?)("
320 line = line.lower().replace("\n", "")
321 if line[-1] not in sentence_end_markers:
323 tokens += line.split()
324 if len(tokens) - 1 <= select_length:
325 notice("not enough text to markov")
328 # Replace URLs with escape string for now, so that the Markov selector
329 # won't see them as different strings. Stash replaced URLs in urls.
332 url_starts = ["http://", "https://", "<http://", "<https://"]
333 for i in range(len(tokens)):
334 for url_start in url_starts:
335 if tokens[i][:len(url_start)] == url_start:
336 length = len(tokens[i])
337 if url_start[0] == "<":
339 length = tokens[i].index(">") + 1
342 urls += [tokens[i][:length]]
343 tokens[i] = url_escape + tokens[i][length:]
346 # For each snippet of select_length, use markov() to find continuation
347 # token from selections. Replace present users' names with malkovich.
348 # Start snippets with the beginning of a sentence, if possible.
349 for i in range(len(tokens) - select_length):
351 for j in range(select_length + 1):
352 token_list += [tokens[i + j]]
353 selections += [token_list]
355 for i in range(select_length):
358 for i in range(len(selections)):
359 if selections[i][0][-1] in sentence_end_markers:
360 for j in range(select_length):
361 snippet[j] = selections[j][j + 1]
364 malkovich = "malkovich"
366 new_end = markov(snippet)
367 for name in session.users_in_chan:
368 if new_end[:len(name)] == name.lower():
369 new_end = malkovich + new_end[len(name):]
371 if len(msg) + len(new_end) > 200:
374 for i in range(select_length - 1):
375 snippet[i] = snippet[i + 1]
376 snippet[select_length - 1] = new_end
378 # Replace occurences of url escape string with random choice from urls.
380 index = msg.find(url_escape)
383 msg = msg.replace(url_escape, choice(urls), 1)
385 # More meaningful ways to randomly end sentences.
386 notice(msg + malkovich + ".")
391 twtfile = open(session.twtfile, mode)
392 except (PermissionError, FileNotFoundError) as err:
393 notice("can't access or create twt file: " + str(err))
397 from datetime import datetime
398 if not os.access(session.twtfile, os.F_OK):
399 twtfile = try_open("w")
403 twtfile = try_open("a")
406 twtfile.write(datetime.utcnow().isoformat() + "\t" + argument + "\n")
410 if "addquote" == command:
412 elif "quote" == command:
414 elif "markov" == command:
416 elif "twt" == command:
420 def handle_url(url, notice, show_url=False):
422 def mobile_twitter_hack(url):
423 re1 = 'https?://(mobile.twitter.com/)[^/]+(/status/)'
424 re2 = 'https?://mobile.twitter.com/([^/]+)/status/([^\?/]+)'
425 m = re.search(re1, url)
426 if m and m.group(1) == 'mobile.twitter.com/' \
427 and m.group(2) == '/status/':
428 m = re.search(re2, url)
429 url = 'https://twitter.com/' + m.group(1) + '/status/' + m.group(2)
430 handle_url(url, notice, True)
433 class TimeOut(Exception):
436 def timeout_handler(ignore1, ignore2):
437 raise TimeOut("timeout")
439 signal.signal(signal.SIGALRM, timeout_handler)
442 r = requests.get(url, headers = {'User-Agent': 'plomlombot'}, stream=True)
443 r.raw.decode_content = True
444 text = r.raw.read(10000000+1)
445 if len(text) > 10000000:
446 raise ValueError('Too large a response')
447 except (requests.exceptions.TooManyRedirects,
448 requests.exceptions.ConnectionError,
449 requests.exceptions.InvalidURL,
453 requests.exceptions.InvalidSchema) as error:
455 notice("trouble following url: " + str(error))
458 if mobile_twitter_hack(url):
460 title = bs4.BeautifulSoup(text, "html5lib").title
461 if title and title.string:
462 prefix = "page title: "
464 prefix = "page title for <" + url + ">: "
465 notice(prefix + title.string.strip())
467 notice("page has no title tag")
473 def __init__(self, io, username, nickname, channel, twtfile, dbdir, rmlogs,
476 self.nickname = nickname
477 self.users_in_chan = []
478 self.twtfile = twtfile
479 hash_channel = hashlib.md5(channel.encode("utf-8")).hexdigest()
480 chandir = dbdir + "/" + hash_channel + "/"
481 self.markov_input = markov_input
482 self.markovfile = chandir + "markovfeed"
483 self.quotesfile = chandir + "quotes"
484 self.log = Log(chandir, self.nickname, username, channel, rmlogs)
485 self.io.send_line("NICK " + self.nickname)
486 self.io.send_line("USER " + username + " 0 * : ")
487 self.io.send_line("JOIN " + channel)
488 self.io.log = self.log
489 self.log.separator_line()
493 def handle_privmsg(line):
496 line = "NOTICE " + target + " :" + msg
497 self.io.send_line(line)
500 if line.receiver != self.nickname:
501 target = line.receiver
502 msg = str.join(" ", line.tokens[3:])[1:]
503 matches = re.findall("(https?://[^\s>]+)", msg)
505 for i in range(len(matches)):
506 if handle_url(matches[i], notice):
509 notice("maximum number of urls to parse per message "
512 if "!" == msg[0] and len(msg) > 1:
513 tokens = msg[1:].split()
514 argument = str.join(" ", tokens[1:])
515 handle_command(tokens[0], argument, notice, target, self)
517 if self.markov_input:
518 write_to_file(self.markovfile, "a", msg + "\n")
522 line = self.io.recv_line()
526 if len(line.tokens) > 1:
527 if line.tokens[0] == "PING":
528 self.io.send_line("PONG " + line.tokens[1])
529 elif line.tokens[1] == "PRIVMSG":
531 elif line.tokens[1] == "353":
532 names = line.tokens[5:]
533 names[0] = names[0][1:]
534 for i in range(len(names)):
535 names[i] = names[i].replace("@", "").replace("+", "")
536 self.users_in_chan += names
537 elif line.tokens[1] == "JOIN" and line.sender != self.nickname:
538 self.users_in_chan += [line.sender]
539 elif line.tokens[1] == "PART":
540 del(self.users_in_chan[self.users_in_chan.index(line.sender)])
541 elif line.tokens[1] == "NICK":
542 del(self.users_in_chan[self.users_in_chan.index(line.sender)])
543 self.users_in_chan += [line.receiver]
546 def parse_command_line_arguments():
547 parser = argparse.ArgumentParser()
548 parser.add_argument("-s, --server", action="store", dest="server",
550 help="server or server net to connect to (default: "
552 parser.add_argument("-p, --port", action="store", dest="port", type=int,
553 default=PORT, help="port to connect to (default : "
555 parser.add_argument("-w, --wait", action="store", dest="timeout",
556 type=int, default=TIMEOUT,
557 help="timeout in seconds after which to attempt "
558 "reconnect (default: " + str(TIMEOUT) + ")")
559 parser.add_argument("-u, --username", action="store", dest="username",
560 default=USERNAME, help="username to use (default: "
562 parser.add_argument("-n, --nickname", action="store", dest="nickname",
563 default=NICKNAME, help="nickname to use (default: "
565 parser.add_argument("-t, --twtxtfile", action="store", dest="twtfile",
566 default=TWTFILE, help="twtxt file to use (default: "
568 parser.add_argument("-d, --dbdir", action="store", dest="dbdir",
569 default=DBDIR, help="directory to store DB files in")
570 parser.add_argument("-r, --rmlogs", action="store", dest="rmlogs",
572 help="maximum age in seconds for logfiles in logs/ "
573 "(0 means: never delete, and is default)")
574 parser.add_argument("-m, --markov_store", action="store_true",
576 help="log channel discussions for !markov input")
577 parser.add_argument("CHANNEL", action="store", help="channel to join")
578 opts, unknown = parser.parse_known_args()
582 opts = parse_command_line_arguments()
585 io = IO(opts.server, opts.port, opts.timeout)
586 hash_server = hashlib.md5(opts.server.encode("utf-8")).hexdigest()
587 dbdir = opts.dbdir + "/" + hash_server
588 session = Session(io, opts.username, opts.nickname, opts.CHANNEL,
589 opts.twtfile, dbdir, opts.rmlogs, opts.markov_store)
591 except ExceptionForRestart: