#!/usr/bin/env /usr/local/bin/python3


#  A trivial example of a custom user program for use with PaperCut NG or MF
#  See http://www.papercut.com/kb/Main/CaseStudyCustomUserSyncIntegration
#
#  Uses Python 3 for dictionaries and be X platform. Make sure that the
#  Python intepreter is on the path for the PaperCut service account or specify
#  the path explicitly.

#  N.B. This example is for illistrative purposes only. Any production solution
#  needs to be optomised for performance.

import sys
import logging

userDatabase = {
    "john": {"fullname":"John Smith",   "email":"johns@here.com",  "dept":"Accounts",  "office":"Melbourne",   "cardno":"1234", "password":"password1"},
    "jane": {"fullname":"Jane Rodgers", "email":"janer@here.com",  "dept":"Sales",     "office":"Docklands",   "cardno":"5678", "password":"password2"},
    "ahmed":{"fullname":"Ahmed Yakubb", "email":"ahmedy@here.com", "dept":"Marketing", "office":"Home Office", "cardno":"4321", "password":"password3"},
}

groupDatabase = {
    "groupA":["john"],
    "groupB":["ahmed","jane"],
    }

logging.basicConfig(level=logging.DEBUG, filename="/tmp/logfile", filemode="a+",
                        format="%(asctime)-15s %(levelname)-8s %(message)s")

logging.info("Called with {}".format(sys.argv))

def formatUserDetails(userName):
   if userName in userDatabase:
      return '\t'.join([userName, userDatabase[userName]["fullname"], userDatabase[userName]["email"], userDatabase[userName]["dept"], userDatabase[userName]["office"], userDatabase[userName]["cardno"]])
   else:
      print("Call to formatUserDetails error for username {}".format(userName),file=sys.stderr)
      sys.exit(-1)

# Being called as user auth program
if len(sys.argv) == 1:
   name = input()
   password = input()
   if name in userDatabase and userDatabase[name]["password"] == password:
      print("OK")
      sys.exit(0)
   else:
      print("Wrong username or password",file=sys.stderr)
      sys.exit(-1)

if len(sys.argv) < 2 or  sys.argv[1] != '-':
   print("incorrect argument passed {0}".format(sys.argv), file=sys.stderr)
   sys.exit(-1)


# Being called as user sync program
if sys.argv[2] == "is-valid":
   print('Y')
   sys.exit(0)
  
if sys.argv[2] == "all-users":
   for name in userDatabase:
      print(formatUserDetails(name))
   sys.exit(0)

if sys.argv[2] == "all-groups":
   print('\n'.join([g for g in groupDatabase]))
   sys.exit(0)

if sys.argv[2] == "get-user-details":
      name = input()
      if  name in userDatabase:
         print(formatUserDetails(name))
         sys.exit(0)
      else:
         print("Can't find user {0}".format(name), file=sys.stderr)
         sys.exit(-1)

if sys.argv[2] == "group-member-names":
      if  sys.argv[3] in groupDatabase:
         for user in groupDatabase[sys.argv[3]]:
            if user in userDatabase:
               print(user)
            else:
               print("Invalid user name {} found in group list {}".format(user, group),file=sys.stderr)
               sys.exit(-1)
         sys.exit(0)
      else:
         print("Group name {} not found".format(sys.argv[3]),file=sys.stderr)
         sys.exit(-1)


if sys.argv[2] == "group-members":
      if  sys.argv[3] in groupDatabase:
         for user in groupDatabase[sys.argv[3]]:
            if user in userDatabase:
               print(formatUserDetails(user))
            else:
               print("Invalid user name {} found in group list {}".format(user, group),file=sys.stderr)
               sys.exit(-1)
         sys.exit(0)
      else:
         print("Group name {} not found".format(sys.argv[3]),file=sys.stderr)
         sys.exit(-1)


   
if sys.argv[2] == "is-user-in-group":
   if  sys.argv[3] in groupDatabase:
      if sys.argv[4] in groupDatabase[sys.argv[3]]:
               print('Y')
               sys.exit(0)
      print('N')
      sys.exit(0)
   print("Invalid Group name {}".format(sys.argv[3]),file=sys.stderr)
   sys.exit(-1)
   

print("Can't process arguments {0}".format(sys.argv), file=sys.stderr)

@@ -1071,16 +1071,18 @@
 %0A    %7D%0A%0A
+# 
 logging.
@@ -1154,16 +1154,18 @@
 e=%22a+%22,%0A
+# 
         
@@ -1234,16 +1234,18 @@
 ge)s%22)%0A%0A
+# 
 logging.
@@ -1285,16 +1285,17 @@
 argv))%0A%0A
+%0A
 def form
@@ -1680,16 +1680,17 @@
 it(-1)%0A%0A
+%0A
 # Being 
@@ -1857,24 +1857,25 @@
 word:%0A      
+ 
 print(%22OK%22)%0A
@@ -1871,18 +1871,72 @@
 rint(%22OK
-%22)
+%5Cn%7B%7D%5Cn%22.format(name)) # Note: return canonical user name
 %0A      s
@@ -2005,32 +2005,55 @@
 ile=sys.stderr)%0A
+      print(%22ERROR%5Cn%22)%0A
       sys.exit(-
@@ -2056,16 +2056,18 @@
 it(-1)%0A%0A
+%0A%0A
 if len(s
@@ -2189,32 +2189,33 @@
  sys.exit(-1)%0A%0A%0A
+%0A
 # Being called a
@@ -2293,18 +2293,17 @@
 exit(0)%0A
-  
+%0A
 %0Aif sys.
@@ -2400,32 +2400,33 @@
    sys.exit(0)%0A%0A
+%0A
 if sys.argv%5B2%5D =
@@ -2440,24 +2440,25 @@
 groups%22:%0A   
+ 
 print('%5Cn'.j
@@ -2489,24 +2489,25 @@
 abase%5D))%0A   
+ 
 sys.exit(0)%0A
@@ -2507,16 +2507,17 @@
 xit(0)%0A%0A
+%0A
 if sys.a
@@ -2763,16 +2763,17 @@
 it(-1)%0A%0A
+%0A
 if sys.a
@@ -3734,20 +3734,16 @@
 t(-1)%0A%0A%0A
-   %0A
 if sys.a
@@ -4035,19 +4035,16 @@
 xit(-1)%0A
-   
 %0A%0Aprint(

Return canonical user name -- plus general tidy up