aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Bremner <david@tethera.net>2021-07-31 15:48:25 -0400
committerDavid Bremner <david@tethera.net>2021-07-31 15:53:22 -0400
commite416fe6ae6a6b3cf71acd79f7f3575351d39dcf8 (patch)
tree180535b23898b14c7bc2724d8acf10fd4f7e5915
parent7a46a51e47c2b37aae32f4bea06a8cedaa179850 (diff)
implement command loop and discard command
-rw-r--r--mm3mod/__init__.py88
1 files changed, 85 insertions, 3 deletions
diff --git a/mm3mod/__init__.py b/mm3mod/__init__.py
index 44e6744..83577ae 100644
--- a/mm3mod/__init__.py
+++ b/mm3mod/__init__.py
@@ -1,15 +1,97 @@
+import os
+import cmd
import configparser
-from xdg import xdg_config_home
from pathlib import PurePath
+from mailmanclient.client import Client
+
+class ModShell(cmd.Cmd):
+ intro = 'Type help or ? for a list of commands'
+
+ def __init__(self, client, mlist):
+ self.client = client;
+ self.mlist = mlist;
+ self.prompt = '({:s}) '.format(mlist.list_name)
+ cmd.Cmd.__init__(self)
+
+ def emptyline(self):
+ pass
+
+ def print_held(self):
+ self.held_messages = self.mlist.get_held_page (50,1)
+ print ()
+ for msg in self.held_messages:
+ print(msg.request_id, msg.subject, msg.sender)
+ print ()
+
+ def matching_request_ids(self,text):
+ candidates = []
+ prefix_len = len(text)
+ if text == "page"[0:prefix_len]:
+ candidates.append('page')
+
+ for msg in self.held_messages:
+ string = str(msg.request_id)
+ if text == string[0:prefix_len]:
+ candidates.append(string)
+ return candidates
+
+ def complete_discard(self, text, line, begidx, endidx):
+ return self.matching_request_ids(text)
+
+ def do_discard(self, arg):
+ '''
+ discard <number> | page
+ '''
+ try:
+ if arg == 'page':
+ for msg in self.held_messages:
+ msg.discard()
+ else:
+ n = int(arg)
+ self.mlist.get_held_message (n).discard()
+ except ValueError:
+ print('Illegal request_id: {:s}'.format(arg))
+
+ return False
+
+ def preloop(self):
+ self.print_held()
+
+ def postcmd(self, stop, line):
+ if (not stop):
+ self.print_held()
+ return stop
+
+ def do_EOF(self,arg):
+ 'exit'
+ return True
+
+ def do_quit(self,arg):
+ '''
+ exit moderation shell
+ '''
+ return True
def main ():
- config_path = PurePath (xdg_config_home (), 'mm3mod', 'config.ini')
+ home = os.environ.get('HOME')
+ xdg_config_home = os.environ.get('XDG_CONFIG_HOME') or \
+ os.path.join(home, '.config')
+
+ config_path = PurePath (xdg_config_home, 'mm3mod', 'config.ini')
config = configparser.ConfigParser ()
+
with open(config_path) as f:
config.read_file (f)
- print("user =", config.get('rest','user'))
+ baseurl = config.get('rest', 'baseurl', fallback="http://localhost:8001/3.1/")
+ user = config.get('rest', 'user', fallback='restadmin')
+ password = config.get('rest', 'password')
+ client = Client (baseurl, name=user, password=password);
+ list_name = config.get('list','name')
+
+ mlist = client.get_list (list_name)
+ ModShell(client, mlist).cmdloop()
if __name__ == "__main__":
main()