[1144] | 1 | # PyKota |
---|
| 2 | # -*- coding: ISO-8859-15 -*- |
---|
[695] | 3 | |
---|
[952] | 4 | # PyKota - Print Quotas for CUPS and LPRng |
---|
[695] | 5 | # |
---|
[1257] | 6 | # (c) 2003-2004 Jerome Alet <alet@librelogiciel.com> |
---|
[873] | 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. |
---|
[695] | 11 | # |
---|
[873] | 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. |
---|
[695] | 20 | # |
---|
| 21 | # $Id$ |
---|
| 22 | # |
---|
| 23 | # $Log$ |
---|
[1695] | 24 | # Revision 1.120 2004/09/02 13:26:29 jalet |
---|
| 25 | # Small fix for old versions of LPRng |
---|
| 26 | # |
---|
[1694] | 27 | # Revision 1.119 2004/09/02 13:09:58 jalet |
---|
| 28 | # Now exports PYKOTAPRINTERHOSTNAME |
---|
| 29 | # |
---|
[1666] | 30 | # Revision 1.118 2004/08/06 20:46:45 jalet |
---|
| 31 | # Finished group quota fix for balance when no user in group has a balance |
---|
| 32 | # |
---|
[1665] | 33 | # Revision 1.117 2004/08/06 13:45:51 jalet |
---|
| 34 | # Fixed french translation problem. |
---|
| 35 | # Fixed problem with group quotas and strict enforcement. |
---|
| 36 | # |
---|
[1637] | 37 | # Revision 1.116 2004/07/24 20:20:29 jalet |
---|
| 38 | # Unitialized variable |
---|
| 39 | # |
---|
[1611] | 40 | # Revision 1.115 2004/07/21 09:35:48 jalet |
---|
| 41 | # Software accounting seems to be OK with LPRng support now |
---|
| 42 | # |
---|
[1606] | 43 | # Revision 1.114 2004/07/20 22:19:45 jalet |
---|
| 44 | # Sanitized a bit + use of gettext |
---|
| 45 | # |
---|
[1601] | 46 | # Revision 1.113 2004/07/17 20:37:27 jalet |
---|
| 47 | # Missing file... Am I really stupid ? |
---|
| 48 | # |
---|
[1600] | 49 | # Revision 1.112 2004/07/16 12:22:47 jalet |
---|
| 50 | # LPRng support early version |
---|
| 51 | # |
---|
[1593] | 52 | # Revision 1.111 2004/07/06 18:09:42 jalet |
---|
| 53 | # Reduced the set of invalid characters in names |
---|
| 54 | # |
---|
[1584] | 55 | # Revision 1.110 2004/07/01 19:56:42 jalet |
---|
| 56 | # Better dispatching of error messages |
---|
| 57 | # |
---|
[1582] | 58 | # Revision 1.109 2004/07/01 17:45:49 jalet |
---|
| 59 | # Added code to handle the description field for printers |
---|
| 60 | # |
---|
[1565] | 61 | # Revision 1.108 2004/06/24 23:09:30 jalet |
---|
| 62 | # Also prints read size on last block |
---|
| 63 | # |
---|
[1563] | 64 | # Revision 1.107 2004/06/23 13:03:28 jalet |
---|
| 65 | # Catches accounter configuration errors earlier |
---|
| 66 | # |
---|
[1562] | 67 | # Revision 1.106 2004/06/22 09:31:18 jalet |
---|
| 68 | # Always send some debug info to CUPS' back channel stream (stderr) as |
---|
| 69 | # informationnal messages. |
---|
| 70 | # |
---|
[1560] | 71 | # Revision 1.105 2004/06/21 08:17:38 jalet |
---|
| 72 | # Added version number in subject message for directive crashrecipient. |
---|
| 73 | # |
---|
[1546] | 74 | # Revision 1.104 2004/06/18 13:34:49 jalet |
---|
| 75 | # Now all tracebacks include PyKota's version number |
---|
| 76 | # |
---|
[1545] | 77 | # Revision 1.103 2004/06/18 13:17:26 jalet |
---|
| 78 | # Now includes PyKota's version number in messages sent by the crashrecipient |
---|
| 79 | # directive. |
---|
| 80 | # |
---|
[1542] | 81 | # Revision 1.102 2004/06/17 13:26:51 jalet |
---|
| 82 | # Better exception handling code |
---|
| 83 | # |
---|
[1541] | 84 | # Revision 1.101 2004/06/16 20:56:34 jalet |
---|
| 85 | # Smarter initialisation code |
---|
| 86 | # |
---|
[1538] | 87 | # Revision 1.100 2004/06/11 08:16:03 jalet |
---|
| 88 | # More exceptions catched in case of very early failure. |
---|
| 89 | # |
---|
[1537] | 90 | # Revision 1.99 2004/06/11 07:07:38 jalet |
---|
| 91 | # Now detects and logs configuration syntax errors instead of failing without |
---|
| 92 | # any notice message. |
---|
| 93 | # |
---|
[1532] | 94 | # Revision 1.98 2004/06/08 19:27:12 jalet |
---|
| 95 | # Doesn't ignore SIGCHLD anymore |
---|
| 96 | # |
---|
[1529] | 97 | # Revision 1.97 2004/06/07 22:45:35 jalet |
---|
| 98 | # Now accepts a job when enforcement is STRICT and predicted account balance |
---|
| 99 | # is equal to 0.0 : since the job hasn't been printed yet, only its printing |
---|
| 100 | # will really render balance equal to 0.0, so we should be allowed to print. |
---|
| 101 | # |
---|
[1523] | 102 | # Revision 1.96 2004/06/05 22:18:04 jalet |
---|
| 103 | # Now catches some exceptions earlier. |
---|
| 104 | # storage.py and ldapstorage.py : removed old comments |
---|
| 105 | # |
---|
[1517] | 106 | # Revision 1.95 2004/06/03 21:50:34 jalet |
---|
| 107 | # Improved error logging. |
---|
| 108 | # crashrecipient directive added. |
---|
| 109 | # Now exports the job's size in bytes too. |
---|
| 110 | # |
---|
[1516] | 111 | # Revision 1.94 2004/06/03 08:51:03 jalet |
---|
| 112 | # logs job's size in bytes now |
---|
| 113 | # |
---|
[1514] | 114 | # Revision 1.93 2004/06/02 21:51:02 jalet |
---|
| 115 | # Moved the sigterm capturing elsewhere |
---|
| 116 | # |
---|
[1512] | 117 | # Revision 1.92 2004/06/02 13:21:38 jalet |
---|
| 118 | # Debug message added |
---|
| 119 | # |
---|
[1497] | 120 | # Revision 1.91 2004/05/25 05:17:52 jalet |
---|
| 121 | # Now precomputes the job's size only if current printer's enforcement |
---|
| 122 | # is "STRICT" |
---|
| 123 | # |
---|
[1495] | 124 | # Revision 1.90 2004/05/24 22:45:49 jalet |
---|
| 125 | # New 'enforcement' directive added |
---|
| 126 | # Polling loop improvements |
---|
| 127 | # |
---|
[1492] | 128 | # Revision 1.89 2004/05/21 22:02:52 jalet |
---|
| 129 | # Preliminary work on pre-accounting |
---|
| 130 | # |
---|
[1483] | 131 | # Revision 1.88 2004/05/18 14:49:20 jalet |
---|
| 132 | # Big code changes to completely remove the need for "requester" directives, |
---|
| 133 | # jsut use "hardware(... your previous requester directive's content ...)" |
---|
| 134 | # |
---|
[1481] | 135 | # Revision 1.87 2004/05/17 19:14:59 jalet |
---|
| 136 | # Now catches SIGPIPE and SIGCHLD |
---|
| 137 | # |
---|
[1475] | 138 | # Revision 1.86 2004/05/13 13:59:28 jalet |
---|
| 139 | # Code simplifications |
---|
| 140 | # |
---|
[1469] | 141 | # Revision 1.85 2004/05/11 08:26:27 jalet |
---|
| 142 | # Now catches connection problems to SMTP server |
---|
| 143 | # |
---|
[1443] | 144 | # Revision 1.84 2004/04/21 08:36:32 jalet |
---|
| 145 | # Exports the PYKOTASTATUS environment variable when SIGTERM is received. |
---|
| 146 | # |
---|
[1439] | 147 | # Revision 1.83 2004/04/16 17:03:49 jalet |
---|
| 148 | # The list of printers groups the current printer is a member of is |
---|
| 149 | # now exported in the PYKOTAPGROUPS environment variable |
---|
| 150 | # |
---|
[1434] | 151 | # Revision 1.82 2004/04/13 09:38:03 jalet |
---|
| 152 | # More work on correct child processes handling |
---|
| 153 | # |
---|
[1433] | 154 | # Revision 1.81 2004/04/09 22:24:47 jalet |
---|
| 155 | # Began work on correct handling of child processes when jobs are cancelled by |
---|
| 156 | # the user. Especially important when an external requester is running for a |
---|
| 157 | # long time. |
---|
| 158 | # |
---|
[1426] | 159 | # Revision 1.80 2004/04/06 12:00:21 jalet |
---|
| 160 | # uninitialized values caused problems |
---|
| 161 | # |
---|
[1421] | 162 | # Revision 1.79 2004/03/28 21:01:29 jalet |
---|
| 163 | # PYKOTALIMITBY environment variable is now exported too |
---|
| 164 | # |
---|
[1394] | 165 | # Revision 1.78 2004/03/08 20:13:25 jalet |
---|
| 166 | # Allow names to begin with a digit |
---|
| 167 | # |
---|
[1384] | 168 | # Revision 1.77 2004/03/03 13:10:35 jalet |
---|
| 169 | # Now catches all smtplib exceptions when there's a problem sending messages |
---|
| 170 | # |
---|
[1374] | 171 | # Revision 1.76 2004/03/01 14:34:15 jalet |
---|
| 172 | # PYKOTAPHASE wasn't set at the right time at the end of data transmission |
---|
| 173 | # to underlying layer (real backend) |
---|
| 174 | # |
---|
[1372] | 175 | # Revision 1.75 2004/03/01 11:23:25 jalet |
---|
| 176 | # Pre and Post hooks to external commands are available in the cupspykota |
---|
| 177 | # backend. Forthe pykota filter they will be implemented real soon now. |
---|
| 178 | # |
---|
[1365] | 179 | # Revision 1.74 2004/02/26 14:18:07 jalet |
---|
| 180 | # Should fix the remaining bugs wrt printers groups and users groups. |
---|
| 181 | # |
---|
[1353] | 182 | # Revision 1.73 2004/02/19 14:20:21 jalet |
---|
| 183 | # maildomain pykota.conf directive added. |
---|
| 184 | # Small improvements on mail headers quality. |
---|
| 185 | # |
---|
[1290] | 186 | # Revision 1.72 2004/01/14 15:51:19 jalet |
---|
| 187 | # Docstring added. |
---|
| 188 | # |
---|
[1271] | 189 | # Revision 1.71 2004/01/11 23:22:42 jalet |
---|
| 190 | # Major code refactoring, it's way cleaner, and now allows automated addition |
---|
| 191 | # of printers on first print. |
---|
| 192 | # |
---|
[1257] | 193 | # Revision 1.70 2004/01/08 14:10:32 jalet |
---|
| 194 | # Copyright year changed. |
---|
| 195 | # |
---|
[1248] | 196 | # Revision 1.69 2004/01/05 16:02:18 jalet |
---|
| 197 | # Dots in user, groups and printer names should be allowed. |
---|
| 198 | # |
---|
[1247] | 199 | # Revision 1.68 2004/01/02 17:38:40 jalet |
---|
| 200 | # This time it should be better... |
---|
| 201 | # |
---|
[1246] | 202 | # Revision 1.67 2004/01/02 17:37:09 jalet |
---|
| 203 | # I'm completely stupid !!! Better to not talk while coding ! |
---|
| 204 | # |
---|
[1245] | 205 | # Revision 1.66 2004/01/02 17:31:26 jalet |
---|
| 206 | # Forgot to remove some code |
---|
[1240] | 207 | # |
---|
| 208 | # Revision 1.65 2003/12/06 08:14:38 jalet |
---|
| 209 | # Added support for CUPS device uris which contain authentication information. |
---|
| 210 | # |
---|
[1229] | 211 | # Revision 1.64 2003/11/29 22:03:17 jalet |
---|
| 212 | # Some code refactoring work. New code is not used at this time. |
---|
| 213 | # |
---|
[1227] | 214 | # Revision 1.63 2003/11/29 20:06:20 jalet |
---|
| 215 | # Added 'utolower' configuration option to convert all usernames to |
---|
| 216 | # lowercase when printing. All database accesses are still and will |
---|
| 217 | # remain case sensitive though. |
---|
| 218 | # |
---|
[1218] | 219 | # Revision 1.62 2003/11/25 22:37:22 jalet |
---|
| 220 | # Small code move |
---|
| 221 | # |
---|
[1217] | 222 | # Revision 1.61 2003/11/25 22:03:28 jalet |
---|
| 223 | # No more message on stderr when the translation is not available. |
---|
| 224 | # |
---|
[1216] | 225 | # Revision 1.60 2003/11/25 21:54:05 jalet |
---|
| 226 | # updated FAQ |
---|
| 227 | # |
---|
[1211] | 228 | # Revision 1.59 2003/11/25 13:33:43 jalet |
---|
| 229 | # Puts 'root' instead of '' when printing from CUPS web interface (which |
---|
| 230 | # gives an empty username) |
---|
| 231 | # |
---|
[1200] | 232 | # Revision 1.58 2003/11/21 14:28:45 jalet |
---|
| 233 | # More complete job history. |
---|
| 234 | # |
---|
[1196] | 235 | # Revision 1.57 2003/11/19 23:19:38 jalet |
---|
| 236 | # Code refactoring work. |
---|
| 237 | # Explicit redirection to /dev/null has to be set in external policy now, just |
---|
| 238 | # like in external mailto. |
---|
| 239 | # |
---|
[1193] | 240 | # Revision 1.56 2003/11/19 07:40:20 jalet |
---|
| 241 | # Missing import statement. |
---|
| 242 | # Better documentation for mailto: external(...) |
---|
| 243 | # |
---|
[1192] | 244 | # Revision 1.55 2003/11/18 23:43:12 jalet |
---|
| 245 | # Mailto can be any external command now, as usual. |
---|
| 246 | # |
---|
[1171] | 247 | # Revision 1.54 2003/10/24 21:52:46 jalet |
---|
| 248 | # Now can force language when coming from CGI script. |
---|
| 249 | # |
---|
[1152] | 250 | # Revision 1.53 2003/10/08 21:41:38 jalet |
---|
| 251 | # External policies for printers works ! |
---|
| 252 | # We can now auto-add users on first print, and do other useful things if needed. |
---|
| 253 | # |
---|
[1144] | 254 | # Revision 1.52 2003/10/07 09:07:28 jalet |
---|
| 255 | # Character encoding added to please latest version of Python |
---|
| 256 | # |
---|
[1140] | 257 | # Revision 1.51 2003/10/06 14:21:41 jalet |
---|
| 258 | # Test reversed to not retrieve group members when no messages for them. |
---|
| 259 | # |
---|
[1130] | 260 | # Revision 1.50 2003/10/02 20:23:18 jalet |
---|
| 261 | # Storage caching mechanism added. |
---|
| 262 | # |
---|
[1113] | 263 | # Revision 1.49 2003/07/29 20:55:17 jalet |
---|
| 264 | # 1.14 is out ! |
---|
| 265 | # |
---|
[1091] | 266 | # Revision 1.48 2003/07/21 23:01:56 jalet |
---|
| 267 | # Modified some messages aout soft limit |
---|
| 268 | # |
---|
[1087] | 269 | # Revision 1.47 2003/07/16 21:53:08 jalet |
---|
| 270 | # Really big modifications wrt new configuration file's location and content. |
---|
| 271 | # |
---|
[1079] | 272 | # Revision 1.46 2003/07/09 20:17:07 jalet |
---|
| 273 | # Email field added to PostgreSQL schema |
---|
| 274 | # |
---|
[1077] | 275 | # Revision 1.45 2003/07/08 19:43:51 jalet |
---|
| 276 | # Configurable warning messages. |
---|
| 277 | # Poor man's treshold value added. |
---|
| 278 | # |
---|
[1068] | 279 | # Revision 1.44 2003/07/07 11:49:24 jalet |
---|
| 280 | # Lots of small fixes with the help of PyChecker |
---|
| 281 | # |
---|
[1061] | 282 | # Revision 1.43 2003/07/04 09:06:32 jalet |
---|
| 283 | # Small bug fix wrt undefined "LimitBy" field. |
---|
| 284 | # |
---|
[1048] | 285 | # Revision 1.42 2003/06/30 12:46:15 jalet |
---|
| 286 | # Extracted reporting code. |
---|
| 287 | # |
---|
[1041] | 288 | # Revision 1.41 2003/06/25 14:10:01 jalet |
---|
| 289 | # Hey, it may work (edpykota --reset excepted) ! |
---|
| 290 | # |
---|
[1021] | 291 | # Revision 1.40 2003/06/10 16:37:54 jalet |
---|
| 292 | # Deletion of the second user which is not needed anymore. |
---|
| 293 | # Added a debug configuration field in /etc/pykota.conf |
---|
| 294 | # All queries can now be sent to the logger in debug mode, this will |
---|
| 295 | # greatly help improve performance when time for this will come. |
---|
| 296 | # |
---|
[973] | 297 | # Revision 1.39 2003/04/29 18:37:54 jalet |
---|
| 298 | # Pluggable accounting methods (actually doesn't support external scripts) |
---|
| 299 | # |
---|
[956] | 300 | # Revision 1.38 2003/04/24 11:53:48 jalet |
---|
| 301 | # Default policy for unknown users/groups is to DENY printing instead |
---|
| 302 | # of the previous default to ALLOW printing. This is to solve an accuracy |
---|
| 303 | # problem. If you set the policy to ALLOW, jobs printed by in nexistant user |
---|
| 304 | # (from PyKota's POV) will be charged to the next user who prints on the |
---|
| 305 | # same printer. |
---|
| 306 | # |
---|
[955] | 307 | # Revision 1.37 2003/04/24 08:08:27 jalet |
---|
| 308 | # Debug message forgotten |
---|
| 309 | # |
---|
[954] | 310 | # Revision 1.36 2003/04/24 07:59:40 jalet |
---|
| 311 | # LPRng support now works ! |
---|
| 312 | # |
---|
[952] | 313 | # Revision 1.35 2003/04/23 22:13:57 jalet |
---|
| 314 | # Preliminary support for LPRng added BUT STILL UNTESTED. |
---|
| 315 | # |
---|
[929] | 316 | # Revision 1.34 2003/04/17 09:26:21 jalet |
---|
| 317 | # repykota now reports account balances too. |
---|
| 318 | # |
---|
[927] | 319 | # Revision 1.33 2003/04/16 12:35:49 jalet |
---|
| 320 | # Groups quota work now ! |
---|
| 321 | # |
---|
[925] | 322 | # Revision 1.32 2003/04/16 08:53:14 jalet |
---|
| 323 | # Printing can now be limited either by user's account balance or by |
---|
| 324 | # page quota (the default). Quota report doesn't include account balance |
---|
| 325 | # yet, though. |
---|
| 326 | # |
---|
[915] | 327 | # Revision 1.31 2003/04/15 11:30:57 jalet |
---|
| 328 | # More work done on money print charging. |
---|
| 329 | # Minor bugs corrected. |
---|
| 330 | # All tools now access to the storage as priviledged users, repykota excepted. |
---|
| 331 | # |
---|
[900] | 332 | # Revision 1.30 2003/04/10 21:47:20 jalet |
---|
| 333 | # Job history added. Upgrade script neutralized for now ! |
---|
| 334 | # |
---|
[873] | 335 | # Revision 1.29 2003/03/29 13:45:27 jalet |
---|
| 336 | # GPL paragraphs were incorrectly (from memory) copied into the sources. |
---|
| 337 | # Two README files were added. |
---|
| 338 | # Upgrade script for PostgreSQL pre 1.01 schema was added. |
---|
| 339 | # |
---|
[872] | 340 | # Revision 1.28 2003/03/29 13:08:28 jalet |
---|
| 341 | # Configuration is now expected to be found in /etc/pykota.conf instead of |
---|
| 342 | # in /etc/cups/pykota.conf |
---|
| 343 | # Installation script can move old config files to the new location if needed. |
---|
| 344 | # Better error handling if configuration file is absent. |
---|
| 345 | # |
---|
[852] | 346 | # Revision 1.27 2003/03/15 23:01:28 jalet |
---|
| 347 | # New mailto option in configuration file added. |
---|
| 348 | # No time to test this tonight (although it should work). |
---|
| 349 | # |
---|
[844] | 350 | # Revision 1.26 2003/03/09 23:58:16 jalet |
---|
| 351 | # Comment |
---|
| 352 | # |
---|
[834] | 353 | # Revision 1.25 2003/03/07 22:56:14 jalet |
---|
| 354 | # 0.99 is out with some bug fixes. |
---|
| 355 | # |
---|
[825] | 356 | # Revision 1.24 2003/02/27 23:48:41 jalet |
---|
| 357 | # Correctly maps PyKota's log levels to syslog log levels |
---|
| 358 | # |
---|
[824] | 359 | # Revision 1.23 2003/02/27 22:55:20 jalet |
---|
| 360 | # WARN log priority doesn't exist. |
---|
| 361 | # |
---|
[817] | 362 | # Revision 1.22 2003/02/27 09:09:20 jalet |
---|
| 363 | # Added a method to match strings against wildcard patterns |
---|
| 364 | # |
---|
[804] | 365 | # Revision 1.21 2003/02/17 23:01:56 jalet |
---|
| 366 | # Typos |
---|
| 367 | # |
---|
[802] | 368 | # Revision 1.20 2003/02/17 22:55:01 jalet |
---|
| 369 | # More options can now be set per printer or globally : |
---|
| 370 | # |
---|
[804] | 371 | # admin |
---|
| 372 | # adminmail |
---|
| 373 | # gracedelay |
---|
| 374 | # requester |
---|
[802] | 375 | # |
---|
| 376 | # the printer option has priority when both are defined. |
---|
| 377 | # |
---|
[788] | 378 | # Revision 1.19 2003/02/10 11:28:45 jalet |
---|
| 379 | # Localization |
---|
| 380 | # |
---|
[782] | 381 | # Revision 1.18 2003/02/10 01:02:17 jalet |
---|
| 382 | # External requester is about to work, but I must sleep |
---|
| 383 | # |
---|
[773] | 384 | # Revision 1.17 2003/02/09 13:05:43 jalet |
---|
| 385 | # Internationalization continues... |
---|
| 386 | # |
---|
[772] | 387 | # Revision 1.16 2003/02/09 12:56:53 jalet |
---|
| 388 | # Internationalization begins... |
---|
| 389 | # |
---|
[764] | 390 | # Revision 1.15 2003/02/08 22:09:52 jalet |
---|
| 391 | # Name check method moved here |
---|
| 392 | # |
---|
[742] | 393 | # Revision 1.14 2003/02/07 10:42:45 jalet |
---|
| 394 | # Indentation problem |
---|
| 395 | # |
---|
[733] | 396 | # Revision 1.13 2003/02/07 08:34:16 jalet |
---|
| 397 | # Test wrt date limit was wrong |
---|
| 398 | # |
---|
[729] | 399 | # Revision 1.12 2003/02/06 23:20:02 jalet |
---|
| 400 | # warnpykota doesn't need any user/group name argument, mimicing the |
---|
| 401 | # warnquota disk quota tool. |
---|
| 402 | # |
---|
[728] | 403 | # Revision 1.11 2003/02/06 22:54:33 jalet |
---|
| 404 | # warnpykota should be ok |
---|
| 405 | # |
---|
[722] | 406 | # Revision 1.10 2003/02/06 15:03:11 jalet |
---|
| 407 | # added a method to set the limit date |
---|
| 408 | # |
---|
[715] | 409 | # Revision 1.9 2003/02/06 10:39:23 jalet |
---|
| 410 | # Preliminary edpykota work. |
---|
| 411 | # |
---|
[713] | 412 | # Revision 1.8 2003/02/06 09:19:02 jalet |
---|
| 413 | # More robust behavior (hopefully) when the user or printer is not managed |
---|
| 414 | # correctly by the Quota System : e.g. cupsFilter added in ppd file, but |
---|
| 415 | # printer and/or user not 'yet?' in storage. |
---|
| 416 | # |
---|
[712] | 417 | # Revision 1.7 2003/02/06 00:00:45 jalet |
---|
| 418 | # Now includes the printer name in email messages |
---|
| 419 | # |
---|
[711] | 420 | # Revision 1.6 2003/02/05 23:55:02 jalet |
---|
| 421 | # Cleaner email messages |
---|
| 422 | # |
---|
[709] | 423 | # Revision 1.5 2003/02/05 23:45:09 jalet |
---|
| 424 | # Better DateTime manipulation wrt grace delay |
---|
| 425 | # |
---|
[708] | 426 | # Revision 1.4 2003/02/05 23:26:22 jalet |
---|
| 427 | # Incorrect handling of grace delay |
---|
| 428 | # |
---|
[699] | 429 | # Revision 1.3 2003/02/05 22:16:20 jalet |
---|
| 430 | # DEVICE_URI is undefined outside of CUPS, i.e. for normal command line tools |
---|
| 431 | # |
---|
[698] | 432 | # Revision 1.2 2003/02/05 22:10:29 jalet |
---|
| 433 | # Typos |
---|
| 434 | # |
---|
[695] | 435 | # Revision 1.1 2003/02/05 21:28:17 jalet |
---|
| 436 | # Initial import into CVS |
---|
| 437 | # |
---|
| 438 | # |
---|
| 439 | # |
---|
| 440 | |
---|
| 441 | import sys |
---|
[1193] | 442 | import os |
---|
[817] | 443 | import fnmatch |
---|
[715] | 444 | import getopt |
---|
[695] | 445 | import smtplib |
---|
[772] | 446 | import gettext |
---|
[782] | 447 | import locale |
---|
[1434] | 448 | import signal |
---|
[1469] | 449 | import socket |
---|
[1492] | 450 | import tempfile |
---|
[1537] | 451 | import ConfigParser |
---|
[695] | 452 | |
---|
[708] | 453 | from mx import DateTime |
---|
| 454 | |
---|
[1492] | 455 | from pykota import version, config, storage, logger, accounter, pdlanalyzer |
---|
[695] | 456 | |
---|
| 457 | class PyKotaToolError(Exception): |
---|
| 458 | """An exception for PyKota config related stuff.""" |
---|
| 459 | def __init__(self, message = ""): |
---|
| 460 | self.message = message |
---|
| 461 | Exception.__init__(self, message) |
---|
| 462 | def __repr__(self): |
---|
| 463 | return self.message |
---|
| 464 | __str__ = __repr__ |
---|
| 465 | |
---|
[1546] | 466 | def crashed(message) : |
---|
| 467 | """Minimal crash method.""" |
---|
| 468 | import traceback |
---|
| 469 | lines = [] |
---|
| 470 | for line in traceback.format_exception(*sys.exc_info()) : |
---|
| 471 | lines.extend([l for l in line.split("\n") if l]) |
---|
| 472 | msg = "ERROR: ".join(["%s\n" % l for l in (["ERROR: PyKota v%s" % version.__version__, message] + lines)]) |
---|
| 473 | sys.stderr.write(msg) |
---|
| 474 | sys.stderr.flush() |
---|
| 475 | return msg |
---|
| 476 | |
---|
[695] | 477 | class PyKotaTool : |
---|
| 478 | """Base class for all PyKota command line tools.""" |
---|
[1257] | 479 | def __init__(self, lang=None, doc="PyKota %s (c) 2003-2004 %s" % (version.__version__, version.__author__)) : |
---|
[695] | 480 | """Initializes the command line tool.""" |
---|
[772] | 481 | # locale stuff |
---|
[788] | 482 | try : |
---|
[1171] | 483 | locale.setlocale(locale.LC_ALL, lang) |
---|
[788] | 484 | gettext.install("pykota") |
---|
| 485 | except (locale.Error, IOError) : |
---|
| 486 | gettext.NullTranslations().install() |
---|
[772] | 487 | |
---|
| 488 | # pykota specific stuff |
---|
[715] | 489 | self.documentation = doc |
---|
[1537] | 490 | try : |
---|
| 491 | self.config = config.PyKotaConfig("/etc/pykota") |
---|
| 492 | except ConfigParser.ParsingError, msg : |
---|
| 493 | sys.stderr.write("ERROR: Problem encountered while parsing configuration file : %s\n" % msg) |
---|
| 494 | sys.stderr.flush() |
---|
| 495 | sys.exit(-1) |
---|
[1542] | 496 | |
---|
[1523] | 497 | try : |
---|
[1541] | 498 | self.debug = self.config.getDebug() |
---|
| 499 | self.smtpserver = self.config.getSMTPServer() |
---|
| 500 | self.maildomain = self.config.getMailDomain() |
---|
[1523] | 501 | self.logger = logger.openLogger(self.config.getLoggingBackend()) |
---|
| 502 | self.storage = storage.openConnection(self) |
---|
[1537] | 503 | except (config.PyKotaConfigError, logger.PyKotaLoggingError, storage.PyKotaStorageError), msg : |
---|
[1523] | 504 | self.crashed(msg) |
---|
| 505 | raise |
---|
[1497] | 506 | self.softwareJobSize = 0 |
---|
[1495] | 507 | self.softwareJobPrice = 0.0 |
---|
[695] | 508 | |
---|
[1130] | 509 | def logdebug(self, message) : |
---|
| 510 | """Logs something to debug output if debug is enabled.""" |
---|
| 511 | if self.debug : |
---|
| 512 | self.logger.log_message(message, "debug") |
---|
[1582] | 513 | |
---|
[1584] | 514 | def printInfo(self, message, level="info") : |
---|
[1582] | 515 | """Sends a message to standard error.""" |
---|
[1584] | 516 | sys.stderr.write("%s: %s\n" % (level.upper(), message)) |
---|
[1582] | 517 | sys.stderr.flush() |
---|
[1130] | 518 | |
---|
[1113] | 519 | def clean(self) : |
---|
| 520 | """Ensures that the database is closed.""" |
---|
| 521 | try : |
---|
| 522 | self.storage.close() |
---|
| 523 | except (TypeError, NameError, AttributeError) : |
---|
| 524 | pass |
---|
| 525 | |
---|
[715] | 526 | def display_version_and_quit(self) : |
---|
| 527 | """Displays version number, then exists successfully.""" |
---|
[1113] | 528 | self.clean() |
---|
[715] | 529 | print version.__version__ |
---|
| 530 | sys.exit(0) |
---|
| 531 | |
---|
| 532 | def display_usage_and_quit(self) : |
---|
| 533 | """Displays command line usage, then exists successfully.""" |
---|
[1113] | 534 | self.clean() |
---|
[715] | 535 | print self.documentation |
---|
| 536 | sys.exit(0) |
---|
| 537 | |
---|
[1517] | 538 | def crashed(self, message) : |
---|
| 539 | """Outputs a crash message, and optionally sends it to software author.""" |
---|
[1546] | 540 | msg = crashed(message) |
---|
[1541] | 541 | try : |
---|
| 542 | crashrecipient = self.config.getCrashRecipient() |
---|
| 543 | if crashrecipient : |
---|
[1517] | 544 | admin = self.config.getAdminMail("global") # Nice trick, isn't it ? |
---|
[1546] | 545 | fullmessage = "========== Traceback :\n\n%s\n\n========== sys.argv :\n\n%s\n\n========== Environment :\n\n%s\n" % \ |
---|
| 546 | (msg, \ |
---|
[1517] | 547 | "\n".join([" %s" % repr(a) for a in sys.argv]), \ |
---|
| 548 | "\n".join([" %s=%s" % (k, v) for (k, v) in os.environ.items()])) |
---|
| 549 | server = smtplib.SMTP(self.smtpserver) |
---|
[1546] | 550 | server.sendmail(admin, [admin, crashrecipient], \ |
---|
[1560] | 551 | "From: %s\nTo: %s\nCc: %s\nSubject: PyKota v%s crash traceback !\n\n%s" % \ |
---|
| 552 | (admin, crashrecipient, admin, version.__version__, fullmessage)) |
---|
[1517] | 553 | server.quit() |
---|
[1541] | 554 | except : |
---|
| 555 | pass |
---|
[1517] | 556 | |
---|
[729] | 557 | def parseCommandline(self, argv, short, long, allownothing=0) : |
---|
[715] | 558 | """Parses the command line, controlling options.""" |
---|
| 559 | # split options in two lists: those which need an argument, those which don't need any |
---|
| 560 | withoutarg = [] |
---|
| 561 | witharg = [] |
---|
| 562 | lgs = len(short) |
---|
| 563 | i = 0 |
---|
| 564 | while i < lgs : |
---|
| 565 | ii = i + 1 |
---|
| 566 | if (ii < lgs) and (short[ii] == ':') : |
---|
| 567 | # needs an argument |
---|
| 568 | witharg.append(short[i]) |
---|
| 569 | ii = ii + 1 # skip the ':' |
---|
| 570 | else : |
---|
| 571 | # doesn't need an argument |
---|
| 572 | withoutarg.append(short[i]) |
---|
| 573 | i = ii |
---|
| 574 | |
---|
| 575 | for option in long : |
---|
| 576 | if option[-1] == '=' : |
---|
| 577 | # needs an argument |
---|
| 578 | witharg.append(option[:-1]) |
---|
| 579 | else : |
---|
| 580 | # doesn't need an argument |
---|
| 581 | withoutarg.append(option) |
---|
| 582 | |
---|
| 583 | # we begin with all possible options unset |
---|
| 584 | parsed = {} |
---|
| 585 | for option in withoutarg + witharg : |
---|
| 586 | parsed[option] = None |
---|
| 587 | |
---|
| 588 | # then we parse the command line |
---|
| 589 | args = [] # to not break if something unexpected happened |
---|
| 590 | try : |
---|
| 591 | options, args = getopt.getopt(argv, short, long) |
---|
| 592 | if options : |
---|
| 593 | for (o, v) in options : |
---|
| 594 | # we skip the '-' chars |
---|
| 595 | lgo = len(o) |
---|
| 596 | i = 0 |
---|
| 597 | while (i < lgo) and (o[i] == '-') : |
---|
| 598 | i = i + 1 |
---|
| 599 | o = o[i:] |
---|
| 600 | if o in witharg : |
---|
| 601 | # needs an argument : set it |
---|
| 602 | parsed[o] = v |
---|
| 603 | elif o in withoutarg : |
---|
| 604 | # doesn't need an argument : boolean |
---|
| 605 | parsed[o] = 1 |
---|
| 606 | else : |
---|
| 607 | # should never occur |
---|
| 608 | raise PyKotaToolError, "Unexpected problem when parsing command line" |
---|
[729] | 609 | elif (not args) and (not allownothing) and sys.stdin.isatty() : # no option and no argument, we display help if we are a tty |
---|
[715] | 610 | self.display_usage_and_quit() |
---|
| 611 | except getopt.error, msg : |
---|
[1584] | 612 | self.printInfo(msg) |
---|
[715] | 613 | self.display_usage_and_quit() |
---|
| 614 | return (parsed, args) |
---|
| 615 | |
---|
[764] | 616 | def isValidName(self, name) : |
---|
| 617 | """Checks if a user or printer name is valid.""" |
---|
[1593] | 618 | invalidchars = "/@" |
---|
[1637] | 619 | for c in list(invalidchars) : |
---|
[1593] | 620 | if c in name : |
---|
[1394] | 621 | return 0 |
---|
| 622 | return 1 |
---|
[764] | 623 | |
---|
[817] | 624 | def matchString(self, s, patterns) : |
---|
| 625 | """Returns 1 if the string s matches one of the patterns, else 0.""" |
---|
| 626 | for pattern in patterns : |
---|
| 627 | if fnmatch.fnmatchcase(s, pattern) : |
---|
| 628 | return 1 |
---|
| 629 | return 0 |
---|
| 630 | |
---|
[802] | 631 | def sendMessage(self, adminmail, touser, fullmessage) : |
---|
[695] | 632 | """Sends an email message containing headers to some user.""" |
---|
| 633 | if "@" not in touser : |
---|
[1353] | 634 | touser = "%s@%s" % (touser, self.maildomain or self.smtpserver) |
---|
[1469] | 635 | try : |
---|
| 636 | server = smtplib.SMTP(self.smtpserver) |
---|
| 637 | except socket.error, msg : |
---|
[1584] | 638 | self.printInfo(_("Impossible to connect to SMTP server : %s") % msg, "error") |
---|
[1469] | 639 | else : |
---|
| 640 | try : |
---|
| 641 | server.sendmail(adminmail, [touser], "From: %s\nTo: %s\n%s" % (adminmail, touser, fullmessage)) |
---|
| 642 | except smtplib.SMTPException, answer : |
---|
| 643 | for (k, v) in answer.recipients.items() : |
---|
[1584] | 644 | self.printInfo(_("Impossible to send mail to %s, error %s : %s") % (k, v[0], v[1]), "error") |
---|
[1469] | 645 | server.quit() |
---|
[695] | 646 | |
---|
[1079] | 647 | def sendMessageToUser(self, admin, adminmail, user, subject, message) : |
---|
[695] | 648 | """Sends an email message to a user.""" |
---|
[802] | 649 | message += _("\n\nPlease contact your system administrator :\n\n\t%s - <%s>\n") % (admin, adminmail) |
---|
[1079] | 650 | self.sendMessage(adminmail, user.Email or user.Name, "Subject: %s\n\n%s" % (subject, message)) |
---|
[695] | 651 | |
---|
[802] | 652 | def sendMessageToAdmin(self, adminmail, subject, message) : |
---|
[695] | 653 | """Sends an email message to the Print Quota administrator.""" |
---|
[802] | 654 | self.sendMessage(adminmail, adminmail, "Subject: %s\n\n%s" % (subject, message)) |
---|
[695] | 655 | |
---|
[1365] | 656 | def _checkUserPQuota(self, userpquota) : |
---|
| 657 | """Checks the user quota on a printer and deny or accept the job.""" |
---|
| 658 | # then we check the user's own quota |
---|
| 659 | # if we get there we are sure that policy is not EXTERNAL |
---|
| 660 | user = userpquota.User |
---|
| 661 | printer = userpquota.Printer |
---|
[1495] | 662 | enforcement = self.config.getPrinterEnforcement(printer.Name) |
---|
[1365] | 663 | self.logdebug("Checking user %s's quota on printer %s" % (user.Name, printer.Name)) |
---|
| 664 | (policy, dummy) = self.config.getPrinterPolicy(userpquota.Printer.Name) |
---|
| 665 | if not userpquota.Exists : |
---|
| 666 | # Unknown userquota |
---|
| 667 | if policy == "ALLOW" : |
---|
| 668 | action = "POLICY_ALLOW" |
---|
| 669 | else : |
---|
| 670 | action = "POLICY_DENY" |
---|
[1584] | 671 | self.printInfo(_("Unable to match user %s on printer %s, applying default policy (%s)") % (user.Name, printer.Name, action)) |
---|
[1365] | 672 | else : |
---|
| 673 | pagecounter = int(userpquota.PageCounter or 0) |
---|
[1495] | 674 | if enforcement == "STRICT" : |
---|
| 675 | pagecounter += self.softwareJobSize |
---|
[1365] | 676 | if userpquota.SoftLimit is not None : |
---|
| 677 | softlimit = int(userpquota.SoftLimit) |
---|
| 678 | if pagecounter < softlimit : |
---|
| 679 | action = "ALLOW" |
---|
| 680 | else : |
---|
| 681 | if userpquota.HardLimit is None : |
---|
| 682 | # only a soft limit, this is equivalent to having only a hard limit |
---|
| 683 | action = "DENY" |
---|
| 684 | else : |
---|
| 685 | hardlimit = int(userpquota.HardLimit) |
---|
| 686 | if softlimit <= pagecounter < hardlimit : |
---|
| 687 | now = DateTime.now() |
---|
| 688 | if userpquota.DateLimit is not None : |
---|
| 689 | datelimit = DateTime.ISO.ParseDateTime(userpquota.DateLimit) |
---|
| 690 | else : |
---|
| 691 | datelimit = now + self.config.getGraceDelay(printer.Name) |
---|
| 692 | userpquota.setDateLimit(datelimit) |
---|
| 693 | if now < datelimit : |
---|
| 694 | action = "WARN" |
---|
| 695 | else : |
---|
| 696 | action = "DENY" |
---|
| 697 | else : |
---|
| 698 | action = "DENY" |
---|
| 699 | else : |
---|
| 700 | if userpquota.HardLimit is not None : |
---|
| 701 | # no soft limit, only a hard one. |
---|
| 702 | hardlimit = int(userpquota.HardLimit) |
---|
| 703 | if pagecounter < hardlimit : |
---|
| 704 | action = "ALLOW" |
---|
| 705 | else : |
---|
| 706 | action = "DENY" |
---|
| 707 | else : |
---|
| 708 | # Both are unset, no quota, i.e. accounting only |
---|
| 709 | action = "ALLOW" |
---|
| 710 | return action |
---|
| 711 | |
---|
[1041] | 712 | def checkGroupPQuota(self, grouppquota) : |
---|
[927] | 713 | """Checks the group quota on a printer and deny or accept the job.""" |
---|
[1041] | 714 | group = grouppquota.Group |
---|
| 715 | printer = grouppquota.Printer |
---|
[1495] | 716 | enforcement = self.config.getPrinterEnforcement(printer.Name) |
---|
[1365] | 717 | self.logdebug("Checking group %s's quota on printer %s" % (group.Name, printer.Name)) |
---|
[1061] | 718 | if group.LimitBy and (group.LimitBy.lower() == "balance") : |
---|
[1666] | 719 | val = group.AccountBalance or 0.0 |
---|
[1495] | 720 | if enforcement == "STRICT" : |
---|
| 721 | val -= self.softwareJobPrice # use precomputed size. |
---|
| 722 | if val <= 0.0 : |
---|
[1041] | 723 | action = "DENY" |
---|
[1495] | 724 | elif val <= self.config.getPoorMan() : |
---|
[1077] | 725 | action = "WARN" |
---|
[927] | 726 | else : |
---|
[1041] | 727 | action = "ALLOW" |
---|
[1529] | 728 | if (enforcement == "STRICT") and (val == 0.0) : |
---|
| 729 | action = "WARN" # we can still print until account is 0 |
---|
[927] | 730 | else : |
---|
[1665] | 731 | val = grouppquota.PageCounter or 0 |
---|
[1495] | 732 | if enforcement == "STRICT" : |
---|
| 733 | val += self.softwareJobSize |
---|
[1041] | 734 | if grouppquota.SoftLimit is not None : |
---|
| 735 | softlimit = int(grouppquota.SoftLimit) |
---|
[1495] | 736 | if val < softlimit : |
---|
[1041] | 737 | action = "ALLOW" |
---|
[927] | 738 | else : |
---|
[1041] | 739 | if grouppquota.HardLimit is None : |
---|
| 740 | # only a soft limit, this is equivalent to having only a hard limit |
---|
| 741 | action = "DENY" |
---|
[927] | 742 | else : |
---|
[1041] | 743 | hardlimit = int(grouppquota.HardLimit) |
---|
[1495] | 744 | if softlimit <= val < hardlimit : |
---|
[1041] | 745 | now = DateTime.now() |
---|
| 746 | if grouppquota.DateLimit is not None : |
---|
| 747 | datelimit = DateTime.ISO.ParseDateTime(grouppquota.DateLimit) |
---|
| 748 | else : |
---|
| 749 | datelimit = now + self.config.getGraceDelay(printer.Name) |
---|
| 750 | grouppquota.setDateLimit(datelimit) |
---|
| 751 | if now < datelimit : |
---|
| 752 | action = "WARN" |
---|
| 753 | else : |
---|
[927] | 754 | action = "DENY" |
---|
[1041] | 755 | else : |
---|
[927] | 756 | action = "DENY" |
---|
[1041] | 757 | else : |
---|
| 758 | if grouppquota.HardLimit is not None : |
---|
| 759 | # no soft limit, only a hard one. |
---|
| 760 | hardlimit = int(grouppquota.HardLimit) |
---|
[1495] | 761 | if val < hardlimit : |
---|
[927] | 762 | action = "ALLOW" |
---|
[1041] | 763 | else : |
---|
| 764 | action = "DENY" |
---|
| 765 | else : |
---|
| 766 | # Both are unset, no quota, i.e. accounting only |
---|
| 767 | action = "ALLOW" |
---|
[927] | 768 | return action |
---|
| 769 | |
---|
[1041] | 770 | def checkUserPQuota(self, userpquota) : |
---|
[1365] | 771 | """Checks the user quota on a printer and all its parents and deny or accept the job.""" |
---|
[1041] | 772 | user = userpquota.User |
---|
| 773 | printer = userpquota.Printer |
---|
| 774 | |
---|
[1365] | 775 | # indicates that a warning needs to be sent |
---|
| 776 | warned = 0 |
---|
| 777 | |
---|
[927] | 778 | # first we check any group the user is a member of |
---|
[1041] | 779 | for group in self.storage.getUserGroups(user) : |
---|
| 780 | grouppquota = self.storage.getGroupPQuota(group, printer) |
---|
[1365] | 781 | # for the printer and all its parents |
---|
| 782 | for gpquota in [ grouppquota ] + grouppquota.ParentPrintersGroupPQuota : |
---|
| 783 | if gpquota.Exists : |
---|
| 784 | action = self.checkGroupPQuota(gpquota) |
---|
| 785 | if action == "DENY" : |
---|
| 786 | return action |
---|
| 787 | elif action == "WARN" : |
---|
| 788 | warned = 1 |
---|
| 789 | |
---|
| 790 | # Then we check the user's account balance |
---|
[1152] | 791 | # if we get there we are sure that policy is not EXTERNAL |
---|
| 792 | (policy, dummy) = self.config.getPrinterPolicy(printer.Name) |
---|
[1061] | 793 | if user.LimitBy and (user.LimitBy.lower() == "balance") : |
---|
[1365] | 794 | self.logdebug("Checking account balance for user %s" % user.Name) |
---|
[1041] | 795 | if user.AccountBalance is None : |
---|
[956] | 796 | if policy == "ALLOW" : |
---|
[925] | 797 | action = "POLICY_ALLOW" |
---|
| 798 | else : |
---|
| 799 | action = "POLICY_DENY" |
---|
[1584] | 800 | self.printInfo(_("Unable to find user %s's account balance, applying default policy (%s) for printer %s") % (user.Name, action, printer.Name)) |
---|
[1365] | 801 | return action |
---|
[713] | 802 | else : |
---|
[1077] | 803 | val = float(user.AccountBalance or 0.0) |
---|
[1529] | 804 | enforcement = self.config.getPrinterEnforcement(printer.Name) |
---|
| 805 | if enforcement == "STRICT" : |
---|
[1495] | 806 | val -= self.softwareJobPrice # use precomputed size. |
---|
[1077] | 807 | if val <= 0.0 : |
---|
[1529] | 808 | action = "DENY" |
---|
[1077] | 809 | elif val <= self.config.getPoorMan() : |
---|
[1529] | 810 | action = "WARN" |
---|
| 811 | else : |
---|
| 812 | action = "ALLOW" |
---|
| 813 | if (enforcement == "STRICT") and (val == 0.0) : |
---|
| 814 | action = "WARN" # we can still print until account is 0 |
---|
| 815 | return action |
---|
[925] | 816 | else : |
---|
[1365] | 817 | # Then check the user quota on current printer and all its parents. |
---|
| 818 | policyallowed = 0 |
---|
| 819 | for upquota in [ userpquota ] + userpquota.ParentPrintersUserPQuota : |
---|
| 820 | action = self._checkUserPQuota(upquota) |
---|
| 821 | if action in ("DENY", "POLICY_DENY") : |
---|
| 822 | return action |
---|
| 823 | elif action == "WARN" : |
---|
| 824 | warned = 1 |
---|
| 825 | elif action == "POLICY_ALLOW" : |
---|
| 826 | policyallowed = 1 |
---|
| 827 | if warned : |
---|
| 828 | return "WARN" |
---|
| 829 | elif policyallowed : |
---|
| 830 | return "POLICY_ALLOW" |
---|
[925] | 831 | else : |
---|
[1365] | 832 | return "ALLOW" |
---|
| 833 | |
---|
[1196] | 834 | def externalMailTo(self, cmd, action, user, printer, message) : |
---|
[1192] | 835 | """Warns the user with an external command.""" |
---|
| 836 | username = user.Name |
---|
[1196] | 837 | printername = printer.Name |
---|
[1192] | 838 | email = user.Email or user.Name |
---|
| 839 | if "@" not in email : |
---|
[1353] | 840 | email = "%s@%s" % (email, self.maildomain or self.smtpserver) |
---|
[1192] | 841 | os.system(cmd % locals()) |
---|
| 842 | |
---|
[1196] | 843 | def formatCommandLine(self, cmd, user, printer) : |
---|
| 844 | """Executes an external command.""" |
---|
| 845 | username = user.Name |
---|
| 846 | printername = printer.Name |
---|
| 847 | return cmd % locals() |
---|
| 848 | |
---|
[1041] | 849 | def warnGroupPQuota(self, grouppquota) : |
---|
[927] | 850 | """Checks a group quota and send messages if quota is exceeded on current printer.""" |
---|
[1041] | 851 | group = grouppquota.Group |
---|
| 852 | printer = grouppquota.Printer |
---|
| 853 | admin = self.config.getAdmin(printer.Name) |
---|
| 854 | adminmail = self.config.getAdminMail(printer.Name) |
---|
[1192] | 855 | (mailto, arguments) = self.config.getMailTo(printer.Name) |
---|
[1068] | 856 | action = self.checkGroupPQuota(grouppquota) |
---|
[927] | 857 | if action.startswith("POLICY_") : |
---|
| 858 | action = action[7:] |
---|
| 859 | if action == "DENY" : |
---|
[1041] | 860 | adminmessage = _("Print Quota exceeded for group %s on printer %s") % (group.Name, printer.Name) |
---|
[1584] | 861 | self.printInfo(adminmessage) |
---|
[927] | 862 | if mailto in [ "BOTH", "ADMIN" ] : |
---|
| 863 | self.sendMessageToAdmin(adminmail, _("Print Quota"), adminmessage) |
---|
[1192] | 864 | if mailto in [ "BOTH", "USER", "EXTERNAL" ] : |
---|
[1140] | 865 | for user in self.storage.getGroupMembers(group) : |
---|
[1192] | 866 | if mailto != "EXTERNAL" : |
---|
[1245] | 867 | self.sendMessageToUser(admin, adminmail, user, _("Print Quota Exceeded"), self.config.getHardWarn(printer.Name)) |
---|
[1192] | 868 | else : |
---|
[1637] | 869 | self.externalMailTo(arguments, action, user, printer, self.config.getHardWarn(printer.Name)) |
---|
[927] | 870 | elif action == "WARN" : |
---|
[1091] | 871 | adminmessage = _("Print Quota low for group %s on printer %s") % (group.Name, printer.Name) |
---|
[1584] | 872 | self.printInfo(adminmessage) |
---|
[927] | 873 | if mailto in [ "BOTH", "ADMIN" ] : |
---|
| 874 | self.sendMessageToAdmin(adminmail, _("Print Quota"), adminmessage) |
---|
[1077] | 875 | if group.LimitBy and (group.LimitBy.lower() == "balance") : |
---|
| 876 | message = self.config.getPoorWarn() |
---|
| 877 | else : |
---|
| 878 | message = self.config.getSoftWarn(printer.Name) |
---|
[1192] | 879 | if mailto in [ "BOTH", "USER", "EXTERNAL" ] : |
---|
[1140] | 880 | for user in self.storage.getGroupMembers(group) : |
---|
[1192] | 881 | if mailto != "EXTERNAL" : |
---|
| 882 | self.sendMessageToUser(admin, adminmail, user, _("Print Quota Exceeded"), message) |
---|
| 883 | else : |
---|
[1196] | 884 | self.externalMailTo(arguments, action, user, printer, message) |
---|
[927] | 885 | return action |
---|
[728] | 886 | |
---|
[1041] | 887 | def warnUserPQuota(self, userpquota) : |
---|
[728] | 888 | """Checks a user quota and send him a message if quota is exceeded on current printer.""" |
---|
[1041] | 889 | user = userpquota.User |
---|
[1245] | 890 | printer = userpquota.Printer |
---|
| 891 | admin = self.config.getAdmin(printer.Name) |
---|
| 892 | adminmail = self.config.getAdminMail(printer.Name) |
---|
| 893 | (mailto, arguments) = self.config.getMailTo(printer.Name) |
---|
| 894 | action = self.checkUserPQuota(userpquota) |
---|
| 895 | if action.startswith("POLICY_") : |
---|
| 896 | action = action[7:] |
---|
| 897 | if action == "DENY" : |
---|
| 898 | adminmessage = _("Print Quota exceeded for user %s on printer %s") % (user.Name, printer.Name) |
---|
[1584] | 899 | self.printInfo(adminmessage) |
---|
[1245] | 900 | if mailto in [ "BOTH", "USER", "EXTERNAL" ] : |
---|
| 901 | message = self.config.getHardWarn(printer.Name) |
---|
| 902 | if mailto != "EXTERNAL" : |
---|
| 903 | self.sendMessageToUser(admin, adminmail, user, _("Print Quota Exceeded"), message) |
---|
| 904 | else : |
---|
| 905 | self.externalMailTo(arguments, action, user, printer, message) |
---|
| 906 | if mailto in [ "BOTH", "ADMIN" ] : |
---|
| 907 | self.sendMessageToAdmin(adminmail, _("Print Quota"), adminmessage) |
---|
| 908 | elif action == "WARN" : |
---|
| 909 | adminmessage = _("Print Quota low for user %s on printer %s") % (user.Name, printer.Name) |
---|
[1584] | 910 | self.printInfo(adminmessage) |
---|
[1245] | 911 | if mailto in [ "BOTH", "USER", "EXTERNAL" ] : |
---|
| 912 | if user.LimitBy and (user.LimitBy.lower() == "balance") : |
---|
| 913 | message = self.config.getPoorWarn() |
---|
| 914 | else : |
---|
| 915 | message = self.config.getSoftWarn(printer.Name) |
---|
| 916 | if mailto != "EXTERNAL" : |
---|
| 917 | self.sendMessageToUser(admin, adminmail, user, _("Print Quota Low"), message) |
---|
| 918 | else : |
---|
| 919 | self.externalMailTo(arguments, action, user, printer, message) |
---|
| 920 | if mailto in [ "BOTH", "ADMIN" ] : |
---|
| 921 | self.sendMessageToAdmin(adminmail, _("Print Quota"), adminmessage) |
---|
| 922 | return action |
---|
[1196] | 923 | |
---|
| 924 | class PyKotaFilterOrBackend(PyKotaTool) : |
---|
| 925 | """Class for the PyKota filter or backend.""" |
---|
| 926 | def __init__(self) : |
---|
[1290] | 927 | """Initialize local datas from current environment.""" |
---|
[1492] | 928 | # We begin with ignoring signals, we may de-ignore them later on. |
---|
| 929 | self.gotSigTerm = 0 |
---|
| 930 | signal.signal(signal.SIGTERM, signal.SIG_IGN) |
---|
[1532] | 931 | # signal.signal(signal.SIGCHLD, signal.SIG_IGN) |
---|
[1492] | 932 | signal.signal(signal.SIGPIPE, signal.SIG_IGN) |
---|
| 933 | |
---|
[1196] | 934 | PyKotaTool.__init__(self) |
---|
| 935 | (self.printingsystem, \ |
---|
| 936 | self.printerhostname, \ |
---|
| 937 | self.printername, \ |
---|
| 938 | self.username, \ |
---|
| 939 | self.jobid, \ |
---|
| 940 | self.inputfile, \ |
---|
| 941 | self.copies, \ |
---|
| 942 | self.title, \ |
---|
| 943 | self.options, \ |
---|
| 944 | self.originalbackend) = self.extractInfoFromCupsOrLprng() |
---|
[1601] | 945 | |
---|
| 946 | self.logdebug(_("Printing system %s, args=%s") % (str(self.printingsystem), " ".join(sys.argv))) |
---|
| 947 | |
---|
[1483] | 948 | self.username = self.username or 'root' # when printing test page from CUPS web interface, username is empty |
---|
[1227] | 949 | if self.config.getUserNameToLower() : |
---|
| 950 | self.username = self.username.lower() |
---|
[1200] | 951 | self.preserveinputfile = self.inputfile |
---|
[1538] | 952 | try : |
---|
| 953 | self.accounter = accounter.openAccounter(self) |
---|
[1563] | 954 | except (config.PyKotaConfigError, accounter.PyKotaAccounterError), msg : |
---|
[1538] | 955 | self.crashed(msg) |
---|
| 956 | raise |
---|
[1372] | 957 | self.exportJobInfo() |
---|
[1492] | 958 | self.jobdatastream = self.openJobDataStream() |
---|
[1517] | 959 | os.environ["PYKOTAJOBSIZEBYTES"] = str(self.jobSizeBytes) |
---|
| 960 | self.logdebug("Job size is %s bytes" % self.jobSizeBytes) |
---|
[1514] | 961 | self.logdebug("Capturing SIGTERM events.") |
---|
| 962 | signal.signal(signal.SIGTERM, self.sigterm_handler) |
---|
[1372] | 963 | |
---|
[1606] | 964 | def sendBackChannelData(self, message, level="info") : |
---|
[1600] | 965 | """Sends an informational message to CUPS via back channel stream (stderr).""" |
---|
[1606] | 966 | sys.stderr.write("%s: PyKota (PID %s) : %s\n" % (level.upper(), os.getpid(), message.strip())) |
---|
| 967 | sys.stderr.flush() |
---|
[1600] | 968 | |
---|
[1492] | 969 | def openJobDataStream(self) : |
---|
| 970 | """Opens the file which contains the job's datas.""" |
---|
| 971 | if self.preserveinputfile is None : |
---|
| 972 | # Job comes from sys.stdin, but this is not |
---|
| 973 | # seekable and complexifies our task, so create |
---|
| 974 | # a temporary file and use it instead |
---|
[1606] | 975 | self.logdebug("Duplicating data stream from stdin to temporary file") |
---|
[1562] | 976 | dummy = 0 |
---|
[1492] | 977 | MEGABYTE = 1024*1024 |
---|
[1517] | 978 | self.jobSizeBytes = 0 |
---|
[1492] | 979 | infile = tempfile.TemporaryFile() |
---|
| 980 | while 1 : |
---|
| 981 | data = sys.stdin.read(MEGABYTE) |
---|
| 982 | if not data : |
---|
| 983 | break |
---|
[1517] | 984 | self.jobSizeBytes += len(data) |
---|
[1562] | 985 | if not (dummy % 10) : |
---|
[1606] | 986 | self.logdebug("%s bytes read..." % self.jobSizeBytes) |
---|
[1562] | 987 | dummy += 1 |
---|
[1492] | 988 | infile.write(data) |
---|
[1606] | 989 | self.logdebug("%s bytes read total." % self.jobSizeBytes) |
---|
[1492] | 990 | infile.flush() |
---|
| 991 | infile.seek(0) |
---|
| 992 | return infile |
---|
| 993 | else : |
---|
| 994 | # real file, just open it |
---|
[1606] | 995 | self.logdebug("Opening data stream %s" % self.preserveinputfile) |
---|
[1517] | 996 | self.jobSizeBytes = os.stat(self.preserveinputfile)[6] |
---|
[1492] | 997 | return open(self.preserveinputfile, "rb") |
---|
[1433] | 998 | |
---|
[1492] | 999 | def closeJobDataStream(self) : |
---|
| 1000 | """Closes the file which contains the job's datas.""" |
---|
[1606] | 1001 | self.logdebug("Closing data stream.") |
---|
[1492] | 1002 | try : |
---|
| 1003 | self.jobdatastream.close() |
---|
| 1004 | except : |
---|
| 1005 | pass |
---|
| 1006 | |
---|
| 1007 | def precomputeJobSize(self) : |
---|
| 1008 | """Computes the job size with a software method.""" |
---|
[1495] | 1009 | self.logdebug("Precomputing job's size with generic PDL analyzer...") |
---|
[1497] | 1010 | self.jobdatastream.seek(0) |
---|
[1492] | 1011 | try : |
---|
| 1012 | parser = pdlanalyzer.PDLAnalyzer(self.jobdatastream) |
---|
[1495] | 1013 | jobsize = parser.getJobSize() |
---|
[1492] | 1014 | except pdlanalyzer.PDLAnalyzerError, msg : |
---|
| 1015 | # Here we just log the failure, but |
---|
| 1016 | # we finally ignore it and return 0 since this |
---|
| 1017 | # computation is just an indication of what the |
---|
| 1018 | # job's size MAY be. |
---|
[1584] | 1019 | self.printInfo(_("Unable to precompute the job's size with the generic PDL analyzer : %s") % msg, "warn") |
---|
[1492] | 1020 | return 0 |
---|
[1495] | 1021 | else : |
---|
| 1022 | if ((self.printingsystem == "CUPS") \ |
---|
| 1023 | and (self.preserveinputfile is not None)) \ |
---|
| 1024 | or (self.printingsystem != "CUPS") : |
---|
| 1025 | return jobsize * self.copies |
---|
| 1026 | else : |
---|
| 1027 | return jobsize |
---|
[1492] | 1028 | |
---|
[1433] | 1029 | def sigterm_handler(self, signum, frame) : |
---|
[1481] | 1030 | """Sets an attribute whenever SIGTERM is received.""" |
---|
[1433] | 1031 | self.gotSigTerm = 1 |
---|
[1517] | 1032 | os.environ["PYKOTASTATUS"] = "CANCELLED" |
---|
[1584] | 1033 | self.printInfo(_("SIGTERM received, job %s cancelled.") % self.jobid) |
---|
[1433] | 1034 | |
---|
[1372] | 1035 | def exportJobInfo(self) : |
---|
| 1036 | """Exports job information to the environment.""" |
---|
[1517] | 1037 | os.environ["PYKOTAUSERNAME"] = str(self.username) |
---|
| 1038 | os.environ["PYKOTAPRINTERNAME"] = str(self.printername) |
---|
| 1039 | os.environ["PYKOTAJOBID"] = str(self.jobid) |
---|
| 1040 | os.environ["PYKOTATITLE"] = self.title or "" |
---|
| 1041 | os.environ["PYKOTAFILENAME"] = self.preserveinputfile or "" |
---|
| 1042 | os.environ["PYKOTACOPIES"] = str(self.copies) |
---|
| 1043 | os.environ["PYKOTAOPTIONS"] = self.options or "" |
---|
[1694] | 1044 | os.environ["PYKOTAPRINTERHOSTNAME"] = self.printerhostname or "localhost" |
---|
[1196] | 1045 | |
---|
[1372] | 1046 | def exportUserInfo(self, userpquota) : |
---|
| 1047 | """Exports user information to the environment.""" |
---|
[1517] | 1048 | os.environ["PYKOTALIMITBY"] = str(userpquota.User.LimitBy) |
---|
| 1049 | os.environ["PYKOTABALANCE"] = str(userpquota.User.AccountBalance or 0.0) |
---|
| 1050 | os.environ["PYKOTALIFETIMEPAID"] = str(userpquota.User.LifeTimePaid or 0.0) |
---|
| 1051 | os.environ["PYKOTAPAGECOUNTER"] = str(userpquota.PageCounter or 0) |
---|
| 1052 | os.environ["PYKOTALIFEPAGECOUNTER"] = str(userpquota.LifePageCounter or 0) |
---|
| 1053 | os.environ["PYKOTASOFTLIMIT"] = str(userpquota.SoftLimit) |
---|
| 1054 | os.environ["PYKOTAHARDLIMIT"] = str(userpquota.HardLimit) |
---|
| 1055 | os.environ["PYKOTADATELIMIT"] = str(userpquota.DateLimit) |
---|
[1439] | 1056 | |
---|
| 1057 | # not really an user information, but anyway |
---|
| 1058 | # exports the list of printers groups the current |
---|
| 1059 | # printer is a member of |
---|
[1517] | 1060 | os.environ["PYKOTAPGROUPS"] = ",".join([p.Name for p in self.storage.getParentPrinters(userpquota.Printer)]) |
---|
| 1061 | |
---|
[1372] | 1062 | def prehook(self, userpquota) : |
---|
[1475] | 1063 | """Allows plugging of an external hook before the job gets printed.""" |
---|
[1372] | 1064 | prehook = self.config.getPreHook(userpquota.Printer.Name) |
---|
| 1065 | if prehook : |
---|
| 1066 | self.logdebug("Executing pre-hook [%s]" % prehook) |
---|
| 1067 | os.system(prehook) |
---|
| 1068 | |
---|
| 1069 | def posthook(self, userpquota) : |
---|
[1475] | 1070 | """Allows plugging of an external hook after the job gets printed and/or denied.""" |
---|
[1372] | 1071 | posthook = self.config.getPostHook(userpquota.Printer.Name) |
---|
| 1072 | if posthook : |
---|
| 1073 | self.logdebug("Executing post-hook [%s]" % posthook) |
---|
| 1074 | os.system(posthook) |
---|
| 1075 | |
---|
[1584] | 1076 | def printInfo(self, message, level="info") : |
---|
| 1077 | """Sends a message to standard error.""" |
---|
| 1078 | self.logger.log_message("%s" % message, level) |
---|
| 1079 | |
---|
[1196] | 1080 | def extractInfoFromCupsOrLprng(self) : |
---|
| 1081 | """Returns a tuple (printingsystem, printerhostname, printername, username, jobid, filename, title, options, backend). |
---|
| 1082 | |
---|
| 1083 | Returns (None, None, None, None, None, None, None, None, None, None) if no printing system is recognized. |
---|
| 1084 | """ |
---|
| 1085 | # Try to detect CUPS |
---|
| 1086 | if os.environ.has_key("CUPS_SERVERROOT") and os.path.isdir(os.environ.get("CUPS_SERVERROOT", "")) : |
---|
| 1087 | if len(sys.argv) == 7 : |
---|
| 1088 | inputfile = sys.argv[6] |
---|
| 1089 | else : |
---|
| 1090 | inputfile = None |
---|
| 1091 | |
---|
| 1092 | # check that the DEVICE_URI environment variable's value is |
---|
| 1093 | # prefixed with "cupspykota:" otherwise don't touch it. |
---|
| 1094 | # If this is the case, we have to remove the prefix from |
---|
| 1095 | # the environment before launching the real backend in cupspykota |
---|
| 1096 | device_uri = os.environ.get("DEVICE_URI", "") |
---|
| 1097 | if device_uri.startswith("cupspykota:") : |
---|
| 1098 | fulldevice_uri = device_uri[:] |
---|
| 1099 | device_uri = fulldevice_uri[len("cupspykota:"):] |
---|
| 1100 | if device_uri.startswith("//") : # lpd (at least) |
---|
| 1101 | device_uri = device_uri[2:] |
---|
| 1102 | os.environ["DEVICE_URI"] = device_uri # TODO : side effect ! |
---|
| 1103 | # TODO : check this for more complex urls than ipp://myprinter.dot.com:631/printers/lp |
---|
| 1104 | try : |
---|
| 1105 | (backend, destination) = device_uri.split(":", 1) |
---|
| 1106 | except ValueError : |
---|
| 1107 | raise PyKotaToolError, "Invalid DEVICE_URI : %s\n" % device_uri |
---|
| 1108 | while destination.startswith("/") : |
---|
| 1109 | destination = destination[1:] |
---|
[1240] | 1110 | checkauth = destination.split("@", 1) |
---|
| 1111 | if len(checkauth) == 2 : |
---|
| 1112 | destination = checkauth[1] |
---|
[1196] | 1113 | printerhostname = destination.split("/")[0].split(":")[0] |
---|
| 1114 | return ("CUPS", \ |
---|
| 1115 | printerhostname, \ |
---|
| 1116 | os.environ.get("PRINTER"), \ |
---|
[1216] | 1117 | sys.argv[2].strip(), \ |
---|
[1196] | 1118 | sys.argv[1].strip(), \ |
---|
| 1119 | inputfile, \ |
---|
| 1120 | int(sys.argv[4].strip()), \ |
---|
| 1121 | sys.argv[3], \ |
---|
| 1122 | sys.argv[5], \ |
---|
| 1123 | backend) |
---|
| 1124 | else : |
---|
| 1125 | # Try to detect LPRng |
---|
[1606] | 1126 | # TODO : try to extract filename, options if available |
---|
| 1127 | jseen = Jseen = Pseen = nseen = rseen = Kseen = None |
---|
[1196] | 1128 | for arg in sys.argv : |
---|
| 1129 | if arg.startswith("-j") : |
---|
| 1130 | jseen = arg[2:].strip() |
---|
| 1131 | elif arg.startswith("-n") : |
---|
| 1132 | nseen = arg[2:].strip() |
---|
| 1133 | elif arg.startswith("-P") : |
---|
| 1134 | Pseen = arg[2:].strip() |
---|
| 1135 | elif arg.startswith("-r") : |
---|
| 1136 | rseen = arg[2:].strip() |
---|
[1606] | 1137 | elif arg.startswith("-J") : |
---|
| 1138 | Jseen = arg[2:].strip() |
---|
[1196] | 1139 | elif arg.startswith("-K") or arg.startswith("-#") : |
---|
| 1140 | Kseen = int(arg[2:].strip()) |
---|
| 1141 | if Kseen is None : |
---|
| 1142 | Kseen = 1 # we assume the user wants at least one copy... |
---|
| 1143 | if (rseen is None) and jseen and Pseen and nseen : |
---|
[1584] | 1144 | self.printInfo(_("Printer hostname undefined, set to 'localhost'"), "warn") |
---|
[1196] | 1145 | rseen = "localhost" |
---|
[1695] | 1146 | |
---|
| 1147 | spooldir = os.environ.get("SPOOL_DIR", ".") |
---|
[1600] | 1148 | try : |
---|
[1601] | 1149 | df_name = [line[8:] for line in os.environ.get("HF", "").split() if line.startswith("df_name=")][0] |
---|
[1600] | 1150 | except IndexError : |
---|
[1611] | 1151 | try : |
---|
| 1152 | cftransfername = [line[15:] for line in os.environ.get("HF", "").split() if line.startswith("cftransfername=")][0] |
---|
| 1153 | except IndexError : |
---|
[1695] | 1154 | try : |
---|
| 1155 | df_name = [line[1:] for line in os.environ.get("CONTROL", "").split() if line.startswith("fdf") or line.startswith("Udf")][0] |
---|
| 1156 | except IndexError : |
---|
| 1157 | inputfile = None |
---|
| 1158 | else : |
---|
| 1159 | inputfile = os.path.join(spooldir, df_name) |
---|
[1611] | 1160 | else : |
---|
[1695] | 1161 | inputfile = os.path.join(spooldir, "d" + cftransfername[1:]) |
---|
[1600] | 1162 | else : |
---|
[1695] | 1163 | inputfile = os.path.join(spooldir, df_name) |
---|
| 1164 | |
---|
[1196] | 1165 | if jseen and Pseen and nseen and rseen : |
---|
[1695] | 1166 | options = os.environ.get("HF", "") or os.environ.get("CONTROL", "") |
---|
[1606] | 1167 | return ("LPRNG", rseen, Pseen, nseen, jseen, inputfile, Kseen, Jseen, options, None) |
---|
[1584] | 1168 | self.printInfo(_("Printing system unknown, args=%s") % " ".join(sys.argv), "warn") |
---|
[1196] | 1169 | return (None, None, None, None, None, None, None, None, None, None) # Unknown printing system |
---|
[1229] | 1170 | |
---|
| 1171 | def getPrinterUserAndUserPQuota(self) : |
---|
| 1172 | """Returns a tuple (policy, printer, user, and user print quota) on this printer. |
---|
| 1173 | |
---|
| 1174 | "OK" is returned in the policy if both printer, user and user print quota |
---|
| 1175 | exist in the Quota Storage. |
---|
| 1176 | Otherwise, the policy as defined for this printer in pykota.conf is returned. |
---|
| 1177 | |
---|
| 1178 | If policy was set to "EXTERNAL" and one of printer, user, or user print quota |
---|
| 1179 | doesn't exist in the Quota Storage, then an external command is launched, as |
---|
| 1180 | defined in the external policy for this printer in pykota.conf |
---|
| 1181 | This external command can do anything, like automatically adding printers |
---|
| 1182 | or users, for example, and finally extracting printer, user and user print |
---|
| 1183 | quota from the Quota Storage is tried a second time. |
---|
| 1184 | |
---|
| 1185 | "EXTERNALERROR" is returned in case policy was "EXTERNAL" and an error status |
---|
| 1186 | was returned by the external command. |
---|
| 1187 | """ |
---|
[1245] | 1188 | for passnumber in range(1, 3) : |
---|
[1229] | 1189 | printer = self.storage.getPrinter(self.printername) |
---|
| 1190 | user = self.storage.getUser(self.username) |
---|
| 1191 | userpquota = self.storage.getUserPQuota(user, printer) |
---|
| 1192 | if printer.Exists and user.Exists and userpquota.Exists : |
---|
| 1193 | policy = "OK" |
---|
| 1194 | break |
---|
| 1195 | (policy, args) = self.config.getPrinterPolicy(self.printername) |
---|
| 1196 | if policy == "EXTERNAL" : |
---|
| 1197 | commandline = self.formatCommandLine(args, user, printer) |
---|
| 1198 | if not printer.Exists : |
---|
[1584] | 1199 | self.printInfo(_("Printer %s not registered in the PyKota system, applying external policy (%s) for printer %s") % (self.printername, commandline, self.printername)) |
---|
[1229] | 1200 | if not user.Exists : |
---|
[1584] | 1201 | self.printInfo(_("User %s not registered in the PyKota system, applying external policy (%s) for printer %s") % (self.username, commandline, self.printername)) |
---|
[1229] | 1202 | if not userpquota.Exists : |
---|
[1584] | 1203 | self.printInfo(_("User %s doesn't have quota on printer %s in the PyKota system, applying external policy (%s) for printer %s") % (self.username, self.printername, commandline, self.printername)) |
---|
[1229] | 1204 | if os.system(commandline) : |
---|
[1584] | 1205 | self.printInfo(_("External policy %s for printer %s produced an error. Job rejected. Please check PyKota's configuration files.") % (commandline, self.printername), "error") |
---|
[1229] | 1206 | policy = "EXTERNALERROR" |
---|
| 1207 | break |
---|
| 1208 | else : |
---|
[1271] | 1209 | if not printer.Exists : |
---|
[1584] | 1210 | self.printInfo(_("Printer %s not registered in the PyKota system, applying default policy (%s)") % (self.printername, policy)) |
---|
[1271] | 1211 | if not user.Exists : |
---|
[1584] | 1212 | self.printInfo(_("User %s not registered in the PyKota system, applying default policy (%s) for printer %s") % (self.username, policy, self.printername)) |
---|
[1271] | 1213 | if not userpquota.Exists : |
---|
[1584] | 1214 | self.printInfo(_("User %s doesn't have quota on printer %s in the PyKota system, applying default policy (%s)") % (self.username, self.printername, policy)) |
---|
[1229] | 1215 | break |
---|
[1271] | 1216 | if policy == "EXTERNAL" : |
---|
| 1217 | if not printer.Exists : |
---|
[1584] | 1218 | self.printInfo(_("Printer %s still not registered in the PyKota system, job will be rejected") % self.printername) |
---|
[1271] | 1219 | if not user.Exists : |
---|
[1584] | 1220 | self.printInfo(_("User %s still not registered in the PyKota system, job will be rejected on printer %s") % (self.username, self.printername)) |
---|
[1271] | 1221 | if not userpquota.Exists : |
---|
[1584] | 1222 | self.printInfo(_("User %s still doesn't have quota on printer %s in the PyKota system, job will be rejected") % (self.username, self.printername)) |
---|
[1229] | 1223 | return (policy, printer, user, userpquota) |
---|
| 1224 | |
---|
[1271] | 1225 | def mainWork(self) : |
---|
[1229] | 1226 | """Main work is done here.""" |
---|
| 1227 | (policy, printer, user, userpquota) = self.getPrinterUserAndUserPQuota() |
---|
[1271] | 1228 | # TODO : check for last user's quota in case pykota filter is used with querying |
---|
[1229] | 1229 | if policy == "EXTERNALERROR" : |
---|
| 1230 | # Policy was 'EXTERNAL' and the external command returned an error code |
---|
[1271] | 1231 | return self.removeJob() |
---|
[1229] | 1232 | elif policy == "EXTERNAL" : |
---|
| 1233 | # Policy was 'EXTERNAL' and the external command wasn't able |
---|
| 1234 | # to add either the printer, user or user print quota |
---|
[1271] | 1235 | return self.removeJob() |
---|
| 1236 | elif policy == "DENY" : |
---|
[1229] | 1237 | # Either printer, user or user print quota doesn't exist, |
---|
| 1238 | # and the job should be rejected. |
---|
[1271] | 1239 | return self.removeJob() |
---|
| 1240 | else : |
---|
| 1241 | if policy not in ("OK", "ALLOW") : |
---|
[1584] | 1242 | self.printInfo(_("Invalid policy %s for printer %s") % (policy, self.printername)) |
---|
[1271] | 1243 | return self.removeJob() |
---|
| 1244 | else : |
---|
| 1245 | return self.doWork(policy, printer, user, userpquota) |
---|