about summary refs log tree commit diff
path: root/tracer.py
diff options
context:
space:
mode:
Diffstat (limited to 'tracer.py')
-rwxr-xr-xtracer.py59
1 files changed, 33 insertions, 26 deletions
diff --git a/tracer.py b/tracer.py
index 070d4fb..8b34a9e 100755
--- a/tracer.py
+++ b/tracer.py
@@ -1,3 +1,5 @@
+#!/usr/bin/env python3
+
 import socket
 import re
 from random import randint, choice
@@ -30,7 +32,7 @@ def populate():
     global DB
     DB = {}
     for subdir in os.listdir("/home"):
-        d = "/home/"+subdir+"/.tracery"
+        d = f"/home/{subdir}/.tracery"
         if os.path.isdir(d):
             for p in os.listdir(d):
                 rule = d+"/"+p
@@ -42,7 +44,7 @@ def populate():
                     DB[name] = [grammar(load_rules(rule))]
 
 populate()
-#print(DB)
+print(DB)
 
 def generate(rule):
     populate()
@@ -85,37 +87,28 @@ def fuse(argv):
 
 
 server = "127.0.0.1"
-channels = []
+channels = ["#bots"]
 if len(sys.argv) > 1:
     for c in sys.argv[1:]:
         channels.append("#"+c)
 botnick = "tracer"
 
-def ping():
-    ircsock.send("PONG :Pong\n")
+def rawsend(msg):
+    print(msg)
+    ircsock.send(f"{msg}\r\n".encode())
 
 def joinchan(chan):
-    ircsock.send("JOIN "+ chan +"\n")
-
-def sendmsg(chan , msg): # This is the send message function, it simply sends messages to the channel.
-      ircsock.send("PRIVMSG "+ chan +" :"+ msg.encode("utf8") +"\n")
-
-def wexec(msg):
-    ircsock.send("EXEC -msg "+channel+" "+msg)
-
-if __name__ == "__main__":
-    ircsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
-    ircsock.connect((server, 6667)) # Here we connect to the server using port 6667
-    ircsock.send("USER "+ botnick +" "+ botnick +" "+ botnick +" :tracery bot.\n") # user authentication
-    ircsock.send("NICK "+ botnick +"\n") # here we actually assign the nick to the bot
-
-    for c in channels:
-        joinchan(c)
+    rawsend(f"JOIN {chan}")
 
+def sendmsg(chan, msg): 
+    # This is the send message function, it simply sends messages to the channel.
+    rawsend(f"PRIVMSG {chan} :{msg}")
 
+# def wexec(msg):
+#    ircsock.send("EXEC -msg "+channel+" "+msg)
 
 def send(channel, s):
-    sendmsg("#"+channel, s)
+    sendmsg(f"#{channel}", s)
 
 def think(chan, nick, msg):
     words = re.split('[ \t\s]+', msg)
@@ -124,7 +117,7 @@ def think(chan, nick, msg):
             res = ""
             for k in DB:
                 res += k+" "
-            send(chan, res.encode("utf8")[:475])
+            send(chan, res[:475])
         elif words[0] == "!!fuse":
             if "|" in words:
                 res = fuse(words[1:words.index("|")])
@@ -147,12 +140,25 @@ def think(chan, nick, msg):
                     send(chan, res)
 
 if __name__ == "__main__":
+    ircsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+    ircsock.connect((server, 6667)) # Here we connect to the server using port 6667
+    rawsend(f"NICK {botnick}") # here we actually assign the nick to the bot
+    rawsend(f"USER {botnick} 0 * :tracery bot") # user authentication
+
+    for c in channels:
+        joinchan(c)
+
     while 1:
         ircmsg = ircsock.recv(2048)
-        ircmsg = ircmsg.strip('\n\r')
+        ircmsg = ircmsg.decode().strip('\n\r')
+        print(ircmsg)
 
         if ircmsg.find("PING") != -1:
-            ping()
+            rawsend(ircmsg.replace("I", "O"))
+
+        if ircmsg.find("001") != -1:
+            for c in channels:
+                joinchan(c)
 
         m = re.match(':(?P<nick>[^ ]+)!.*PRIVMSG #(?P<chan>\w+) :(?P<msg>.*)', ircmsg)
         if m and m.groupdict():
@@ -160,5 +166,6 @@ if __name__ == "__main__":
             try:
                 think(m["chan"], m["nick"], m["msg"])
             except Exception as e:
-                print "ERROR" + str(m)
+                print("ERROR" + str(m))
                 print(e)
+