#! /usr/bin/env python # PyKota - Print Quotas for CUPS # # (c) 2003 Jerome Alet # You're welcome to redistribute this software under the # terms of the GNU General Public Licence version 2.0 # or, at your option, any higher version. # # You can read the complete GNU GPL in the file COPYING # which should come along with this software, or visit # the Free Software Foundation's WEB site http://www.fsf.org # # $Id$ # # $Log$ # Revision 1.8 2003/02/06 09:19:02 jalet # More robust behavior (hopefully) when the user or printer is not managed # correctly by the Quota System : e.g. cupsFilter added in ppd file, but # printer and/or user not 'yet?' in storage. # # Revision 1.7 2003/02/06 00:00:45 jalet # Now includes the printer name in email messages # # Revision 1.6 2003/02/05 23:55:02 jalet # Cleaner email messages # # Revision 1.5 2003/02/05 23:45:09 jalet # Better DateTime manipulation wrt grace delay # # Revision 1.4 2003/02/05 23:26:22 jalet # Incorrect handling of grace delay # # Revision 1.3 2003/02/05 22:16:20 jalet # DEVICE_URI is undefined outside of CUPS, i.e. for normal command line tools # # Revision 1.2 2003/02/05 22:10:29 jalet # Typos # # Revision 1.1 2003/02/05 21:28:17 jalet # Initial import into CVS # # # import sys import os import smtplib from mx import DateTime from pykota import config from pykota import storage from pykota import logger class PyKotaToolError(Exception): """An exception for PyKota config related stuff.""" def __init__(self, message = ""): self.message = message Exception.__init__(self, message) def __repr__(self): return self.message __str__ = __repr__ class PyKotaTool : """Base class for all PyKota command line tools.""" def __init__(self, isfilter=0) : """Initializes the command line tool.""" self.config = config.PyKotaConfig(os.environ.get("CUPS_SERVERROOT", "/etc/cups")) self.logger = logger.openLogger(self.config) self.storage = storage.openConnection(self.config, asadmin=(not isfilter)) self.printername = os.environ.get("PRINTER", None) self.smtpserver = self.config.getSMTPServer() self.admin = self.config.getAdmin() self.adminmail = self.config.getAdminMail() def sendMessage(self, touser, fullmessage) : """Sends an email message containing headers to some user.""" if "@" not in touser : touser = "%s@%s" % (touser, self.smtpserver) server = smtplib.SMTP(self.smtpserver) server.sendmail(self.adminmail, [touser], fullmessage) server.quit() def sendMessageToUser(self, username, subject, message) : """Sends an email message to a user.""" message += "\n\nPlease contact your system administrator :\n\n\t%s - <%s>\n" % (self.admin, self.adminmail) self.sendMessage(username, "Subject: %s\n\n%s" % (subject, message)) def sendMessageToAdmin(self, subject, message) : """Sends an email message to the Print Quota administrator.""" self.sendMessage(self.adminmail, "Subject: %s\n\n%s" % (subject, message)) def checkUserPQuota(self, username, printername) : """Checks the user quota on a printer and deny or accept the job.""" now = DateTime.now() quota = self.storage.getUserPQuota(username, printername) if quota is None : # Unknown user or printer or combination policy = self.config.getPrinterPolicy(printername) if policy in [None, "ALLOW"] : action = "ALLOW" else : action = "DENY" self.logger.log_message("Unable to match user %s on printer %s, applying default policy (%s)" % (username, printername, action), "warn") return (action, None, None) else : pagecounter = quota["pagecounter"] softlimit = quota["softlimit"] hardlimit = quota["hardlimit"] datelimit = quota["datelimit"] if datelimit is not None : datelimit = DateTime.ISO.ParseDateTime(datelimit) if softlimit is not None : if pagecounter < softlimit : action = "ALLOW" elif hardlimit is not None : gracedelay = self.config.getGraceDelay() if softlimit <= pagecounter < hardlimit : if datelimit is None : datelimit = now + gracedelay self.storage.doQuery("UPDATE userpquota SET datelimit=%s::DATETIME WHERE userid=%s AND printerid=%s;" % (self.doQuote("%04i-%02i-%02i %02i:%02i:%02i" % (datelimit.year, datelimit.month, datelimit.day, datelimit.hour, datelimit.minute, datelimit.second)), self.doQuote(self.getUserId(username)), self.doQuote(self.getPrinterId(printername)))) if (now + gracedelay) < datelimit : action = "WARN" else : action = "DENY" else : action = "DENY" else : action = "DENY" else : action = "ALLOW" return (action, (hardlimit - pagecounter), datelimit) def warnQuotaPrinter(self, username) : """Checks a user quota and send him a message if quota is exceeded on current printer.""" (action, grace, gracedate) = self.checkUserPQuota(username, self.printername) if action == "DENY" : if (grace is not None) and (gracedate is not None) : # only when both user and printer are known adminmessage = "Print Quota exceeded for user %s on printer %s" % (username, self.printername) self.logger.log_message(adminmessage) self.sendMessageToUser(username, "Print Quota Exceeded", "You are not allowed to print anymore because\nyour Print Quota is exceeded on printer %s." % self.printername) self.sendMessageToAdmin("Print Quota", adminmessage) elif action == "WARN" : adminmessage = "Print Quota soft limit exceeded for user %s on printer %s" % (username, self.printername) self.logger.log_message(adminmessage) self.sendMessageToUser(username, "Print Quota Exceeded", "You will soon be forbidden to print anymore because\nyour Print Quota is almost reached on printer %s." % self.printername) self.sendMessageToAdmin("Print Quota", adminmessage) return action