[mfingerd] Addwd some basic command line options main
authorMalte Bublitz <malte@rolltreppe3.de>
Thu, 2 Oct 2025 08:22:33 +0000 (10:22 +0200)
committerMalte Bublitz <malte@rolltreppe3.de>
Thu, 2 Oct 2025 08:22:33 +0000 (10:22 +0200)
bbs/mfingerd.py

index fd585a56f37cf7a6542de87cbadce7c3e8725e7e..9d39ec08880407030941dcc7b369abe2ae01c6c5 100644 (file)
@@ -9,6 +9,7 @@ Copyright © 2012-2022 Malte Bublitz. All rights reserved.
 
 class AppInfo(object):
        Name       = "mFingerd"
+       ScriptName = "mfingerd"
        Version    = "0.2"
        Vendor     = "rolltreppe3"
        WebsiteURL = "https://rolltreppe3.de"
@@ -16,10 +17,7 @@ class AppInfo(object):
        DBFile     = "/opt/bbs/fingerinfo.db"
 
 import sys, os
-try:
-       import SocketServer as socketserver # Python 2.7
-except ImportError:
-       import socketserver # Python 3.x
+import socketserver
 import string
 from socket import getfqdn
 import signal
@@ -118,10 +116,45 @@ def stop_server(signum = 0, frame = 0):
        sys.exit(0)
 
 
-def main():
+def usage():
+       print("Usage:")
+       print(f"\t{AppInfo.ScriptName} [-h|--help|-V|--version]")
+       print(f"\t{AppInfo.ScriptName} [*options*]")
+       print()
+       print("Options:")
+       print("\t-h, --help       Show this usage help")
+       print("\t-V, --version    Show program version")
+       print("\t--force-root     Always run on a privileged port (<1024)")
+       print("\t--force-no-root  Run on an unprivileged port even as root")
+       print()
+       print("The default port if running as root is 79, with 7079 as fallback if running as a regular user")
+       print()
+
+
+def main(argv, argc):
        # Get UID
        uid = os.getuid()
 
+       if argc > 1:
+               for arg in argv[1:]:
+                       if arg in ("-h", "--help"):
+                               print(f"Database file: {AppInfo.DBFile}", file=sys.stderr)
+                               usage()
+                               sys.exit(0)
+
+                       elif arg in ("-V", "--version"):
+                               print(f"{AppInfo.Name} {AppInfo.Version}")
+
+                       elif arg == "--force-root":
+                               uid = 0
+
+                       elif arg == "--force-no-root":
+                               uid = 1000
+
+                       elif arg[0] == "-":
+                               print("Invalid option: "+arg, file=sys.stderr)
+                               sys.exit(2)
+
        # Port to listen on
        # If running as root/uid=0, use default port 79; if not,
        # use 7079 as a fallback.
@@ -170,6 +203,8 @@ def main():
                server.server_close()
                #sys.exit(0)
 
+
+
 if __name__=='__main__':
-       main()
+       main(sys.argv, len(sys.argv))