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