Move sample run routine
parent
797ed660dd
commit
3914db4685
54
irc.py
54
irc.py
|
@ -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,22 +8,18 @@ class IRC:
|
||||||
|
|
||||||
debug = False
|
debug = False
|
||||||
|
|
||||||
def send(self, command, text, *args, **kwargs):
|
def run(self, handler, conf):
|
||||||
"""Send messages given the IRC command and text. Optionally specify a
|
"""A routine that connects to a server, joins channels, and initialises
|
||||||
message recipient with `recvr=user`."""
|
the request handler in a loop."""
|
||||||
recvr = kwargs.get("recvr", "")
|
self.connect(conf.server, conf.bot_nick)
|
||||||
if recvr != "":
|
self.join_channels(conf.channels)
|
||||||
recvr += " :"
|
while 1:
|
||||||
if self.debug:
|
sleep(2)
|
||||||
print("[debug][send] " + command + " " + recvr + text)
|
data = self.receive(debug=conf.debug)
|
||||||
bs = bytes(command + " " + recvr + text + "\r\n", "utf-8")
|
self.keep_alive(data)
|
||||||
try:
|
self.msg = self.parse_msg(data, conf.req_prefix)
|
||||||
self.sock.sendall(bs)
|
for c in conf.channels:
|
||||||
except BrokenPipeError as err:
|
handler(self.msg, c)
|
||||||
if self.debug:
|
|
||||||
print("[debug] " + str(err) + " at `" + \
|
|
||||||
bs.decode("utf-8").strip() + "`")
|
|
||||||
pass
|
|
||||||
|
|
||||||
def connect(self, server, bot_nick):
|
def connect(self, server, bot_nick):
|
||||||
"""Connect to the server and sends user/nick information."""
|
"""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("PRIVMSG", resp_msg, recvr=admin_user)
|
||||||
self.send("QUIT", ":" + quit_msg)
|
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):
|
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(":" + \
|
||||||
|
|
31
ramen.py
31
ramen.py
|
@ -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()
|
||||||
|
|
Loading…
Reference in New Issue