small refactor and nickserv account stuff

This commit is contained in:
Ben Harris 2020-02-24 11:14:49 -05:00
parent bb80546e45
commit f7c8740729
5 changed files with 47 additions and 39 deletions

2
.gitignore vendored
View File

@ -61,3 +61,5 @@ target/
tildeteam.json tildeteam.json
tildeverse.json tildeverse.json
config.json config.json
venv/
account.json

5
account.json.sample Normal file
View File

@ -0,0 +1,5 @@
{
"username": "bensbots",
"password": "my super secret password"
}

2
requirements.txt Normal file
View File

@ -0,0 +1,2 @@
Mastodon.py==1.5.0
emoji==0.5.4

75
tooter.py Executable file → Normal file
View File

@ -1,44 +1,22 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
from mastodon import Mastodon
import emoji import emoji
import json import json
import os import os
import re import re
import socket import socket
import sys import sys
from mastodon import Mastodon
path = os.path.dirname(os.path.abspath(__file__))
with open(os.path.join(path, "config.json")) as f: def masto_from_json(conf):
config = json.load(f) conf = json.load(conf)
return Mastodon(
botnick = config["botnick"] client_id=conf["client_id"],
channels = config["channels"] client_secret=conf["client_secret"],
if len(sys.argv) > 1: access_token=conf["access_token"],
for c in sys.argv[1:]: api_base_url=conf["base_url"]
channels.append("#" + c) )
# read masto creds
with open(os.path.join(path, "tildeverse.json")) as f:
tildeverse_config = json.load(f)
tildeverse = Mastodon(
client_id=tildeverse_config["client_id"],
client_secret=tildeverse_config["client_secret"],
access_token=tildeverse_config["access_token"],
api_base_url=tildeverse_config["base_url"],
)
with open(os.path.join(path, "tildeteam.json")) as f:
tildeteam_config = json.load(f)
tildeteam = Mastodon(
client_id=tildeteam_config["client_id"],
client_secret=tildeteam_config["client_secret"],
access_token=tildeteam_config["access_token"],
api_base_url=tildeteam_config["base_url"],
)
def rawsend(msg): def rawsend(msg):
@ -52,7 +30,7 @@ def send(chan, msg):
def eventloop(chan, nick, msg): def eventloop(chan, nick, msg):
words = re.split("[ \t\s]+", msg) words = re.split("[ \t\s]+", msg)
if len(words) > 0 and nick != "tooter": if len(words) > 0 and nick != botnick:
if words[0] == "!toot": if words[0] == "!toot":
status = emoji.emojize(" ".join(words[1:]), use_aliases=True) status = emoji.emojize(" ".join(words[1:]), use_aliases=True)
print(f"{status} posted by {nick}") print(f"{status} posted by {nick}")
@ -65,12 +43,32 @@ def eventloop(chan, nick, msg):
elif words[0] == "!source": elif words[0] == "!source":
send(chan, "https://tildegit.org/ben/tooter") send(chan, "https://tildegit.org/ben/tooter")
elif words[0] == "!botlist" or words[0] == "!toothelp": elif words[0] == "!botlist" or words[0] == "!toothelp":
send( send(chan, "helo i can send toots from irc: @tildeverse@tilde.zone - from @tildeteam from the #team channel)")
chan,
"helo i can send toots from irc: https://tilde.zone/~tildeverse - (https://tilde.zone/~tildeteam from the #team channel)",
)
# do setup
path = os.path.dirname(os.path.abspath(__file__))
with open(os.path.join(path, "config.json"), "r") as f:
config = json.load(f)
if os.path.isfile(os.path.join(path, "account.json")):
with open(os.path.join(path, "account.json"), "r") as f:
account = json.load(f)
botnick = config["botnick"]
channels = config["channels"]
if len(sys.argv) > 1:
for c in sys.argv[1:]:
channels.append("#" + c)
# read masto creds
with open(os.path.join(path, "tildeverse.json"), "r") as f:
tildeverse = masto_from_json(f)
with open(os.path.join(path, "tildeteam.json"), "r") as f:
tildeteam = masto_from_json(f)
if __name__ == "__main__": if __name__ == "__main__":
ircsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) ircsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ircsock.connect((config["address"], config["port"])) ircsock.connect((config["address"], config["port"]))
@ -78,8 +76,7 @@ if __name__ == "__main__":
rawsend(f"USER {botnick} 0 * :mastodon tooter") rawsend(f"USER {botnick} 0 * :mastodon tooter")
while 1: while 1:
ircmsg = ircsock.recv(2048).decode().split("\r\n") for msg in ircsock.recv(2048).decode().split("\r\n"):
for msg in ircmsg:
print(msg) print(msg)
if "PING" in msg: if "PING" in msg:
@ -93,6 +90,8 @@ if __name__ == "__main__":
for c in channels: for c in channels:
rawsend(f"JOIN {c}") rawsend(f"JOIN {c}")
rawsend(f"MODE {botnick} +B") rawsend(f"MODE {botnick} +B")
if account is not None:
rawsend("SQUERY NickServ IDENTIFY %s %s" % (account["username"], account["password"]))
m = re.match(":(?P<nick>[^ ]+)!.*PRIVMSG #(?P<chan>\w+) :(?P<msg>.*)", msg) m = re.match(":(?P<nick>[^ ]+)!.*PRIVMSG #(?P<chan>\w+) :(?P<msg>.*)", msg)
if m and m.groupdict(): if m and m.groupdict():

View File

@ -5,7 +5,7 @@ After=tooter.service
[Service] [Service]
Type=simple Type=simple
WorkingDirectory=/home/ben/workspace/tooter WorkingDirectory=/home/ben/workspace/tooter
ExecStart=/home/ben/workspace/tooter/tooter.py ExecStart=/home/ben/workspace/tooter/venv/bin/python3 tooter.py
Restart=always Restart=always
RestartSec=5 RestartSec=5
StartLimitInterval=60s StartLimitInterval=60s