+ msg = msg.replace(url_escape, choice(urls), 1)
+ 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
+
+ try:
+ r = requests.get(url, timeout=15)
+ except (requests.exceptions.TooManyRedirects,
+ requests.exceptions.ConnectionError,
+ requests.exceptions.InvalidURL,
+ UnicodeError,
+ requests.exceptions.InvalidSchema) as error:
+ notice("TROUBLE FOLLOWING URL: " + str(error))
+ return
+ if mobile_twitter_hack(url):
+ return
+ title = bs4.BeautifulSoup(r.text, "html.parser").title
+ if title:
+ prefix = "PAGE TITLE: "
+ if show_url:
+ prefix = "PAGE TITLE FOR <" + url + ">: "
+ notice(prefix + title.string.strip())
+ else:
+ notice("PAGE HAS NO TITLE TAG")
+
+
+class Session:
+
+ def __init__(self, io, username, nickname, channel, twtfile):
+ self.io = io
+ self.nickname = nickname
+ self.channel = channel
+ self.users_in_chan = []
+ self.twtfile = twtfile
+ self.io.send_line("NICK " + self.nickname)
+ self.io.send_line("USER " + username + " 0 * : ")
+ self.io.send_line("JOIN " + self.channel)
+
+ def loop(self):
+
+ def handle_privmsg(tokens):
+
+ def handle_input(msg, target):