1 | #include <stdlib.h> |
---|
2 | #include <unistd.h> |
---|
3 | #include <sys/types.h> |
---|
4 | #include <sys/wait.h> |
---|
5 | #include <sys/stat.h> |
---|
6 | #include <string.h> |
---|
7 | #include "owl.h" |
---|
8 | |
---|
9 | #ifdef HAVE_LIBZEPHYR |
---|
10 | static GList *deferred_subs = NULL; |
---|
11 | |
---|
12 | typedef struct _owl_sub_list { /* noproto */ |
---|
13 | ZSubscription_t *subs; |
---|
14 | int nsubs; |
---|
15 | } owl_sub_list; |
---|
16 | |
---|
17 | Code_t ZResetAuthentication(void); |
---|
18 | #endif |
---|
19 | |
---|
20 | #define HM_SVC_FALLBACK htons((unsigned short) 2104) |
---|
21 | |
---|
22 | static char *owl_zephyr_dotfile(const char *name, const char *input) |
---|
23 | { |
---|
24 | if (input != NULL) |
---|
25 | return g_strdup(input); |
---|
26 | else |
---|
27 | return g_strdup_printf("%s/%s", owl_global_get_homedir(&g), name); |
---|
28 | } |
---|
29 | |
---|
30 | #ifdef HAVE_LIBZEPHYR |
---|
31 | void owl_zephyr_initialize(void) |
---|
32 | { |
---|
33 | int ret; |
---|
34 | struct servent *sp; |
---|
35 | struct sockaddr_in sin; |
---|
36 | ZNotice_t req; |
---|
37 | Code_t code; |
---|
38 | |
---|
39 | /* |
---|
40 | * Code modified from libzephyr's ZhmStat.c |
---|
41 | * |
---|
42 | * Modified to add the fd to our select loop, rather than hanging |
---|
43 | * until we get an ack. |
---|
44 | */ |
---|
45 | |
---|
46 | if ((ret = ZOpenPort(NULL)) != ZERR_NONE) { |
---|
47 | owl_function_error("Error opening Zephyr port: %s", error_message(ret)); |
---|
48 | return; |
---|
49 | } |
---|
50 | |
---|
51 | (void) memset(&sin, 0, sizeof(struct sockaddr_in)); |
---|
52 | |
---|
53 | sp = getservbyname(HM_SVCNAME, "udp"); |
---|
54 | |
---|
55 | sin.sin_port = (sp) ? sp->s_port : HM_SVC_FALLBACK; |
---|
56 | sin.sin_family = AF_INET; |
---|
57 | |
---|
58 | sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK); |
---|
59 | |
---|
60 | (void) memset(&req, 0, sizeof(req)); |
---|
61 | req.z_kind = STAT; |
---|
62 | req.z_port = 0; |
---|
63 | req.z_class = zstr(HM_STAT_CLASS); |
---|
64 | req.z_class_inst = zstr(HM_STAT_CLIENT); |
---|
65 | req.z_opcode = zstr(HM_GIMMESTATS); |
---|
66 | req.z_sender = zstr(""); |
---|
67 | req.z_recipient = zstr(""); |
---|
68 | req.z_default_format = zstr(""); |
---|
69 | req.z_message_len = 0; |
---|
70 | |
---|
71 | if ((code = ZSetDestAddr(&sin)) != ZERR_NONE) { |
---|
72 | owl_function_error("Initializing Zephyr: %s", error_message(code)); |
---|
73 | return; |
---|
74 | } |
---|
75 | |
---|
76 | if ((code = ZSendNotice(&req, ZNOAUTH)) != ZERR_NONE) { |
---|
77 | owl_function_error("Initializing Zephyr: %s", error_message(code)); |
---|
78 | return; |
---|
79 | } |
---|
80 | |
---|
81 | owl_select_add_io_dispatch(ZGetFD(), OWL_IO_READ|OWL_IO_EXCEPT, &owl_zephyr_finish_initialization, NULL, NULL); |
---|
82 | } |
---|
83 | |
---|
84 | void owl_zephyr_finish_initialization(const owl_io_dispatch *d, void *data) { |
---|
85 | Code_t code; |
---|
86 | char *perl; |
---|
87 | |
---|
88 | owl_select_remove_io_dispatch(d); |
---|
89 | |
---|
90 | ZClosePort(); |
---|
91 | |
---|
92 | if ((code = ZInitialize()) != ZERR_NONE) { |
---|
93 | owl_function_error("Initializing Zephyr: %s", error_message(code)); |
---|
94 | return; |
---|
95 | } |
---|
96 | |
---|
97 | if ((code = ZOpenPort(NULL)) != ZERR_NONE) { |
---|
98 | owl_function_error("Initializing Zephyr: %s", error_message(code)); |
---|
99 | return; |
---|
100 | } |
---|
101 | |
---|
102 | owl_select_add_io_dispatch(ZGetFD(), OWL_IO_READ|OWL_IO_EXCEPT, &owl_zephyr_process_events, NULL, NULL); |
---|
103 | |
---|
104 | owl_global_set_havezephyr(&g); |
---|
105 | |
---|
106 | if(g.load_initial_subs) { |
---|
107 | owl_zephyr_load_initial_subs(); |
---|
108 | } |
---|
109 | while(deferred_subs != NULL) { |
---|
110 | owl_sub_list *subs = deferred_subs->data; |
---|
111 | owl_function_debugmsg("Loading %d deferred subs.", subs->nsubs); |
---|
112 | owl_zephyr_loadsubs_helper(subs->subs, subs->nsubs); |
---|
113 | deferred_subs = g_list_delete_link(deferred_subs, deferred_subs); |
---|
114 | g_free(subs); |
---|
115 | } |
---|
116 | |
---|
117 | /* zlog in if we need to */ |
---|
118 | if (owl_global_is_startuplogin(&g)) { |
---|
119 | owl_function_debugmsg("startup: doing zlog in"); |
---|
120 | owl_zephyr_zlog_in(); |
---|
121 | } |
---|
122 | /* check pseudo-logins if we need to */ |
---|
123 | if (owl_global_is_pseudologins(&g)) { |
---|
124 | owl_function_debugmsg("startup: checking pseudo-logins"); |
---|
125 | owl_function_zephyr_buddy_check(0); |
---|
126 | } |
---|
127 | |
---|
128 | perl = owl_perlconfig_execute("BarnOwl::Zephyr::_zephyr_startup()"); |
---|
129 | g_free(perl); |
---|
130 | |
---|
131 | owl_select_add_pre_select_action(owl_zephyr_pre_select_action, NULL, NULL); |
---|
132 | } |
---|
133 | |
---|
134 | void owl_zephyr_load_initial_subs(void) { |
---|
135 | int ret_sd, ret_bd, ret_u; |
---|
136 | |
---|
137 | owl_function_debugmsg("startup: loading initial zephyr subs"); |
---|
138 | |
---|
139 | /* load default subscriptions */ |
---|
140 | ret_sd = owl_zephyr_loaddefaultsubs(); |
---|
141 | |
---|
142 | /* load Barnowl default subscriptions */ |
---|
143 | ret_bd = owl_zephyr_loadbarnowldefaultsubs(); |
---|
144 | |
---|
145 | /* load subscriptions from subs file */ |
---|
146 | ret_u = owl_zephyr_loadsubs(NULL, 0); |
---|
147 | |
---|
148 | if (ret_sd || ret_bd || ret_u) { |
---|
149 | owl_function_error("Error loading zephyr subscriptions"); |
---|
150 | } |
---|
151 | |
---|
152 | /* load login subscriptions */ |
---|
153 | if (owl_global_is_loginsubs(&g)) { |
---|
154 | owl_function_debugmsg("startup: loading login subs"); |
---|
155 | owl_function_loadloginsubs(NULL); |
---|
156 | } |
---|
157 | } |
---|
158 | #else |
---|
159 | void owl_zephyr_initialize(void) |
---|
160 | { |
---|
161 | } |
---|
162 | #endif |
---|
163 | |
---|
164 | |
---|
165 | int owl_zephyr_shutdown(void) |
---|
166 | { |
---|
167 | #ifdef HAVE_LIBZEPHYR |
---|
168 | if(owl_global_is_havezephyr(&g)) { |
---|
169 | unsuball(); |
---|
170 | ZClosePort(); |
---|
171 | } |
---|
172 | #endif |
---|
173 | return 0; |
---|
174 | } |
---|
175 | |
---|
176 | int owl_zephyr_zpending(void) |
---|
177 | { |
---|
178 | #ifdef HAVE_LIBZEPHYR |
---|
179 | Code_t code; |
---|
180 | if(owl_global_is_havezephyr(&g)) { |
---|
181 | if((code = ZPending()) < 0) { |
---|
182 | owl_function_debugmsg("Error (%s) in ZPending()\n", |
---|
183 | error_message(code)); |
---|
184 | return 0; |
---|
185 | } |
---|
186 | return code; |
---|
187 | } |
---|
188 | #endif |
---|
189 | return 0; |
---|
190 | } |
---|
191 | |
---|
192 | const char *owl_zephyr_get_realm(void) |
---|
193 | { |
---|
194 | #ifdef HAVE_LIBZEPHYR |
---|
195 | if (owl_global_is_havezephyr(&g)) |
---|
196 | return(ZGetRealm()); |
---|
197 | #endif |
---|
198 | return ""; |
---|
199 | } |
---|
200 | |
---|
201 | const char *owl_zephyr_get_sender(void) |
---|
202 | { |
---|
203 | #ifdef HAVE_LIBZEPHYR |
---|
204 | if (owl_global_is_havezephyr(&g)) |
---|
205 | return(ZGetSender()); |
---|
206 | #endif |
---|
207 | return ""; |
---|
208 | } |
---|
209 | |
---|
210 | #ifdef HAVE_LIBZEPHYR |
---|
211 | int owl_zephyr_loadsubs_helper(ZSubscription_t subs[], int count) |
---|
212 | { |
---|
213 | int ret = 0; |
---|
214 | if (owl_global_is_havezephyr(&g)) { |
---|
215 | int i; |
---|
216 | /* sub without defaults */ |
---|
217 | if (ZSubscribeToSansDefaults(subs,count,0) != ZERR_NONE) { |
---|
218 | owl_function_error("Error subscribing to zephyr notifications."); |
---|
219 | ret=-2; |
---|
220 | } |
---|
221 | |
---|
222 | /* free stuff */ |
---|
223 | for (i=0; i<count; i++) { |
---|
224 | g_free(subs[i].zsub_class); |
---|
225 | g_free(subs[i].zsub_classinst); |
---|
226 | g_free(subs[i].zsub_recipient); |
---|
227 | } |
---|
228 | |
---|
229 | g_free(subs); |
---|
230 | } else { |
---|
231 | owl_sub_list *s = g_new(owl_sub_list, 1); |
---|
232 | s->subs = subs; |
---|
233 | s->nsubs = count; |
---|
234 | deferred_subs = g_list_append(deferred_subs, s); |
---|
235 | } |
---|
236 | |
---|
237 | return ret; |
---|
238 | } |
---|
239 | #endif |
---|
240 | |
---|
241 | /* Load zephyr subscriptions from 'filename'. If 'filename' is NULL, |
---|
242 | * the default file $HOME/.zephyr.subs will be used. |
---|
243 | * |
---|
244 | * Returns 0 on success. If the file does not exist, return -1 if |
---|
245 | * 'error_on_nofile' is 1, otherwise return 0. Return -1 if the file |
---|
246 | * exists but can not be read. Return -2 if there is a failure from |
---|
247 | * zephyr to load the subscriptions. |
---|
248 | */ |
---|
249 | int owl_zephyr_loadsubs(const char *filename, int error_on_nofile) |
---|
250 | { |
---|
251 | #ifdef HAVE_LIBZEPHYR |
---|
252 | FILE *file; |
---|
253 | char *tmp, *start; |
---|
254 | char *buffer = NULL; |
---|
255 | char *subsfile; |
---|
256 | ZSubscription_t *subs; |
---|
257 | int subSize = 1024; |
---|
258 | int count; |
---|
259 | struct stat statbuff; |
---|
260 | |
---|
261 | subs = g_new(ZSubscription_t, subSize); |
---|
262 | subsfile = owl_zephyr_dotfile(".zephyr.subs", filename); |
---|
263 | |
---|
264 | if (stat(subsfile, &statbuff) != 0) { |
---|
265 | g_free(subsfile); |
---|
266 | if (error_on_nofile == 1) |
---|
267 | return -1; |
---|
268 | return 0; |
---|
269 | } |
---|
270 | |
---|
271 | ZResetAuthentication(); |
---|
272 | count = 0; |
---|
273 | file = fopen(subsfile, "r"); |
---|
274 | g_free(subsfile); |
---|
275 | if (!file) |
---|
276 | return -1; |
---|
277 | while (owl_getline(&buffer, file)) { |
---|
278 | if (buffer[0] == '#' || buffer[0] == '\n') |
---|
279 | continue; |
---|
280 | |
---|
281 | if (buffer[0] == '-') |
---|
282 | start = buffer + 1; |
---|
283 | else |
---|
284 | start = buffer; |
---|
285 | |
---|
286 | if (count >= subSize) { |
---|
287 | subSize *= 2; |
---|
288 | subs = g_renew(ZSubscription_t, subs, subSize); |
---|
289 | } |
---|
290 | |
---|
291 | /* add it to the list of subs */ |
---|
292 | if ((tmp = strtok(start, ",\n\r")) == NULL) |
---|
293 | continue; |
---|
294 | subs[count].zsub_class = g_strdup(tmp); |
---|
295 | if ((tmp=strtok(NULL, ",\n\r")) == NULL) |
---|
296 | continue; |
---|
297 | subs[count].zsub_classinst = g_strdup(tmp); |
---|
298 | if ((tmp = strtok(NULL, " \t\n\r")) == NULL) |
---|
299 | continue; |
---|
300 | subs[count].zsub_recipient = g_strdup(tmp); |
---|
301 | |
---|
302 | /* if it started with '-' then add it to the global punt list, and |
---|
303 | * remove it from the list of subs. */ |
---|
304 | if (buffer[0] == '-') { |
---|
305 | owl_function_zpunt(subs[count].zsub_class, subs[count].zsub_classinst, subs[count].zsub_recipient, 0); |
---|
306 | g_free(subs[count].zsub_class); |
---|
307 | g_free(subs[count].zsub_classinst); |
---|
308 | g_free(subs[count].zsub_recipient); |
---|
309 | } else { |
---|
310 | count++; |
---|
311 | } |
---|
312 | } |
---|
313 | fclose(file); |
---|
314 | if (buffer) |
---|
315 | g_free(buffer); |
---|
316 | |
---|
317 | return owl_zephyr_loadsubs_helper(subs, count); |
---|
318 | #else |
---|
319 | return 0; |
---|
320 | #endif |
---|
321 | } |
---|
322 | |
---|
323 | /* Load default Barnowl subscriptions |
---|
324 | * |
---|
325 | * Returns 0 on success. |
---|
326 | * Return -2 if there is a failure from zephyr to load the subscriptions. |
---|
327 | */ |
---|
328 | int owl_zephyr_loadbarnowldefaultsubs(void) |
---|
329 | { |
---|
330 | #ifdef HAVE_LIBZEPHYR |
---|
331 | ZSubscription_t *subs; |
---|
332 | int subSize = 10; /* Max Barnowl default subs we allow */ |
---|
333 | int count, ret; |
---|
334 | |
---|
335 | subs = g_new(ZSubscription_t, subSize); |
---|
336 | ZResetAuthentication(); |
---|
337 | count=0; |
---|
338 | |
---|
339 | subs[count].zsub_class=g_strdup("message"); |
---|
340 | subs[count].zsub_classinst=g_strdup("*"); |
---|
341 | subs[count].zsub_recipient=g_strdup("%me%"); |
---|
342 | count++; |
---|
343 | |
---|
344 | ret = owl_zephyr_loadsubs_helper(subs, count); |
---|
345 | return(ret); |
---|
346 | #else |
---|
347 | return(0); |
---|
348 | #endif |
---|
349 | } |
---|
350 | |
---|
351 | int owl_zephyr_loaddefaultsubs(void) |
---|
352 | { |
---|
353 | #ifdef HAVE_LIBZEPHYR |
---|
354 | if (owl_global_is_havezephyr(&g)) { |
---|
355 | ZSubscription_t subs[10]; |
---|
356 | |
---|
357 | if (ZSubscribeTo(subs,0,0) != ZERR_NONE) { |
---|
358 | owl_function_error("Error subscribing to default zephyr notifications."); |
---|
359 | return(-1); |
---|
360 | } |
---|
361 | } |
---|
362 | return(0); |
---|
363 | #else |
---|
364 | return(0); |
---|
365 | #endif |
---|
366 | } |
---|
367 | |
---|
368 | int owl_zephyr_loadloginsubs(const char *filename) |
---|
369 | { |
---|
370 | #ifdef HAVE_LIBZEPHYR |
---|
371 | FILE *file; |
---|
372 | ZSubscription_t *subs; |
---|
373 | int numSubs = 100; |
---|
374 | char *subsfile; |
---|
375 | char *buffer = NULL; |
---|
376 | int count; |
---|
377 | struct stat statbuff; |
---|
378 | |
---|
379 | subs = g_new(ZSubscription_t, numSubs); |
---|
380 | subsfile = owl_zephyr_dotfile(".anyone", filename); |
---|
381 | |
---|
382 | if (stat(subsfile, &statbuff) == -1) { |
---|
383 | g_free(subs); |
---|
384 | g_free(subsfile); |
---|
385 | return 0; |
---|
386 | } |
---|
387 | |
---|
388 | ZResetAuthentication(); |
---|
389 | count = 0; |
---|
390 | file = fopen(subsfile, "r"); |
---|
391 | g_free(subsfile); |
---|
392 | if (file) { |
---|
393 | while (owl_getline_chomp(&buffer, file)) { |
---|
394 | if (buffer[0] == '\0' || buffer[0] == '#') |
---|
395 | continue; |
---|
396 | |
---|
397 | if (count == numSubs) { |
---|
398 | numSubs *= 2; |
---|
399 | subs = g_renew(ZSubscription_t, subs, numSubs); |
---|
400 | } |
---|
401 | |
---|
402 | subs[count].zsub_class = g_strdup("login"); |
---|
403 | subs[count].zsub_recipient = g_strdup("*"); |
---|
404 | subs[count].zsub_classinst = long_zuser(buffer); |
---|
405 | |
---|
406 | count++; |
---|
407 | } |
---|
408 | fclose(file); |
---|
409 | } else { |
---|
410 | return 0; |
---|
411 | } |
---|
412 | if (buffer) |
---|
413 | g_free(buffer); |
---|
414 | |
---|
415 | return owl_zephyr_loadsubs_helper(subs, count); |
---|
416 | #else |
---|
417 | return 0; |
---|
418 | #endif |
---|
419 | } |
---|
420 | |
---|
421 | void unsuball(void) |
---|
422 | { |
---|
423 | #if HAVE_LIBZEPHYR |
---|
424 | int ret; |
---|
425 | |
---|
426 | ZResetAuthentication(); |
---|
427 | ret=ZCancelSubscriptions(0); |
---|
428 | if (ret != ZERR_NONE) { |
---|
429 | com_err("owl",ret,"while unsubscribing"); |
---|
430 | } |
---|
431 | #endif |
---|
432 | } |
---|
433 | |
---|
434 | int owl_zephyr_sub(const char *class, const char *inst, const char *recip) |
---|
435 | { |
---|
436 | #ifdef HAVE_LIBZEPHYR |
---|
437 | ZSubscription_t subs[5]; |
---|
438 | |
---|
439 | subs[0].zsub_class=zstr(class); |
---|
440 | subs[0].zsub_classinst=zstr(inst); |
---|
441 | subs[0].zsub_recipient=zstr(recip); |
---|
442 | |
---|
443 | ZResetAuthentication(); |
---|
444 | if (ZSubscribeTo(subs,1,0) != ZERR_NONE) { |
---|
445 | owl_function_error("Error subbing to <%s,%s,%s>", class, inst, recip); |
---|
446 | return(-2); |
---|
447 | } |
---|
448 | return(0); |
---|
449 | #else |
---|
450 | return(0); |
---|
451 | #endif |
---|
452 | } |
---|
453 | |
---|
454 | |
---|
455 | int owl_zephyr_unsub(const char *class, const char *inst, const char *recip) |
---|
456 | { |
---|
457 | #ifdef HAVE_LIBZEPHYR |
---|
458 | ZSubscription_t subs[5]; |
---|
459 | |
---|
460 | subs[0].zsub_class=zstr(class); |
---|
461 | subs[0].zsub_classinst=zstr(inst); |
---|
462 | subs[0].zsub_recipient=zstr(recip); |
---|
463 | |
---|
464 | ZResetAuthentication(); |
---|
465 | if (ZUnsubscribeTo(subs,1,0) != ZERR_NONE) { |
---|
466 | owl_function_error("Error unsubbing from <%s,%s,%s>", class, inst, recip); |
---|
467 | return(-2); |
---|
468 | } |
---|
469 | return(0); |
---|
470 | #else |
---|
471 | return(0); |
---|
472 | #endif |
---|
473 | } |
---|
474 | |
---|
475 | /* return a pointer to the data in the Jth field, (NULL terminated by |
---|
476 | * definition). Caller must free the return. |
---|
477 | */ |
---|
478 | #ifdef HAVE_LIBZEPHYR |
---|
479 | char *owl_zephyr_get_field(const ZNotice_t *n, int j) |
---|
480 | { |
---|
481 | int i, count, save; |
---|
482 | |
---|
483 | /* If there's no message here, just run along now */ |
---|
484 | if (n->z_message_len == 0) |
---|
485 | return(g_strdup("")); |
---|
486 | |
---|
487 | count=save=0; |
---|
488 | for (i=0; i<n->z_message_len; i++) { |
---|
489 | if (n->z_message[i]=='\0') { |
---|
490 | count++; |
---|
491 | if (count==j) { |
---|
492 | /* just found the end of the field we're looking for */ |
---|
493 | return(g_strdup(n->z_message+save)); |
---|
494 | } else { |
---|
495 | save=i+1; |
---|
496 | } |
---|
497 | } |
---|
498 | } |
---|
499 | /* catch the last field, which might not be null terminated */ |
---|
500 | if (count==j-1) { |
---|
501 | return g_strndup(n->z_message + save, n->z_message_len - save); |
---|
502 | } |
---|
503 | |
---|
504 | return(g_strdup("")); |
---|
505 | } |
---|
506 | |
---|
507 | char *owl_zephyr_get_field_as_utf8(const ZNotice_t *n, int j) |
---|
508 | { |
---|
509 | int i, count, save; |
---|
510 | |
---|
511 | /* If there's no message here, just run along now */ |
---|
512 | if (n->z_message_len == 0) |
---|
513 | return(g_strdup("")); |
---|
514 | |
---|
515 | count=save=0; |
---|
516 | for (i = 0; i < n->z_message_len; i++) { |
---|
517 | if (n->z_message[i]=='\0') { |
---|
518 | count++; |
---|
519 | if (count == j) { |
---|
520 | /* just found the end of the field we're looking for */ |
---|
521 | return(owl_validate_or_convert(n->z_message + save)); |
---|
522 | } else { |
---|
523 | save = i + 1; |
---|
524 | } |
---|
525 | } |
---|
526 | } |
---|
527 | /* catch the last field, which might not be null terminated */ |
---|
528 | if (count == j - 1) { |
---|
529 | char *tmp, *out; |
---|
530 | tmp = g_strndup(n->z_message + save, n->z_message_len - save); |
---|
531 | out = owl_validate_or_convert(tmp); |
---|
532 | g_free(tmp); |
---|
533 | return out; |
---|
534 | } |
---|
535 | |
---|
536 | return(g_strdup("")); |
---|
537 | } |
---|
538 | #else |
---|
539 | char *owl_zephyr_get_field(void *n, int j) |
---|
540 | { |
---|
541 | return(g_strdup("")); |
---|
542 | } |
---|
543 | char *owl_zephyr_get_field_as_utf8(void *n, int j) |
---|
544 | { |
---|
545 | return owl_zephyr_get_field(n, j); |
---|
546 | } |
---|
547 | #endif |
---|
548 | |
---|
549 | |
---|
550 | #ifdef HAVE_LIBZEPHYR |
---|
551 | int owl_zephyr_get_num_fields(const ZNotice_t *n) |
---|
552 | { |
---|
553 | int i, fields; |
---|
554 | |
---|
555 | if(n->z_message_len == 0) |
---|
556 | return 0; |
---|
557 | |
---|
558 | fields=1; |
---|
559 | for (i=0; i<n->z_message_len; i++) { |
---|
560 | if (n->z_message[i]=='\0') fields++; |
---|
561 | } |
---|
562 | |
---|
563 | return(fields); |
---|
564 | } |
---|
565 | #else |
---|
566 | int owl_zephyr_get_num_fields(const void *n) |
---|
567 | { |
---|
568 | return(0); |
---|
569 | } |
---|
570 | #endif |
---|
571 | |
---|
572 | #ifdef HAVE_LIBZEPHYR |
---|
573 | /* return a pointer to the message, place the message length in k |
---|
574 | * caller must free the return |
---|
575 | */ |
---|
576 | char *owl_zephyr_get_message(const ZNotice_t *n, const owl_message *m) |
---|
577 | { |
---|
578 | /* don't let ping messages have a body */ |
---|
579 | if (!strcasecmp(n->z_opcode, "ping")) { |
---|
580 | return(g_strdup("")); |
---|
581 | } |
---|
582 | |
---|
583 | /* deal with MIT NOC messages */ |
---|
584 | if (!strcasecmp(n->z_default_format, "@center(@bold(NOC Message))\n\n@bold(Sender:) $1 <$sender>\n@bold(Time: ) $time\n\n@italic($opcode service on $instance $3.) $4\n")) { |
---|
585 | char *msg, *field3, *field4; |
---|
586 | |
---|
587 | field3 = owl_zephyr_get_field(n, 3); |
---|
588 | field4 = owl_zephyr_get_field(n, 4); |
---|
589 | |
---|
590 | msg = g_strdup_printf("%s service on %s %s\n%s", n->z_opcode, n->z_class_inst, field3, field4); |
---|
591 | g_free(field3); |
---|
592 | g_free(field4); |
---|
593 | if (msg) { |
---|
594 | return msg; |
---|
595 | } |
---|
596 | } |
---|
597 | /* deal with MIT Discuss messages */ |
---|
598 | else if (!strcasecmp(n->z_default_format, "New transaction [$1] entered in $2\nFrom: $3 ($5)\nSubject: $4") || |
---|
599 | !strcasecmp(n->z_default_format, "New transaction [$1] entered in $2\nFrom: $3\nSubject: $4")) { |
---|
600 | char *msg, *field1, *field2, *field3, *field4, *field5; |
---|
601 | |
---|
602 | field1 = owl_zephyr_get_field(n, 1); |
---|
603 | field2 = owl_zephyr_get_field(n, 2); |
---|
604 | field3 = owl_zephyr_get_field(n, 3); |
---|
605 | field4 = owl_zephyr_get_field(n, 4); |
---|
606 | field5 = owl_zephyr_get_field(n, 5); |
---|
607 | |
---|
608 | msg = g_strdup_printf("New transaction [%s] entered in %s\nFrom: %s (%s)\nSubject: %s", field1, field2, field3, field5, field4); |
---|
609 | g_free(field1); |
---|
610 | g_free(field2); |
---|
611 | g_free(field3); |
---|
612 | g_free(field4); |
---|
613 | g_free(field5); |
---|
614 | if (msg) { |
---|
615 | return msg; |
---|
616 | } |
---|
617 | } |
---|
618 | /* deal with MIT Moira messages */ |
---|
619 | else if (!strcasecmp(n->z_default_format, "MOIRA $instance on $fromhost:\n $message\n")) { |
---|
620 | char *msg, *field1; |
---|
621 | |
---|
622 | field1 = owl_zephyr_get_field(n, 1); |
---|
623 | |
---|
624 | msg = g_strdup_printf("MOIRA %s on %s: %s", n->z_class_inst, owl_message_get_hostname(m), field1); |
---|
625 | g_free(field1); |
---|
626 | if (msg) { |
---|
627 | return msg; |
---|
628 | } |
---|
629 | } |
---|
630 | |
---|
631 | if (owl_zephyr_get_num_fields(n) == 1) { |
---|
632 | return(owl_zephyr_get_field(n, 1)); |
---|
633 | } |
---|
634 | else { |
---|
635 | return(owl_zephyr_get_field(n, 2)); |
---|
636 | } |
---|
637 | } |
---|
638 | #endif |
---|
639 | |
---|
640 | #ifdef HAVE_LIBZEPHYR |
---|
641 | const char *owl_zephyr_get_zsig(const ZNotice_t *n, int *k) |
---|
642 | { |
---|
643 | /* return a pointer to the zsig if there is one */ |
---|
644 | |
---|
645 | /* message length 0? No zsig */ |
---|
646 | if (n->z_message_len==0) { |
---|
647 | *k=0; |
---|
648 | return(""); |
---|
649 | } |
---|
650 | |
---|
651 | /* If there's only one field, no zsig */ |
---|
652 | if (owl_zephyr_get_num_fields(n) == 1) { |
---|
653 | *k=0; |
---|
654 | return(""); |
---|
655 | } |
---|
656 | |
---|
657 | /* Everything else is field 1 */ |
---|
658 | *k=strlen(n->z_message); |
---|
659 | return(n->z_message); |
---|
660 | } |
---|
661 | #else |
---|
662 | const char *owl_zephyr_get_zsig(const void *n, int *k) |
---|
663 | { |
---|
664 | return(""); |
---|
665 | } |
---|
666 | #endif |
---|
667 | |
---|
668 | int send_zephyr(const char *opcode, const char *zsig, const char *class, const char *instance, const char *recipient, const char *message) |
---|
669 | { |
---|
670 | #ifdef HAVE_LIBZEPHYR |
---|
671 | int ret; |
---|
672 | ZNotice_t notice; |
---|
673 | |
---|
674 | memset(¬ice, 0, sizeof(notice)); |
---|
675 | |
---|
676 | ZResetAuthentication(); |
---|
677 | |
---|
678 | if (!zsig) zsig=""; |
---|
679 | |
---|
680 | notice.z_kind=ACKED; |
---|
681 | notice.z_port=0; |
---|
682 | notice.z_class=zstr(class); |
---|
683 | notice.z_class_inst=zstr(instance); |
---|
684 | notice.z_sender=NULL; |
---|
685 | if (!strcmp(recipient, "*") || !strcmp(recipient, "@")) { |
---|
686 | notice.z_recipient=zstr(""); |
---|
687 | if (*owl_global_get_zsender(&g)) |
---|
688 | notice.z_sender=zstr(owl_global_get_zsender(&g)); |
---|
689 | } else { |
---|
690 | notice.z_recipient=zstr(recipient); |
---|
691 | } |
---|
692 | notice.z_default_format=zstr("Class $class, Instance $instance:\nTo: @bold($recipient) at $time $date\nFrom: @bold{$1 <$sender>}\n\n$2"); |
---|
693 | if (opcode) notice.z_opcode=zstr(opcode); |
---|
694 | |
---|
695 | notice.z_message_len=strlen(zsig)+1+strlen(message); |
---|
696 | notice.z_message=g_new(char, notice.z_message_len+10); |
---|
697 | strcpy(notice.z_message, zsig); |
---|
698 | memcpy(notice.z_message+strlen(zsig)+1, message, strlen(message)); |
---|
699 | |
---|
700 | /* ret=ZSendNotice(¬ice, ZAUTH); */ |
---|
701 | ret=ZSrvSendNotice(¬ice, ZAUTH, send_zephyr_helper); |
---|
702 | |
---|
703 | /* free then check the return */ |
---|
704 | g_free(notice.z_message); |
---|
705 | ZFreeNotice(¬ice); |
---|
706 | if (ret!=ZERR_NONE) { |
---|
707 | owl_function_error("Error sending zephyr"); |
---|
708 | return(ret); |
---|
709 | } |
---|
710 | return(0); |
---|
711 | #else |
---|
712 | return(0); |
---|
713 | #endif |
---|
714 | } |
---|
715 | |
---|
716 | #ifdef HAVE_LIBZEPHYR |
---|
717 | Code_t send_zephyr_helper(ZNotice_t *notice, char *buf, int len, int wait) |
---|
718 | { |
---|
719 | return(ZSendPacket(buf, len, 0)); |
---|
720 | } |
---|
721 | #endif |
---|
722 | |
---|
723 | void send_ping(const char *to, const char *zclass, const char *zinstance) |
---|
724 | { |
---|
725 | #ifdef HAVE_LIBZEPHYR |
---|
726 | send_zephyr("PING", "", zclass, zinstance, to, ""); |
---|
727 | #endif |
---|
728 | } |
---|
729 | |
---|
730 | #ifdef HAVE_LIBZEPHYR |
---|
731 | void owl_zephyr_handle_ack(const ZNotice_t *retnotice) |
---|
732 | { |
---|
733 | char *tmp; |
---|
734 | |
---|
735 | /* if it's an HMACK ignore it */ |
---|
736 | if (retnotice->z_kind == HMACK) return; |
---|
737 | |
---|
738 | if (retnotice->z_kind == SERVNAK) { |
---|
739 | owl_function_error("Authorization failure sending zephyr"); |
---|
740 | } else if ((retnotice->z_kind != SERVACK) || !retnotice->z_message_len) { |
---|
741 | owl_function_error("Detected server failure while receiving acknowledgement"); |
---|
742 | } else if (!strcmp(retnotice->z_message, ZSRVACK_SENT)) { |
---|
743 | if (!strcasecmp(retnotice->z_opcode, "ping")) { |
---|
744 | return; |
---|
745 | } else { |
---|
746 | if (strcasecmp(retnotice->z_recipient, "")) |
---|
747 | { /* personal */ |
---|
748 | tmp=short_zuser(retnotice->z_recipient); |
---|
749 | if(!strcasecmp(retnotice->z_class, "message") && |
---|
750 | !strcasecmp(retnotice->z_class_inst, "personal")) { |
---|
751 | owl_function_makemsg("Message sent to %s.", tmp); |
---|
752 | } else if(!strcasecmp(retnotice->z_class, "message")) { /* instanced, but not classed, personal */ |
---|
753 | owl_function_makemsg("Message sent to %s on -i %s\n", tmp, retnotice->z_class_inst); |
---|
754 | } else { /* classed personal */ |
---|
755 | owl_function_makemsg("Message sent to %s on -c %s -i %s\n", tmp, retnotice->z_class, retnotice->z_class_inst); |
---|
756 | } |
---|
757 | g_free(tmp); |
---|
758 | } else { |
---|
759 | /* class / instance message */ |
---|
760 | owl_function_makemsg("Message sent to -c %s -i %s\n", retnotice->z_class, retnotice->z_class_inst); |
---|
761 | } |
---|
762 | } |
---|
763 | } else if (!strcmp(retnotice->z_message, ZSRVACK_NOTSENT)) { |
---|
764 | #define BUFFLEN 1024 |
---|
765 | if (retnotice->z_recipient == NULL |
---|
766 | || *retnotice->z_recipient == 0 |
---|
767 | || *retnotice->z_recipient == '@') { |
---|
768 | char buff[BUFFLEN]; |
---|
769 | owl_function_error("No one subscribed to class %s", retnotice->z_class); |
---|
770 | snprintf(buff, BUFFLEN, "Could not send message to class %s: no one subscribed.\n", retnotice->z_class); |
---|
771 | owl_function_adminmsg("", buff); |
---|
772 | } else { |
---|
773 | char buff[BUFFLEN]; |
---|
774 | owl_zwrite zw; |
---|
775 | char *realm; |
---|
776 | |
---|
777 | tmp = short_zuser(retnotice->z_recipient); |
---|
778 | owl_function_error("%s: Not logged in or subscribing.", tmp); |
---|
779 | /* |
---|
780 | * These error messages are often over 80 chars, but users who want to |
---|
781 | * see the whole thing can scroll to the side, and for those with wide |
---|
782 | * terminals or who don't care, not splitting saves a line in the UI |
---|
783 | */ |
---|
784 | if(strcasecmp(retnotice->z_class, "message")) { |
---|
785 | snprintf(buff, BUFFLEN, |
---|
786 | "Could not send message to %s: " |
---|
787 | "not logged in or subscribing to class %s, instance %s.\n", |
---|
788 | tmp, |
---|
789 | retnotice->z_class, |
---|
790 | retnotice->z_class_inst); |
---|
791 | } else if(strcasecmp(retnotice->z_class_inst, "personal")) { |
---|
792 | snprintf(buff, BUFFLEN, |
---|
793 | "Could not send message to %s: " |
---|
794 | "not logged in or subscribing to instance %s.\n", |
---|
795 | tmp, |
---|
796 | retnotice->z_class_inst); |
---|
797 | } else { |
---|
798 | snprintf(buff, BUFFLEN, |
---|
799 | "Could not send message to %s: " |
---|
800 | "not logged in or subscribing to messages.\n", |
---|
801 | tmp); |
---|
802 | } |
---|
803 | owl_function_adminmsg("", buff); |
---|
804 | |
---|
805 | memset(&zw, 0, sizeof(zw)); |
---|
806 | zw.class = g_strdup(retnotice->z_class); |
---|
807 | zw.inst = g_strdup(retnotice->z_class_inst); |
---|
808 | realm = strchr(retnotice->z_recipient, '@'); |
---|
809 | if(realm) { |
---|
810 | zw.realm = g_strdup(realm + 1); |
---|
811 | } else { |
---|
812 | zw.realm = g_strdup(owl_zephyr_get_realm()); |
---|
813 | } |
---|
814 | zw.opcode = g_strdup(retnotice->z_opcode); |
---|
815 | zw.zsig = g_strdup(""); |
---|
816 | owl_list_create(&(zw.recips)); |
---|
817 | owl_list_append_element(&(zw.recips), g_strdup(tmp)); |
---|
818 | |
---|
819 | owl_log_outgoing_zephyr_error(&zw, buff); |
---|
820 | |
---|
821 | owl_zwrite_cleanup(&zw); |
---|
822 | g_free(tmp); |
---|
823 | } |
---|
824 | } else { |
---|
825 | owl_function_error("Internal error on ack (%s)", retnotice->z_message); |
---|
826 | } |
---|
827 | } |
---|
828 | #else |
---|
829 | void owl_zephyr_handle_ack(const void *retnotice) |
---|
830 | { |
---|
831 | } |
---|
832 | #endif |
---|
833 | |
---|
834 | #ifdef HAVE_LIBZEPHYR |
---|
835 | int owl_zephyr_notice_is_ack(const ZNotice_t *n) |
---|
836 | { |
---|
837 | if (n->z_kind == SERVNAK || n->z_kind == SERVACK || n->z_kind == HMACK) { |
---|
838 | if (!strcasecmp(n->z_class, LOGIN_CLASS)) return(0); |
---|
839 | return(1); |
---|
840 | } |
---|
841 | return(0); |
---|
842 | } |
---|
843 | #else |
---|
844 | int owl_zephyr_notice_is_ack(const void *n) |
---|
845 | { |
---|
846 | return(0); |
---|
847 | } |
---|
848 | #endif |
---|
849 | |
---|
850 | void owl_zephyr_zaway(const owl_message *m) |
---|
851 | { |
---|
852 | #ifdef HAVE_LIBZEPHYR |
---|
853 | char *tmpbuff, *myuser, *to; |
---|
854 | owl_message *mout; |
---|
855 | owl_zwrite *z; |
---|
856 | |
---|
857 | /* bail if it doesn't look like a message we should reply to. Some |
---|
858 | * of this defined by the way zaway(1) works |
---|
859 | */ |
---|
860 | if (strcasecmp(owl_message_get_class(m), "message")) return; |
---|
861 | if (strcasecmp(owl_message_get_recipient(m), ZGetSender())) return; |
---|
862 | if (!strcasecmp(owl_message_get_sender(m), "")) return; |
---|
863 | if (!strcasecmp(owl_message_get_opcode(m), "ping")) return; |
---|
864 | if (!strcasecmp(owl_message_get_opcode(m), "auto")) return; |
---|
865 | if (!strcasecmp(owl_message_get_zsig(m), "Automated reply:")) return; |
---|
866 | if (!strcasecmp(owl_message_get_sender(m), ZGetSender())) return; |
---|
867 | if (owl_message_get_attribute_value(m, "isauto")) return; |
---|
868 | |
---|
869 | if (owl_global_is_smartstrip(&g)) { |
---|
870 | to=owl_zephyr_smartstripped_user(owl_message_get_sender(m)); |
---|
871 | } else { |
---|
872 | to=g_strdup(owl_message_get_sender(m)); |
---|
873 | } |
---|
874 | |
---|
875 | send_zephyr("", |
---|
876 | "Automated reply:", |
---|
877 | owl_message_get_class(m), |
---|
878 | owl_message_get_instance(m), |
---|
879 | to, |
---|
880 | owl_global_get_zaway_msg(&g)); |
---|
881 | |
---|
882 | myuser=short_zuser(to); |
---|
883 | if (!strcasecmp(owl_message_get_instance(m), "personal")) { |
---|
884 | tmpbuff = owl_string_build_quoted("zwrite %q", myuser); |
---|
885 | } else { |
---|
886 | tmpbuff = owl_string_build_quoted("zwrite -i %q %q", owl_message_get_instance(m), myuser); |
---|
887 | } |
---|
888 | g_free(myuser); |
---|
889 | g_free(to); |
---|
890 | |
---|
891 | z = owl_zwrite_new(tmpbuff); |
---|
892 | g_free(tmpbuff); |
---|
893 | if (z == NULL) { |
---|
894 | owl_function_error("Error creating outgoing zephyr."); |
---|
895 | return; |
---|
896 | } |
---|
897 | owl_zwrite_set_message(z, owl_global_get_zaway_msg(&g)); |
---|
898 | owl_zwrite_set_zsig(z, "Automated reply:"); |
---|
899 | |
---|
900 | /* display the message as an admin message in the receive window */ |
---|
901 | mout=owl_function_make_outgoing_zephyr(z); |
---|
902 | owl_global_messagequeue_addmsg(&g, mout); |
---|
903 | owl_zwrite_delete(z); |
---|
904 | #endif |
---|
905 | } |
---|
906 | |
---|
907 | #ifdef HAVE_LIBZEPHYR |
---|
908 | void owl_zephyr_hackaway_cr(ZNotice_t *n) |
---|
909 | { |
---|
910 | /* replace \r's with ' '. Gross-ish */ |
---|
911 | int i; |
---|
912 | |
---|
913 | for (i=0; i<n->z_message_len; i++) { |
---|
914 | if (n->z_message[i]=='\r') { |
---|
915 | n->z_message[i]=' '; |
---|
916 | } |
---|
917 | } |
---|
918 | } |
---|
919 | #endif |
---|
920 | |
---|
921 | char *owl_zephyr_zlocate(const char *user, int auth) |
---|
922 | { |
---|
923 | #ifdef HAVE_LIBZEPHYR |
---|
924 | int ret, numlocs; |
---|
925 | int one = 1; |
---|
926 | ZLocations_t locations; |
---|
927 | char *myuser; |
---|
928 | char *p, *result; |
---|
929 | |
---|
930 | ZResetAuthentication(); |
---|
931 | ret = ZLocateUser(zstr(user), &numlocs, auth ? ZAUTH : ZNOAUTH); |
---|
932 | if (ret != ZERR_NONE) |
---|
933 | return g_strdup_printf("Error locating user %s: %s\n", |
---|
934 | user, error_message(ret)); |
---|
935 | |
---|
936 | myuser = short_zuser(user); |
---|
937 | if (numlocs == 0) { |
---|
938 | result = g_strdup_printf("%s: Hidden or not logged in\n", myuser); |
---|
939 | } else { |
---|
940 | result = g_strdup(""); |
---|
941 | for (; numlocs; numlocs--) { |
---|
942 | ZGetLocations(&locations, &one); |
---|
943 | p = g_strdup_printf("%s%s: %s\t%s\t%s\n", |
---|
944 | result, myuser, |
---|
945 | locations.host ? locations.host : "?", |
---|
946 | locations.tty ? locations.tty : "?", |
---|
947 | locations.time ? locations.time : "?"); |
---|
948 | g_free(result); |
---|
949 | result = p; |
---|
950 | } |
---|
951 | } |
---|
952 | g_free(myuser); |
---|
953 | |
---|
954 | return result; |
---|
955 | #else |
---|
956 | return g_strdup(""); |
---|
957 | #endif |
---|
958 | } |
---|
959 | |
---|
960 | void owl_zephyr_addsub(const char *filename, const char *class, const char *inst, const char *recip) |
---|
961 | { |
---|
962 | #ifdef HAVE_LIBZEPHYR |
---|
963 | char *line, *subsfile, *s = NULL; |
---|
964 | FILE *file; |
---|
965 | int duplicate = 0; |
---|
966 | |
---|
967 | line = owl_zephyr_makesubline(class, inst, recip); |
---|
968 | subsfile = owl_zephyr_dotfile(".zephyr.subs", filename); |
---|
969 | |
---|
970 | /* if the file already exists, check to see if the sub is already there */ |
---|
971 | file = fopen(subsfile, "r"); |
---|
972 | if (file) { |
---|
973 | while (owl_getline(&s, file)) { |
---|
974 | if (strcasecmp(s, line) == 0) { |
---|
975 | owl_function_error("Subscription already present in %s", subsfile); |
---|
976 | duplicate++; |
---|
977 | } |
---|
978 | } |
---|
979 | fclose(file); |
---|
980 | g_free(s); |
---|
981 | } |
---|
982 | |
---|
983 | if (!duplicate) { |
---|
984 | file = fopen(subsfile, "a"); |
---|
985 | if (file) { |
---|
986 | fputs(line, file); |
---|
987 | fclose(file); |
---|
988 | owl_function_makemsg("Subscription added"); |
---|
989 | } else { |
---|
990 | owl_function_error("Error opening file %s for writing", subsfile); |
---|
991 | } |
---|
992 | } |
---|
993 | |
---|
994 | g_free(line); |
---|
995 | #endif |
---|
996 | } |
---|
997 | |
---|
998 | void owl_zephyr_delsub(const char *filename, const char *class, const char *inst, const char *recip) |
---|
999 | { |
---|
1000 | #ifdef HAVE_LIBZEPHYR |
---|
1001 | char *line, *subsfile; |
---|
1002 | int linesdeleted; |
---|
1003 | |
---|
1004 | line=owl_zephyr_makesubline(class, inst, recip); |
---|
1005 | line[strlen(line)-1]='\0'; |
---|
1006 | |
---|
1007 | subsfile = owl_zephyr_dotfile(".zephyr.subs", filename); |
---|
1008 | |
---|
1009 | linesdeleted = owl_util_file_deleteline(subsfile, line, 1); |
---|
1010 | if (linesdeleted > 0) { |
---|
1011 | owl_function_makemsg("Subscription removed"); |
---|
1012 | } else if (linesdeleted == 0) { |
---|
1013 | owl_function_error("No subscription present in %s", subsfile); |
---|
1014 | } |
---|
1015 | g_free(subsfile); |
---|
1016 | g_free(line); |
---|
1017 | #endif |
---|
1018 | } |
---|
1019 | |
---|
1020 | /* caller must free the return */ |
---|
1021 | char *owl_zephyr_makesubline(const char *class, const char *inst, const char *recip) |
---|
1022 | { |
---|
1023 | return g_strdup_printf("%s,%s,%s\n", class, inst, !strcmp(recip, "") ? "*" : recip); |
---|
1024 | } |
---|
1025 | |
---|
1026 | |
---|
1027 | void owl_zephyr_zlog_in(void) |
---|
1028 | { |
---|
1029 | #ifdef HAVE_LIBZEPHYR |
---|
1030 | const char *exposure, *eset; |
---|
1031 | int ret; |
---|
1032 | |
---|
1033 | ZResetAuthentication(); |
---|
1034 | |
---|
1035 | eset=EXPOSE_REALMVIS; |
---|
1036 | exposure=ZGetVariable(zstr("exposure")); |
---|
1037 | if (exposure==NULL) { |
---|
1038 | eset=EXPOSE_REALMVIS; |
---|
1039 | } else if (!strcasecmp(exposure,EXPOSE_NONE)) { |
---|
1040 | eset = EXPOSE_NONE; |
---|
1041 | } else if (!strcasecmp(exposure,EXPOSE_OPSTAFF)) { |
---|
1042 | eset = EXPOSE_OPSTAFF; |
---|
1043 | } else if (!strcasecmp(exposure,EXPOSE_REALMVIS)) { |
---|
1044 | eset = EXPOSE_REALMVIS; |
---|
1045 | } else if (!strcasecmp(exposure,EXPOSE_REALMANN)) { |
---|
1046 | eset = EXPOSE_REALMANN; |
---|
1047 | } else if (!strcasecmp(exposure,EXPOSE_NETVIS)) { |
---|
1048 | eset = EXPOSE_NETVIS; |
---|
1049 | } else if (!strcasecmp(exposure,EXPOSE_NETANN)) { |
---|
1050 | eset = EXPOSE_NETANN; |
---|
1051 | } |
---|
1052 | |
---|
1053 | ret=ZSetLocation(zstr(eset)); |
---|
1054 | if (ret != ZERR_NONE) { |
---|
1055 | /* |
---|
1056 | owl_function_makemsg("Error setting location: %s", error_message(ret)); |
---|
1057 | */ |
---|
1058 | } |
---|
1059 | #endif |
---|
1060 | } |
---|
1061 | |
---|
1062 | void owl_zephyr_zlog_out(void) |
---|
1063 | { |
---|
1064 | #ifdef HAVE_LIBZEPHYR |
---|
1065 | int ret; |
---|
1066 | |
---|
1067 | ZResetAuthentication(); |
---|
1068 | ret=ZUnsetLocation(); |
---|
1069 | if (ret != ZERR_NONE) { |
---|
1070 | /* |
---|
1071 | owl_function_makemsg("Error unsetting location: %s", error_message(ret)); |
---|
1072 | */ |
---|
1073 | } |
---|
1074 | #endif |
---|
1075 | } |
---|
1076 | |
---|
1077 | void owl_zephyr_addbuddy(const char *name) |
---|
1078 | { |
---|
1079 | char *filename; |
---|
1080 | FILE *file; |
---|
1081 | |
---|
1082 | filename = owl_zephyr_dotfile(".anyone", NULL); |
---|
1083 | file = fopen(filename, "a"); |
---|
1084 | g_free(filename); |
---|
1085 | if (!file) { |
---|
1086 | owl_function_error("Error opening zephyr buddy file for append"); |
---|
1087 | return; |
---|
1088 | } |
---|
1089 | fprintf(file, "%s\n", name); |
---|
1090 | fclose(file); |
---|
1091 | } |
---|
1092 | |
---|
1093 | void owl_zephyr_delbuddy(const char *name) |
---|
1094 | { |
---|
1095 | char *filename; |
---|
1096 | |
---|
1097 | filename = owl_zephyr_dotfile(".anyone", NULL); |
---|
1098 | owl_util_file_deleteline(filename, name, 0); |
---|
1099 | g_free(filename); |
---|
1100 | } |
---|
1101 | |
---|
1102 | /* return auth string */ |
---|
1103 | #ifdef HAVE_LIBZEPHYR |
---|
1104 | const char *owl_zephyr_get_authstr(const ZNotice_t *n) |
---|
1105 | { |
---|
1106 | |
---|
1107 | if (!n) return("UNKNOWN"); |
---|
1108 | |
---|
1109 | if (n->z_auth == ZAUTH_FAILED) { |
---|
1110 | return ("FAILED"); |
---|
1111 | } else if (n->z_auth == ZAUTH_NO) { |
---|
1112 | return ("NO"); |
---|
1113 | } else if (n->z_auth == ZAUTH_YES) { |
---|
1114 | return ("YES"); |
---|
1115 | } else { |
---|
1116 | return ("UNKNOWN"); |
---|
1117 | } |
---|
1118 | } |
---|
1119 | #else |
---|
1120 | const char *owl_zephyr_get_authstr(const void *n) |
---|
1121 | { |
---|
1122 | return(""); |
---|
1123 | } |
---|
1124 | #endif |
---|
1125 | |
---|
1126 | /* Returns a buffer of subscriptions or an error message. Caller must |
---|
1127 | * free the return. |
---|
1128 | */ |
---|
1129 | char *owl_zephyr_getsubs(void) |
---|
1130 | { |
---|
1131 | #ifdef HAVE_LIBZEPHYR |
---|
1132 | int ret, num, i, one; |
---|
1133 | ZSubscription_t sub; |
---|
1134 | GString *buf; |
---|
1135 | |
---|
1136 | ret=ZRetrieveSubscriptions(0, &num); |
---|
1137 | if (ret==ZERR_TOOMANYSUBS) { |
---|
1138 | return(g_strdup("Zephyr: too many subscriptions\n")); |
---|
1139 | } else if (ret || (num <= 0)) { |
---|
1140 | return(g_strdup("Zephyr: error retrieving subscriptions\n")); |
---|
1141 | } |
---|
1142 | |
---|
1143 | buf = g_string_new(""); |
---|
1144 | for (i=0; i<num; i++) { |
---|
1145 | one = 1; |
---|
1146 | if ((ret = ZGetSubscriptions(&sub, &one)) != ZERR_NONE) { |
---|
1147 | ZFlushSubscriptions(); |
---|
1148 | g_string_free(buf, true); |
---|
1149 | return g_strdup("Error while getting subscriptions\n"); |
---|
1150 | } else { |
---|
1151 | /* g_string_append_printf would be backwards. */ |
---|
1152 | char *tmp = g_strdup_printf("<%s,%s,%s>\n", |
---|
1153 | sub.zsub_class, |
---|
1154 | sub.zsub_classinst, |
---|
1155 | sub.zsub_recipient); |
---|
1156 | g_string_prepend(buf, tmp); |
---|
1157 | g_free(tmp); |
---|
1158 | } |
---|
1159 | } |
---|
1160 | |
---|
1161 | ZFlushSubscriptions(); |
---|
1162 | return g_string_free(buf, false); |
---|
1163 | #else |
---|
1164 | return(g_strdup("Zephyr not available")); |
---|
1165 | #endif |
---|
1166 | } |
---|
1167 | |
---|
1168 | const char *owl_zephyr_get_variable(const char *var) |
---|
1169 | { |
---|
1170 | #ifdef HAVE_LIBZEPHYR |
---|
1171 | return(ZGetVariable(zstr(var))); |
---|
1172 | #else |
---|
1173 | return(""); |
---|
1174 | #endif |
---|
1175 | } |
---|
1176 | |
---|
1177 | void owl_zephyr_set_locationinfo(const char *host, const char *val) |
---|
1178 | { |
---|
1179 | #ifdef HAVE_LIBZEPHYR |
---|
1180 | ZInitLocationInfo(zstr(host), zstr(val)); |
---|
1181 | #endif |
---|
1182 | } |
---|
1183 | |
---|
1184 | /* Strip a local realm fron the zephyr user name. |
---|
1185 | * The caller must free the return |
---|
1186 | */ |
---|
1187 | char *short_zuser(const char *in) |
---|
1188 | { |
---|
1189 | char *out, *ptr; |
---|
1190 | |
---|
1191 | out=g_strdup(in); |
---|
1192 | ptr=strchr(out, '@'); |
---|
1193 | if (ptr) { |
---|
1194 | if (!strcasecmp(ptr+1, owl_zephyr_get_realm())) { |
---|
1195 | *ptr='\0'; |
---|
1196 | } |
---|
1197 | } |
---|
1198 | return(out); |
---|
1199 | } |
---|
1200 | |
---|
1201 | /* Append a local realm to the zephyr user name if necessary. |
---|
1202 | * The caller must free the return. |
---|
1203 | */ |
---|
1204 | char *long_zuser(const char *in) |
---|
1205 | { |
---|
1206 | if (strchr(in, '@')) { |
---|
1207 | return(g_strdup(in)); |
---|
1208 | } |
---|
1209 | return(g_strdup_printf("%s@%s", in, owl_zephyr_get_realm())); |
---|
1210 | } |
---|
1211 | |
---|
1212 | /* strip out the instance from a zsender's principal. Preserves the |
---|
1213 | * realm if present. Leave host/ and daemon/ krb5 principals |
---|
1214 | * alone. Also leave rcmd. and daemon. krb4 principals alone. The |
---|
1215 | * caller must free the return. |
---|
1216 | */ |
---|
1217 | char *owl_zephyr_smartstripped_user(const char *in) |
---|
1218 | { |
---|
1219 | char *slash, *dot, *realm, *out; |
---|
1220 | |
---|
1221 | out = g_strdup(in); |
---|
1222 | |
---|
1223 | /* bail immeaditly if we don't have to do any work */ |
---|
1224 | slash = strchr(out, '/'); |
---|
1225 | dot = strchr(out, '.'); |
---|
1226 | if (!slash && !dot) { |
---|
1227 | return(out); |
---|
1228 | } |
---|
1229 | |
---|
1230 | if (!strncasecmp(out, OWL_ZEPHYR_NOSTRIP_HOST, strlen(OWL_ZEPHYR_NOSTRIP_HOST)) || |
---|
1231 | !strncasecmp(out, OWL_ZEPHYR_NOSTRIP_RCMD, strlen(OWL_ZEPHYR_NOSTRIP_RCMD)) || |
---|
1232 | !strncasecmp(out, OWL_ZEPHYR_NOSTRIP_DAEMON5, strlen(OWL_ZEPHYR_NOSTRIP_DAEMON5)) || |
---|
1233 | !strncasecmp(out, OWL_ZEPHYR_NOSTRIP_DAEMON4, strlen(OWL_ZEPHYR_NOSTRIP_DAEMON4))) { |
---|
1234 | return(out); |
---|
1235 | } |
---|
1236 | |
---|
1237 | realm = strchr(out, '@'); |
---|
1238 | if (!slash && dot && realm && (dot > realm)) { |
---|
1239 | /* There's no '/', and the first '.' is in the realm */ |
---|
1240 | return(out); |
---|
1241 | } |
---|
1242 | |
---|
1243 | /* remove the realm from out, but hold on to it */ |
---|
1244 | if (realm) realm[0]='\0'; |
---|
1245 | |
---|
1246 | /* strip */ |
---|
1247 | if (slash) slash[0] = '\0'; /* krb5 style user/instance */ |
---|
1248 | else if (dot) dot[0] = '\0'; /* krb4 style user.instance */ |
---|
1249 | |
---|
1250 | /* reattach the realm if we had one */ |
---|
1251 | if (realm) { |
---|
1252 | strcat(out, "@"); |
---|
1253 | strcat(out, realm+1); |
---|
1254 | } |
---|
1255 | |
---|
1256 | return(out); |
---|
1257 | } |
---|
1258 | |
---|
1259 | /* read the list of users in 'filename' as a .anyone file, and put the |
---|
1260 | * names of the zephyr users in the list 'in'. If 'filename' is NULL, |
---|
1261 | * use the default .anyone file in the users home directory. Returns |
---|
1262 | * -1 on failure, 0 on success. |
---|
1263 | */ |
---|
1264 | int owl_zephyr_get_anyone_list(owl_list *in, const char *filename) |
---|
1265 | { |
---|
1266 | #ifdef HAVE_LIBZEPHYR |
---|
1267 | char *ourfile, *tmp, *s = NULL; |
---|
1268 | FILE *f; |
---|
1269 | |
---|
1270 | ourfile = owl_zephyr_dotfile(".anyone", filename); |
---|
1271 | |
---|
1272 | f = fopen(ourfile, "r"); |
---|
1273 | if (!f) { |
---|
1274 | owl_function_error("Error opening file %s: %s", ourfile, strerror(errno) ? strerror(errno) : ""); |
---|
1275 | g_free(ourfile); |
---|
1276 | return -1; |
---|
1277 | } |
---|
1278 | g_free(ourfile); |
---|
1279 | |
---|
1280 | while (owl_getline_chomp(&s, f)) { |
---|
1281 | /* ignore comments, blank lines etc. */ |
---|
1282 | if (s[0] == '#' || s[0] == '\0') |
---|
1283 | continue; |
---|
1284 | |
---|
1285 | /* ignore from # on */ |
---|
1286 | tmp = strchr(s, '#'); |
---|
1287 | if (tmp) |
---|
1288 | tmp[0] = '\0'; |
---|
1289 | |
---|
1290 | /* ignore from SPC */ |
---|
1291 | tmp = strchr(s, ' '); |
---|
1292 | if (tmp) |
---|
1293 | tmp[0] = '\0'; |
---|
1294 | |
---|
1295 | owl_list_append_element(in, long_zuser(s)); |
---|
1296 | } |
---|
1297 | g_free(s); |
---|
1298 | fclose(f); |
---|
1299 | return 0; |
---|
1300 | #else |
---|
1301 | return -1; |
---|
1302 | #endif |
---|
1303 | } |
---|
1304 | |
---|
1305 | #ifdef HAVE_LIBZEPHYR |
---|
1306 | void owl_zephyr_process_pseudologin(ZNotice_t *n) |
---|
1307 | { |
---|
1308 | owl_message *m; |
---|
1309 | owl_zbuddylist *zbl; |
---|
1310 | GList **zaldlist; |
---|
1311 | GList *zaldptr; |
---|
1312 | ZAsyncLocateData_t *zald = NULL; |
---|
1313 | ZLocations_t location; |
---|
1314 | int numlocs, ret, notify; |
---|
1315 | |
---|
1316 | /* Find a ZALD to match this notice. */ |
---|
1317 | zaldlist = owl_global_get_zaldlist(&g); |
---|
1318 | zaldptr = g_list_first(*zaldlist); |
---|
1319 | while (zaldptr) { |
---|
1320 | if (ZCompareALDPred(n, zaldptr->data)) { |
---|
1321 | zald = zaldptr->data; |
---|
1322 | *zaldlist = g_list_remove(*zaldlist, zaldptr->data); |
---|
1323 | break; |
---|
1324 | } |
---|
1325 | zaldptr = g_list_next(zaldptr); |
---|
1326 | } |
---|
1327 | if (zald) { |
---|
1328 | /* Deal with notice. */ |
---|
1329 | notify = owl_global_get_pseudologin_notify(&g); |
---|
1330 | zbl = owl_global_get_zephyr_buddylist(&g); |
---|
1331 | ret = ZParseLocations(n, zald, &numlocs, NULL); |
---|
1332 | if (ret == ZERR_NONE) { |
---|
1333 | if (numlocs > 0 && !owl_zbuddylist_contains_user(zbl, zald->user)) { |
---|
1334 | if (notify) { |
---|
1335 | numlocs = 1; |
---|
1336 | ret = ZGetLocations(&location, &numlocs); |
---|
1337 | if (ret == ZERR_NONE) { |
---|
1338 | /* Send a PSEUDO LOGIN! */ |
---|
1339 | m = g_new(owl_message, 1); |
---|
1340 | owl_message_create_pseudo_zlogin(m, 0, zald->user, |
---|
1341 | location.host, |
---|
1342 | location.time, |
---|
1343 | location.tty); |
---|
1344 | owl_global_messagequeue_addmsg(&g, m); |
---|
1345 | } |
---|
1346 | owl_zbuddylist_adduser(zbl, zald->user); |
---|
1347 | owl_function_debugmsg("owl_function_zephyr_buddy_check: login for %s ", zald->user); |
---|
1348 | } |
---|
1349 | } else if (numlocs == 0 && owl_zbuddylist_contains_user(zbl, zald->user)) { |
---|
1350 | /* Send a PSEUDO LOGOUT! */ |
---|
1351 | if (notify) { |
---|
1352 | m = g_new(owl_message, 1); |
---|
1353 | owl_message_create_pseudo_zlogin(m, 1, zald->user, "", "", ""); |
---|
1354 | owl_global_messagequeue_addmsg(&g, m); |
---|
1355 | } |
---|
1356 | owl_zbuddylist_deluser(zbl, zald->user); |
---|
1357 | owl_function_debugmsg("owl_function_zephyr_buddy_check: logout for %s ", zald->user); |
---|
1358 | } |
---|
1359 | } |
---|
1360 | ZFreeALD(zald); |
---|
1361 | g_free(zald); |
---|
1362 | } |
---|
1363 | } |
---|
1364 | #else |
---|
1365 | void owl_zephyr_process_pseudologin(void *n) |
---|
1366 | { |
---|
1367 | } |
---|
1368 | #endif |
---|
1369 | |
---|
1370 | void owl_zephyr_buddycheck_timer(owl_timer *t, void *data) |
---|
1371 | { |
---|
1372 | if (owl_global_is_pseudologins(&g)) { |
---|
1373 | owl_function_debugmsg("Doing zephyr buddy check"); |
---|
1374 | owl_function_zephyr_buddy_check(1); |
---|
1375 | } else { |
---|
1376 | owl_function_debugmsg("Warning: owl_zephyr_buddycheck_timer call pointless; timer should have been disabled"); |
---|
1377 | } |
---|
1378 | } |
---|
1379 | |
---|
1380 | /* |
---|
1381 | * Process zephyrgrams from libzephyr's queue. To prevent starvation, |
---|
1382 | * process a maximum of OWL_MAX_ZEPHYRGRAMS_TO_PROCESS. |
---|
1383 | * |
---|
1384 | * Returns the number of zephyrgrams processed. |
---|
1385 | */ |
---|
1386 | |
---|
1387 | #define OWL_MAX_ZEPHYRGRAMS_TO_PROCESS 20 |
---|
1388 | |
---|
1389 | static int _owl_zephyr_process_events(void) |
---|
1390 | { |
---|
1391 | int zpendcount=0; |
---|
1392 | #ifdef HAVE_LIBZEPHYR |
---|
1393 | ZNotice_t notice; |
---|
1394 | Code_t code; |
---|
1395 | owl_message *m=NULL; |
---|
1396 | |
---|
1397 | while(owl_zephyr_zpending() && zpendcount < OWL_MAX_ZEPHYRGRAMS_TO_PROCESS) { |
---|
1398 | if (owl_zephyr_zpending()) { |
---|
1399 | if ((code = ZReceiveNotice(¬ice, NULL)) != ZERR_NONE) { |
---|
1400 | owl_function_debugmsg("Error: %s while calling ZReceiveNotice\n", |
---|
1401 | error_message(code)); |
---|
1402 | continue; |
---|
1403 | } |
---|
1404 | zpendcount++; |
---|
1405 | |
---|
1406 | /* is this an ack from a zephyr we sent? */ |
---|
1407 | if (owl_zephyr_notice_is_ack(¬ice)) { |
---|
1408 | owl_zephyr_handle_ack(¬ice); |
---|
1409 | ZFreeNotice(¬ice); |
---|
1410 | continue; |
---|
1411 | } |
---|
1412 | |
---|
1413 | /* if it's a ping and we're not viewing pings then skip it */ |
---|
1414 | if (!owl_global_is_rxping(&g) && !strcasecmp(notice.z_opcode, "ping")) { |
---|
1415 | ZFreeNotice(¬ice); |
---|
1416 | continue; |
---|
1417 | } |
---|
1418 | |
---|
1419 | /* if it is a LOCATE message, it's for pseudologins. */ |
---|
1420 | if (strcmp(notice.z_opcode, LOCATE_LOCATE) == 0) { |
---|
1421 | owl_zephyr_process_pseudologin(¬ice); |
---|
1422 | ZFreeNotice(¬ice); |
---|
1423 | continue; |
---|
1424 | } |
---|
1425 | |
---|
1426 | /* create the new message */ |
---|
1427 | m=g_new(owl_message, 1); |
---|
1428 | owl_message_create_from_znotice(m, ¬ice); |
---|
1429 | |
---|
1430 | owl_global_messagequeue_addmsg(&g, m); |
---|
1431 | } |
---|
1432 | } |
---|
1433 | #endif |
---|
1434 | return zpendcount; |
---|
1435 | } |
---|
1436 | |
---|
1437 | void owl_zephyr_process_events(const owl_io_dispatch *d, void *data) |
---|
1438 | { |
---|
1439 | _owl_zephyr_process_events(); |
---|
1440 | } |
---|
1441 | |
---|
1442 | int owl_zephyr_pre_select_action(owl_ps_action *a, void *p) |
---|
1443 | { |
---|
1444 | return _owl_zephyr_process_events(); |
---|
1445 | } |
---|