Show
Ignore:
Timestamp:
04/24/03 00:13:57 (21 years ago)
Author:
jalet
Message:

Preliminary support for LPRng added BUT STILL UNTESTED.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • pykota/trunk/bin/pykota

    r915 r952  
    33# PyKota accounting filter 
    44# 
    5 # PyKota - Print Quotas for CUPS 
     5# PyKota - Print Quotas for CUPS and LPRng 
    66# 
    77# (c) 2003 Jerome Alet <alet@librelogiciel.com> 
     
    2323# 
    2424# $Log$ 
     25# Revision 1.24  2003/04/23 22:13:56  jalet 
     26# Preliminary support for LPRng added BUT STILL UNTESTED. 
     27# 
    2528# Revision 1.23  2003/04/15 11:30:57  jalet 
    2629# More work done on money print charging. 
     
    115118class PyKotaFilter(PyKotaTool) :     
    116119    """Class for the PyKota filter.""" 
    117     def __init__(self, username) : 
     120    def __init__(self) : 
    118121        PyKotaTool.__init__(self) 
    119         self.username = username 
     122        (self.printingsystem, self.printerhostname, self.printername, self.username, self.jobid, self.inputfile) = self.extractInfoFromCupsOrLprng() 
    120123        self.requester = openRequester(self.config, self.printername) 
    121         self.printerhostname = self.getPrinterHostname() 
    122      
    123     def getPrinterHostname(self) : 
    124         """Returns the printer hostname.""" 
    125         device_uri = os.environ.get("DEVICE_URI", "") 
    126         # TODO : check this for more complex urls than ipp://myprinter.dot.com:631/printers/lp 
    127         try : 
    128             (backend, destination) = device_uri.split(":", 1)  
    129         except ValueError :     
    130             raise PyKotaToolError, "Invalid DEVICE_URI : %s\n" % device_uri 
    131         while destination.startswith("/") : 
    132             destination = destination[1:] 
    133         return destination.split("/")[0].split(":")[0] 
    134          
     124     
    135125    def filterInput(self, inputfile) : 
    136126        """Transparent filter.""" 
     
    148138            infile.close() 
    149139             
     140    def acceptJob(self) :         
     141        """Returns the exit code needed by the printing backend to accept the job and print it.""" 
     142        if self.printingsystem == "CUPS" : 
     143            return 0 
     144        elif self.printingsystem == "LPRNG" :     
     145            return 0 
     146        else :     
     147            # UNKNOWN 
     148            return -1 
     149             
     150    def removeJob(self) :             
     151        """Returns the exit code needed by the printing backend to refuse the job and remove it.""" 
     152        if self.printingsystem == "CUPS" : 
     153            return 1 
     154        elif self.printingsystem == "LPRNG" :     
     155            return 3 
     156        else :     
     157            # UNKNOWN 
     158            return -1 
     159             
    150160def main() :     
    151161    """Do it, and do it right !""" 
     162         
     163    # Initializes the current tool 
     164    kotafilter = PyKotaFilter()     
     165     
    152166    # 
    153     # This is a CUPS filter, so we should act and die like a CUPS filter when needed 
    154     narg = len(sys.argv) 
    155     if narg not in (6, 7) :     
    156         sys.stderr.write("ERROR: %s job-id user title copies options [file]\n" % sys.argv[0]) 
    157         return 1 
    158     elif narg == 7 :     
    159         # input file 
    160         inputfile = sys.argv[6] 
    161     else :     
    162         # stdin 
    163         inputfile = None 
    164          
    165     #     
    166     # According to CUPS documentation, the job id is the second command line argument 
    167     jobid = sys.argv[1].strip() 
    168      
    169     #     
    170     # According to CUPS documentation, the username is the third command line argument 
    171     username = sys.argv[2].strip()     
    172      
    173     # Initializes the current tool 
    174     kotafilter = PyKotaFilter(username)     
     167    # If this is a CUPS filter, we should act and die like a CUPS filter when needed 
     168    if kotafilter.printingsystem == "CUPS" : 
     169        if len(sys.argv) not in (6, 7) :     
     170            sys.stderr.write("ERROR: %s job-id user title copies options [file]\n" % sys.argv[0]) 
     171            return kotafilter.removeJob() 
    175172     
    176173    # Get the page counter directly from the printer itself 
    177174    try : 
    178         counterbeforejob = kotafilter.requester.getPrinterPageCounter(kotafilter.printerhostname) # TODO use printername instead, make them match from CUPS' config files 
     175        counterbeforejob = kotafilter.requester.getPrinterPageCounter(kotafilter.printerhostname) 
    179176    except PyKotaRequesterError, msg : 
    180177        # can't get actual page counter, assume printer is off, but warns in log 
     
    192189        kotafilter.logger.log_message(_("Printer %s not registered in the PyKota system") % kotafilter.printername, "warn") 
    193190    else :     
    194         userid = kotafilter.storage.getUserId(username) 
     191        userid = kotafilter.storage.getUserId(kotafilter.username) 
    195192        if userid is None : 
    196193            # The user is unknown from the Quota Storage perspective 
    197             # we let the job pass through, but log a warning message 
    198             kotafilter.logger.log_message(_("User %s not registered in the PyKota system") % username, "warn") 
     194            # Depending on the default policy for this printer, we 
     195            # either let the job pass through or reject it, but we 
     196            # log a message in any case. 
     197            policy = kotafilter.config.getPrinterPolicy(kotafilter.printername) 
     198            if policy in [None, "ALLOW"] : 
     199                action = "POLICY_ALLOW" 
     200            else :     
     201                action = "POLICY_DENY" 
     202            kotafilter.logger.log_message(_("User %s not registered in the PyKota system, applying default policy (%s) for printer %s") % (kotafilter.username, action, kotafilter.printername), "warn") 
     203            if action == "POLICY_DENY" : 
     204                return kotafilter.removeJob() 
    199205        else : 
    200206            # get last job information for this printer 
     
    203209                # The printer hasn't been used yet, from PyKota's point of view 
    204210                lasthistoryid = None 
    205                 lastjobid = jobid 
     211                lastjobid = kotafilter.jobid 
    206212                lastuserid = userid 
    207                 lastusername = username 
     213                lastusername = kotafilter.username 
    208214                lastpagecounter = counterbeforejob 
    209215            else :     
     
    251257             
    252258            # warns the last user if he is over quota 
    253             kotafilter.warnUserPQuota(lastusername) 
     259            kotafilter.warnUserPQuota(lastusername, kotafilter.printername) 
    254260                 
    255261            # Is the current user allowed to print at all ? 
    256             action = kotafilter.warnUserPQuota(username) 
     262            action = kotafilter.warnUserPQuota(kotafilter.username, kotafilter.printername) 
    257263             
    258264            # adds the current job to history     
    259             kotafilter.storage.addJobToHistory(jobid, kotafilter.storage.getUserId(username), printerid, counterbeforejob, action) 
     265            kotafilter.storage.addJobToHistory(kotafilter.jobid, kotafilter.storage.getUserId(kotafilter.username), printerid, counterbeforejob, action) 
    260266             
    261267            # if not allowed to print then die, else proceed. 
    262268            if action == "DENY" : 
    263269                # No, just die cleanly 
    264                 return 1 
     270                return kotafilter.removeJob() 
    265271         
    266272    # pass the job untouched to the underlying layer 
    267     kotafilter.filterInput(inputfile)       
    268      
    269     return 0 
     273    kotafilter.filterInput(kotafilter.inputfile)       
     274     
     275    return kotafilter.acceptJob() 
    270276 
    271277if __name__ == "__main__" :     
    272     sys.exit(main() or 0) 
     278    sys.exit(main())