Show
Ignore:
Timestamp:
09/24/05 00:18:43 (19 years ago)
Author:
jerome
Message:

1.23beta is out with some goodies.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • pykota/trunk/bin/cupspykota

    r2463 r2474  
    3636import fnmatch 
    3737import pwd 
     38import socket 
     39import smtplib 
    3840 
    3941from pykota.tool import PyKotaTool, PyKotaToolError, crashed 
     
    188190         
    189191        self.Action = "ALLOW"   # job allowed by default 
     192        self.Reason = None 
    190193        self.JobId = sys.argv[1].strip() 
    191194        # use CUPS' user when printing test pages from CUPS' web interface 
     
    292295            if action : 
    293296                self.Action = action 
     297                self.Reason = _("You are not allowed to print at this time.") 
    294298            if username : 
    295299                self.UserName = username 
     
    517521        os.environ["PYKOTAJOBSIZE"] = str(self.JobSize) 
    518522        os.environ["PYKOTAJOBPRICE"] = str(self.JobPrice) 
     523        self.logdebug("Environment updated.") 
     524         
     525    def exportReason(self) : 
     526        """Exports the job's action status and optional reason.""" 
     527        self.logdebug("Exporting job's action status...") 
     528        os.environ["PYKOTAACTION"] = str(self.Action) 
     529        if self.Reason : 
     530            os.environ["PYKOTAREASON"] = str(self.Reason) 
    519531        self.logdebug("Environment updated.") 
    520532         
     
    768780                self.printInfo("%s : %s." % (msg, _("Printing is denied by configuration")), "warn") 
    769781                self.Action = "DENY" 
     782                self.Reason = _("Duplicate print jobs are not allowed on printer %s.") % self.PrinterName 
    770783            else :     
    771784                self.logdebug("Launching subprocess [%s] to see if dupes should be allowed or not." % denyduplicates) 
     
    775788                if self.Action == "DENY" :      
    776789                    self.printInfo("%s : %s." % (msg, _("Subprocess denied printing of a dupe")), "warn") 
     790                    self.Reason = _("Duplicate print jobs are not allowed on printer %s at this time.") % self.PrinterName 
    777791                else :     
    778792                    self.printInfo("%s : %s." % (msg, _("Subprocess allowed printing of a dupe")), "warn") 
     
    781795        self.logdebug("Checking if the job is a dupe done.") 
    782796         
     797    def tellUser(self) : 
     798        """Sends a message to an user.""" 
     799        self.logdebug("Sending some feedback to user %s..." % self.UserName)   
     800        if not self.Reason : 
     801            self.logdebug("No feedback to send to user %s." % self.UserName) 
     802        else :     
     803            (mailto, arguments) = self.config.getMailTo(self.PrinterName) 
     804            if mailto == "EXTERNAL" : 
     805                # TODO : clean this again 
     806                self.externalMailTo(arguments, self.Action, self.User, self.Printer, self.Reason) 
     807            else :     
     808                # TODO : clean this again 
     809                admin = self.config.getAdmin(self.PrinterName) 
     810                adminmail = self.config.getAdminMail(self.PrinterName) 
     811                usermail = self.User.Email or self.User.Name 
     812                if "@" not in usermail : 
     813                    usermail = "%s@%s" % (usermail, self.maildomain or self.smtpserver) 
     814                destination = [] 
     815                if mailto in ("BOTH", "ADMIN") : 
     816                    destination.append(adminmail) 
     817                if mailto in ("BOTH", "USER") :     
     818                    destination.append(usermail) 
     819                     
     820                fullmessage = self.Reason + (_("\n\nYour system administrator :\n\n\t%s - <%s>\n") % (admin, adminmail)) 
     821                try :     
     822                    server = smtplib.SMTP(self.smtpserver) 
     823                except socket.error, msg :     
     824                    self.printInfo(_("Impossible to connect to SMTP server : %s") % msg, "error") 
     825                else : 
     826                    try : 
     827                        server.sendmail(adminmail, destination, \ 
     828                          "From: %s\nTo: %s\nCc: %s\nSubject: %s\n\n%s" \ 
     829                          % (adminmail, usermail, adminmail, _("Print Quota"), fullmessage)) 
     830                    except smtplib.SMTPException, answer :     
     831                        for (k, v) in answer.recipients.items() : 
     832                            self.printInfo(_("Impossible to send mail to %s, error %s : %s") % (k, v[0], v[1]), "error") 
     833                    server.quit() 
     834            self.logdebug("Feedback sent to user %s." % self.UserName) 
     835                 
    783836    def mainWork(self) :     
    784837        """Main work is done here.""" 
     
    827880                self.printInfo(_("Precomputed job size (%s pages) too large for printer %s.") % (self.softwareJobSize, self.PrinterName), "warn") 
    828881                self.Action = "DENY" 
     882                # here we don't put the precomputed job size in the message 
     883                # because in case of error the user could complain :-) 
     884                self.Reason = _("You are not allowed to print so many pages on printer %s at this time.") % self.PrinterName 
    829885             
    830886        if self.Action != "DENY" : 
     
    832888                self.printInfo(_("User %s is not allowed to print at this time.") % self.UserName, "warn") 
    833889                self.Action = "DENY" 
     890                self.Reason = _("Your account settings forbid you to print at this time.") 
    834891                 
    835892        if self.Action != "DENY" : 
     
    859916                self.logdebug("Checking user %s print quota entry on printer %s" \ 
    860917                                    % (self.UserName, self.PrinterName)) 
    861                 self.Action = self.warnUserPQuota(self.UserPQuota) 
     918                self.Action = self.checkUserPQuota(self.UserPQuota) 
     919                if self.Action.startswith("POLICY_") : 
     920                    self.Action = self.Action[7:] 
     921                if self.Action == "DENY" : 
     922                    self.printInfo(_("Print Quota exceeded for user %s on printer %s") % (self.UserName, self.PrinterName)) 
     923                    self.Reason = self.config.getHardWarn(self.PrinterName) 
     924                elif self.Action == "WARN" :     
     925                    self.printInfo(_("Print Quota low for user %s on printer %s") % (self.UserName, self.PrinterName)) 
     926                    if self.User.LimitBy and (self.User.LimitBy.lower() == "balance") :  
     927                        self.Reason = self.config.getPoorWarn() 
     928                    else :      
     929                        self.Reason = self.config.getSoftWarn(self.PrinterName) 
    862930             
    863931        # exports some new environment variables 
    864         os.environ["PYKOTAACTION"] = str(self.Action) 
     932        self.exportReason() 
     933         
     934        # now tell the user if he needs to know something 
     935        self.tellUser() 
    865936         
    866937        # launches the pre hook