tracer/tracer.py

184 lines
4.7 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 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
2021-02-19 04:31:58 +00:00
HELPTEXT = "helo i'm a tracery bot that makes cool things from tracery grammars in your ~/.tracery. see http://tracery.io for more info"
REPOLINK = "https://tildegit.org/ben/tracer"
2019-01-29 02:33:35 +00:00
DB = {}
2021-02-24 20:06:13 +00:00
# read configuration
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")
2021-02-24 20:06:13 +00:00
config = config["irc"]
2019-01-29 02:33:35 +00:00
2021-02-24 20:06:13 +00:00
account = configparser.ConfigParser()
account.read("account.ini")
account = account["nickserv"]
2020-02-24 16:32:56 +00:00
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:31:58 +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)
2021-02-24 20:06:13 +00:00
if name.startswith(".") or ext not in [".json", ""]:
2020-02-24 15:47:57 +00:00
continue
if p in DB:
2021-02-19 04:40:56 +00:00
DB[name].append(p)
2020-02-24 15:47:57 +00:00
else:
2021-02-19 04:40:56 +00:00
DB[name] = [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:
2021-02-24 20:14:06 +00:00
return grammar(load_rules(random.choice(DB[rule]))).flatten("#origin#")
2019-01-29 02:33:35 +00:00
def listify(col):
2021-02-24 20:06:13 +00:00
return col if type(col) == type([]) else [col]
2019-01-29 02:33:35 +00:00
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:
2021-02-19 04:40:56 +00:00
g = grammar(load_rules(random.choice(DB[gk]))).raw
2019-01-29 02:33:35 +00:00
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
words = line.params[1].split(" ")
2021-02-24 20:14:06 +00:00
if words[0] == "!!list":
return " ".join(DB)
elif words[0] == "!!source":
return REPOLINK
elif words[0] in ["!!help", "!botlist"]:
return HELPTEXT
elif words[0] == "!!fuse":
if "|" in words:
w = words.index("|")
res = fuse(words[1:w])
if res:
return " ".join(words[w + 1 :]) + " " + res
else:
res = fuse(words[1:])
if res:
return res
elif words[0].startswith("!!"):
res = generate(words[0][2:])
if res:
2019-01-29 02:33:35 +00:00
if "|" in words:
2021-02-24 20:06:13 +00:00
w = words.index("|")
2021-02-24 20:14:06 +00:00
return " ".join(words[w + 1 :]) + " " + res
2019-01-29 02:33:35 +00:00
else:
2021-02-24 20:14:06 +00:00
return res
2021-02-19 04:13:16 +00:00
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":
2021-02-24 20:06:13 +00:00
await self.send(build("JOIN", [",".join(config.getlist("channels"))]))
2021-02-19 04:13:16 +00:00
await self.send(build("MODE", [self.nickname, "+B"]))
if line.command == "INVITE":
await self.send(build("JOIN", [line.params[1]]))
if (
line.command == "PRIVMSG"
and self.has_channel(line.params[0])
2021-02-19 14:58:50 +00:00
and self.has_user(line.hostmask.nickname)
2021-02-24 20:14:06 +00:00
and len(line.params[1].split(" ")) > 0
2021-02-19 04:13:16 +00:00
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
):
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():
params = ConnectionParams(
2021-02-24 20:06:13 +00:00
config["nick"],
host=config["server"],
port=config.getint("port"),
tls=config.getboolean("tls"),
2021-02-19 04:13:16 +00:00
sasl=SASLUserPass(account["username"], account["password"]),
)
2021-02-24 20:14:06 +00:00
bot = Bot()
2021-02-24 20:06:13 +00:00
await bot.add_server("tilde", params)
await bot.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())