tracer/tracer.py

196 lines
5.1 KiB
Python
Raw Normal View History

2019-05-28 16:16:58 +00:00
#!/usr/bin/env python3
2021-02-19 04:13:16 +00:00
from irctokens import build, Line
from ircrobots import Bot as BaseBot
from ircrobots import Server as BaseServer
from ircrobots import ConnectionParams, SASLUserPass
2019-01-29 02:33:35 +00:00
from random import randint, choice
from tracery.modifiers import base_english
2021-02-19 04:13:16 +00:00
import asyncio
2020-02-24 15:47:57 +00:00
import configparser
import glob
2019-01-29 02:33:35 +00:00
import json
2020-02-24 15:47:57 +00:00
import os
2019-01-29 02:33:35 +00:00
import random
2020-02-24 15:47:57 +00:00
import traceback
import tracery
2019-01-29 02:33:35 +00:00
DB = {}
2020-02-24 15:47:57 +00:00
config = configparser.ConfigParser(
converters={"list": lambda x: [i.strip() for i in x.split(",")]}
)
config.read("config.ini")
bot = config["irc"]
2019-01-29 02:33:35 +00:00
2020-02-24 16:32:56 +00:00
# read account info if it exists
if os.path.isfile("account.ini"):
account = configparser.ConfigParser()
account.read("account.ini")
account = account["nickserv"]
2019-05-28 17:31:21 +00:00
2019-01-29 02:33:35 +00:00
def grammar(rules):
try:
res = tracery.Grammar(rules)
res.add_modifiers(base_english)
return res
except Exception as e:
print(e)
2019-05-28 17:31:21 +00:00
2019-01-29 02:33:35 +00:00
def load_rules(path):
try:
with open(path) as f:
return json.loads(f.read())
except Exception as e:
print(e)
2019-05-28 17:31:21 +00:00
2019-01-29 02:33:35 +00:00
def populate():
global DB
2021-02-19 04:13:16 +00:00
# DB = {}
2020-02-24 15:47:57 +00:00
for p in glob.glob("/home/*/.tracery/*"):
name, ext = os.path.splitext(p)
2020-02-24 16:32:56 +00:00
name = os.path.basename(name)
2020-02-24 15:47:57 +00:00
if name.startswith(".") or ext not in (".json", ""):
continue
if p in DB:
DB[name].append(grammar(load_rules(p)))
else:
DB[name] = [grammar(load_rules(p))]
2019-01-29 02:33:35 +00:00
2019-05-28 17:31:21 +00:00
2019-01-29 02:33:35 +00:00
populate()
2019-05-28 17:31:21 +00:00
2019-01-29 02:33:35 +00:00
def generate(rule):
populate()
if rule in DB:
g = random.choice(DB[rule])
return g.flatten("#origin#")
def listify(col):
if type(col) == type([]):
return col
else:
return [col]
2019-05-28 17:31:21 +00:00
2019-01-29 02:33:35 +00:00
def shuffle(col):
2019-06-25 02:58:42 +00:00
a = random.choice(list(col))
b = random.choice(list(col))
2019-05-28 17:31:21 +00:00
if "origin" in [a, b]:
2019-01-29 02:33:35 +00:00
return col
2019-05-28 17:47:59 +00:00
col[a], col[b] = col[b], col[a]
2019-01-29 02:33:35 +00:00
return col
2019-05-28 17:31:21 +00:00
2019-01-29 02:33:35 +00:00
def fuse(argv):
populate()
raw = {}
for gk in argv:
if gk in DB:
g = random.choice(DB[gk]).raw
for k in g:
if k in raw:
2019-05-28 17:31:21 +00:00
raw[k] = listify(raw[k]) + listify(g[k])
2019-01-29 02:33:35 +00:00
else:
raw[k] = g[k]
for i in range(20):
raw = shuffle(raw)
return grammar(raw).flatten("#origin#")
2020-03-11 18:10:44 +00:00
def think(line):
2021-02-19 04:13:16 +00:00
chan = line.params[0]
words = line.params[1].split(" ")
2020-03-11 18:10:44 +00:00
2021-02-19 04:13:16 +00:00
if len(words) > 0:
2019-01-29 02:33:35 +00:00
if words[0] == "!!list":
res = ""
for k in DB:
2019-05-28 17:31:21 +00:00
res += k + " "
2021-02-19 04:13:16 +00:00
return res
2019-01-29 02:33:35 +00:00
elif words[0] == "!!fuse":
if "|" in words:
2019-05-28 17:31:21 +00:00
res = fuse(words[1 : words.index("|")])
2019-01-29 02:33:35 +00:00
if res:
2021-02-19 04:13:16 +00:00
return " ".join(words[words.index("|") + 1 :]) + " " + res
2019-01-29 02:33:35 +00:00
else:
res = fuse(words[1:])
if res:
2021-02-19 04:13:16 +00:00
return res
2019-05-28 17:40:51 +00:00
elif words[0] == "!!source":
2021-02-19 04:13:16 +00:00
return "https://tildegit.org/ben/tracer"
2019-05-28 17:47:59 +00:00
elif words[0] == "!botlist" or words[0] == "!!help":
2021-02-19 04:13:16 +00:00
return "helo i'm a tracery bot that makes cool things from tracery grammars in your ~/.tracery. see http://tracery.io for more info"
2019-01-29 02:33:35 +00:00
elif words[0][0:2] == "!!":
print(words)
res = generate(words[0][2:])
if res:
if len(words) >= 3:
if words[1] == "|":
2021-02-19 04:13:16 +00:00
return " ".join(words[2:]) + " " + res
2019-01-29 02:33:35 +00:00
else:
2021-02-19 04:13:16 +00:00
return res
2019-01-29 02:33:35 +00:00
else:
2021-02-19 04:13:16 +00:00
return res
class Server(BaseServer):
async def line_send(self, line: Line):
print(f"{self.name} > {line.format()}")
async def line_read(self, line: Line):
print(f"{self.name} < {line.format()}")
if line.command == "001":
await self.send(build("JOIN", [",".join(bot.getlist("channels"))]))
await self.send(build("MODE", [self.nickname, "+B"]))
if line.command == "INVITE":
print(f"received invite to {line.params[1]}")
await self.send(build("JOIN", [line.params[1]]))
if (
line.command == "PRIVMSG"
and self.has_channel(line.params[0])
and not line.hostmask is None
and not self.casefold(line.hostmask.nickname) == self.nickname_lower
and not ("batch" in line.tags and line.tags["batch"] == "1")
and not "inspircd.org/bot" in line.tags
and self.has_user(line.hostmask.nickname)
):
try:
response = think(line)
if response is not None:
await self.send(build("PRIVMSG", [line.params[0], response]))
except Exception as e:
print("ERROR", line)
print(e)
traceback.print_exc()
class Bot(BaseBot):
def create_server(self, name: str):
return Server(self, name)
async def main():
ircrobot = Bot()
params = ConnectionParams(
bot["nick"],
host=bot["server"],
port=int(bot["port"]),
tls=bool(bot["tls"]),
sasl=SASLUserPass(account["username"], account["password"]),
)
await ircrobot.add_server("tilde", params)
await ircrobot.run()
2019-01-29 02:33:35 +00:00
2019-05-28 17:31:21 +00:00
2019-01-29 02:33:35 +00:00
if __name__ == "__main__":
2021-02-19 04:13:16 +00:00
asyncio.run(main())