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.1 2003/02/05 21:28:17 jalet |
---|
18 | # Initial import into CVS |
---|
19 | # |
---|
20 | # |
---|
21 | # |
---|
22 | |
---|
23 | import sys |
---|
24 | import os |
---|
25 | import smtplib |
---|
26 | |
---|
27 | from pykota import config |
---|
28 | from pykota import storage |
---|
29 | from pykota import logger |
---|
30 | |
---|
31 | class PyKotaToolError(Exception): |
---|
32 | """An exception for PyKota config related stuff.""" |
---|
33 | def __init__(self, message = ""): |
---|
34 | self.message = message |
---|
35 | Exception.__init__(self, message) |
---|
36 | def __repr__(self): |
---|
37 | return self.message |
---|
38 | __str__ = __repr__ |
---|
39 | |
---|
40 | class PyKotaTool : |
---|
41 | """Base class for all PyKota command line tools.""" |
---|
42 | def __init__(self, isfilter=0) : |
---|
43 | """Initializes the command line tool.""" |
---|
44 | self.config = config.PyKotaConfig(os.environ.get("CUPS_SERVERROOT", "/etc/cups")) |
---|
45 | self.logger = logger.Logger(self.config) |
---|
46 | self.storage = storage.openConnection(self.config, asadmin=(not isfilter)) |
---|
47 | self.printername = os.environ.get("PRINTER", None) |
---|
48 | self.printerhostname = self.getPrinterHostname() |
---|
49 | self.smtpserver = self.config.getSMTPServer() |
---|
50 | self.admin = self.config.getAdmin() |
---|
51 | self.adminmail = self.config.getAdminMail() |
---|
52 | |
---|
53 | def sendMessage(self, touser, fullmessage) : |
---|
54 | """Sends an email message containing headers to some user.""" |
---|
55 | fullmessage += "\n\nPlease contact your system administrator %s - <%s>\n" % (self.admin, self.adminmail) |
---|
56 | if "@" not in touser : |
---|
57 | touser = "%s@%s" % (touser, self.smtpserver) |
---|
58 | server = smtplib.SMTP(self.smtpserver) |
---|
59 | server.sendmail(self.adminmail, [touser], fullmessage) |
---|
60 | server.quit() |
---|
61 | |
---|
62 | def sendMessageToUser(self, username, subject, message) : |
---|
63 | """Sends an email message to a user.""" |
---|
64 | self.sendMessage(username, "Subject: %s\n\n%s" % (subject, message)) |
---|
65 | |
---|
66 | def sendMessageToAdmin(self, subject, message) : |
---|
67 | """Sends an email message to the Print Quota administrator.""" |
---|
68 | self.sendMessage(self.adminmail, "Subject: %s\n\n%s" % (subject, message)) |
---|
69 | |
---|
70 | def getPrinterHostname(self) : |
---|
71 | """Returns the printer hostname.""" |
---|
72 | device_uri = os.environ.get("DEVICE_URI", "") |
---|
73 | # TODO : check this for more complex urls than ipp://myprinter.dot.com:631/printers/lp |
---|
74 | try : |
---|
75 | (backend, destination) = device_uri.split(":", 1) |
---|
76 | except ValueError : |
---|
77 | raise PyKotaToolError, "Invalid DEVICE_URI : %s\n" % device_uri |
---|
78 | while destination.startswith("/") : |
---|
79 | destination = destination[1:] |
---|
80 | return destination.split("/")[0].split(":")[0] |
---|
81 | |
---|
82 | def warnQuotaPrinter(self, username) : |
---|
83 | """Checks a user quota and send him a message if quota is exceeded on current printer.""" |
---|
84 | (action, grace, gracedate) = self.storage.checkUserPQuota(username, self.printername) |
---|
85 | if action == "DENY" : |
---|
86 | adminmessage = "Print Quota exceeded for user %s on printer %s" % (username, self.printername) |
---|
87 | self.logger.log_message(adminmessage) |
---|
88 | self.sendMessageToUser(username, "Print Quota Exceeded", "You are not allowed to print anymore because your Print Quota is exceeded.") |
---|
89 | self.sendMessageToAdmin("Print Quota", adminmessage) |
---|
90 | elif action == "WARN" : |
---|
91 | adminmessage = "Print Quota soft limit exceeded for user %s on printer %s" % (username, self.printername) |
---|
92 | self.logger.log_message(adminmessage) |
---|
93 | self.sendMessageToUser(username, "Print Quota Exceeded", "You will soon be forbidden to print anymore because your Print Quota is almost reached.") |
---|
94 | self.sendMessageToAdmin("Print Quota", adminmessage) |
---|
95 | return action |
---|
96 | |
---|