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

Revision 1192, 23.8 kB (checked in by jalet, 20 years ago)

Mailto can be any external command now, as usual.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1# PyKota
2# -*- coding: ISO-8859-15 -*-
3
4# PyKota - Print Quotas for CUPS and LPRng
5#
6# (c) 2003 Jerome Alet <alet@librelogiciel.com>
7# This program is free software; you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation; either version 2 of the License, or
10# (at your option) any later version.
11#
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with this program; if not, write to the Free Software
19# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
20#
21# $Id$
22#
23# $Log$
24# Revision 1.55  2003/11/18 23:43:12  jalet
25# Mailto can be any external command now, as usual.
26#
27# Revision 1.54  2003/10/24 21:52:46  jalet
28# Now can force language when coming from CGI script.
29#
30# Revision 1.53  2003/10/08 21:41:38  jalet
31# External policies for printers works !
32# We can now auto-add users on first print, and do other useful things if needed.
33#
34# Revision 1.52  2003/10/07 09:07:28  jalet
35# Character encoding added to please latest version of Python
36#
37# Revision 1.51  2003/10/06 14:21:41  jalet
38# Test reversed to not retrieve group members when no messages for them.
39#
40# Revision 1.50  2003/10/02 20:23:18  jalet
41# Storage caching mechanism added.
42#
43# Revision 1.49  2003/07/29 20:55:17  jalet
44# 1.14 is out !
45#
46# Revision 1.48  2003/07/21 23:01:56  jalet
47# Modified some messages aout soft limit
48#
49# Revision 1.47  2003/07/16 21:53:08  jalet
50# Really big modifications wrt new configuration file's location and content.
51#
52# Revision 1.46  2003/07/09 20:17:07  jalet
53# Email field added to PostgreSQL schema
54#
55# Revision 1.45  2003/07/08 19:43:51  jalet
56# Configurable warning messages.
57# Poor man's treshold value added.
58#
59# Revision 1.44  2003/07/07 11:49:24  jalet
60# Lots of small fixes with the help of PyChecker
61#
62# Revision 1.43  2003/07/04 09:06:32  jalet
63# Small bug fix wrt undefined "LimitBy" field.
64#
65# Revision 1.42  2003/06/30 12:46:15  jalet
66# Extracted reporting code.
67#
68# Revision 1.41  2003/06/25 14:10:01  jalet
69# Hey, it may work (edpykota --reset excepted) !
70#
71# Revision 1.40  2003/06/10 16:37:54  jalet
72# Deletion of the second user which is not needed anymore.
73# Added a debug configuration field in /etc/pykota.conf
74# All queries can now be sent to the logger in debug mode, this will
75# greatly help improve performance when time for this will come.
76#
77# Revision 1.39  2003/04/29 18:37:54  jalet
78# Pluggable accounting methods (actually doesn't support external scripts)
79#
80# Revision 1.38  2003/04/24 11:53:48  jalet
81# Default policy for unknown users/groups is to DENY printing instead
82# of the previous default to ALLOW printing. This is to solve an accuracy
83# problem. If you set the policy to ALLOW, jobs printed by in nexistant user
84# (from PyKota's POV) will be charged to the next user who prints on the
85# same printer.
86#
87# Revision 1.37  2003/04/24 08:08:27  jalet
88# Debug message forgotten
89#
90# Revision 1.36  2003/04/24 07:59:40  jalet
91# LPRng support now works !
92#
93# Revision 1.35  2003/04/23 22:13:57  jalet
94# Preliminary support for LPRng added BUT STILL UNTESTED.
95#
96# Revision 1.34  2003/04/17 09:26:21  jalet
97# repykota now reports account balances too.
98#
99# Revision 1.33  2003/04/16 12:35:49  jalet
100# Groups quota work now !
101#
102# Revision 1.32  2003/04/16 08:53:14  jalet
103# Printing can now be limited either by user's account balance or by
104# page quota (the default). Quota report doesn't include account balance
105# yet, though.
106#
107# Revision 1.31  2003/04/15 11:30:57  jalet
108# More work done on money print charging.
109# Minor bugs corrected.
110# All tools now access to the storage as priviledged users, repykota excepted.
111#
112# Revision 1.30  2003/04/10 21:47:20  jalet
113# Job history added. Upgrade script neutralized for now !
114#
115# Revision 1.29  2003/03/29 13:45:27  jalet
116# GPL paragraphs were incorrectly (from memory) copied into the sources.
117# Two README files were added.
118# Upgrade script for PostgreSQL pre 1.01 schema was added.
119#
120# Revision 1.28  2003/03/29 13:08:28  jalet
121# Configuration is now expected to be found in /etc/pykota.conf instead of
122# in /etc/cups/pykota.conf
123# Installation script can move old config files to the new location if needed.
124# Better error handling if configuration file is absent.
125#
126# Revision 1.27  2003/03/15 23:01:28  jalet
127# New mailto option in configuration file added.
128# No time to test this tonight (although it should work).
129#
130# Revision 1.26  2003/03/09 23:58:16  jalet
131# Comment
132#
133# Revision 1.25  2003/03/07 22:56:14  jalet
134# 0.99 is out with some bug fixes.
135#
136# Revision 1.24  2003/02/27 23:48:41  jalet
137# Correctly maps PyKota's log levels to syslog log levels
138#
139# Revision 1.23  2003/02/27 22:55:20  jalet
140# WARN log priority doesn't exist.
141#
142# Revision 1.22  2003/02/27 09:09:20  jalet
143# Added a method to match strings against wildcard patterns
144#
145# Revision 1.21  2003/02/17 23:01:56  jalet
146# Typos
147#
148# Revision 1.20  2003/02/17 22:55:01  jalet
149# More options can now be set per printer or globally :
150#
151#       admin
152#       adminmail
153#       gracedelay
154#       requester
155#
156# the printer option has priority when both are defined.
157#
158# Revision 1.19  2003/02/10 11:28:45  jalet
159# Localization
160#
161# Revision 1.18  2003/02/10 01:02:17  jalet
162# External requester is about to work, but I must sleep
163#
164# Revision 1.17  2003/02/09 13:05:43  jalet
165# Internationalization continues...
166#
167# Revision 1.16  2003/02/09 12:56:53  jalet
168# Internationalization begins...
169#
170# Revision 1.15  2003/02/08 22:09:52  jalet
171# Name check method moved here
172#
173# Revision 1.14  2003/02/07 10:42:45  jalet
174# Indentation problem
175#
176# Revision 1.13  2003/02/07 08:34:16  jalet
177# Test wrt date limit was wrong
178#
179# Revision 1.12  2003/02/06 23:20:02  jalet
180# warnpykota doesn't need any user/group name argument, mimicing the
181# warnquota disk quota tool.
182#
183# Revision 1.11  2003/02/06 22:54:33  jalet
184# warnpykota should be ok
185#
186# Revision 1.10  2003/02/06 15:03:11  jalet
187# added a method to set the limit date
188#
189# Revision 1.9  2003/02/06 10:39:23  jalet
190# Preliminary edpykota work.
191#
192# Revision 1.8  2003/02/06 09:19:02  jalet
193# More robust behavior (hopefully) when the user or printer is not managed
194# correctly by the Quota System : e.g. cupsFilter added in ppd file, but
195# printer and/or user not 'yet?' in storage.
196#
197# Revision 1.7  2003/02/06 00:00:45  jalet
198# Now includes the printer name in email messages
199#
200# Revision 1.6  2003/02/05 23:55:02  jalet
201# Cleaner email messages
202#
203# Revision 1.5  2003/02/05 23:45:09  jalet
204# Better DateTime manipulation wrt grace delay
205#
206# Revision 1.4  2003/02/05 23:26:22  jalet
207# Incorrect handling of grace delay
208#
209# Revision 1.3  2003/02/05 22:16:20  jalet
210# DEVICE_URI is undefined outside of CUPS, i.e. for normal command line tools
211#
212# Revision 1.2  2003/02/05 22:10:29  jalet
213# Typos
214#
215# Revision 1.1  2003/02/05 21:28:17  jalet
216# Initial import into CVS
217#
218#
219#
220
221import sys
222import fnmatch
223import getopt
224import smtplib
225import gettext
226import locale
227
228from mx import DateTime
229
230from pykota import version, config, storage, logger
231
232class PyKotaToolError(Exception):
233    """An exception for PyKota config related stuff."""
234    def __init__(self, message = ""):
235        self.message = message
236        Exception.__init__(self, message)
237    def __repr__(self):
238        return self.message
239    __str__ = __repr__
240   
241class PyKotaTool :   
242    """Base class for all PyKota command line tools."""
243    def __init__(self, lang=None, doc="PyKota %s (c) 2003 %s" % (version.__version__, version.__author__)) :
244        """Initializes the command line tool."""
245        # locale stuff
246        try :
247            locale.setlocale(locale.LC_ALL, lang)
248            gettext.install("pykota")
249        except (locale.Error, IOError) :
250            gettext.NullTranslations().install()
251            sys.stderr.write("PyKota : Error while loading translations\n")
252   
253        # pykota specific stuff
254        self.documentation = doc
255        self.config = config.PyKotaConfig("/etc/pykota")
256        self.logger = logger.openLogger(self)
257        self.debug = self.config.getDebug()
258        self.storage = storage.openConnection(self)
259        self.smtpserver = self.config.getSMTPServer()
260       
261    def logdebug(self, message) :   
262        """Logs something to debug output if debug is enabled."""
263        if self.debug :
264            self.logger.log_message(message, "debug")
265       
266    def clean(self) :   
267        """Ensures that the database is closed."""
268        try :
269            self.storage.close()
270        except (TypeError, NameError, AttributeError) :   
271            pass
272           
273    def display_version_and_quit(self) :
274        """Displays version number, then exists successfully."""
275        self.clean()
276        print version.__version__
277        sys.exit(0)
278   
279    def display_usage_and_quit(self) :
280        """Displays command line usage, then exists successfully."""
281        self.clean()
282        print self.documentation
283        sys.exit(0)
284       
285    def parseCommandline(self, argv, short, long, allownothing=0) :
286        """Parses the command line, controlling options."""
287        # split options in two lists: those which need an argument, those which don't need any
288        withoutarg = []
289        witharg = []
290        lgs = len(short)
291        i = 0
292        while i < lgs :
293            ii = i + 1
294            if (ii < lgs) and (short[ii] == ':') :
295                # needs an argument
296                witharg.append(short[i])
297                ii = ii + 1 # skip the ':'
298            else :
299                # doesn't need an argument
300                withoutarg.append(short[i])
301            i = ii
302               
303        for option in long :
304            if option[-1] == '=' :
305                # needs an argument
306                witharg.append(option[:-1])
307            else :
308                # doesn't need an argument
309                withoutarg.append(option)
310       
311        # we begin with all possible options unset
312        parsed = {}
313        for option in withoutarg + witharg :
314            parsed[option] = None
315       
316        # then we parse the command line
317        args = []       # to not break if something unexpected happened
318        try :
319            options, args = getopt.getopt(argv, short, long)
320            if options :
321                for (o, v) in options :
322                    # we skip the '-' chars
323                    lgo = len(o)
324                    i = 0
325                    while (i < lgo) and (o[i] == '-') :
326                        i = i + 1
327                    o = o[i:]
328                    if o in witharg :
329                        # needs an argument : set it
330                        parsed[o] = v
331                    elif o in withoutarg :
332                        # doesn't need an argument : boolean
333                        parsed[o] = 1
334                    else :
335                        # should never occur
336                        raise PyKotaToolError, "Unexpected problem when parsing command line"
337            elif (not args) and (not allownothing) and sys.stdin.isatty() : # no option and no argument, we display help if we are a tty
338                self.display_usage_and_quit()
339        except getopt.error, msg :
340            sys.stderr.write("%s\n" % msg)
341            sys.stderr.flush()
342            self.display_usage_and_quit()
343        return (parsed, args)
344   
345    def isValidName(self, name) :
346        """Checks if a user or printer name is valid."""
347        # unfortunately Python 2.1 string modules doesn't define ascii_letters...
348        asciiletters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
349        digits = '0123456789'
350        if name[0] in asciiletters :
351            validchars = asciiletters + digits + "-_"
352            for c in name[1:] :
353                if c not in validchars :
354                    return 0
355            return 1       
356        return 0
357       
358    def matchString(self, s, patterns) :
359        """Returns 1 if the string s matches one of the patterns, else 0."""
360        for pattern in patterns :
361            if fnmatch.fnmatchcase(s, pattern) :
362                return 1
363        return 0
364       
365    def sendMessage(self, adminmail, touser, fullmessage) :
366        """Sends an email message containing headers to some user."""
367        if "@" not in touser :
368            touser = "%s@%s" % (touser, self.smtpserver)
369        server = smtplib.SMTP(self.smtpserver)
370        try :
371            server.sendmail(adminmail, [touser], fullmessage)
372        except smtplib.SMTPRecipientsRefused, answer :   
373            for (k, v) in answer.recipients.items() :
374                self.logger.log_message(_("Impossible to send mail to %s, error %s : %s") % (k, v[0], v[1]), "error")
375        server.quit()
376       
377    def sendMessageToUser(self, admin, adminmail, user, subject, message) :
378        """Sends an email message to a user."""
379        message += _("\n\nPlease contact your system administrator :\n\n\t%s - <%s>\n") % (admin, adminmail)
380        self.sendMessage(adminmail, user.Email or user.Name, "Subject: %s\n\n%s" % (subject, message))
381       
382    def sendMessageToAdmin(self, adminmail, subject, message) :
383        """Sends an email message to the Print Quota administrator."""
384        self.sendMessage(adminmail, adminmail, "Subject: %s\n\n%s" % (subject, message))
385       
386    def checkGroupPQuota(self, grouppquota) :   
387        """Checks the group quota on a printer and deny or accept the job."""
388        group = grouppquota.Group
389        printer = grouppquota.Printer
390        if group.LimitBy and (group.LimitBy.lower() == "balance") : 
391            if group.AccountBalance <= 0.0 :
392                action = "DENY"
393            elif group.AccountBalance <= self.config.getPoorMan() :   
394                action = "WARN"
395            else :   
396                action = "ALLOW"
397        else :
398            if grouppquota.SoftLimit is not None :
399                softlimit = int(grouppquota.SoftLimit)
400                if grouppquota.PageCounter < softlimit :
401                    action = "ALLOW"
402                else :   
403                    if grouppquota.HardLimit is None :
404                        # only a soft limit, this is equivalent to having only a hard limit
405                        action = "DENY"
406                    else :   
407                        hardlimit = int(grouppquota.HardLimit)
408                        if softlimit <= grouppquota.PageCounter < hardlimit :   
409                            now = DateTime.now()
410                            if grouppquota.DateLimit is not None :
411                                datelimit = DateTime.ISO.ParseDateTime(grouppquota.DateLimit)
412                            else :
413                                datelimit = now + self.config.getGraceDelay(printer.Name)
414                                grouppquota.setDateLimit(datelimit)
415                            if now < datelimit :
416                                action = "WARN"
417                            else :   
418                                action = "DENY"
419                        else :         
420                            action = "DENY"
421            else :       
422                if grouppquota.HardLimit is not None :
423                    # no soft limit, only a hard one.
424                    hardlimit = int(grouppquota.HardLimit)
425                    if grouppquota.PageCounter < hardlimit :
426                        action = "ALLOW"
427                    else :     
428                        action = "DENY"
429                else :
430                    # Both are unset, no quota, i.e. accounting only
431                    action = "ALLOW"
432        return action
433   
434    def checkUserPQuota(self, userpquota) :
435        """Checks the user quota on a printer and deny or accept the job."""
436        user = userpquota.User
437        printer = userpquota.Printer
438       
439        # first we check any group the user is a member of
440        for group in self.storage.getUserGroups(user) :
441            grouppquota = self.storage.getGroupPQuota(group, printer)
442            if grouppquota.Exists :
443                action = self.checkGroupPQuota(grouppquota)
444                if action == "DENY" :
445                    return action
446               
447        # then we check the user's own quota
448        # if we get there we are sure that policy is not EXTERNAL
449        (policy, dummy) = self.config.getPrinterPolicy(printer.Name)
450        if user.LimitBy and (user.LimitBy.lower() == "balance") : 
451            if user.AccountBalance is None :
452                if policy == "ALLOW" :
453                    action = "POLICY_ALLOW"
454                else :   
455                    action = "POLICY_DENY"
456                self.logger.log_message(_("Unable to find user %s's account balance, applying default policy (%s) for printer %s") % (user.Name, action, printer.Name))
457            else :   
458                val = float(user.AccountBalance or 0.0)
459                if val <= 0.0 :
460                    action = "DENY"
461                elif val <= self.config.getPoorMan() :   
462                    action = "WARN"
463                else :   
464                    action = "ALLOW"
465        else :
466            if not userpquota.Exists :
467                # Unknown userquota
468                if policy == "ALLOW" :
469                    action = "POLICY_ALLOW"
470                else :   
471                    action = "POLICY_DENY"
472                self.logger.log_message(_("Unable to match user %s on printer %s, applying default policy (%s)") % (user.Name, printer.Name, action))
473            else :   
474                pagecounter = int(userpquota.PageCounter or 0)
475                if userpquota.SoftLimit is not None :
476                    softlimit = int(userpquota.SoftLimit)
477                    if pagecounter < softlimit :
478                        action = "ALLOW"
479                    else :   
480                        if userpquota.HardLimit is None :
481                            # only a soft limit, this is equivalent to having only a hard limit
482                            action = "DENY"
483                        else :   
484                            hardlimit = int(userpquota.HardLimit)
485                            if softlimit <= pagecounter < hardlimit :   
486                                now = DateTime.now()
487                                if userpquota.DateLimit is not None :
488                                    datelimit = DateTime.ISO.ParseDateTime(userpquota.DateLimit)
489                                else :
490                                    datelimit = now + self.config.getGraceDelay(printer.Name)
491                                    userpquota.setDateLimit(datelimit)
492                                if now < datelimit :
493                                    action = "WARN"
494                                else :   
495                                    action = "DENY"
496                            else :         
497                                action = "DENY"
498                else :       
499                    if userpquota.HardLimit is not None :
500                        # no soft limit, only a hard one.
501                        hardlimit = int(userpquota.HardLimit)
502                        if pagecounter < hardlimit :
503                            action = "ALLOW"
504                        else :     
505                            action = "DENY"
506                    else :
507                        # Both are unset, no quota, i.e. accounting only
508                        action = "ALLOW"
509        return action
510   
511    def externalMailTo(self, cmd, action, user, printername, message) :
512        """Warns the user with an external command."""
513        username = user.Name
514        email = user.Email or user.Name
515        if "@" not in email :
516            email = "%s@%s" % (email, self.smtpserver)
517        os.system(cmd % locals())
518   
519    def warnGroupPQuota(self, grouppquota) :
520        """Checks a group quota and send messages if quota is exceeded on current printer."""
521        group = grouppquota.Group
522        printer = grouppquota.Printer
523        admin = self.config.getAdmin(printer.Name)
524        adminmail = self.config.getAdminMail(printer.Name)
525        (mailto, arguments) = self.config.getMailTo(printer.Name)
526        action = self.checkGroupPQuota(grouppquota)
527        if action.startswith("POLICY_") :
528            action = action[7:]
529        if action == "DENY" :
530            adminmessage = _("Print Quota exceeded for group %s on printer %s") % (group.Name, printer.Name)
531            self.logger.log_message(adminmessage)
532            if mailto in [ "BOTH", "ADMIN" ] :
533                self.sendMessageToAdmin(adminmail, _("Print Quota"), adminmessage)
534            if mailto in [ "BOTH", "USER", "EXTERNAL" ] :
535                for user in self.storage.getGroupMembers(group) :
536                    if mailto != "EXTERNAL" :
537                        self.sendMessageToUser(admin, adminmail, user, _("Print Quota Exceeded"), self.config.getHardWarn(printer.Name))
538                    else :   
539                        self.externalMailTo(arguments, action, user, printer.Name, message)
540        elif action == "WARN" :   
541            adminmessage = _("Print Quota low for group %s on printer %s") % (group.Name, printer.Name)
542            self.logger.log_message(adminmessage)
543            if mailto in [ "BOTH", "ADMIN" ] :
544                self.sendMessageToAdmin(adminmail, _("Print Quota"), adminmessage)
545            if group.LimitBy and (group.LimitBy.lower() == "balance") : 
546                message = self.config.getPoorWarn()
547            else :     
548                message = self.config.getSoftWarn(printer.Name)
549            if mailto in [ "BOTH", "USER", "EXTERNAL" ] :
550                for user in self.storage.getGroupMembers(group) :
551                    if mailto != "EXTERNAL" :
552                        self.sendMessageToUser(admin, adminmail, user, _("Print Quota Exceeded"), message)
553                    else :   
554                        self.externalMailTo(arguments, action, user, printer.Name, message)
555        return action       
556       
557    def warnUserPQuota(self, userpquota) :
558        """Checks a user quota and send him a message if quota is exceeded on current printer."""
559        user = userpquota.User
560        printer = userpquota.Printer
561        admin = self.config.getAdmin(printer.Name)
562        adminmail = self.config.getAdminMail(printer.Name)
563        (mailto, arguments) = self.config.getMailTo(printer.Name)
564        action = self.checkUserPQuota(userpquota)
565        if action.startswith("POLICY_") :
566            action = action[7:]
567        if action == "DENY" :
568            adminmessage = _("Print Quota exceeded for user %s on printer %s") % (user.Name, printer.Name)
569            self.logger.log_message(adminmessage)
570            if mailto in [ "BOTH", "USER", "EXTERNAL" ] :
571                message = self.config.getHardWarn(printer.Name)
572                if mailto != "EXTERNAL" :
573                    self.sendMessageToUser(admin, adminmail, user, _("Print Quota Exceeded"), message)
574                else :   
575                    self.externalMailTo(arguments, action, user, printer.Name, message)
576            if mailto in [ "BOTH", "ADMIN" ] :
577                self.sendMessageToAdmin(adminmail, _("Print Quota"), adminmessage)
578        elif action == "WARN" :   
579            adminmessage = _("Print Quota low for user %s on printer %s") % (user.Name, printer.Name)
580            self.logger.log_message(adminmessage)
581            if mailto in [ "BOTH", "USER", "EXTERNAL" ] :
582                if user.LimitBy and (user.LimitBy.lower() == "balance") : 
583                    message = self.config.getPoorWarn()
584                else :     
585                    message = self.config.getSoftWarn(printer.Name)
586                if mailto != "EXTERNAL" :   
587                    self.sendMessageToUser(admin, adminmail, user, _("Print Quota Low"), message)
588                else :   
589                    self.externalMailTo(arguments, action, user, printer.Name, message)
590            if mailto in [ "BOTH", "ADMIN" ] :
591                self.sendMessageToAdmin(adminmail, _("Print Quota"), adminmessage)
592        return action       
Note: See TracBrowser for help on using the browser.