use irctokens library
This commit is contained in:
parent
82313c091e
commit
c4a963cc17
|
@ -1 +1,2 @@
|
|||
tracery==0.1.1
|
||||
irctokens==0.4.3
|
||||
|
|
78
tracer.py
78
tracer.py
|
@ -4,6 +4,7 @@ from random import randint, choice
|
|||
from tracery.modifiers import base_english
|
||||
import configparser
|
||||
import glob
|
||||
import irctokens
|
||||
import json
|
||||
import os
|
||||
import random
|
||||
|
@ -102,17 +103,22 @@ def fuse(argv):
|
|||
return grammar(raw).flatten("#origin#")
|
||||
|
||||
|
||||
def rawsend(msg):
|
||||
print(msg)
|
||||
ircsock.send(f"{msg}\r\n".encode())
|
||||
def _send(line):
|
||||
print(f"> {line.format()}")
|
||||
e.push(line)
|
||||
while e.pending():
|
||||
e.pop(s.send(e.pending()))
|
||||
|
||||
|
||||
def send(chan, msg):
|
||||
rawsend(f"PRIVMSG #{chan} :{msg}")
|
||||
_send(irctokens.format("PRIVMSG", [chan, msg]))
|
||||
|
||||
|
||||
def think(chan, nick, msg):
|
||||
words = re.split("[ \t\s]+", msg)
|
||||
def think(line):
|
||||
chan = line.params.pop(0)
|
||||
words = line.params[0].split(" ")
|
||||
nick = line.source.split("!", 1)[0]
|
||||
|
||||
if len(words) > 0 and nick != bot["nick"]:
|
||||
if words[0] == "!!list":
|
||||
res = ""
|
||||
|
@ -149,38 +155,50 @@ def think(chan, nick, msg):
|
|||
|
||||
|
||||
if __name__ == "__main__":
|
||||
ircsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
ircsock.connect((bot["server"], int(bot["port"])))
|
||||
rawsend("NICK %s" % bot["nick"])
|
||||
rawsend("USER %s 0 * :tracery bot" % bot["nick"])
|
||||
d = irctokens.StatefulDecoder()
|
||||
e = irctokens.StatefulEncoder()
|
||||
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
s.connect((bot["server"], int(bot["port"])))
|
||||
|
||||
while 1:
|
||||
for msg in ircsock.recv(2048).decode().split("\r\n"):
|
||||
print(msg)
|
||||
_send(irctokens.format("USER", [bot["nick"], "0", "*", "tracery bot"]))
|
||||
_send(irctokens.format("NICK", [bot["nick"]]))
|
||||
|
||||
if "PING" in msg:
|
||||
rawsend(msg.replace("I", "O", 1))
|
||||
while True:
|
||||
lines = d.push(s.recv(1024))
|
||||
|
||||
if "INVITE" in msg:
|
||||
chan = msg.split(":")[-1]
|
||||
rawsend(f"JOIN {chan}")
|
||||
if lines == None:
|
||||
print("! disconnected")
|
||||
break
|
||||
|
||||
if "001" in msg:
|
||||
for line in lines:
|
||||
print(f"< {line.format()}")
|
||||
|
||||
if line.command == "PING":
|
||||
_send(irctokens.format("PONG", [line.params[0]]))
|
||||
|
||||
elif line.command == "001":
|
||||
_send(irctokens.format("MODE", [bot["nick"], "+B"]))
|
||||
if account is not None:
|
||||
rawsend(
|
||||
"SQUERY NickServ IDENTIFY %s %s"
|
||||
% (account["username"], account["password"])
|
||||
_send(
|
||||
irctokens.format(
|
||||
"SQUERY",
|
||||
[
|
||||
"NickServ",
|
||||
"IDENTIFY",
|
||||
account["username"],
|
||||
account["password"],
|
||||
],
|
||||
)
|
||||
)
|
||||
for c in bot.getlist("channels"):
|
||||
rawsend(f"JOIN {c}")
|
||||
rawsend("MODE %s +B" % bot["nick"])
|
||||
_send(irctokens.format("JOIN", bot.getlist("channels")))
|
||||
|
||||
m = re.match(":(?P<nick>[^ ]+)!.*PRIVMSG #(?P<chan>\w+) :(?P<msg>.*)", msg)
|
||||
if m and m.groupdict():
|
||||
m = m.groupdict()
|
||||
elif line.command == "INVITE":
|
||||
_send(irctokens.format("JOIN", [line.params[0]]))
|
||||
|
||||
elif line.command == "PRIVMSG":
|
||||
try:
|
||||
think(m["chan"], m["nick"], m["msg"])
|
||||
think(line)
|
||||
except Exception as e:
|
||||
print("ERROR" + str(m))
|
||||
print("ERROR", line)
|
||||
print(e)
|
||||
traceback.print_exc()
|
||||
|
|
Loading…
Reference in New Issue