support both repl and bot
This commit is contained in:
parent
589fbebf3e
commit
7e624d550b
81
uxnrepl.py
81
uxnrepl.py
|
@ -1,11 +1,18 @@
|
|||
#!/usr/bin/python
|
||||
|
||||
from os import system
|
||||
import re
|
||||
from socket import socket, AF_INET, SOCK_STREAM
|
||||
from subprocess import run, TimeoutExpired
|
||||
from sys import stdin, stdout
|
||||
from tempfile import mkstemp
|
||||
from sys import argv, stdin, stdout
|
||||
from tempfile import mkdtemp, mkstemp
|
||||
|
||||
server = "irc.libera.chat"
|
||||
nick = b"uxnbot7"
|
||||
channel = b"#uxnbot"
|
||||
|
||||
sandbox = None
|
||||
irc = None
|
||||
|
||||
template = '''
|
||||
|0100
|
||||
|
@ -64,7 +71,7 @@ def execute(s, sandbox=None, timeout=2.0):
|
|||
return b'uxncli: timed out'
|
||||
return res.stdout
|
||||
|
||||
def main():
|
||||
def repl():
|
||||
print('uxnrepl (ctrl-d to exit)')
|
||||
while True:
|
||||
stdout.write('> ')
|
||||
|
@ -76,5 +83,73 @@ def main():
|
|||
stdout.write(execute(s).decode('utf-8'))
|
||||
stdout.flush()
|
||||
|
||||
def send(msg):
|
||||
print('>>> %r' % msg)
|
||||
irc.send(msg + b'\r\n')
|
||||
|
||||
def recv():
|
||||
msg = irc.recv(2040)
|
||||
print('<<< %r' % msg)
|
||||
return msg
|
||||
|
||||
def evaluate(msg):
|
||||
output = execute(msg.decode('utf-8'), sandbox=sandbox)
|
||||
lines = [s.strip() for s in output.split(b'\n')]
|
||||
interesting = [s for s in lines if s and s != 'wst' and s != 'rst']
|
||||
result = b' | '.join(interesting)
|
||||
print('*** executing %r gave %r -> %r -> %r' % (msg, output, interesting, result))
|
||||
return result
|
||||
|
||||
def ircbot():
|
||||
global sandbox, irc
|
||||
sandbox = mkdtemp(prefix='uxnrepl')
|
||||
irc = socket(AF_INET, SOCK_STREAM)
|
||||
|
||||
irc.connect((server, 6667))
|
||||
send(b"USER %s %s %s :bot for testing uxntal code" % (nick, nick, nick))
|
||||
send(b"NICK %s" % nick)
|
||||
#send("PRIVMSG nickserv :iNOOPE\r\n")
|
||||
send(b"JOIN %s" % channel)
|
||||
|
||||
ping_re = re.compile(br'PING (.+)$')
|
||||
chan_msg_re = re.compile(br':([^!]+)![^ ]+ PRIVMSG ([^ ]+) :' + nick + br': (.*)$')
|
||||
priv_msg_re = re.compile(br':([^!]+)![^ ]+ PRIVMSG ' + nick + br' :(.*)$')
|
||||
|
||||
while True:
|
||||
text = recv()
|
||||
|
||||
m = ping_re.match(text)
|
||||
if m:
|
||||
send(b'PONG %s' % m.group(1).strip())
|
||||
continue
|
||||
|
||||
m = chan_msg_re.match(text)
|
||||
if m and m.group(2) == channel:
|
||||
user = m.group(1)
|
||||
msg = m.group(3).strip()
|
||||
result = evaluate(msg)
|
||||
send(b'PRIVMSG %s :%s: %s' % (channel, user, result))
|
||||
continue
|
||||
|
||||
m = priv_msg_re.match(text)
|
||||
if m:
|
||||
user = m.group(1)
|
||||
msg = m.group(2).strip()
|
||||
result = evaluate(msg)
|
||||
send(b'PRIVMSG %s :%s' % (user, result))
|
||||
continue
|
||||
|
||||
def main():
|
||||
if argv[1:] == [] or argv[1:] == ["repl"]:
|
||||
repl()
|
||||
elif argv[1] == "bot" and len(argv) == 5:
|
||||
# TODO: plug in server, nick, channel
|
||||
server, nick, channel = argv[2:]
|
||||
ircbot()
|
||||
else:
|
||||
print("usage: %s [repl]" % argv[0])
|
||||
print(" %s bot <server> <nick> <channel>" % argv[0])
|
||||
exit(1)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
|
Loading…
Reference in New Issue