From 3914db4685f08f1a8758029db5c4dfe24b43386f Mon Sep 17 00:00:00 2001 From: mio Date: Fri, 14 Sep 2018 05:54:21 +0000 Subject: [PATCH] Move sample run routine --- irc.py | 54 ++++++++++++++++++++++++++++++++++-------------------- ramen.py | 31 ++++++++++--------------------- 2 files changed, 44 insertions(+), 41 deletions(-) diff --git a/irc.py b/irc.py index 2e86c9c..9e4037d 100644 --- a/irc.py +++ b/irc.py @@ -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(":" + \ diff --git a/ramen.py b/ramen.py index f6020eb..febe403 100644 --- a/ramen.py +++ b/ramen.py @@ -1,33 +1,22 @@ from sys import exit from random import randint -from time import sleep import config as cfg from irc import IRC class Ramen: - """Functions for ramenkan.""" + """Requests with a ramen theme.""" - irc = IRC() - irc.debug = cfg.debug + def main(self): + """Instantiate an IRC object and pass in the request handler.""" + self.irc = IRC() + self.irc.debug = cfg.debug + self.irc.run(self.handle, cfg) - def run(self): - """Connect to the server and channels, initialise the listener in the - application loop.""" - self.irc.connect(cfg.server, cfg.bot_nick) - self.irc.join_channels(cfg.channels) - while 1: - sleep(2) - data = self.irc.receive(debug=cfg.debug) - self.irc.keep_alive(data) - self.msg = self.irc.parse_msg(data, cfg.req_prefix) - for chan in cfg.channels: - self.handle(chan) - - def handle(self, channel): - """Listen for requests in a channel and pass them to handler - functions.""" + def handle(self, msg, channel): + """Listen for requests in a channel and match them to functions.""" + self.msg = msg if self.msg["req_chan"] == cfg.bot_nick: # Respond to some commands only from admin user if self.msg["user"].lower() == cfg.admin_user.lower() and \ @@ -78,4 +67,4 @@ class Ramen: app = Ramen() -app.run() +app.main()