root / pykota / trunk / pykota / tool.py @ 708

Revision 708, 5.2 kB (checked in by jalet, 21 years ago)

Incorrect handling of grace delay

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1#! /usr/bin/env python
2
3# PyKota - Print Quotas for CUPS
4#
5# (c) 2003 Jerome Alet <alet@librelogiciel.com>
6# You're welcome to redistribute this software under the
7# terms of the GNU General Public Licence version 2.0
8# or, at your option, any higher version.
9#
10# You can read the complete GNU GPL in the file COPYING
11# which should come along with this software, or visit
12# the Free Software Foundation's WEB site http://www.fsf.org
13#
14# $Id$
15#
16# $Log$
17# Revision 1.4  2003/02/05 23:26:22  jalet
18# Incorrect handling of grace delay
19#
20# Revision 1.3  2003/02/05 22:16:20  jalet
21# DEVICE_URI is undefined outside of CUPS, i.e. for normal command line tools
22#
23# Revision 1.2  2003/02/05 22:10:29  jalet
24# Typos
25#
26# Revision 1.1  2003/02/05 21:28:17  jalet
27# Initial import into CVS
28#
29#
30#
31
32import sys
33import os
34import smtplib
35
36from mx import DateTime
37
38from pykota import config
39from pykota import storage
40from pykota import logger
41
42class PyKotaToolError(Exception):
43    """An exception for PyKota config related stuff."""
44    def __init__(self, message = ""):
45        self.message = message
46        Exception.__init__(self, message)
47    def __repr__(self):
48        return self.message
49    __str__ = __repr__
50   
51class PyKotaTool :   
52    """Base class for all PyKota command line tools."""
53    def __init__(self, isfilter=0) :
54        """Initializes the command line tool."""
55        self.config = config.PyKotaConfig(os.environ.get("CUPS_SERVERROOT", "/etc/cups"))
56        self.logger = logger.openLogger(self.config)
57        self.storage = storage.openConnection(self.config, asadmin=(not isfilter))
58        self.printername = os.environ.get("PRINTER", None)
59        self.smtpserver = self.config.getSMTPServer()
60        self.admin = self.config.getAdmin()
61        self.adminmail = self.config.getAdminMail()
62       
63    def sendMessage(self, touser, fullmessage) :
64        """Sends an email message containing headers to some user."""
65        fullmessage += "\n\nPlease contact your system administrator %s - <%s>\n" % (self.admin, self.adminmail)
66        if "@" not in touser :
67            touser = "%s@%s" % (touser, self.smtpserver)
68        server = smtplib.SMTP(self.smtpserver)
69        server.sendmail(self.adminmail, [touser], fullmessage)
70        server.quit()
71       
72    def sendMessageToUser(self, username, subject, message) :
73        """Sends an email message to a user."""
74        self.sendMessage(username, "Subject: %s\n\n%s" % (subject, message))
75       
76    def sendMessageToAdmin(self, subject, message) :
77        """Sends an email message to the Print Quota administrator."""
78        self.sendMessage(self.adminmail, "Subject: %s\n\n%s" % (subject, message))
79       
80    def checkUserPQuota(self, username, printername) :
81        """Checks the user quota on a printer and deny or accept the job."""
82        now = DateTime.now()
83        quota = self.storage.getUserPQuota(username, printername)
84        pagecounter = quota["pagecounter"]
85        softlimit = quota["softlimit"]
86        hardlimit = quota["hardlimit"]
87        datelimit = quota["datelimit"]
88        if datelimit :
89            datelimit = DateTime.DateTime(datelimit)    # TODO : check this !
90        if softlimit is not None :
91            if pagecounter < softlimit :
92                action = "ALLOW"
93            elif hardlimit is not None :
94                 if softlimit <= pagecounter < hardlimit :   
95                     if datelimit is None :
96                         self.storage.doQuery("UPDATE userpquota SET datelimit=%s::DATETIME WHERE userid=%s AND printerid=%s;" % (self.doQuote("%04i-%02i-%02i %02i:%02i" % (now.year, now.month, now.day, now.hour, now.minute)), self.doQuote(self.getUserId(username)), self.doQuote(self.getPrinterId(printername))))
97                         datelimit = now
98                     if (now - datelimit) <= self.config.getGraceDelay() :
99                         action = "WARN"
100                     else :   
101                         action = "DENY"
102                 else :         
103                     action = "DENY"
104            else :       
105                action = "DENY"
106        else :       
107            action = "ALLOW"
108        return (action, (hardlimit - pagecounter), datelimit)
109   
110    def warnQuotaPrinter(self, username) :
111        """Checks a user quota and send him a message if quota is exceeded on current printer."""
112        (action, grace, gracedate) = self.storage.checkUserPQuota(username, self.printername)
113        if action == "DENY" :
114            adminmessage = "Print Quota exceeded for user %s on printer %s" % (username, self.printername)
115            self.logger.log_message(adminmessage)
116            self.sendMessageToUser(username, "Print Quota Exceeded", "You are not allowed to print anymore because your Print Quota is exceeded.")
117            self.sendMessageToAdmin("Print Quota", adminmessage)
118        elif action == "WARN" :   
119            adminmessage = "Print Quota soft limit exceeded for user %s on printer %s" % (username, self.printername)
120            self.logger.log_message(adminmessage)
121            self.sendMessageToUser(username, "Print Quota Exceeded", "You will soon be forbidden to print anymore because your Print Quota is almost reached.")
122            self.sendMessageToAdmin("Print Quota", adminmessage)
123        return action       
124   
Note: See TracBrowser for help on using the browser.