+ 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, "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")
+
+
+class Session:
+
+ def __init__(self, io, username, nickname, channel, twtfile, dbdir):
+ self.io = io
+ self.nickname = nickname
+ self.username = username
+ self.channel = channel
+ self.users_in_chan = []
+ self.twtfile = twtfile
+ self.dbdir = dbdir
+ self.io.send_line("NICK " + self.nickname)
+ self.io.send_line("USER " + self.username + " 0 * : ")
+ self.io.send_line("JOIN " + self.channel)
+ hash_channel = hashlib.md5(self.channel.encode("utf-8")).hexdigest()
+ self.chandir = self.dbdir + "/" + hash_channel + "/"
+ self.logdir = self.chandir + "logs/"
+ if not os.path.exists(self.logdir):
+ os.makedirs(self.logdir)
+ self.markovfile = self.chandir + "markovfeed"
+ self.quotesfile = self.chandir + "quotes"
+
+ def loop(self):
+
+ def log(line):
+ if type(line) == str:
+ line = Line(":" + self.nickname + "!~" + self.username +
+ "@localhost" + " " + line)
+ now = datetime.datetime.utcnow()
+ logfile = open(self.logdir + now.strftime("%Y-%m-%d") + ".raw_log", "a")
+ form = "%Y-%m-%d %H:%M:%S UTC\t"
+ logfile.write(now.strftime(form) + " " + line.line + "\n")
+ logfile.close()
+ to_log = irclog.format_logline(line, self.channel)
+ if to_log != None:
+ logfile = open(self.logdir + now.strftime("%Y-%m-%d") + ".txt", "a")
+ logfile.write(now.strftime(form) + " " + to_log + "\n")
+ logfile.close()
+
+ def handle_privmsg(line):
+
+ def notice(msg):
+ line = "NOTICE " + target + " :" + msg
+ self.io.send_line(line)
+ log(line)
+
+ target = line.sender
+ if line.receiver != self.nickname:
+ target = line.receiver
+ msg = str.join(" ", line.tokens[3:])[1:]