Move sample run routine
parent
797ed660dd
commit
3914db4685
54
irc.py
54
irc.py
|
@ -1,5 +1,6 @@
|
|||
import socket
|
||||
from sys import exit
|
||||
from time import sleep
|
||||
|
||||
|
||||
class IRC:
|
||||
|
@ -7,6 +8,35 @@ class IRC:
|
|||
|
||||
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):
|
||||
"""Send messages given the IRC command and text. Optionally specify a
|
||||
message recipient with `recvr=user`."""
|
||||
|
@ -24,25 +54,9 @@ class IRC:
|
|||
bs.decode("utf-8").strip() + "`")
|
||||
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):
|
||||
"""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(":" + \
|
||||
|
|
31
ramen.py
31
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()
|
||||
|
|
Loading…
Reference in New Issue