aboutsummaryrefslogtreecommitdiffstats
path: root/alfred/twt.py
blob: 0953d3c928842539a9350ee062687bcd0b0530f9 (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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
"""
Sopel plugin: twtxt feed

Commands (owner only):
  !twt                       - show usage
  !twt last [n]              - show last n twts, most recent first (default 5)
  !twt count                 - total number of twts
  !twt delete                - delete the most recent twt (asks yes/no)
  !twt yes / !twt no         - confirm or cancel a pending delete
  !twt <message>             - post a twt

Config ([twt] section in sopel config):
  twtxt_path  - path to twtxt.txt (default /var/www/twt.gumx.cc/twtxt.txt)
  timezone    - timezone name for timestamps (default UTC)
  nick        - nick value written in file header (default gumx)
  url         - url value written in file header
"""

import os
from datetime import datetime
from zoneinfo import ZoneInfo

from sopel import plugin
from sopel.config.types import FilenameAttribute, StaticSection, ValidatedAttribute

import os as _os, sys as _sys
_alfred_dir = _os.path.dirname(_os.path.abspath(__file__))
if _alfred_dir not in _sys.path:
    _sys.path.insert(0, _alfred_dir)
import helpstrings as h

_pending_delete = {}

_SUBCOMMANDS = {"last", "count", "delete", "yes", "no"}


class TwtSection(StaticSection):
    twtxt_path = FilenameAttribute("twtxt_path", relative=False,
                                   default="/var/www/twt.gumx.cc/twtxt.txt")
    timezone   = ValidatedAttribute("timezone", default="UTC")
    nick       = ValidatedAttribute("nick", default="gumx")
    url        = ValidatedAttribute("url", default="https://twt.gumx.cc/twtxt.txt")


def setup(bot):
    bot.settings.define_section("twt", TwtSection)


def _is_owner(bot, trigger):
    return trigger.nick == bot.settings.core.owner


def _path(bot):
    return bot.settings.twt.twtxt_path


def _read(bot):
    p = _path(bot)
    if not os.path.exists(p):
        return []
    with open(p) as f:
        lines = f.readlines()
    twts = []
    for line in lines:
        line = line.rstrip("\n")
        if not line or line.startswith("#"):
            continue
        tab = line.find("\t")
        if tab == -1:
            continue
        twts.append((line[:tab], line[tab + 1:]))
    return twts


def _write(bot, twts):
    p = _path(bot)
    nick = bot.settings.twt.nick
    url  = bot.settings.twt.url
    with open(p, "w") as f:
        f.write(f"# nick = {nick}\n")
        f.write(f"# url  = {url}\n")
        for ts, text in twts:
            f.write(f"{ts}\t{text}\n")


def _append(bot, text):
    tz = ZoneInfo(bot.settings.twt.timezone)
    now = datetime.now(tz)
    offset = now.strftime("%z")
    offset = offset[:3] + ":" + offset[3:]
    ts = now.strftime("%Y-%m-%dT%H:%M:%S") + offset

    p = _path(bot)
    nick = bot.settings.twt.nick
    url  = bot.settings.twt.url
    if not os.path.exists(p):
        os.makedirs(os.path.dirname(p), exist_ok=True)
        with open(p, "w") as f:
            f.write(f"# nick = {nick}\n")
            f.write(f"# url  = {url}\n")
    with open(p, "a") as f:
        f.write(f"{ts}\t{text}\n")
    return ts


@plugin.commands("twt")
def cmd_twt(bot, trigger):
    if not _is_owner(bot, trigger):
        return

    raw  = (trigger.group(2) or "").strip()
    args = raw.split()
    sub  = args[0].lower() if args else ""

    if not sub:
        bot.say(h.TWF_TOPIC)
        return

    # --- last ---
    if sub == "last":
        try:
            n = int(args[1]) if len(args) > 1 else 5
            n = max(1, min(n, 10))
        except ValueError:
            bot.say("usage: !twt last [n]")
            return
        twts = _read(bot)
        if not twts:
            bot.say("no twts yet.")
            return
        for ts, text in reversed(twts[-n:]):
            bot.say(f"[{ts[:10]}] {text}")
        return

    # --- count ---
    if sub == "count":
        n = len(_read(bot))
        bot.say(f"{n} twt{'s' if n != 1 else ''}")
        return

    # --- delete ---
    if sub == "delete":
        twts = _read(bot)
        if not twts:
            bot.say("nothing to delete.")
            return
        ts, text = twts[-1]
        _pending_delete[trigger.nick] = True
        bot.say(f"delete [{ts[:10]}] {text!r}? !twt yes / !twt no")
        return

    # --- yes/no (confirm delete) ---
    if sub == "yes":
        if not _pending_delete.pop(trigger.nick, False):
            bot.say("nothing pending.")
            return
        twts = _read(bot)
        if not twts:
            bot.say("nothing to delete.")
            return
        removed = twts.pop()
        _write(bot, twts)
        bot.say(f"deleted [{removed[0][:10]}] {removed[1]!r}")
        return

    if sub == "no":
        if _pending_delete.pop(trigger.nick, False):
            bot.say("cancelled.")
        return

    # --- post (anything that's not a subcommand) ---
    ts = _append(bot, raw)
    bot.say(f"twted [{ts[:10]}]: {raw}")