aboutsummaryrefslogtreecommitdiffstats
path: root/alfred/mlmmj.py
blob: b691a7ca2a615676403de555604c290a4f9e6c19 (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
"""
Sopel plugin: mlmmj mailing list management

  !list subscribe <email>    - subscribe (forced, no confirmation email)
  !list unsubscribe <email>  - unsubscribe
  !list members              - show all subscribers
  !list send <message>       - send a message to the list as owner (bypasses moderation)
  !list mod                  - show pending moderation queue
  !list approve <id>         - approve a held post
  !list discard <id>         - discard a held post
"""

import email.utils, glob, mailbox, os, subprocess, tempfile
from sopel import plugin

LIST_DIR   = "/var/spool/mlmmj/list"
LIST_ADDR  = "list@gumx.cc"
OWNER_ADDR = "ahmed@gumx.cc"
MOD_DIR    = os.path.join(LIST_DIR, "moderation")


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


@plugin.command("list")
def cmd_list(bot, trigger):
    if not _owner(bot, trigger):
        return

    args = (trigger.group(2) or "").split(None, 1)
    if not args:
        bot.say("usage: !list <subscribe|unsubscribe|members|send|mod|approve|discard>")
        return

    action = args[0]

    if action == "subscribe":
        if len(args) < 2:
            bot.say("usage: !list subscribe <email>")
            return
        email_addr = args[1].strip()
        r = subprocess.run(
            ["/usr/bin/mlmmj-sub", "-L", LIST_DIR, "-a", email_addr, "-s", "-f"],
            capture_output=True, text=True
        )
        bot.say(f"subscribed: {email_addr}" if r.returncode == 0
                else f"error: {(r.stderr or r.stdout).strip()}")

    elif action == "unsubscribe":
        if len(args) < 2:
            bot.say("usage: !list unsubscribe <email>")
            return
        email_addr = args[1].strip()
        r = subprocess.run(
            ["/usr/bin/mlmmj-unsub", "-L", LIST_DIR, "-a", email_addr, "-s", "-f"],
            capture_output=True, text=True
        )
        bot.say(f"unsubscribed: {email_addr}" if r.returncode == 0
                else f"error: {(r.stderr or r.stdout).strip()}")

    elif action == "members":
        subs_dir = os.path.join(LIST_DIR, "subscribers.d")
        subs = []
        if os.path.isdir(subs_dir):
            for fname in sorted(glob.glob(os.path.join(subs_dir, "*"))):
                with open(fname) as f:
                    subs.extend(line.strip() for line in f if line.strip())
        if subs:
            bot.say(f"{len(subs)} subscriber(s): " + ", ".join(subs))
        else:
            bot.say("no subscribers")

    elif action == "send":
        if len(args) < 2:
            bot.say("usage: !list send <message>")
            return
        message = args[1].strip()
        msg = (
            f"From: {OWNER_ADDR}\n"
            f"To: {LIST_ADDR}\n"
            f"Subject: {message}\n"
            f"Date: {email.utils.formatdate(localtime=True)}\n"
            f"\n"
            f"{message}\n"
        )
        with tempfile.NamedTemporaryFile(mode="w", suffix=".eml", delete=False) as f:
            f.write(msg)
            tmpname = f.name
        try:
            r = subprocess.run(
                ["/usr/bin/mlmmj-send", "-L", LIST_DIR, "-l", "1", "-m", tmpname],
                capture_output=True, text=True
            )
            bot.say("sent" if r.returncode == 0
                    else f"error: {(r.stderr or r.stdout).strip()}")
        finally:
            os.unlink(tmpname)

    elif action == "mod":
        files = sorted(os.listdir(MOD_DIR)) if os.path.isdir(MOD_DIR) else []
        if not files:
            bot.say("moderation queue empty")
            return
        for fname in files[:5]:
            fpath = os.path.join(MOD_DIR, fname)
            try:
                with open(fpath, "rb") as f:
                    msg = mailbox.Message(f)
                subj = (msg.get("Subject", "(no subject)") or "")[:50]
                frm  = (msg.get("From", "") or "")[:30]
            except Exception:
                subj, frm = "(unreadable)", ""
            bot.say(f"{fname}  {subj}{frm}")
        if len(files) > 5:
            bot.say(f"... and {len(files) - 5} more")

    elif action == "approve":
        args2 = (trigger.group(2) or "").split(None, 2)
        if len(args2) < 2:
            bot.say("usage: !list approve <id>")
            return
        fid  = args2[1].strip()
        path = os.path.join(MOD_DIR, fid)
        if not os.path.isfile(path):
            bot.say(f"not found: {fid}")
            return
        r = subprocess.run(
            ["/usr/bin/mlmmj-send", "-L", LIST_DIR, "-l", "1", "-m", path],
            capture_output=True, text=True
        )
        if r.returncode == 0:
            os.unlink(path)
            bot.say(f"approved: {fid}")
        else:
            bot.say(f"error: {(r.stderr or r.stdout).strip()}")

    elif action == "discard":
        args2 = (trigger.group(2) or "").split(None, 2)
        if len(args2) < 2:
            bot.say("usage: !list discard <id>")
            return
        fid  = args2[1].strip()
        path = os.path.join(MOD_DIR, fid)
        if not os.path.isfile(path):
            bot.say(f"not found: {fid}")
            return
        os.unlink(path)
        bot.say(f"discarded: {fid}")

    else:
        bot.say("usage: !list <subscribe|unsubscribe|members|send|mod|approve|discard>")