[1901] | 1 | #! /usr/bin/env python |
---|
| 2 | # -*- coding: ISO-8859-15 -*- |
---|
| 3 | # |
---|
| 4 | # PyKota - Print Quotas for CUPS and LPRng |
---|
| 5 | # |
---|
| 6 | # (c) 2003-2004 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.1 2004/11/06 22:35:58 jalet |
---|
| 25 | # Added a miniparser for IPP messages (RFC 2910). The job-originating-host-name |
---|
| 26 | # retrieval is now fiable, unless the CUPS developpers change something... |
---|
| 27 | # |
---|
| 28 | # |
---|
| 29 | |
---|
| 30 | import sys |
---|
| 31 | from struct import unpack |
---|
| 32 | |
---|
| 33 | OPERATION_ATTRIBUTES_TAG = 0x01 |
---|
| 34 | JOB_ATTRIBUTES_TAG = 0x02 |
---|
| 35 | END_OF_ATTRIBUTES_TAG = 0x03 |
---|
| 36 | PRINTER_ATTRIBUTES_TAG = 0x04 |
---|
| 37 | UNSUPPORTED_ATTRIBUTES_TAG = 0x05 |
---|
| 38 | |
---|
| 39 | class PyKotaIPPError(Exception): |
---|
| 40 | """An exception for PyKota IPP related stuff.""" |
---|
| 41 | def __init__(self, message = ""): |
---|
| 42 | self.message = message |
---|
| 43 | Exception.__init__(self, message) |
---|
| 44 | def __repr__(self): |
---|
| 45 | return self.message |
---|
| 46 | __str__ = __repr__ |
---|
| 47 | |
---|
| 48 | class IPPMessage : |
---|
| 49 | """A class for IPP message files.""" |
---|
| 50 | def __init__(self, data) : |
---|
| 51 | """Initializes an IPP Message object.""" |
---|
| 52 | self.data = data |
---|
| 53 | self._attributes = {} |
---|
| 54 | self.curname = None |
---|
| 55 | self.tags = [ None ] * 256 # by default all tags reserved |
---|
| 56 | |
---|
| 57 | # Delimiter tags |
---|
| 58 | self.tags[0x01] = "operation-attributes-tag" |
---|
| 59 | self.tags[0x02] = "job-attributes-tag" |
---|
| 60 | self.tags[0x03] = "end-of-attributes-tag" |
---|
| 61 | self.tags[0x04] = "printer-attributes-tag" |
---|
| 62 | self.tags[0x05] = "unsupported-attributes-tag" |
---|
| 63 | |
---|
| 64 | # out of band values |
---|
| 65 | self.tags[0x10] = "unsupported" |
---|
| 66 | self.tags[0x11] = "reserved-for-future-default" |
---|
| 67 | self.tags[0x12] = "unknown" |
---|
| 68 | self.tags[0x13] = "no-value" |
---|
| 69 | |
---|
| 70 | # integer values |
---|
| 71 | self.tags[0x20] = "generic-integer" |
---|
| 72 | self.tags[0x21] = "integer" |
---|
| 73 | self.tags[0x22] = "boolean" |
---|
| 74 | self.tags[0x23] = "enum" |
---|
| 75 | |
---|
| 76 | # octetString |
---|
| 77 | self.tags[0x30] = "octetString-with-an-unspecified-format" |
---|
| 78 | self.tags[0x31] = "dateTime" |
---|
| 79 | self.tags[0x32] = "resolution" |
---|
| 80 | self.tags[0x33] = "rangeOfInteger" |
---|
| 81 | self.tags[0x34] = "reserved-for-collection" |
---|
| 82 | self.tags[0x35] = "textWithLanguage" |
---|
| 83 | self.tags[0x36] = "nameWithLanguage" |
---|
| 84 | |
---|
| 85 | # character strings |
---|
| 86 | self.tags[0x20] = "generic-character-string" |
---|
| 87 | self.tags[0x41] = "textWithoutLanguage" |
---|
| 88 | self.tags[0x42] = "nameWithoutLanguage" |
---|
| 89 | # self.tags[0x43] = "reserved" |
---|
| 90 | self.tags[0x44] = "keyword" |
---|
| 91 | self.tags[0x45] = "uri" |
---|
| 92 | self.tags[0x46] = "uriScheme" |
---|
| 93 | self.tags[0x47] = "charset" |
---|
| 94 | self.tags[0x48] = "naturalLanguage" |
---|
| 95 | self.tags[0x49] = "mimeMediaType" |
---|
| 96 | |
---|
| 97 | # now parses the IPP message |
---|
| 98 | self.parse() |
---|
| 99 | |
---|
| 100 | def __getattr__(self, attrname) : |
---|
| 101 | """Allows self.attributes to return the attributes names.""" |
---|
| 102 | if attrname == "attributes" : |
---|
| 103 | keys = self._attributes.keys() |
---|
| 104 | keys.sort() |
---|
| 105 | return keys |
---|
| 106 | raise AttributeError, attrname |
---|
| 107 | |
---|
| 108 | def __getitem__(self, ippattrname) : |
---|
| 109 | """Fakes a dictionnary d['key'] notation.""" |
---|
| 110 | value = self._attributes.get(ippattrname) |
---|
| 111 | if value is not None : |
---|
| 112 | if len(value) == 1 : |
---|
| 113 | value = value[0] |
---|
| 114 | return value |
---|
| 115 | get = __getitem__ |
---|
| 116 | |
---|
| 117 | def parseTag(self) : |
---|
| 118 | """Extracts information from an IPP tag.""" |
---|
| 119 | pos = self.position |
---|
| 120 | valuetag = self.tags[ord(self.data[pos])] |
---|
| 121 | # print valuetag.get("name") |
---|
| 122 | pos += 1 |
---|
| 123 | posend = pos2 = pos + 2 |
---|
| 124 | namelength = unpack(">H", self.data[pos:pos2])[0] |
---|
| 125 | if not namelength : |
---|
| 126 | name = self.curname |
---|
| 127 | else : |
---|
| 128 | posend += namelength |
---|
| 129 | self.curname = name = self.data[pos2:posend] |
---|
| 130 | pos2 = posend + 2 |
---|
| 131 | valuelength = unpack(">H", self.data[posend:pos2])[0] |
---|
| 132 | posend = pos2 + valuelength |
---|
| 133 | value = self.data[pos2:posend] |
---|
| 134 | oldval = self._attributes.setdefault(name, []) |
---|
| 135 | oldval.append(value) |
---|
| 136 | return posend - self.position |
---|
| 137 | |
---|
| 138 | def operation_attributes_tag(self) : |
---|
| 139 | """Indicates that the parser enters into an operation-attributes-tag group.""" |
---|
| 140 | return self.parseTag() |
---|
| 141 | |
---|
| 142 | def job_attributes_tag(self) : |
---|
| 143 | """Indicates that the parser enters into an operation-attributes-tag group.""" |
---|
| 144 | return self.parseTag() |
---|
| 145 | |
---|
| 146 | def printer_attributes_tag(self) : |
---|
| 147 | """Indicates that the parser enters into an operation-attributes-tag group.""" |
---|
| 148 | return self.parseTag() |
---|
| 149 | |
---|
| 150 | def parse(self) : |
---|
| 151 | """Parses an IPP Message. |
---|
| 152 | |
---|
| 153 | NB : Only a subset of RFC2910 is implemented. |
---|
| 154 | We are only interested in textual informations for now anyway. |
---|
| 155 | """ |
---|
| 156 | self.version = "%s.%s" % (ord(self.data[0]), ord(self.data[1])) |
---|
| 157 | self.operation_id = "0x%04x" % unpack(">H", self.data[2:4])[0] |
---|
| 158 | self.request_id = "0x%08x" % unpack(">I", self.data[4:8])[0] |
---|
| 159 | self.position = 8 |
---|
| 160 | try : |
---|
| 161 | tag = ord(self.data[self.position]) |
---|
| 162 | while tag != END_OF_ATTRIBUTES_TAG : |
---|
| 163 | self.position += 1 |
---|
| 164 | name = self.tags[tag] |
---|
| 165 | if name is not None : |
---|
| 166 | func = getattr(self, name.replace("-", "_"), None) |
---|
| 167 | if func is not None : |
---|
| 168 | self.position += func() |
---|
| 169 | if ord(self.data[self.position]) > UNSUPPORTED_ATTRIBUTES_TAG : |
---|
| 170 | self.position -= 1 |
---|
| 171 | continue |
---|
| 172 | tag = ord(self.data[self.position]) |
---|
| 173 | except IndexError : |
---|
| 174 | raise PyKotaIPPError, "Unexpected end of IPP message." |
---|
| 175 | |
---|