Merge of (apparently working :-) new printing notify code.
[tprouty/samba.git] / source / printing / notify.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 2.2
4    printing backend routines
5    Copyright (C) Tim Potter, 2002
6    Copyright (C) Gerald Carter,         2002
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "printing.h"
24
25 static TALLOC_CTX *send_ctx;
26
27 static struct notify_queue {
28         struct notify_queue *next, *prev;
29         void *buf;
30         size_t buflen;
31 } *notify_queue_head = NULL;
32
33 /*******************************************************************
34  Used to decide if we need a short select timeout.
35 *******************************************************************/
36
37 BOOL print_notify_messages_pending(void)
38 {
39         return (notify_queue_head != NULL);
40 }
41
42 /*******************************************************************
43  Actually send the batched messages.
44 *******************************************************************/
45
46 void print_notify_send_messages(void)
47 {
48         TDB_CONTEXT *tdb;
49         char *buf;
50         struct notify_queue *pq;
51         size_t msg_count = 0, offset = 0;
52
53         if (!print_notify_messages_pending())
54                 return;
55
56         if (!send_ctx)
57                 return;
58
59         tdb = conn_tdb_ctx();
60
61         if (!tdb) {
62                 DEBUG(3, ("Failed to open connections database in send_spoolss_notify2_msg\n"));
63                 return;
64         }
65         
66         /* Count the space needed to send the messages. */
67         for (pq = notify_queue_head; pq; pq = pq->next, msg_count++)
68                 offset += (pq->buflen + 4);
69                 
70         offset += 4; /* For count. */
71
72         buf = talloc(send_ctx, offset);
73         if (!buf) {
74                 DEBUG(0,("print_notify_send_messages: Out of memory\n"));
75                 talloc_destroy_pool(send_ctx);
76                 return;
77         }
78
79         offset = 0;
80         SIVAL(buf,offset,msg_count);
81         offset += 4;
82         for (pq = notify_queue_head; pq; pq = pq->next) {
83                 SIVAL(buf,offset,pq->buflen);
84                 offset += 4;
85                 memcpy(buf + offset, pq->buf, pq->buflen);
86                 offset += pq->buflen;
87         }
88
89         message_send_all(tdb, MSG_PRINTER_NOTIFY2, buf, offset, False, NULL);
90         talloc_destroy_pool(send_ctx);
91         notify_queue_head = NULL;
92 }
93
94 /*******************************************************************
95  Batch up print notify messages.
96 *******************************************************************/
97
98 static void send_spoolss_notify2_msg(struct spoolss_notify_msg *msg)
99 {
100         char *buf = NULL;
101         size_t buflen = 0, len;
102         struct notify_queue *pnqueue;
103
104         /* Let's not waste any time with this */
105
106         if (lp_disable_spoolss())
107                 return;
108
109         if (!send_ctx)
110                 send_ctx = talloc_init_named("print notify queue");
111
112         if (!send_ctx)
113                 goto fail;
114
115         /* Flatten data into a message */
116
117 again:
118         len = 0;
119
120         /* Pack header */
121
122         len += tdb_pack(buf + len, buflen - len, "f", msg->printer);
123
124         len += tdb_pack(buf + len, buflen - len, "ddddd",
125                         msg->type, msg->field, msg->id, msg->len, msg->flags);
126
127         /* Pack data */
128
129         if (msg->len == 0)
130                 len += tdb_pack(buf + len, buflen - len, "dd",
131                                 msg->notify.value[0], msg->notify.value[1]);
132         else
133                 len += tdb_pack(buf + len, buflen - len, "B",
134                                 msg->len, msg->notify.data);
135
136         if (buflen != len) {
137                 buf = talloc_realloc(send_ctx, buf, len);
138                 if (!buf)
139                         goto fail;
140                 buflen = len;
141                 goto again;
142         }
143
144         /* Store the message on the pending queue. */
145
146         pnqueue = talloc(send_ctx, sizeof(*pnqueue));
147         if (!pnqueue)
148                 goto fail;
149
150         pnqueue->buf = buf;
151         pnqueue->buflen = buflen;
152         DLIST_ADD(notify_queue_head, pnqueue);
153         return;
154
155   fail:
156
157         DEBUG(0,("send_spoolss_notify2_msg: Out of memory.\n"));
158 }
159
160 static void send_notify_field_values(const char *printer_name, uint32 type,
161                                      uint32 field, uint32 id, uint32 value1, 
162                                      uint32 value2, uint32 flags)
163 {
164         struct spoolss_notify_msg msg;
165
166         ZERO_STRUCT(msg);
167
168         fstrcpy(msg.printer, printer_name);
169         msg.type = type;
170         msg.field = field;
171         msg.id = id;
172         msg.notify.value[0] = value1;
173         msg.notify.value[1] = value2;
174         msg.flags = flags;
175
176         send_spoolss_notify2_msg(&msg);
177 }
178
179 static void send_notify_field_buffer(const char *printer_name, uint32 type,
180                                      uint32 field, uint32 id, uint32 len,
181                                      char *buffer)
182 {
183         struct spoolss_notify_msg msg;
184
185         ZERO_STRUCT(msg);
186
187         fstrcpy(msg.printer, printer_name);
188         msg.type = type;
189         msg.field = field;
190         msg.id = id;
191         msg.len = len;
192         msg.notify.data = buffer;
193
194         send_spoolss_notify2_msg(&msg);
195 }
196
197 /* Send a message that the printer status has changed */
198
199 void notify_printer_status_byname(const char *printer_name, uint32 status)
200 {
201         /* Printer status stored in value1 */
202
203         send_notify_field_values(printer_name, PRINTER_NOTIFY_TYPE, 
204                                  PRINTER_NOTIFY_STATUS, 0, 
205                                  status, 0, 0);
206 }
207
208 void notify_printer_status(int snum, uint32 status)
209 {
210         const char *printer_name = SERVICE(snum); 
211
212         if (printer_name)
213                 notify_printer_status_byname(printer_name, status);
214 }
215
216 void notify_job_status_byname(const char *printer_name, uint32 jobid, uint32 status,
217                               uint32 flags)
218 {
219         /* Job id stored in id field, status in value1 */
220
221         send_notify_field_values(printer_name, JOB_NOTIFY_TYPE,
222                                  JOB_NOTIFY_STATUS, jobid,
223                                  status, 0, flags);
224 }
225
226 void notify_job_status(int snum, uint32 jobid, uint32 status)
227 {
228         const char *printer_name = SERVICE(snum);
229
230         notify_job_status_byname(printer_name, jobid, status, 0);
231 }
232
233 void notify_job_total_bytes(int snum, uint32 jobid, uint32 size)
234 {
235         const char *printer_name = SERVICE(snum);
236
237         /* Job id stored in id field, status in value1 */
238
239         send_notify_field_values(printer_name, JOB_NOTIFY_TYPE,
240                                  JOB_NOTIFY_TOTAL_BYTES, jobid,
241                                  size, 0, 0);
242 }
243
244 void notify_job_total_pages(int snum, uint32 jobid, uint32 pages)
245 {
246         const char *printer_name = SERVICE(snum);
247
248         /* Job id stored in id field, status in value1 */
249
250         send_notify_field_values(printer_name, JOB_NOTIFY_TYPE,
251                                  JOB_NOTIFY_TOTAL_PAGES, jobid,
252                                  pages, 0, 0);
253 }
254
255 void notify_job_username(int snum, uint32 jobid, char *name)
256 {
257         const char *printer_name = SERVICE(snum);
258
259         send_notify_field_buffer(
260                 printer_name, JOB_NOTIFY_TYPE, JOB_NOTIFY_USER_NAME,
261                 jobid, strlen(name) + 1, name);
262 }
263
264 void notify_job_name(int snum, uint32 jobid, char *name)
265 {
266         const char *printer_name = SERVICE(snum);
267
268         send_notify_field_buffer(
269                 printer_name, JOB_NOTIFY_TYPE, JOB_NOTIFY_DOCUMENT,
270                 jobid, strlen(name) + 1, name);
271 }
272
273 void notify_job_submitted(int snum, uint32 jobid, time_t submitted)
274 {
275         const char *printer_name = SERVICE(snum);
276
277         send_notify_field_buffer(
278                 printer_name, JOB_NOTIFY_TYPE, JOB_NOTIFY_SUBMITTED,
279                 jobid, sizeof(submitted), (char *)&submitted);
280 }
281
282 void notify_printer_driver(int snum, char *driver_name)
283 {
284         const char *printer_name = SERVICE(snum);
285
286         send_notify_field_buffer(
287                 printer_name, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_DRIVER_NAME,
288                 snum, strlen(driver_name) + 1, driver_name);
289 }
290
291 void notify_printer_comment(int snum, char *comment)
292 {
293         const char *printer_name = SERVICE(snum);
294
295         send_notify_field_buffer(
296                 printer_name, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_COMMENT,
297                 snum, strlen(comment) + 1, comment);
298 }
299
300 void notify_printer_sharename(int snum, char *share_name)
301 {
302         const char *printer_name = SERVICE(snum);
303
304         send_notify_field_buffer(
305                 printer_name, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_SHARE_NAME,
306                 snum, strlen(share_name) + 1, share_name);
307 }
308
309 void notify_printer_port(int snum, char *port_name)
310 {
311         const char *printer_name = SERVICE(snum);
312
313         send_notify_field_buffer(
314                 printer_name, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_PORT_NAME,
315                 snum, strlen(port_name) + 1, port_name);
316 }
317
318 void notify_printer_location(int snum, char *location)
319 {
320         const char *printer_name = SERVICE(snum);
321
322         send_notify_field_buffer(
323                 printer_name, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_LOCATION,
324                 snum, strlen(location) + 1, location);
325 }