home · contact · privacy
Improve mobile twitter URL pattern matching.
[plomlombot-irc.git] / plomlombot.py
1 #!/usr/bin/python3
2
3 import argparse
4 import socket
5 import datetime
6 import select
7 import time
8 import re
9 import requests
10 import bs4
11 import random
12 import hashlib
13 import os
14
15 # Defaults, may be overwritten by command line arguments.
16 SERVER = "irc.freenode.net"
17 PORT = 6667
18 TIMEOUT = 240
19 USERNAME = "plomlombot"
20 NICKNAME = USERNAME
21
22
23 class ExceptionForRestart(Exception):
24     pass
25
26
27 class IO:
28
29     def __init__(self, server, port, timeout):
30         self.timeout = timeout
31         self.socket = socket.socket()
32         self.socket.connect((server, port))
33         self.socket.setblocking(0)
34         self.line_buffer = []
35         self.rune_buffer = ""
36         self.last_pong = time.time()
37         self.servername = self.recv_line(send_ping=False).split(" ")[0][1:]
38
39     def _pingtest(self, send_ping=True):
40         if self.last_pong + self.timeout < time.time():
41             print("SERVER NOT ANSWERING")
42             raise ExceptionForRestart
43         if send_ping:
44             self.send_line("PING " + self.servername)
45
46     def send_line(self, msg):
47         msg = msg.replace("\r", " ")
48         msg = msg.replace("\n", " ")
49         if len(msg.encode("utf-8")) > 510:
50             print("NOT SENT LINE TO SERVER (too long): " + msg)
51         print("LINE TO SERVER: "
52               + str(datetime.datetime.now()) + ": " + msg)
53         msg = msg + "\r\n"
54         msg_len = len(msg)
55         total_sent_len = 0
56         while total_sent_len < msg_len:
57             sent_len = self.socket.send(bytes(msg[total_sent_len:], "UTF-8"))
58             if sent_len == 0:
59                 print("SOCKET CONNECTION BROKEN")
60                 raise ExceptionForRestart
61             total_sent_len += sent_len
62
63     def _recv_line_wrapped(self, send_ping=True):
64         if len(self.line_buffer) > 0:
65             return self.line_buffer.pop(0)
66         while True:
67             ready = select.select([self.socket], [], [], int(self.timeout / 2))
68             if not ready[0]:
69                 self._pingtest(send_ping)
70                 return None
71             self.last_pong = time.time()
72             received_bytes = self.socket.recv(1024)
73             try:
74                 received_runes = received_bytes.decode("UTF-8")
75             except UnicodeDecodeError:
76                 received_runes = received_bytes.decode("latin1")
77             if len(received_runes) == 0:
78                 print("SOCKET CONNECTION BROKEN")
79                 raise ExceptionForRestart
80             self.rune_buffer += received_runes
81             lines_split = str.split(self.rune_buffer, "\r\n")
82             self.line_buffer += lines_split[:-1]
83             self.rune_buffer = lines_split[-1]
84             if len(self.line_buffer) > 0:
85                 return self.line_buffer.pop(0)
86
87     def recv_line(self, send_ping=True):
88         line = self._recv_line_wrapped(send_ping)
89         if line:
90             print("LINE FROM SERVER " + str(datetime.datetime.now()) + ": " +
91                   line)
92         return line
93
94
95 def init_session(server, port, timeout, nickname, username, channel):
96     print("CONNECTING TO " + server)
97     io = IO(server, port, timeout)
98     io.send_line("NICK " + nickname)
99     io.send_line("USER " + username + " 0 * : ")
100     io.send_line("JOIN " + channel)
101     return io
102
103
104 def lineparser_loop(io, nickname):
105
106     def act_on_privmsg(tokens):
107
108         def notice(msg):
109             io.send_line("NOTICE " + target + " :" + msg)
110
111         def url_check(msg):
112
113             def handle_url(url, show_url=False):
114
115                 def mobile_twitter_hack(url):
116                     re1 = 'https?://(mobile.twitter.com/)[^/]+(/status/)'
117                     re2 = 'https?://mobile.twitter.com/([^/]+)/status/' \
118                         + '([^\?/]+)'
119                     m = re.search(re1, url)
120                     if m and m.group(1) == 'mobile.twitter.com/' \
121                             and m.group(2) == '/status/':
122                         m = re.search(re2, url)
123                         url = 'https://twitter.com/' + m.group(1) + '/status/' \
124                                 + m.group(2)
125                         handle_url(url, True)
126                         return True
127
128                 try:
129                     r = requests.get(url, timeout=15)
130                 except (requests.exceptions.TooManyRedirects,
131                         requests.exceptions.ConnectionError,
132                         requests.exceptions.InvalidURL,
133                         requests.exceptions.InvalidSchema) as error:
134                     notice("TROUBLE FOLLOWING URL: " + str(error))
135                     return
136                 if mobile_twitter_hack(url):
137                     return
138                 title = bs4.BeautifulSoup(r.text).title
139                 if title:
140                     prefix = "PAGE TITLE: "
141                     if show_url:
142                         prefix = "PAGE TITLE FOR <" + url + ">: "
143                     notice(prefix + title.string.strip())
144                 else:
145                     notice("PAGE HAS NO TITLE TAG")
146
147             matches = re.findall("(https?://[^\s>]+)", msg)
148             for i in range(len(matches)):
149                 handle_url(matches[i])
150
151         def command_check(msg):
152             if msg[0] != "!":
153                 return
154             tokens = msg[1:].split()
155             hash_string = hashlib.md5(target.encode("utf-8")).hexdigest()
156             quotesfile_name = "quotes_" + hash_string
157             if tokens[0] == "addquote":
158                 if not os.access(quotesfile_name, os.F_OK):
159                     quotesfile = open(quotesfile_name, "w")
160                     quotesfile.write("QUOTES FOR " + target + ":\n")
161                     quotesfile.close()
162                 quotesfile = open(quotesfile_name, "a")
163                 quotesfile.write(str.join(" ", tokens[1:]) + "\n")
164                 quotesfile.close()
165                 quotesfile = open(quotesfile_name, "r")
166                 lines = quotesfile.readlines()
167                 quotesfile.close()
168                 notice("ADDED QUOTE #" + str(len(lines) - 1))
169             elif tokens[0] == "quote":
170                 if len(tokens) > 2 or \
171                     (len(tokens) == 2 and not tokens[1].isdigit()):
172                     notice("SYNTAX: !quote [int]")
173                     return
174                 if not os.access(quotesfile_name, os.F_OK):
175                     notice("NO QUOTES AVAILABLE")
176                     return
177                 quotesfile = open(quotesfile_name, "r")
178                 lines = quotesfile.readlines()
179                 quotesfile.close()
180                 lines = lines[1:]
181                 if len(tokens) == 2:
182                     i = int(tokens[1])
183                     if i == 0 or i > len(lines):
184                         notice("THERE'S NO QUOTE OF THAT INDEX")
185                         return
186                     i = i - 1
187                 else:
188                     i = random.randrange(len(lines))
189                 notice("QUOTE #" + str(i + 1) + ": " + lines[i])
190
191         sender = ""
192         for rune in tokens[0]:
193             if rune == "!":
194                 break
195             if rune != ":":
196                 sender += rune
197         receiver = ""
198         for rune in tokens[2]:
199             if rune == "!":
200                 break
201             if rune != ":":
202                 receiver += rune
203         target = sender
204         if receiver != nickname:
205             target = receiver
206         msg = str.join(" ", tokens[3:])[1:]
207         command_check(msg)
208         url_check(msg)
209
210     while True:
211         line = io.recv_line()
212         if not line:
213             continue
214         tokens = line.split(" ")
215         if len(tokens) > 1:
216             if tokens[1] == "PRIVMSG":
217                 act_on_privmsg(tokens)
218             if tokens[0] == "PING":
219                 io.send_line("PONG " + tokens[1])
220
221
222 def parse_command_line_arguments():
223     parser = argparse.ArgumentParser()
224     parser.add_argument("-s, --server", action="store", dest="server",
225                         default=SERVER,
226                         help="server or server net to connect to (default: "
227                         + SERVER + ")")
228     parser.add_argument("-p, --port", action="store", dest="port", type=int,
229                         default=PORT, help="port to connect to (default : "
230                         + str(PORT) + ")")
231     parser.add_argument("-t, --timeout", action="store", dest="timeout",
232                         type=int, default=TIMEOUT,
233                         help="timeout in seconds after which to attempt " +
234                         "reconnect (default: " + str(TIMEOUT) + ")")
235     parser.add_argument("-u, --username", action="store", dest="username",
236                         default=USERNAME, help="username to use (default: "
237                         + USERNAME + ")")
238     parser.add_argument("-n, --nickname", action="store", dest="nickname",
239                         default=NICKNAME, help="nickname to use (default: "
240                         + NICKNAME + ")")
241     parser.add_argument("CHANNEL", action="store", help="channel to join")
242     opts, unknown = parser.parse_known_args()
243     return opts
244
245 opts = parse_command_line_arguments()
246 while True:
247     try:
248         io = init_session(opts.server, opts.port, opts.timeout, opts.nickname,
249                           opts.username, opts.CHANNEL)
250         lineparser_loop(io, opts.nickname)
251     except ExceptionForRestart:
252         io.socket.close()
253         continue