|
|
|
@ -1,5 +1,6 @@
|
|
|
|
|
import socket
|
|
|
|
|
from sys import exit
|
|
|
|
|
from time import sleep
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class IRC:
|
|
|
|
@ -7,22 +8,18 @@ class IRC:
|
|
|
|
|
|
|
|
|
|
debug = False
|
|
|
|
|
|
|
|
|
|
def send(self, command, text, *args, **kwargs):
|
|
|
|
|
"""Send messages given the IRC command and text. Optionally specify a
|
|
|
|
|
message recipient with `recvr=user`."""
|
|
|
|
|
recvr = kwargs.get("recvr", "")
|
|
|
|
|
if recvr != "":
|
|
|
|
|
recvr += " :"
|
|
|
|
|
if self.debug:
|
|
|
|
|
print("[debug][send] " + command + " " + recvr + text)
|
|
|
|
|
bs = bytes(command + " " + recvr + text + "\r\n", "utf-8")
|
|
|
|
|
try:
|
|
|
|
|
self.sock.sendall(bs)
|
|
|
|
|
except BrokenPipeError as err:
|
|
|
|
|
if self.debug:
|
|
|
|
|
print("[debug] " + str(err) + " at `" + \
|
|
|
|
|
bs.decode("utf-8").strip() + "`")
|
|
|
|
|
pass
|
|
|
|
|
def run(self, handler, conf):
|
|
|
|
|
"""A routine that connects to a server, joins channels, and initialises
|
|
|
|
|
the request handler in a loop."""
|
|
|
|
|
self.connect(conf.server, conf.bot_nick)
|
|
|
|
|
self.join_channels(conf.channels)
|
|
|
|
|
while 1:
|
|
|
|
|
sleep(2)
|
|
|
|
|
data = self.receive(debug=conf.debug)
|
|
|
|
|
self.keep_alive(data)
|
|
|
|
|
self.msg = self.parse_msg(data, conf.req_prefix)
|
|
|
|
|
for c in conf.channels:
|
|
|
|
|
handler(self.msg, c)
|
|
|
|
|
|
|
|
|
|
def connect(self, server, bot_nick):
|
|
|
|
|
"""Connect to the server and sends user/nick information."""
|
|
|
|
@ -40,9 +37,26 @@ class IRC:
|
|
|
|
|
self.send("PRIVMSG", resp_msg, recvr=admin_user)
|
|
|
|
|
self.send("QUIT", ":" + quit_msg)
|
|
|
|
|
|
|
|
|
|
def send(self, command, text, *args, **kwargs):
|
|
|
|
|
"""Send messages given the IRC command and text. Optionally specify a
|
|
|
|
|
message recipient with `recvr=user`."""
|
|
|
|
|
recvr = kwargs.get("recvr", "")
|
|
|
|
|
if recvr != "":
|
|
|
|
|
recvr += " :"
|
|
|
|
|
if self.debug:
|
|
|
|
|
print("[debug][send] " + command + " " + recvr + text)
|
|
|
|
|
bs = bytes(command + " " + recvr + text + "\r\n", "utf-8")
|
|
|
|
|
try:
|
|
|
|
|
self.sock.sendall(bs)
|
|
|
|
|
except BrokenPipeError as err:
|
|
|
|
|
if self.debug:
|
|
|
|
|
print("[debug] " + str(err) + " at `" + \
|
|
|
|
|
bs.decode("utf-8").strip() + "`")
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
def receive(self, *args, **kwargs):
|
|
|
|
|
"""Get messages from the connected socket."""
|
|
|
|
|
data = self.sock.recv(2040).decode("utf-8").strip("\n\r")
|
|
|
|
|
data = self.sock.recv(2040).decode("utf-8").strip("\r\n")
|
|
|
|
|
if self.debug:
|
|
|
|
|
print("[debug][recv] " + data)
|
|
|
|
|
return data
|
|
|
|
@ -59,9 +73,9 @@ class IRC:
|
|
|
|
|
self.send("JOIN", c)
|
|
|
|
|
|
|
|
|
|
def parse_msg(self, line, req_prefix):
|
|
|
|
|
"""Extract the request, the nick and username of the requester,
|
|
|
|
|
the channel where the request originated and returns a dictionary of
|
|
|
|
|
values."""
|
|
|
|
|
"""Using received data from a socket, extract the request, the nick and
|
|
|
|
|
username of the requester, the channel where the request originated and
|
|
|
|
|
return a dictionary of values."""
|
|
|
|
|
data = {"req": "", "req_chan": "", "nick": "", "user": ""}
|
|
|
|
|
if (":" + req_prefix) in line:
|
|
|
|
|
data["req"] = line.split("PRIVMSG", 1)[1].split(":" + \
|
|
|
|
|