about summary refs log tree commit diff
path: root/tracer.py
diff options
context:
space:
mode:
authorBen Harris <ben@tilde.team>2020-03-11 14:10:44 -0400
committerBen Harris <ben@tilde.team>2020-03-11 14:10:44 -0400
commitc4a963cc174e194b2a263b7a0830e7d8c107edaf (patch)
tree13ab457634f7215042f206421e7559501dde6c3f /tracer.py
parent82313c091e3049fc9cb65a155934abc2d852c64e (diff)
use irctokens library
Diffstat (limited to 'tracer.py')
-rw-r--r--tracer.py78
1 files changed, 48 insertions, 30 deletions
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<nick>[^ ]+)!.*PRIVMSG #(?P<chan>\w+) :(?P<msg>.*)", 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()