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