From c4a963cc174e194b2a263b7a0830e7d8c107edaf Mon Sep 17 00:00:00 2001 From: Ben Harris Date: Wed, 11 Mar 2020 14:10:44 -0400 Subject: use irctokens library --- requirements.txt | 1 + tracer.py | 78 ++++++++++++++++++++++++++++++++++---------------------- 2 files changed, 49 insertions(+), 30 deletions(-) diff --git a/requirements.txt b/requirements.txt index 0c151f0..f6a8359 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1,2 @@ tracery==0.1.1 +irctokens==0.4.3 diff --git a/tracer.py b/tracer.py index 196bed7..9f59f93 100644 --- a/tracer.py +++ b/tracer.py @@ -4,6 +4,7 @@ from random import randint, choice from tracery.modifiers import base_english import configparser import glob +import irctokens import json import os import random @@ -102,17 +103,22 @@ def fuse(argv): return grammar(raw).flatten("#origin#") -def rawsend(msg): - print(msg) - ircsock.send(f"{msg}\r\n".encode()) +def _send(line): + print(f"> {line.format()}") + e.push(line) + while e.pending(): + e.pop(s.send(e.pending())) def send(chan, msg): - rawsend(f"PRIVMSG #{chan} :{msg}") + _send(irctokens.format("PRIVMSG", [chan, msg])) -def think(chan, nick, msg): - words = re.split("[ \t\s]+", msg) +def think(line): + chan = line.params.pop(0) + words = line.params[0].split(" ") + nick = line.source.split("!", 1)[0] + if len(words) > 0 and nick != bot["nick"]: if words[0] == "!!list": res = "" @@ -149,38 +155,50 @@ def think(chan, nick, msg): if __name__ == "__main__": - ircsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - ircsock.connect((bot["server"], int(bot["port"]))) - rawsend("NICK %s" % bot["nick"]) - rawsend("USER %s 0 * :tracery bot" % bot["nick"]) + d = irctokens.StatefulDecoder() + e = irctokens.StatefulEncoder() + s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + s.connect((bot["server"], int(bot["port"]))) + + _send(irctokens.format("USER", [bot["nick"], "0", "*", "tracery bot"])) + _send(irctokens.format("NICK", [bot["nick"]])) - while 1: - for msg in ircsock.recv(2048).decode().split("\r\n"): - print(msg) + while True: + lines = d.push(s.recv(1024)) - if "PING" in msg: - rawsend(msg.replace("I", "O", 1)) + if lines == None: + print("! disconnected") + break - if "INVITE" in msg: - chan = msg.split(":")[-1] - rawsend(f"JOIN {chan}") + for line in lines: + print(f"< {line.format()}") - if "001" in msg: + if line.command == "PING": + _send(irctokens.format("PONG", [line.params[0]])) + + elif line.command == "001": + _send(irctokens.format("MODE", [bot["nick"], "+B"])) if account is not None: - rawsend( - "SQUERY NickServ IDENTIFY %s %s" - % (account["username"], account["password"]) + _send( + irctokens.format( + "SQUERY", + [ + "NickServ", + "IDENTIFY", + account["username"], + account["password"], + ], + ) ) - for c in bot.getlist("channels"): - rawsend(f"JOIN {c}") - rawsend("MODE %s +B" % bot["nick"]) + _send(irctokens.format("JOIN", bot.getlist("channels"))) + + elif line.command == "INVITE": + _send(irctokens.format("JOIN", [line.params[0]])) - m = re.match(":(?P[^ ]+)!.*PRIVMSG #(?P\w+) :(?P.*)", msg) - if m and m.groupdict(): - m = m.groupdict() + elif line.command == "PRIVMSG": try: - think(m["chan"], m["nick"], m["msg"]) + think(line) except Exception as e: - print("ERROR" + str(m)) + print("ERROR", line) print(e) traceback.print_exc() -- cgit 1.4.1