home · contact · privacy
Improve setup script error handling.
[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.InvalidURL,
118                         requests.exceptions.InvalidSchema) as error:
119                     notice("TROUBLE FOLLOWING URL: " + str(error))
120                     continue
121                 title = bs4.BeautifulSoup(r.text).title
122                 if title:
123                     notice("PAGE TITLE FOR URL: " + title.string)
124                 else:
125                     notice("PAGE HAS NO TITLE TAG")
126
127         sender = ""
128         for rune in tokens[0]:
129             if rune == "!":
130                 break
131             if rune != ":":
132                 sender += rune
133         receiver = ""
134         for rune in tokens[2]:
135             if rune == "!":
136                 break
137             if rune != ":":
138                 receiver += rune
139         target = sender
140         if receiver != nickname:
141             target = receiver
142         msg = str.join(" ", tokens[3:])[1:]
143         url_check(msg)
144
145     while True:
146         line = io.recv_line()
147         if not line:
148             continue
149         tokens = line.split(" ")
150         if len(tokens) > 1:
151             if tokens[1] == "PRIVMSG":
152                 act_on_privmsg(tokens)
153             if tokens[0] == "PING":
154                 io.send_line("PONG " + tokens[1])
155
156
157 def parse_command_line_arguments():
158     parser = argparse.ArgumentParser()
159     parser.add_argument("-s, --server", action="store", dest="server",
160                         default=SERVER,
161                         help="server or server net to connect to (default: "
162                         + SERVER + ")")
163     parser.add_argument("-p, --port", action="store", dest="port", type=int,
164                         default=PORT, help="port to connect to (default : "
165                         + str(PORT) + ")")
166     parser.add_argument("-t, --timeout", action="store", dest="timeout",
167                         type=int, default=TIMEOUT,
168                         help="timeout in seconds after which to attempt " +
169                         "reconnect (default: " + str(TIMEOUT) + ")")
170     parser.add_argument("-u, --username", action="store", dest="username",
171                         default=USERNAME, help="username to use (default: "
172                         + USERNAME + ")")
173     parser.add_argument("-n, --nickname", action="store", dest="nickname",
174                         default=NICKNAME, help="nickname to use (default: "
175                         + NICKNAME + ")")
176     parser.add_argument("CHANNEL", action="store", help="channel to join")
177     opts, unknown = parser.parse_known_args()
178     return opts
179
180 opts = parse_command_line_arguments()
181 while True:
182     try:
183         io = init_session(opts.server, opts.port, opts.timeout, opts.nickname,
184                           opts.username, opts.CHANNEL)
185         lineparser_loop(io, opts.nickname)
186     except ExceptionForRestart:
187         io.socket.close()
188         continue