16 # Defaults, may be overwritten by command line arguments.
17 SERVER = "irc.freenode.net"
20 USERNAME = "plomlombot"
23 DBDIR = os.path.expanduser("~/plomlombot_db")
26 class ExceptionForRestart(Exception):
32 def __init__(self, server, port, timeout):
33 self.timeout = timeout
34 self.socket = socket.socket()
35 self.socket.connect((server, port))
36 self.socket.setblocking(0)
39 self.last_pong = time.time()
40 self.servername = self.recv_line(send_ping=False).split(" ")[0][1:]
42 def _pingtest(self, send_ping=True):
43 if self.last_pong + self.timeout < time.time():
44 print("SERVER NOT ANSWERING")
45 raise ExceptionForRestart
47 self.send_line("PING " + self.servername)
49 def send_line(self, msg):
50 msg = msg.replace("\r", " ")
51 msg = msg.replace("\n", " ")
52 if len(msg.encode("utf-8")) > 510:
53 print("NOT SENT LINE TO SERVER (too long): " + msg)
54 print("LINE TO SERVER: "
55 + str(datetime.datetime.now()) + ": " + msg)
59 while total_sent_len < msg_len:
60 sent_len = self.socket.send(bytes(msg[total_sent_len:], "UTF-8"))
62 print("SOCKET CONNECTION BROKEN")
63 raise ExceptionForRestart
64 total_sent_len += sent_len
66 def _recv_line_wrapped(self, send_ping=True):
67 if len(self.line_buffer) > 0:
68 return self.line_buffer.pop(0)
70 ready = select.select([self.socket], [], [], int(self.timeout / 2))
72 self._pingtest(send_ping)
74 self.last_pong = time.time()
75 received_bytes = self.socket.recv(1024)
77 received_runes = received_bytes.decode("UTF-8")
78 except UnicodeDecodeError:
79 received_runes = received_bytes.decode("latin1")
80 if len(received_runes) == 0:
81 print("SOCKET CONNECTION BROKEN")
82 raise ExceptionForRestart
83 self.rune_buffer += received_runes
84 lines_split = str.split(self.rune_buffer, "\r\n")
85 self.line_buffer += lines_split[:-1]
86 self.rune_buffer = lines_split[-1]
87 if len(self.line_buffer) > 0:
88 return self.line_buffer.pop(0)
90 def recv_line(self, send_ping=True):
91 line = self._recv_line_wrapped(send_ping)
93 print("LINE FROM SERVER " + str(datetime.datetime.now()) + ": " +
98 def handle_command(command, argument, notice, target, session):
99 hash_string = hashlib.md5(target.encode("utf-8")).hexdigest()
100 quotesfile_name = session.dbdir + "/quotes_" + hash_string
103 if not os.access(quotesfile_name, os.F_OK):
104 quotesfile = open(quotesfile_name, "w")
105 quotesfile.write("QUOTES FOR " + target + ":\n")
107 quotesfile = open(quotesfile_name, "a")
108 quotesfile.write(argument + "\n")
110 quotesfile = open(quotesfile_name, "r")
111 lines = quotesfile.readlines()
113 notice("ADDED QUOTE #" + str(len(lines) - 1))
118 notice("SYNTAX: !quote [int] OR !quote search QUERY")
119 notice("QUERY may be a boolean grouping of quoted or unquoted " +
120 "search terms, examples:")
121 notice("!quote search foo")
122 notice("!quote search foo AND (bar OR NOT baz)")
123 notice("!quote search \"foo\\\"bar\" AND ('NOT\"' AND \"'foo'\"" +
129 tokens = argument.split(" ")
130 if (len(tokens) > 1 and tokens[0] != "search") or \
131 (len(tokens) == 1 and
132 (tokens[0] == "search" or not tokens[0].isdigit())):
135 if not os.access(quotesfile_name, os.F_OK):
136 notice("NO QUOTES AVAILABLE")
138 quotesfile = open(quotesfile_name, "r")
139 lines = quotesfile.readlines()
144 if i == 0 or i > len(lines):
145 notice("THERE'S NO QUOTE OF THAT INDEX")
148 elif len(tokens) > 1:
149 query = str.join(" ", tokens[1:])
151 results = plomsearch.search(query, lines)
152 except plomsearch.LogicParserError as err:
153 notice("FAILED QUERY PARSING: " + str(err))
155 if len(results) == 0:
156 notice("NO QUOTES MATCHING QUERY")
158 for result in results:
159 notice("QUOTE #" + str(result[0] + 1) + " : " + result[1])
162 i = random.randrange(len(lines))
163 notice("QUOTE #" + str(i + 1) + ": " + lines[i])
166 from random import choice, shuffle
171 usable_selections = []
172 for i in range(select_length, 0, -1):
173 for selection in selections:
177 if snippet[-j] != selection[-(j+1)]:
181 usable_selections += [selection]
182 if [] != usable_selections:
184 if [] == usable_selections:
185 usable_selections = selections
186 selection = choice(usable_selections)
187 return selection[select_length]
189 hash_string = hashlib.md5(target.encode("utf-8")).hexdigest()
190 markovfeed_name = session.dbdir + "/markovfeed_" + hash_string
191 if not os.access(markovfeed_name, os.F_OK):
192 notice("NOT ENOUGH TEXT TO MARKOV.")
195 # Lowercase incoming lines, ensure they end in a sentence end mark.
196 file = open(markovfeed_name, "r")
197 lines = file.readlines()
200 sentence_end_markers = ".!?)("
202 line = line.lower().replace("\n", "")
203 if line[-1] not in sentence_end_markers:
205 tokens += line.split()
206 if len(tokens) <= select_length:
207 notice("NOT ENOUGH TEXT TO MARKOV.")
210 # Replace URLs with escape string for now, so that the Markov selector
211 # won't see them as different strings. Stash replaced URLs in urls.
214 url_starts = ["http://", "https://", "<http://", "<https://"]
215 for i in range(len(tokens)):
216 for url_start in url_starts:
217 if tokens[i][:len(url_start)] == url_start:
218 length = len(tokens[i])
219 if url_start[0] == "<":
221 length = tokens[i].index(">") + 1
224 urls += [tokens[i][:length]]
225 tokens[i] = url_escape + tokens[i][length:]
228 # For each snippet of select_length, use markov() to find continuation
229 # token from selections. Replace present users' names with malkovich.
230 # Start snippets with the beginning of a sentence, if possible.
231 for i in range(len(tokens) - select_length):
233 for j in range(select_length + 1):
234 token_list += [tokens[i + j]]
235 selections += [token_list]
237 for i in range(select_length):
240 for i in range(len(selections)):
241 if selections[i][0][-1] in sentence_end_markers:
242 for i in range(select_length):
243 snippet[i] = selections[i][i + 1]
246 malkovich = "malkovich"
248 new_end = markov(snippet)
249 for name in session.users_in_chan:
250 if new_end[:len(name)] == name.lower():
251 new_end = malkovich + new_end[len(name):]
253 if len(msg) + len(new_end) > 200:
256 for i in range(select_length - 1):
257 snippet[i] = snippet[i + 1]
258 snippet[select_length - 1] = new_end
260 # Replace occurences of url escape string with random choice from urls.
262 index = msg.find(url_escape)
265 msg = msg.replace(url_escape, choice(urls), 1)
267 # More meaningful ways to randomly end sentences.
268 notice(msg + malkovich + ".")
273 twtfile = open(session.twtfile, mode)
274 except (PermissionError, FileNotFoundError) as err:
275 notice("CAN'T ACCESS OR CREATE TWT FILE: " + str(err))
279 from datetime import datetime
280 if not os.access(session.twtfile, os.F_OK):
281 twtfile = try_open("w")
285 twtfile = try_open("a")
288 twtfile.write(datetime.utcnow().isoformat() + "\t" + argument + "\n")
292 if "addquote" == command:
294 elif "quote" == command:
296 elif "markov" == command:
298 elif "twt" == command:
302 def handle_url(url, notice, show_url=False):
304 def mobile_twitter_hack(url):
305 re1 = 'https?://(mobile.twitter.com/)[^/]+(/status/)'
306 re2 = 'https?://mobile.twitter.com/([^/]+)/status/([^\?/]+)'
307 m = re.search(re1, url)
308 if m and m.group(1) == 'mobile.twitter.com/' \
309 and m.group(2) == '/status/':
310 m = re.search(re2, url)
311 url = 'https://twitter.com/' + m.group(1) + '/status/' + m.group(2)
312 handle_url(url, notice, True)
316 r = requests.get(url, timeout=15)
317 except (requests.exceptions.TooManyRedirects,
318 requests.exceptions.ConnectionError,
319 requests.exceptions.InvalidURL,
321 requests.exceptions.InvalidSchema) as error:
322 notice("TROUBLE FOLLOWING URL: " + str(error))
324 if mobile_twitter_hack(url):
326 title = bs4.BeautifulSoup(r.text, "html5lib").title
327 if title and title.string:
328 prefix = "PAGE TITLE: "
330 prefix = "PAGE TITLE FOR <" + url + ">: "
331 notice(prefix + title.string.strip())
333 notice("PAGE HAS NO TITLE TAG")
338 def __init__(self, io, username, nickname, channel, twtfile, dbdir):
340 self.nickname = nickname
341 self.channel = channel
342 self.users_in_chan = []
343 self.twtfile = twtfile
345 self.io.send_line("NICK " + self.nickname)
346 self.io.send_line("USER " + username + " 0 * : ")
347 self.io.send_line("JOIN " + self.channel)
351 def handle_privmsg(tokens):
353 def handle_input(msg, target):
356 self.io.send_line("NOTICE " + target + " :" + msg)
358 matches = re.findall("(https?://[^\s>]+)", msg)
359 for i in range(len(matches)):
360 handle_url(matches[i], notice)
362 tokens = msg[1:].split()
363 argument = str.join(" ", tokens[1:])
364 handle_command(tokens[0], argument, notice, target, self)
366 hash_string = hashlib.md5(target.encode("utf-8")).hexdigest()
367 markovfeed_name = self.dbdir + "/markovfeed_" + hash_string
368 file = open(markovfeed_name, "a")
369 file.write(msg + "\n")
373 for rune in tokens[0]:
379 for rune in tokens[2]:
385 if receiver != self.nickname:
387 msg = str.join(" ", tokens[3:])[1:]
388 handle_input(msg, target)
390 def name_from_join_or_part(tokens):
391 token = tokens[0][1:]
392 index_cut = token.find("@")
393 index_ex = token.find("!")
394 if index_ex > 0 and index_ex < index_cut:
396 return token[:index_cut]
399 line = self.io.recv_line()
402 tokens = line.split(" ")
404 if tokens[0] == "PING":
405 self.io.send_line("PONG " + tokens[1])
406 elif tokens[1] == "PRIVMSG":
407 handle_privmsg(tokens)
408 elif tokens[1] == "353":
410 names[0] = names[0][1:]
411 self.users_in_chan += names
412 elif tokens[1] == "JOIN":
413 name = name_from_join_or_part(tokens)
414 if name != self.nickname:
415 self.users_in_chan += [name]
416 elif tokens[1] == "PART":
417 name = name_from_join_or_part(tokens)
418 del(self.users_in_chan[self.users_in_chan.index(name)])
420 def parse_command_line_arguments():
421 parser = argparse.ArgumentParser()
422 parser.add_argument("-s, --server", action="store", dest="server",
424 help="server or server net to connect to (default: "
426 parser.add_argument("-p, --port", action="store", dest="port", type=int,
427 default=PORT, help="port to connect to (default : "
429 parser.add_argument("-w, --wait", action="store", dest="timeout",
430 type=int, default=TIMEOUT,
431 help="timeout in seconds after which to attempt " +
432 "reconnect (default: " + str(TIMEOUT) + ")")
433 parser.add_argument("-u, --username", action="store", dest="username",
434 default=USERNAME, help="username to use (default: "
436 parser.add_argument("-n, --nickname", action="store", dest="nickname",
437 default=NICKNAME, help="nickname to use (default: "
439 parser.add_argument("-t, --twtxtfile", action="store", dest="twtfile",
440 default=TWTFILE, help="twtxt file to use (default: "
442 parser.add_argument("-d, --dbdir", action="store", dest="dbdir",
443 default=DBDIR, help="directory to store DB files in")
444 parser.add_argument("CHANNEL", action="store", help="channel to join")
445 opts, unknown = parser.parse_known_args()
449 opts = parse_command_line_arguments()
452 io = IO(opts.server, opts.port, opts.timeout)
453 session = Session(io, opts.username, opts.nickname, opts.CHANNEL,
454 opts.twtfile, opts.dbdir)
456 except ExceptionForRestart: