about summary refs log tree commit diff
path: root/tooter.py
blob: 378cac5dd9a4a3e164ce08f59e29454d2c591566 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/usr/bin/env python3

import emoji
import json
import os
import re
import socket
import sys
from mastodon import Mastodon

path = os.path.dirname(os.path.abspath(__file__))

with open(os.path.join(path, "config.json")) as f:
    config = 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")) 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):
    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("".join(words[1:]), use_aliases=True)
            print(f"{status} posted by {nick}")
            if chan == "#team":
                res = tildeteam.toot(status)
            else:
                res = tildeverse.toot(status)
            print(res)
            send(chan, "tooted! {}".format(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((config["address"], config["port"]))
    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<nick>[^ ]+)!.*PRIVMSG #(?P<chan>\w+) :(?P<msg>.*)", msg)
            if m and m.groupdict():
                m = m.groupdict()
                eventloop(m["chan"], m["nick"], m["msg"])