Changeset 2474

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

1.23beta is out with some goodies.

Location:
pykota/trunk
Files:
4 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 
  • pykota/trunk/conf/pykota.conf.sample

    r2445 r2474  
    758758# PYKOTAOVERCHARGE : user's overcharging factor. 
    759759# PYKOTAJOBBILLING : Job's billing code if present (CUPS only) 
     760# PYKOTAREASON : if the job was denied or a warning needs to be issued, contains 
     761#                the message to send to the user. 
    760762# 
    761763 
  • pykota/trunk/NEWS

    r2467 r2474  
    2222PyKota NEWS : 
    2323        
     24    - 1.23beta : 
     25     
     26        - Now the mailto directive is honored in all cases where the job is 
     27          denied or a warning has to be issued, and the correct reason 
     28          can be sent back to the user. 
     29           
     30        - The PYKOTAREASON environment variable, if defined, contains the   
     31          textual reason why the job was denied or a warning has to be 
     32          issued. 
     33         
    2434    - 1.23alpha31 : 
    2535     
  • pykota/trunk/pykota/version.py

    r2465 r2474  
    2222# 
    2323 
    24 __version__ = "1.23alpha31_unofficial" 
     24__version__ = "1.23beta_unofficial" 
    2525 
    2626__doc__ = "PyKota : a complete Printing Quota Solution for CUPS and LPRng."