9 SERVERNET = "irc.freenode.net"
12 USERNAME = "plomlombot"
14 CHANNEL = "#zrolaps-test"
16 class ExceptionForRestart(Exception):
21 def __init__(self, servernet, port, timeout):
22 self.timeout = timeout
23 self.socket = socket.socket()
24 self.socket.connect((servernet, port))
25 self.socket.setblocking(0)
28 self.last_pong = time.time()
29 self.servername = self.recv_line(send_ping=False).split(" ")[0][1:]
31 def _pingtest(self, send_ping=True):
32 if self.last_pong + self.timeout < time.time():
33 print("SERVER NOT ANSWERING")
34 raise ExceptionForRestart
36 self.send_line("PING " + self.servername)
38 def send_line(self, msg):
39 msg = msg.replace("\r", " ")
40 msg = msg.replace("\n", " ")
41 if len(msg.encode("utf-8")) > 510:
42 print("NOT SENT LINE TO SERVER (too long): " + msg)
43 print("LINE TO SERVER: "
44 + str(datetime.datetime.now()) + ": " + msg)
48 while total_sent_len < msg_len:
49 sent_len = self.socket.send(bytes(msg[total_sent_len:], "UTF-8"))
51 print("SOCKET CONNECTION BROKEN")
52 raise ExceptionForRestart
53 total_sent_len += sent_len
55 def _recv_line_wrapped(self, send_ping=True):
56 if len(self.line_buffer) > 0:
57 return self.line_buffer.pop(0)
59 ready = select.select([self.socket], [], [], int(self.timeout / 2))
61 self._pingtest(send_ping)
63 self.last_pong = time.time()
64 received_runes = self.socket.recv(1024).decode("UTF-8")
65 if len(received_runes) == 0:
66 print("SOCKET CONNECTION BROKEN")
67 raise ExceptionForRestart
68 self.rune_buffer += received_runes
69 lines_split = str.split(self.rune_buffer, "\r\n")
70 self.line_buffer += lines_split[:-1]
71 self.rune_buffer = lines_split[-1]
72 if len(self.line_buffer) > 0:
73 return self.line_buffer.pop(0)
75 def recv_line(self, send_ping=True):
76 line = self._recv_line_wrapped(send_ping)
78 print("LINE FROM SERVER " + str(datetime.datetime.now()) + ": " +
83 print("CONNECTING TO " + SERVERNET)
84 io = IO(SERVERNET, PORT, TIMEOUT)
85 io.send_line("NICK " + NICKNAME)
86 io.send_line("USER " + USERNAME + " 0 * : ")
87 io.send_line("JOIN " + CHANNEL)
90 def lineparser_loop():
92 def act_on_privmsg(tokens):
95 matches = re.findall("(https?://[^\s]+)", msg)
96 for i in range(len(matches)):
99 webpage = urllib.request.urlopen(url, timeout=15)
100 except urllib.error.HTTPError as error:
101 print("TROUBLE FOLLOWING URL: " + str(error))
103 charset = webpage.info().get_content_charset()
106 content_type = webpage.info().get_content_type()
107 if not content_type in ('text/html', 'text/xml',
108 'application/xhtml+xml'):
109 print("TROUBLE INTERPRETING URL: bad content type "
112 content = webpage.read().decode(charset)
113 title = str(content).split('<title>')[1].split('</title>')[0]
114 title = html.unescape(title)
115 io.send_line("PRIVMSG " + target + " :page title for url: "
119 for rune in tokens[0]:
125 for rune in tokens[2]:
131 if receiver != NICKNAME:
133 msg = str.join(" ", tokens[3:])[1:]
137 line = io.recv_line()
140 tokens = line.split(" ")
142 if tokens[1] == "PRIVMSG":
143 act_on_privmsg(tokens)
144 if tokens[0] == "PING":
145 io.send_line("PONG " + tokens[1])
151 except ExceptionForRestart: