Compare commits

..

No commits in common. "c0075e2fbef259268c56350126e85948daf79be1" and "448aa9d4118f3c286e8a924ef5c78a71f07b0420" have entirely different histories.

3 changed files with 13 additions and 52 deletions

1
.gitignore vendored
View File

@ -1 +0,0 @@
__pycache__

40
main.py
View File

@ -3,7 +3,6 @@ from ircked.bot import irc_bot
from ircked.message import *
import traceback
import html
import ratelimiter
class bird_inst():
def __init__(self, endpoint, httpendpoint, config):
@ -16,8 +15,6 @@ class bird_inst():
self.irc.connect_register(self.config["irc_serb"], self.config["irc_port"])
self.send_queue = []
self.limiter = ratelimiter.ratelimit(.5)
def irc_handler(msg, ctx):
#print("<><><><>", str(msg))
if msg.command == "PING":
@ -28,16 +25,15 @@ class bird_inst():
message.manual(":"+msg.parameters[0], "PRIVMSG", [msg.prefix[1:].split("!")[0], ":\x01dorfl bot\x01"]).send(ctx.socket)
if msg.command == "PRIVMSG" and ("py-ctcp" not in msg.prefix):
pm = privmsg.parse(msg)
self.send_post("<"+pm.fr.split("!")[0]+"> "+pm.bod)
self.send_post(pm.fr.split("!")[0]+": "+pm.bod)
threading.Thread(target=self.irc.run, kwargs={"event_handler": irc_handler}, daemon=True).start()
def auth(self, name, passwd):
h = {
"User-Agent": "renabot",
"Origin": "https://deek.chat",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:83.0) Gecko/20100101 Firefox/83.0",
"Origin": "https://deekchat.ml",
"DNT": "1",
}
res = requests.post(self.httpendpoint+"/login/submit", headers=h, data={"name": name, "password": passwd, "submit": "log+in"}, allow_redirects=False)
print(res.headers)
token = re.search("(?:api_token)=[^;]+", res.headers.get("Set-Cookie")).group(0)
sessid = re.search("(?:session_id)=[^;]+", res.headers.get("Set-Cookie")).group(0)
h["Cookie"] = token+"; "+sessid
@ -47,31 +43,25 @@ class bird_inst():
async with websockets.connect(self.endpoint, extra_headers=self.headers) as self.ws:
print(self.ws)
asyncio.get_event_loop().create_task(self._send_post())
asyncio.get_event_loop().create_task(self.ship_queued_messages())
while True:
data = json.loads(await self.ws.recv())
print(">>>", data)
#getattr(self, "handle_"+data["type"], None)(data)
try: getattr(self, "handle_"+data["type"], None)(data)
except Exception as e: print("hey buddy your shits fucked thought you might want to know", e)
def handle_message(self, ctx):
print("btw i just got this", ctx["data"]["text"])
ctx["data"]["text"] = html.unescape(ctx["data"]["text"])
if ctx["data"]["name"] == self.config["deek_user"]: return
mesg = ctx["data"]["text"].replace("\n", " ")
print("btw i just got this", ctx["data"]["message"]["text"])
ctx["data"]["message"]["text"] = html.unescape(ctx["data"]["message"]["text"])
if ctx["data"]["message"]["name"] == self.config["deek_user"]: return
mesg = ctx["data"]["message"]["text"].replace("\n", " ")
chunks = list(mesg[0+i:400+i] for i in range(0, len(mesg), 400))
for m in chunks:
self.limiter.action(True, self.irc.sendraw, (privmsg.build(self.config["irc_nick"], self.config["irc_chan"], "<"+ctx["data"]["name"]+"> "+m).msg,))
def handle_messageStart(self, ctx): pass
def handle_messageChange(self, ctx): pass
def handle_messageEnd(self, ctx): self.handle_message(ctx)
self.irc.sendraw(privmsg.build(self.config["irc_nick"], self.config["irc_chan"], ctx["data"]["message"]["name"]+": "+m).msg)
def handle_avatar(self, ctx): pass
def handle_loadUsers(self, ctx): pass
def handle_files(self, ctx):
ctx["data"]["text"] = html.unescape(ctx["data"]["text"])
self.irc.sendraw(privmsg.build(self.config["irc_nick"], self.config["irc_chan"], "<"+ctx["data"]["name"]+"> "+ctx["data"]["text"]).msg)
ctx["data"]["message"]["text"] = html.unescape(ctx["data"]["message"]["text"])
self.irc.sendraw(privmsg.build(self.config["irc_nick"], self.config["irc_chan"], ctx["data"]["message"]["name"]+": "+ctx["data"]["message"]["text"]).msg)
for f in ctx["data"]["files"]:
self.limiter.action(True, self.irc.sendraw, (privmsg.build(self.config["irc_nick"], self.config["irc_chan"], f"({ctx['data']['name']} uploaded file: {self.httpendpoint}/storage/files/{f['name']} )").msg,))
self.irc.sendraw(privmsg.build(self.config["irc_nick"], self.config["irc_chan"], f"({ctx['name']} uploaded file: {self.httpendpoint}/storage/files/{f['name']})").msg)
def handle_exit(self, ctx): pass
def handle_enter(self, ctx): pass
def handle_userLoaded(self, ctx): pass
@ -82,16 +72,12 @@ class bird_inst():
async def _send_post(self):
while True:
for msg in self.send_queue:
await self.ws.send(json.dumps({"type": "message", "data": msg, "roomId": 1}))
await self.ws.send(json.dumps({"type": "message", "data": {"message": msg}}))
self.send_queue.remove(msg)
print("shipped", msg)
await asyncio.sleep(.1)
async def ship_queued_messages(self):
while True:
self.limiter.lazyrun()
await asyncio.sleep(.1)
cfg = json.loads(open("config.json", "r").read())
bi = bird_inst("wss://deek.chat/ws", "https://deek.chat", cfg)
bi = bird_inst("wss://deekchat.ml/ws", "https://deekchat.ml", cfg)
print("yes hello birdchat here")
bi.auth(cfg["deek_user"], cfg["deek_passwd"])
while True:

View File

@ -1,24 +0,0 @@
#naive ratelimiter
from time import time
class ratelimit():
def __init__(self, min_delay):
self.delay = min_delay
self.last_action = 0
self.action_queue = []
def action(self, manual, target, args):
print("action~", target, args)
if time() - self.last_action > self.delay:
self.last_action = time()
target(*args)
try: self.action_queue.remove((target, args))
except: print("did not remove", target, args)
else:
print("deferred")
if manual:
self.action_queue.append((target, args))
def lazyrun(self):
if len(self.action_queue) <= 0: return
f = self.action_queue[0][0]
args = self.action_queue[0][1]
self.action(False, f, args)