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