+
+ # For each snippet of select_length, use markov() to find continuation
+ # token from selections. Replace present users' names with malkovich.
+ # Start snippets with the beginning of a sentence, if possible.
+ for i in range(len(tokens) - select_length):
+ token_list = []
+ for j in range(select_length + 1):
+ token_list += [tokens[i + j]]
+ selections += [token_list]
+ snippet = []
+ for i in range(select_length):
+ snippet += [""]
+ shuffle(selections)
+ for i in range(len(selections)):
+ if selections[i][0][-1] in sentence_end_markers:
+ for j in range(select_length):
+ snippet[j] = selections[j][j + 1]
+ break
+ msg = ""
+ malkovich = "malkovich"
+ while 1:
+ new_end = markov(snippet)
+ for name in session.users_in_chan:
+ if new_end[:len(name)] == name.lower():
+ new_end = malkovich + new_end[len(name):]
+ break
+ if len(msg) + len(new_end) > 200:
+ break
+ msg += new_end + " "
+ for i in range(select_length - 1):
+ snippet[i] = snippet[i + 1]
+ snippet[select_length - 1] = new_end
+
+ # Replace occurences of url escape string with random choice from urls.
+ while True:
+ index = msg.find(url_escape)
+ if index < 0:
+ break
+ msg = msg.replace(url_escape, choice(urls), 1)
+
+ # More meaningful ways to randomly end sentences.
+ notice(msg + malkovich + ".")
+
+ def twt():
+ def try_open(mode):
+ try:
+ twtfile = open(session.twtfile, mode)
+ except (PermissionError, FileNotFoundError) as err:
+ notice("can't access or create twt file: " + str(err))
+ return None
+ return twtfile
+
+ from datetime import datetime
+ if not os.access(session.twtfile, os.F_OK):
+ twtfile = try_open("w")
+ if None == twtfile:
+ return
+ twtfile.close()
+ twtfile = try_open("a")
+ if None == twtfile:
+ return
+ twtfile.write(datetime.utcnow().isoformat() + "\t" + argument + "\n")
+ twtfile.close()
+ notice("wrote twt.")
+
+ if "addquote" == command:
+ addquote()
+ elif "quote" == command:
+ quote()
+ elif "markov" == command:
+ markov()
+ elif "twt" == command:
+ twt()
+
+
+def handle_url(url, notice, show_url=False):
+
+ def mobile_twitter_hack(url):
+ re1 = 'https?://(mobile.twitter.com/)[^/]+(/status/)'
+ re2 = 'https?://mobile.twitter.com/([^/]+)/status/([^\?/]+)'
+ m = re.search(re1, url)
+ if m and m.group(1) == 'mobile.twitter.com/' \
+ and m.group(2) == '/status/':
+ m = re.search(re2, url)
+ url = 'https://twitter.com/' + m.group(1) + '/status/' + m.group(2)
+ handle_url(url, notice, True)
+ return True
+
+ class TimeOut(Exception):
+ pass
+
+ def timeout_handler(ignore1, ignore2):
+ raise TimeOut("timeout")
+
+ signal.signal(signal.SIGALRM, timeout_handler)
+ signal.alarm(15)
+ try:
+ r = requests.get(url, headers = {'User-Agent': 'plomlombot'}, stream=True)
+ r.raw.decode_content = True
+ text = r.raw.read(10000000+1)
+ if len(text) > 10000000:
+ raise ValueError('Too large a response')
+ except (requests.exceptions.TooManyRedirects,
+ requests.exceptions.ConnectionError,
+ requests.exceptions.InvalidURL,
+ TimeOut,
+ UnicodeError,
+ ValueError,
+ requests.exceptions.InvalidSchema) as error:
+ signal.alarm(0)
+ notice("trouble following url: " + str(error))
+ return False
+ signal.alarm(0)
+ if mobile_twitter_hack(url):
+ return True
+ title = bs4.BeautifulSoup(text, "html5lib").title
+ if title and title.string:
+ prefix = "page title: "
+ if show_url:
+ prefix = "page title for <" + url + ">: "
+ notice(prefix + title.string.strip())
+ else:
+ notice("page has no title tag")
+ return True
+
+
+class Session:
+
+ def __init__(self, io, username, nickname, sasl, channel, twtfile, dbdir, rmlogs,
+ markov_input, no_show_page_titles):
+ import base64
+ self.io = io
+ self.nickname = nickname
+ self.users_in_chan = []
+ self.twtfile = twtfile
+ hash_channel = hashlib.md5(channel.encode("utf-8")).hexdigest()
+ chandir = dbdir + "/" + hash_channel + "/"
+ self.markov_input = markov_input
+ self.markovfile = chandir + "markovfeed"
+ self.quotesfile = chandir + "quotes"
+ self.log = Log(chandir, self.nickname, username, channel, rmlogs)
+ if sasl:
+ self.io.send_line("CAP REQ :sasl")
+ self.io.send_line("NICK " + self.nickname)
+ self.io.send_line("USER " + username + " 0 * : ")
+ if sasl:
+ self.io.send_line("AUTHENTICATE PLAIN")
+ auth = username + '\0' + username + '\0' + sasl
+ auth_encoded = base64.b64encode(auth.encode()).decode().rstrip()
+ self.io.send_line("AUTHENTICATE " + auth_encoded)
+ self.io.send_line("CAP END")
+ self.io.send_line("JOIN " + channel)
+ self.io.log = self.log
+ self.log.separator_line()
+ self.show_page_titles = not no_show_page_titles
+
+ def loop(self):
+
+ def handle_privmsg(line):
+
+ def notice(msg):
+ line = "NOTICE " + target + " :" + msg
+ self.io.send_line(line)
+
+ target = line.sender
+ if line.receiver != self.nickname:
+ target = line.receiver
+ msg = str.join(" ", line.tokens[3:])[1:]
+ if self.show_page_titles:
+ matches = re.findall("(https?://[^\s>]+)", msg)
+ url_count = 0
+ for i in range(len(matches)):
+ if handle_url(matches[i], notice):
+ url_count += 1
+ if url_count == 3:
+ notice("maximum number of urls to parse per "
+ "message reached")
+ break
+ if "!" == msg[0] and len(msg) > 1:
+ tokens = msg[1:].split()
+ argument = str.join(" ", tokens[1:])
+ handle_command(tokens[0], argument, notice, target, self)
+ return
+ if self.markov_input:
+ write_to_file(self.markovfile, "a", msg + "\n")
+
+ while True:
+ self.log.rmlogs()
+ line = self.io.recv_line()
+ if not line:
+ continue
+ line = Line(line)
+ if len(line.tokens) > 1:
+ if line.tokens[0] == "PING":
+ self.io.send_line("PONG " + line.tokens[1])
+ elif line.tokens[1] == "PRIVMSG":
+ handle_privmsg(line)
+ elif line.tokens[1] == "353":
+ names = line.tokens[5:]
+ names[0] = names[0][1:]
+ for i in range(len(names)):
+ names[i] = names[i].replace("@", "").replace("+", "")
+ self.users_in_chan += names
+ elif line.tokens[1] == "JOIN" and line.sender != self.nickname:
+ self.users_in_chan += [line.sender]
+ elif line.tokens[1] == "PART":
+ del(self.users_in_chan[self.users_in_chan.index(line.sender)])
+ elif line.tokens[1] == "NICK":
+ del(self.users_in_chan[self.users_in_chan.index(line.sender)])
+ self.users_in_chan += [line.receiver]
+
+
+def parse_command_line_arguments():
+ parser = argparse.ArgumentParser()
+ parser.add_argument("-s, --server", action="store", dest="server",
+ default=SERVER,
+ help="server or server net to connect to (default: "
+ + SERVER + ")")
+ parser.add_argument("-p, --port", action="store", dest="port", type=int,
+ default=PORT, help="port to connect to (default : "
+ + str(PORT) + ")")
+ parser.add_argument("-c, --channel", action="store", dest="channel",
+ default=SERVER, help="channel to join")
+ parser.add_argument("-w, --wait", action="store", dest="timeout",
+ type=int, default=TIMEOUT,
+ help="timeout in seconds after which to attempt "
+ "reconnect (default: " + str(TIMEOUT) + ")")
+ parser.add_argument("-u, --username", action="store", dest="username",
+ default=USERNAME, help="username to use (default: "
+ + USERNAME + ")")
+ parser.add_argument("-n, --nickname", action="store", dest="nickname",
+ default=NICKNAME, help="nickname to use (default: "
+ + NICKNAME + ")")
+ parser.add_argument("-a, --authenticate", action="store", dest="sasl",
+ default=None, help="SASL password (default: none)")
+ parser.add_argument("-t, --twtxtfile", action="store", dest="twtfile",
+ default=TWTFILE, help="twtxt file to use (default: "
+ + TWTFILE + ")")
+ parser.add_argument("-d, --dbdir", action="store", dest="dbdir",
+ default=DBDIR, help="directory to store DB files in")
+ parser.add_argument("-r, --rmlogs", action="store", dest="rmlogs",
+ type=int, default=0,
+ help="maximum age in seconds for logfiles in logs/ "
+ "(0 means: never delete, and is default)")
+ parser.add_argument("-m, --markov_store", action="store_true",
+ dest="markov_store",
+ help="log channel discussions for !markov input")
+ parser.add_argument("--no-show-page-titles", action="store_true",
+ dest="no_show_page_titles",
+ help="do not show page titles")
+ opts, unknown = parser.parse_known_args()
+ return opts
+
+
+opts = parse_command_line_arguments()
+while True:
+ try:
+ io = IO(opts.server, opts.port, opts.timeout)
+ hash_server = hashlib.md5(opts.server.encode("utf-8")).hexdigest()
+ dbdir = opts.dbdir + "/" + hash_server
+ session = Session(io, opts.username, opts.nickname, opts.sasl, opts.channel,
+ opts.twtfile, dbdir, opts.rmlogs, opts.markov_store,
+ opts.no_show_page_titles)
+ session.loop()
+ except ExceptionForRestart:
+ io.socket.close()
+ continue