""" Sopel plugin: mlmmj mailing list management !list subscribe - subscribe to list@gumx.cc !list unsubscribe - unsubscribe !list members - show all subscribers !list send - send a message to the list as owner !list mod - show recent archive messages !list mod rm - remove message N from archive """ import email.utils, glob, os, subprocess, tempfile import mailbox from email.header import decode_header from sopel import plugin LIST_DIR = "/var/spool/mlmmj/list" LIST_ADDR = "list@gumx.cc" OWNER_ADDR = "ahmed@gumx.cc" 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, 2) if not args: bot.say("usage: !list ") return action = args[0] if action == "subscribe": if len(args) < 2: bot.say("usage: !list subscribe ") return email_addr = args[1].strip() r = subprocess.run( ["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 ") return email_addr = args[1].strip() r = subprocess.run( ["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 ") 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( ["mlmmj-send", "-L", LIST_DIR, "-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": sub = args[1].strip() if len(args) > 1 else "" archive_dir = os.path.join(LIST_DIR, "archive") def _decode(s): if not s: return '' parts = decode_header(s) out = [] for part, charset in parts: if isinstance(part, bytes): out.append(part.decode(charset or 'utf-8', errors='replace')) else: out.append(part) return ''.join(out) if not sub or sub == "list": items = sorted( f for f in os.listdir(archive_dir) if os.path.isfile(os.path.join(archive_dir, f)) and not f.endswith('.html') ) if not items: bot.say("archive: empty") return bot.say(f"archive: {len(items)} message(s)") for fname in items[-10:]: fpath = os.path.join(archive_dir, fname) with open(fpath, 'rb') as f: msg = mailbox.Message(f) subject = _decode(msg.get('Subject', '(no subject)')) sender = _decode(msg.get('From', '')) name = sender.split('<')[0].strip().strip('"') or sender bot.say(f" {fname} {subject[:40]} <{name}>") elif sub == "rm": if len(args) < 3: bot.say("usage: !list mod rm ") return fname = args[2].strip() fpath = os.path.join(archive_dir, fname) if not os.path.isfile(fpath): bot.say(f"mod rm: not found: {fname}") return os.remove(fpath) html_path = fpath + '.html' if os.path.isfile(html_path): os.remove(html_path) subprocess.Popen( ["python3", "/home/ahmed/scripts/archive-gen.py", LIST_DIR], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL ) bot.say(f"removed: {fname}") else: bot.say("usage: !list mod [list|rm ]") else: bot.say("usage: !list ")