1 | #!/usr/bin/env python |
---|
2 | |
---|
3 | # (c) 2010 Allison Jones <ajones6@unl.edu> |
---|
4 | # This program is free software: you can redistribute it and/or modify |
---|
5 | # it under the terms of the GNU General Public License as published by |
---|
6 | # the Free Software Foundation, either version 3 of the License, or |
---|
7 | # (at your option) any later version. |
---|
8 | # |
---|
9 | # This program is distributed in the hope that it will be useful, |
---|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
12 | # GNU General Public License for more details. |
---|
13 | # |
---|
14 | # You should have received a copy of the GNU General Public License |
---|
15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
16 | # |
---|
17 | # |
---|
18 | # In addition to pkipplib, it also relies on the python-xmpp module |
---|
19 | # |
---|
20 | # $Id$ |
---|
21 | |
---|
22 | import sys |
---|
23 | import os |
---|
24 | import fcntl |
---|
25 | from pkipplib import pkipplib |
---|
26 | import xmpp |
---|
27 | |
---|
28 | if __name__ == "__main__" : |
---|
29 | # |
---|
30 | # Set some variables for later |
---|
31 | # You may or may not have to use an authenticated call, depending on your |
---|
32 | # cups conf file. If your notifier fails, there's a good chance you |
---|
33 | # need to use the authenticated call to pkipplib.CUPS |
---|
34 | # |
---|
35 | # Also, this assumes a 1:1 match between usernames on the print server |
---|
36 | # and usernames on the jabber server. |
---|
37 | |
---|
38 | cups_server = "https://cups.example.com:631" |
---|
39 | cups_username = "username" |
---|
40 | cups_password = "password" |
---|
41 | |
---|
42 | jabber_server = "jabber.example.com" |
---|
43 | jabber_username = "username" |
---|
44 | jabber_password = "password" |
---|
45 | |
---|
46 | # First thing we do is put stdin in non-blocking mode. |
---|
47 | fd = sys.stdin.fileno() |
---|
48 | fcntl.fcntl(sys.stdin.fileno(), fcntl.F_SETFL, |
---|
49 | fcntl.fcntl(fd, fcntl.F_GETFL) | os.O_NONBLOCK) |
---|
50 | |
---|
51 | # then we read the notification CUPS sent us to our stdin |
---|
52 | notification = pkipplib.IPPRequest(sys.stdin.read()) |
---|
53 | |
---|
54 | # now we parse it |
---|
55 | notification.parse() |
---|
56 | event = notification.event_notification["notify-subscribed-event"][0][1] |
---|
57 | |
---|
58 | # Get the printername, jobnumber, and jobname. The username unfortunately |
---|
59 | # isn't in this piece |
---|
60 | if event in ("job-completed") : |
---|
61 | printername = notification.event_notification["printer-name"][0][1] |
---|
62 | jobnumber = notification.event_notification["notify-job-id"][0][1] |
---|
63 | jobname = notification.event_notification["job-name"][0][1] |
---|
64 | |
---|
65 | # In the next bit, we get more information from the job which is stored in |
---|
66 | # the cups spool directory, as long as you are retaining job information |
---|
67 | # This is where we can get the username |
---|
68 | cups = pkipplib.CUPS(url=cups_server, \ |
---|
69 | username=cups_username, \ |
---|
70 | password=cups_password) |
---|
71 | |
---|
72 | answer = cups.getJobAttributes(jobnumber) |
---|
73 | username = answer.job["job-originating-user-name"][0][1] |
---|
74 | |
---|
75 | # Get the location and info about the printer. Useful in an office that |
---|
76 | # has mounds of printers |
---|
77 | |
---|
78 | request = cups.newRequest(pkipplib.IPP_GET_PRINTER_ATTRIBUTES) |
---|
79 | request.operation["printer-uri"] = ("uri", |
---|
80 | cups.identifierToURI("printers", printername)) |
---|
81 | for attribute in ("printer-uri-supported", "printer-type", "member-uris") : |
---|
82 | request.operation["requested-attributes"] = ("nameWithoutLanguage", attribute) |
---|
83 | answer = cups.doRequest(request) |
---|
84 | printerLocation = answer.printer["printer-location"][0][1] |
---|
85 | printerInfo = answer.printer["printer-info"][0][1] |
---|
86 | |
---|
87 | # You might consider adding verbiage to indicate it's an automatically |
---|
88 | # generated message. Otherwise, it might take some people by surprise. |
---|
89 | |
---|
90 | jabberMessage = "\nYour print job: " + jobname + "\nSuccessfully printed to " + printerInfo + "\nLocation: " + printerLocation |
---|
91 | tojid = username + "@" + jabber_server |
---|
92 | jidparams={} |
---|
93 | jidparams["jid"] = jabber_username + "@" + jabber_server |
---|
94 | jid = xmpp.protocol.JID(jidparams['jid']) |
---|
95 | jidparams["password"] = jabber_password |
---|
96 | cl=xmpp.Client(jid.getDomain(),debug=[]) |
---|
97 | con=cl.connect() |
---|
98 | if not con: |
---|
99 | print 'could not connect!' |
---|
100 | sys.exit() |
---|
101 | auth=cl.auth(jid.getNode(),jidparams['password'],resource=jid.getResource()) |
---|
102 | if not auth: |
---|
103 | print 'could not authenticate!' |
---|
104 | sys.exit() |
---|
105 | id = cl.send(xmpp.protocol.Message(tojid,jabberMessage)) |
---|