|
@@ -1,3 +1,5 @@
|
|
|
+#!/usr/bin/env python3
|
|
|
+
|
|
|
import socket
|
|
|
import re
|
|
|
from random import randint, choice
|
|
@@ -5,12 +7,14 @@ import sys, os
|
|
|
import time
|
|
|
import subprocess
|
|
|
import tracery
|
|
|
+import traceback
|
|
|
from tracery.modifiers import base_english
|
|
|
import json
|
|
|
import random
|
|
|
|
|
|
DB = {}
|
|
|
|
|
|
+
|
|
|
def grammar(rules):
|
|
|
try:
|
|
|
res = tracery.Grammar(rules)
|
|
@@ -19,6 +23,7 @@ def grammar(rules):
|
|
|
except Exception as e:
|
|
|
print(e)
|
|
|
|
|
|
+
|
|
|
def load_rules(path):
|
|
|
try:
|
|
|
with open(path) as f:
|
|
@@ -26,23 +31,26 @@ def load_rules(path):
|
|
|
except Exception as e:
|
|
|
print(e)
|
|
|
|
|
|
+
|
|
|
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
|
|
|
+ rule = d + "/" + p
|
|
|
name = p.replace(".json", "")
|
|
|
- #print(p, rule, name)
|
|
|
+ # print(p, rule, name)
|
|
|
if p in DB:
|
|
|
DB[name].append(grammar(load_rules(rule)))
|
|
|
else:
|
|
|
DB[name] = [grammar(load_rules(rule))]
|
|
|
|
|
|
+
|
|
|
populate()
|
|
|
-#print(DB)
|
|
|
+print(DB)
|
|
|
+
|
|
|
|
|
|
def generate(rule):
|
|
|
populate()
|
|
@@ -57,17 +65,17 @@ def listify(col):
|
|
|
else:
|
|
|
return [col]
|
|
|
|
|
|
+
|
|
|
def shuffle(col):
|
|
|
- a = random.choice(col.keys())
|
|
|
- b = random.choice(col.keys())
|
|
|
- if 'origin' in [a,b]:
|
|
|
+ a = random.choice(list(col))
|
|
|
+ b = random.choice(list(col))
|
|
|
+ if "origin" in [a, b]:
|
|
|
print("origin discard")
|
|
|
return col
|
|
|
- temp = col[a]
|
|
|
- col[a] = col[b]
|
|
|
- col[b] = temp
|
|
|
+ col[a], col[b] = col[b], col[a]
|
|
|
return col
|
|
|
|
|
|
+
|
|
|
def fuse(argv):
|
|
|
populate()
|
|
|
raw = {}
|
|
@@ -76,7 +84,7 @@ def fuse(argv):
|
|
|
g = random.choice(DB[gk]).raw
|
|
|
for k in g:
|
|
|
if k in raw:
|
|
|
- raw[k] = listify(raw[k])+listify(g[k])
|
|
|
+ raw[k] = listify(raw[k]) + listify(g[k])
|
|
|
else:
|
|
|
raw[k] = g[k]
|
|
|
for i in range(20):
|
|
@@ -85,55 +93,47 @@ def fuse(argv):
|
|
|
|
|
|
|
|
|
server = "127.0.0.1"
|
|
|
-channels = []
|
|
|
+channels = ["#bots", "#meta", "#team"]
|
|
|
if len(sys.argv) > 1:
|
|
|
for c in sys.argv[1:]:
|
|
|
- channels.append("#"+c)
|
|
|
+ channels.append("#" + c)
|
|
|
botnick = "tracer"
|
|
|
|
|
|
-def ping():
|
|
|
- ircsock.send("PONG :Pong\n")
|
|
|
|
|
|
-def joinchan(chan):
|
|
|
- ircsock.send("JOIN "+ chan +"\n")
|
|
|
+def rawsend(msg):
|
|
|
+ print(msg)
|
|
|
+ ircsock.send(f"{msg}\r\n".encode())
|
|
|
|
|
|
-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)
|
|
|
|
|
|
+def send(chan, msg):
|
|
|
+ # This is the send message function, it simply sends messages to the channel.
|
|
|
+ rawsend(f"PRIVMSG #{chan} :{msg}")
|
|
|
|
|
|
|
|
|
-def send(channel, s):
|
|
|
- sendmsg("#"+channel, s)
|
|
|
-
|
|
|
def think(chan, nick, msg):
|
|
|
- words = re.split('[ \t\s]+', msg)
|
|
|
+ words = re.split("[ \t\s]+", msg)
|
|
|
if len(words) > 0 and nick != "tracer":
|
|
|
if words[0] == "!!list":
|
|
|
res = ""
|
|
|
for k in DB:
|
|
|
- res += k+" "
|
|
|
- send(chan, res.encode("utf8")[:475])
|
|
|
+ res += k + " "
|
|
|
+ send(chan, res[:475])
|
|
|
elif words[0] == "!!fuse":
|
|
|
if "|" in words:
|
|
|
- res = fuse(words[1:words.index("|")])
|
|
|
+ res = fuse(words[1 : words.index("|")])
|
|
|
if res:
|
|
|
- send(chan, " ".join(words[words.index("|")+1:])+" "+res)
|
|
|
+ send(chan, " ".join(words[words.index("|") + 1 :]) + " " + res)
|
|
|
else:
|
|
|
res = fuse(words[1:])
|
|
|
if res:
|
|
|
- send(chan,res[0:475])
|
|
|
+ send(chan, res[0:475])
|
|
|
+ elif words[0] == "!!source":
|
|
|
+ send(chan, "https://tildegit.org/ben/tracer")
|
|
|
+ elif words[0] == "!botlist" or words[0] == "!!help":
|
|
|
+ send(
|
|
|
+ chan,
|
|
|
+ "helo i'm a tracery bot that makes cool things from tracery grammars in your ~/.tracery. see http://tracery.io for more info",
|
|
|
+ )
|
|
|
elif words[0][0:2] == "!!":
|
|
|
print(words)
|
|
|
res = generate(words[0][2:])
|
|
@@ -146,19 +146,38 @@ def think(chan, nick, msg):
|
|
|
else:
|
|
|
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
|
|
|
+
|
|
|
while 1:
|
|
|
- ircmsg = ircsock.recv(2048)
|
|
|
- ircmsg = ircmsg.strip('\n\r')
|
|
|
-
|
|
|
- if ircmsg.find("PING") != -1:
|
|
|
- ping()
|
|
|
-
|
|
|
- m = re.match(':(?P<nick>[^ ]+)!.*PRIVMSG #(?P<chan>\w+) :(?P<msg>.*)', ircmsg)
|
|
|
- if m and m.groupdict():
|
|
|
- m = m.groupdict()
|
|
|
- try:
|
|
|
- think(m["chan"], m["nick"], m["msg"])
|
|
|
- except Exception as e:
|
|
|
- print "ERROR" + str(m)
|
|
|
- print(e)
|
|
|
+ msg = ircsock.recv(2048).decode().split("\r\n")
|
|
|
+ for ircmsg in msg:
|
|
|
+ print(ircmsg)
|
|
|
+
|
|
|
+ if "PING" in ircmsg:
|
|
|
+ rawsend(ircmsg.replace("I", "O", 1))
|
|
|
+
|
|
|
+ if "INVITE" in ircmsg:
|
|
|
+ chan = ircmsg.split(":")[-1]
|
|
|
+ rawsend(f"JOIN {chan}")
|
|
|
+
|
|
|
+ if "001" in ircmsg:
|
|
|
+ for c in channels:
|
|
|
+ rawsend(f"JOIN {c}")
|
|
|
+ rawsend(f"MODE {botnick} +B")
|
|
|
+
|
|
|
+ m = re.match(
|
|
|
+ ":(?P<nick>[^ ]+)!.*PRIVMSG #(?P<chan>\w+) :(?P<msg>.*)", ircmsg
|
|
|
+ )
|
|
|
+ if m and m.groupdict():
|
|
|
+ m = m.groupdict()
|
|
|
+ try:
|
|
|
+ think(m["chan"], m["nick"], m["msg"])
|
|
|
+ except Exception as e:
|
|
|
+ print("ERROR" + str(m))
|
|
|
+ print(e)
|
|
|
+ traceback.print_exc()
|