Move sample run routine

trunk
mio 2018-09-14 05:54:21 +00:00
parent 797ed660dd
commit 3914db4685
2 changed files with 44 additions and 41 deletions

54
irc.py
View File

@ -1,5 +1,6 @@
import socket import socket
from sys import exit from sys import exit
from time import sleep
class IRC: class IRC:
@ -7,6 +8,35 @@ class IRC:
debug = False debug = False
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."""
try:
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock.connect(server)
except ConnectionError as err:
exit("[debug] " + str(err))
self.send("USER", bot_nick + " " + bot_nick + " " + \
bot_nick + " " + bot_nick)
self.send("NICK", bot_nick)
def disconnect(self, resp_msg, quit_msg, admin_user):
"""Notify the admin user and disconnect from the server."""
self.send("PRIVMSG", resp_msg, recvr=admin_user)
self.send("QUIT", ":" + quit_msg)
def send(self, command, text, *args, **kwargs): def send(self, command, text, *args, **kwargs):
"""Send messages given the IRC command and text. Optionally specify a """Send messages given the IRC command and text. Optionally specify a
message recipient with `recvr=user`.""" message recipient with `recvr=user`."""
@ -24,25 +54,9 @@ class IRC:
bs.decode("utf-8").strip() + "`") bs.decode("utf-8").strip() + "`")
pass pass
def connect(self, server, bot_nick):
"""Connect to the server and sends user/nick information."""
try:
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock.connect(server)
except ConnectionError as err:
exit("[debug] " + str(err))
self.send("USER", bot_nick + " " + bot_nick + " " + \
bot_nick + " " + bot_nick)
self.send("NICK", bot_nick)
def disconnect(self, resp_msg, quit_msg, admin_user):
"""Notify the admin user and disconnect from the server."""
self.send("PRIVMSG", resp_msg, recvr=admin_user)
self.send("QUIT", ":" + quit_msg)
def receive(self, *args, **kwargs): def receive(self, *args, **kwargs):
"""Get messages from the connected socket.""" """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: if self.debug:
print("[debug][recv] " + data) print("[debug][recv] " + data)
return data return data
@ -59,9 +73,9 @@ class IRC:
self.send("JOIN", c) self.send("JOIN", c)
def parse_msg(self, line, req_prefix): def parse_msg(self, line, req_prefix):
"""Extract the request, the nick and username of the requester, """Using received data from a socket, extract the request, the nick and
the channel where the request originated and returns a dictionary of username of the requester, the channel where the request originated and
values.""" return a dictionary of values."""
data = {"req": "", "req_chan": "", "nick": "", "user": ""} data = {"req": "", "req_chan": "", "nick": "", "user": ""}
if (":" + req_prefix) in line: if (":" + req_prefix) in line:
data["req"] = line.split("PRIVMSG", 1)[1].split(":" + \ data["req"] = line.split("PRIVMSG", 1)[1].split(":" + \

View File

@ -1,33 +1,22 @@
from sys import exit from sys import exit
from random import randint from random import randint
from time import sleep
import config as cfg import config as cfg
from irc import IRC from irc import IRC
class Ramen: class Ramen:
"""Functions for ramenkan.""" """Requests with a ramen theme."""
irc = IRC() def main(self):
irc.debug = cfg.debug """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): def handle(self, msg, channel):
"""Connect to the server and channels, initialise the listener in the """Listen for requests in a channel and match them to functions."""
application loop.""" self.msg = msg
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."""
if self.msg["req_chan"] == cfg.bot_nick: if self.msg["req_chan"] == cfg.bot_nick:
# Respond to some commands only from admin user # Respond to some commands only from admin user
if self.msg["user"].lower() == cfg.admin_user.lower() and \ if self.msg["user"].lower() == cfg.admin_user.lower() and \
@ -78,4 +67,4 @@ class Ramen:
app = Ramen() app = Ramen()
app.run() app.main()