-def url_check(msg):
- matches = re.findall("(https?://[^\s]+)", msg)
- for i in range(len(matches)):
- url = matches[i]
- try:
- webpage = urllib.request.urlopen(url, timeout=15)
- except urllib.error.HTTPError as error:
- print("TROUBLE FOLLOWING URL: " + str(error))
- continue
- charset = webpage.info().get_content_charset()
- if not charset:
- charset="utf-8"
- content_type = webpage.info().get_content_type()
- if not content_type in ('text/html', 'text/xml',
- 'application/xhtml+xml'):
- print("TROUBLE INTERPRETING URL: bad content_type " + content_type)
+def init_session():
+ print("CONNECTING TO " + SERVERNET)
+ io = IO(SERVERNET, PORT, TIMEOUT)
+ io.send_line("NICK " + NICKNAME)
+ io.send_line("USER " + USERNAME + " 0 * : ")
+ io.send_line("JOIN " + CHANNEL)
+ return io
+
+def lineparser_loop():
+
+ def act_on_privmsg(tokens):
+
+ def url_check(msg):
+ matches = re.findall("(https?://[^\s]+)", msg)
+ for i in range(len(matches)):
+ url = matches[i]
+ try:
+ webpage = urllib.request.urlopen(url, timeout=15)
+ except urllib.error.HTTPError as error:
+ print("TROUBLE FOLLOWING URL: " + str(error))
+ continue
+ charset = webpage.info().get_content_charset()
+ if not charset:
+ charset="utf-8"
+ content_type = webpage.info().get_content_type()
+ if not content_type in ('text/html', 'text/xml',
+ 'application/xhtml+xml'):
+ print("TROUBLE INTERPRETING URL: bad content type "
+ + content_type)
+ continue
+ content = webpage.read().decode(charset)
+ title = str(content).split('<title>')[1].split('</title>')[0]
+ title = html.unescape(title)
+ io.send_line("PRIVMSG " + target + " :page title for url: "
+ + title)
+
+ sender = ""
+ for rune in tokens[0]:
+ if rune == "!":
+ break
+ if rune != ":":
+ sender += rune
+ receiver = ""
+ for rune in tokens[2]:
+ if rune == "!":
+ break
+ if rune != ":":
+ receiver += rune
+ target = sender
+ if receiver != NICKNAME:
+ target = receiver
+ msg = str.join(" ", tokens[3:])[1:]
+ url_check(msg)
+
+ while 1:
+ line = io.recv_line()
+ if not line: