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

Revision 712, 5.6 kB (checked in by jalet, 21 years ago)

Now includes the printer name in email messages

  • 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.7  2003/02/06 00:00:45  jalet
18# Now includes the printer name in email messages
19#
20# Revision 1.6  2003/02/05 23:55:02  jalet
21# Cleaner email messages
22#
23# Revision 1.5  2003/02/05 23:45:09  jalet
24# Better DateTime manipulation wrt grace delay
25#
26# Revision 1.4  2003/02/05 23:26:22  jalet
27# Incorrect handling of grace delay
28#
29# Revision 1.3  2003/02/05 22:16:20  jalet
30# DEVICE_URI is undefined outside of CUPS, i.e. for normal command line tools
31#
32# Revision 1.2  2003/02/05 22:10:29  jalet
33# Typos
34#
35# Revision 1.1  2003/02/05 21:28:17  jalet
36# Initial import into CVS
37#
38#
39#
40
41import sys
42import os
43import smtplib
44
45from mx import DateTime
46
47from pykota import config
48from pykota import storage
49from pykota import logger
50
51class PyKotaToolError(Exception):
52    """An exception for PyKota config related stuff."""
53    def __init__(self, message = ""):
54        self.message = message
55        Exception.__init__(self, message)
56    def __repr__(self):
57        return self.message
58    __str__ = __repr__
59   
60class PyKotaTool :   
61    """Base class for all PyKota command line tools."""
62    def __init__(self, isfilter=0) :
63        """Initializes the command line tool."""
64        self.config = config.PyKotaConfig(os.environ.get("CUPS_SERVERROOT", "/etc/cups"))
65        self.logger = logger.openLogger(self.config)
66        self.storage = storage.openConnection(self.config, asadmin=(not isfilter))
67        self.printername = os.environ.get("PRINTER", None)
68        self.smtpserver = self.config.getSMTPServer()
69        self.admin = self.config.getAdmin()
70        self.adminmail = self.config.getAdminMail()
71       
72    def sendMessage(self, touser, fullmessage) :
73        """Sends an email message containing headers to some user."""
74        if "@" not in touser :
75            touser = "%s@%s" % (touser, self.smtpserver)
76        server = smtplib.SMTP(self.smtpserver)
77        server.sendmail(self.adminmail, [touser], fullmessage)
78        server.quit()
79       
80    def sendMessageToUser(self, username, subject, message) :
81        """Sends an email message to a user."""
82        message += "\n\nPlease contact your system administrator :\n\n\t%s - <%s>\n" % (self.admin, self.adminmail)
83        self.sendMessage(username, "Subject: %s\n\n%s" % (subject, message))
84       
85    def sendMessageToAdmin(self, subject, message) :
86        """Sends an email message to the Print Quota administrator."""
87        self.sendMessage(self.adminmail, "Subject: %s\n\n%s" % (subject, message))
88       
89    def checkUserPQuota(self, username, printername) :
90        """Checks the user quota on a printer and deny or accept the job."""
91        now = DateTime.now()
92        quota = self.storage.getUserPQuota(username, printername)
93        pagecounter = quota["pagecounter"]
94        softlimit = quota["softlimit"]
95        hardlimit = quota["hardlimit"]
96        datelimit = quota["datelimit"]
97        if datelimit is not None :
98            datelimit = DateTime.ISO.ParseDateTime(datelimit)
99        if softlimit is not None :
100            if pagecounter < softlimit :
101                action = "ALLOW"
102            elif hardlimit is not None :
103                 gracedelay = self.config.getGraceDelay()
104                 if softlimit <= pagecounter < hardlimit :   
105                     if datelimit is None :
106                         datelimit = now + gracedelay
107                         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))))
108                     if (now + gracedelay) < datelimit :
109                         action = "WARN"
110                     else :   
111                         action = "DENY"
112                 else :         
113                     action = "DENY"
114            else :       
115                action = "DENY"
116        else :       
117            action = "ALLOW"
118        return (action, (hardlimit - pagecounter), datelimit)
119   
120    def warnQuotaPrinter(self, username) :
121        """Checks a user quota and send him a message if quota is exceeded on current printer."""
122        (action, grace, gracedate) = self.checkUserPQuota(username, self.printername)
123        if action == "DENY" :
124            adminmessage = "Print Quota exceeded for user %s on printer %s" % (username, self.printername)
125            self.logger.log_message(adminmessage)
126            self.sendMessageToUser(username, "Print Quota Exceeded", "You are not allowed to print anymore because\nyour Print Quota is exceeded on printer %s." % self.printername)
127            self.sendMessageToAdmin("Print Quota", adminmessage)
128        elif action == "WARN" :   
129            adminmessage = "Print Quota soft limit exceeded for user %s on printer %s" % (username, self.printername)
130            self.logger.log_message(adminmessage)
131            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)
132            self.sendMessageToAdmin("Print Quota", adminmessage)
133        return action       
134   
Note: See TracBrowser for help on using the browser.