From eda932ec06c1b4b275569b4e7dadb9384a3859eb Mon Sep 17 00:00:00 2001 From: Ben Harris Date: Wed, 29 May 2019 21:03:12 -0400 Subject: init --- .gitignore | 60 ++++++++++++++++++++++++++++++++++ LICENSE | 8 +++++ README.md | 13 ++++++++ tooter.py | 101 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ tooter.service | 16 +++++++++ 5 files changed, 198 insertions(+) create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 README.md create mode 100755 tooter.py create mode 100644 tooter.service diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7f7cccc --- /dev/null +++ b/.gitignore @@ -0,0 +1,60 @@ +# ---> Python +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +env/ +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +*.egg-info/ +.installed.cfg +*.egg + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*,cover + +# Translations +*.mo +*.pot + +# Django stuff: +*.log + +# Sphinx documentation +docs/_build/ + +# PyBuilder +target/ + diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..472ac23 --- /dev/null +++ b/LICENSE @@ -0,0 +1,8 @@ +MIT License +Copyright (c) + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..a542ebf --- /dev/null +++ b/README.md @@ -0,0 +1,13 @@ +# tooter + +irc tooter bot + +deps: +* [Mastodon.py](https://github.com/halcy/Mastodon.py) + +edit channels and botnick to test somewhere in irc + +## install systemd user unit +1. edit paths to WorkingDirectory and ExecStart +1. `cp tooter.service ~/.config/systemd/user/` +1. `systemctl --user enable --now tooter` diff --git a/tooter.py b/tooter.py new file mode 100755 index 0000000..66b1afc --- /dev/null +++ b/tooter.py @@ -0,0 +1,101 @@ +#!/usr/bin/env python3 + +import emoji +import json +import os +import re +import socket +import sys +from mastodon import Mastodon + +server = ("127.0.0.1", 6667) +channels = ["#bots", "#meta", "#team"] +if len(sys.argv) > 1: + for c in sys.argv[1:]: + channels.append("#" + c) +botnick = "tooter" + +# read masto creds +path = os.path.dirname(os.path.abspath(__file__)) +tildeverse_file = os.path.join(path, "tildeverse.json") +with open(tildeverse_file) 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"], +) + +tildeteam_file = os.path.join(path, "tildeteam.json") +with open(tildeteam_file) 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): + print(msg) + ircsock.send(f"{msg}\r\n".encode()) + + +def send(chan, msg): + rawsend(f"PRIVMSG #{chan} :{msg}") + + +def eventloop(chan, nick, msg): + words = re.split("[ \t\s]+", msg) + if len(words) > 0 and nick != "tooter": + if words[0] == "!toot": + status = emoji.emojize(words[2:], use_aliases=True) + if chan == "#team": + res = tildeteam.toot(status) + else: + res = tildeverse.toot(status) + send(chan, f"tooted! {res.url}") + elif words[0] == "!source": + send(chan, "https://tildegit.org/ben/tooter") + elif words[0] == "!botlist" or words[0] == "!toothelp": + send( + chan, + "helo i can send toots from irc: https://tilde.zone/~tildeverse - (https://tilde.zone/~tildeteam from the #team channel)", + ) + + +if __name__ == "__main__": + ircsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + ircsock.connect(server) + rawsend(f"NICK {botnick}") + rawsend(f"USER {botnick} 0 * :mastodon tooter") + + while 1: + ircmsg = ircsock.recv(2048).decode().split("\r\n") + for msg in ircmsg: + print(msg) + + if "PING" in msg: + rawsend(msg.replace("I", "O", 1)) + + if "INVITE" in msg: + chan = msg.split(":")[-1] + rawsend(f"JOIN {chan}") + + if "001" in msg: + for c in channels: + rawsend(f"JOIN {c}") + rawsend(f"MODE {botnick} +B") + + m = re.match(":(?P[^ ]+)!.*PRIVMSG #(?P\w+) :(?P.*)", msg) + if m and m.groupdict(): + m = m.groupdict() + try: + eventloop(m["chan"], m["nick"], m["msg"]) + except Exception as e: + print("ERROR" + str(m)) + print(e) diff --git a/tooter.service b/tooter.service new file mode 100644 index 0000000..6d208d6 --- /dev/null +++ b/tooter.service @@ -0,0 +1,16 @@ +[Unit] +Description=tooter +After=tooter.service + +[Service] +Type=simple +WorkingDirectory=/home/ben/workspace/tooter +ExecStart=/home/ben/workspace/tracer/tooter.py +Restart=always +RestartSec=5 +StartLimitInterval=60s +StartLimitBurst=3 + +[Install] +WantedBy=default.target + -- cgit 1.4.1