[695] | 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$ |
---|
[699] | 17 | # Revision 1.3 2003/02/05 22:16:20 jalet |
---|
| 18 | # DEVICE_URI is undefined outside of CUPS, i.e. for normal command line tools |
---|
| 19 | # |
---|
[698] | 20 | # Revision 1.2 2003/02/05 22:10:29 jalet |
---|
| 21 | # Typos |
---|
| 22 | # |
---|
[695] | 23 | # Revision 1.1 2003/02/05 21:28:17 jalet |
---|
| 24 | # Initial import into CVS |
---|
| 25 | # |
---|
| 26 | # |
---|
| 27 | # |
---|
| 28 | |
---|
| 29 | import sys |
---|
| 30 | import os |
---|
| 31 | import smtplib |
---|
| 32 | |
---|
| 33 | from pykota import config |
---|
| 34 | from pykota import storage |
---|
| 35 | from pykota import logger |
---|
| 36 | |
---|
| 37 | class PyKotaToolError(Exception): |
---|
| 38 | """An exception for PyKota config related stuff.""" |
---|
| 39 | def __init__(self, message = ""): |
---|
| 40 | self.message = message |
---|
| 41 | Exception.__init__(self, message) |
---|
| 42 | def __repr__(self): |
---|
| 43 | return self.message |
---|
| 44 | __str__ = __repr__ |
---|
| 45 | |
---|
| 46 | class PyKotaTool : |
---|
| 47 | """Base class for all PyKota command line tools.""" |
---|
| 48 | def __init__(self, isfilter=0) : |
---|
| 49 | """Initializes the command line tool.""" |
---|
| 50 | self.config = config.PyKotaConfig(os.environ.get("CUPS_SERVERROOT", "/etc/cups")) |
---|
[698] | 51 | self.logger = logger.openLogger(self.config) |
---|
[695] | 52 | self.storage = storage.openConnection(self.config, asadmin=(not isfilter)) |
---|
| 53 | self.printername = os.environ.get("PRINTER", None) |
---|
| 54 | self.smtpserver = self.config.getSMTPServer() |
---|
| 55 | self.admin = self.config.getAdmin() |
---|
| 56 | self.adminmail = self.config.getAdminMail() |
---|
| 57 | |
---|
| 58 | def sendMessage(self, touser, fullmessage) : |
---|
| 59 | """Sends an email message containing headers to some user.""" |
---|
| 60 | fullmessage += "\n\nPlease contact your system administrator %s - <%s>\n" % (self.admin, self.adminmail) |
---|
| 61 | if "@" not in touser : |
---|
| 62 | touser = "%s@%s" % (touser, self.smtpserver) |
---|
| 63 | server = smtplib.SMTP(self.smtpserver) |
---|
| 64 | server.sendmail(self.adminmail, [touser], fullmessage) |
---|
| 65 | server.quit() |
---|
| 66 | |
---|
| 67 | def sendMessageToUser(self, username, subject, message) : |
---|
| 68 | """Sends an email message to a user.""" |
---|
| 69 | self.sendMessage(username, "Subject: %s\n\n%s" % (subject, message)) |
---|
| 70 | |
---|
| 71 | def sendMessageToAdmin(self, subject, message) : |
---|
| 72 | """Sends an email message to the Print Quota administrator.""" |
---|
| 73 | self.sendMessage(self.adminmail, "Subject: %s\n\n%s" % (subject, message)) |
---|
| 74 | |
---|
| 75 | def warnQuotaPrinter(self, username) : |
---|
| 76 | """Checks a user quota and send him a message if quota is exceeded on current printer.""" |
---|
| 77 | (action, grace, gracedate) = self.storage.checkUserPQuota(username, self.printername) |
---|
| 78 | if action == "DENY" : |
---|
| 79 | adminmessage = "Print Quota exceeded for user %s on printer %s" % (username, self.printername) |
---|
| 80 | self.logger.log_message(adminmessage) |
---|
| 81 | self.sendMessageToUser(username, "Print Quota Exceeded", "You are not allowed to print anymore because your Print Quota is exceeded.") |
---|
| 82 | self.sendMessageToAdmin("Print Quota", adminmessage) |
---|
| 83 | elif action == "WARN" : |
---|
| 84 | adminmessage = "Print Quota soft limit exceeded for user %s on printer %s" % (username, self.printername) |
---|
| 85 | self.logger.log_message(adminmessage) |
---|
| 86 | self.sendMessageToUser(username, "Print Quota Exceeded", "You will soon be forbidden to print anymore because your Print Quota is almost reached.") |
---|
| 87 | self.sendMessageToAdmin("Print Quota", adminmessage) |
---|
| 88 | return action |
---|
| 89 | |
---|