home · contact · privacy
Use BeautifulSoup for HTML parsing.
[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
12 # Defaults, may be overwritten by command line arguments.
13 SERVER = "irc.freenode.net"
14 PORT = 6667
15 TIMEOUT = 240
16 USERNAME = "plomlombot"
17 NICKNAME = USERNAME
18
19
20 class ExceptionForRestart(Exception):
21     pass
22
23
24 class IO:
25
26     def __init__(self, server, port, timeout):
27         self.timeout = timeout
28         self.socket = socket.socket()
29         self.socket.connect((server, port))
30         self.socket.setblocking(0)
31         self.line_buffer = []
32         self.rune_buffer = ""
33         self.last_pong = time.time()
34         self.servername = self.recv_line(send_ping=False).split(" ")[0][1:]
35
36     def _pingtest(self, send_ping=True):
37         if self.last_pong + self.timeout < time.time():
38             print("SERVER NOT ANSWERING")
39             raise ExceptionForRestart
40         if send_ping:
41             self.send_line("PING " + self.servername)
42
43     def send_line(self, msg):
44         msg = msg.replace("\r", " ")
45         msg = msg.replace("\n", " ")
46         if len(msg.encode("utf-8")) > 510:
47             print("NOT SENT LINE TO SERVER (too long): " + msg)
48         print("LINE TO SERVER: "
49               + str(datetime.datetime.now()) + ": " + msg)
50         msg = msg + "\r\n"
51         msg_len = len(msg)
52         total_sent_len = 0
53         while total_sent_len < msg_len:
54             sent_len = self.socket.send(bytes(msg[total_sent_len:], "UTF-8"))
55             if sent_len == 0:
56                 print("SOCKET CONNECTION BROKEN")
57                 raise ExceptionForRestart
58             total_sent_len += sent_len
59
60     def _recv_line_wrapped(self, send_ping=True):
61         if len(self.line_buffer) > 0:
62             return self.line_buffer.pop(0)
63         while True:
64             ready = select.select([self.socket], [], [], int(self.timeout / 2))
65             if not ready[0]:
66                 self._pingtest(send_ping)
67                 return None
68             self.last_pong = time.time()
69             received_bytes = self.socket.recv(1024)
70             try:
71                 received_runes = received_bytes.decode("UTF-8")
72             except UnicodeDecodeError:
73                 received_runes = received_bytes.decode("latin1")
74             if len(received_runes) == 0:
75                 print("SOCKET CONNECTION BROKEN")
76                 raise ExceptionForRestart
77             self.rune_buffer += received_runes
78             lines_split = str.split(self.rune_buffer, "\r\n")
79             self.line_buffer += lines_split[:-1]
80             self.rune_buffer = lines_split[-1]
81             if len(self.line_buffer) > 0:
82                 return self.line_buffer.pop(0)
83
84     def recv_line(self, send_ping=True):
85         line = self._recv_line_wrapped(send_ping)
86         if line:
87             print("LINE FROM SERVER " + str(datetime.datetime.now()) + ": " +
88                   line)
89         return line
90
91
92 def init_session(server, port, timeout, nickname, username, channel):
93     print("CONNECTING TO " + server)
94     io = IO(server, port, timeout)
95     io.send_line("NICK " + nickname)
96     io.send_line("USER " + username + " 0 * : ")
97     io.send_line("JOIN " + channel)
98     return io
99
100
101 def lineparser_loop(io, nickname):
102
103     def act_on_privmsg(tokens):
104
105         def url_check(msg):
106
107             def notice(msg):
108                 io.send_line("NOTICE " + target + " :" + msg)
109
110             matches = re.findall("(https?://[^\s>]+)", msg)
111             for i in range(len(matches)):
112                 url = matches[i]
113                 try:
114                     r = requests.get(url, timeout=15)
115                 except (requests.exceptions.TooManyRedirects,
116                         requests.exceptions.ConnectionError,
117                         requests.exceptions.InvalidSchema) as error:
118                     notice("TROUBLE FOLLOWING URL: " + str(error))
119                     continue
120                 title = bs4.BeautifulSoup(r.text).title
121                 if title:
122                     notice("PAGE TITLE FOR URL: " + title.string)
123                 else:
124                     notice("PAGE HAS NO TITLE TAG")
125
126         sender = ""
127         for rune in tokens[0]:
128             if rune == "!":
129                 break
130             if rune != ":":
131                 sender += rune
132         receiver = ""
133         for rune in tokens[2]:
134             if rune == "!":
135                 break
136             if rune != ":":
137                 receiver += rune
138         target = sender
139         if receiver != nickname:
140             target = receiver
141         msg = str.join(" ", tokens[3:])[1:]
142         url_check(msg)
143
144     while True:
145         line = io.recv_line()
146         if not line:
147             continue
148         tokens = line.split(" ")
149         if len(tokens) > 1:
150             if tokens[1] == "PRIVMSG":
151                 act_on_privmsg(tokens)
152             if tokens[0] == "PING":
153                 io.send_line("PONG " + tokens[1])
154
155
156 def parse_command_line_arguments():
157     parser = argparse.ArgumentParser()
158     parser.add_argument("-s, --server", action="store", dest="server",
159                         default=SERVER,
160                         help="server or server net to connect to (default: "
161                         + SERVER + ")")
162     parser.add_argument("-p, --port", action="store", dest="port", type=int,
163                         default=PORT, help="port to connect to (default : "
164                         + str(PORT) + ")")
165     parser.add_argument("-t, --timeout", action="store", dest="timeout",
166                         type=int, default=TIMEOUT,
167                         help="timeout in seconds after which to attempt " +
168                         "reconnect (default: " + str(TIMEOUT) + ")")
169     parser.add_argument("-u, --username", action="store", dest="username",
170                         default=USERNAME, help="username to use (default: "
171                         + USERNAME + ")")
172     parser.add_argument("-n, --nickname", action="store", dest="nickname",
173                         default=NICKNAME, help="nickname to use (default: "
174                         + NICKNAME + ")")
175     parser.add_argument("CHANNEL", action="store", help="channel to join")
176     opts, unknown = parser.parse_known_args()
177     return opts
178
179 opts = parse_command_line_arguments()
180 while True:
181     try:
182         io = init_session(opts.server, opts.port, opts.timeout, opts.nickname,
183                           opts.username, opts.CHANNEL)
184         lineparser_loop(io, opts.nickname)
185     except ExceptionForRestart:
186         io.socket.close()
187         continue