source: zephyr.c @ 6d7e6c6

owl
Last change on this file since 6d7e6c6 was 6d7e6c6, checked in by James M. Kretchmar <kretch@mit.edu>, 15 years ago
process aim events with just about any zephyr call
  • Property mode set to 100644
File size: 24.8 KB
Line 
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
9static const char fileIdent[] = "$Id$";
10
11#ifdef HAVE_LIBZEPHYR
12Code_t ZResetAuthentication();
13#endif
14
15int owl_zephyr_initialize()
16{
17#ifdef HAVE_LIBZEPHYR
18  int ret;
19 
20  if ((ret = ZInitialize()) != ZERR_NONE) {
21    com_err("owl",ret,"while initializing");
22    return(1);
23  }
24  if ((ret = ZOpenPort(NULL)) != ZERR_NONE) {
25    com_err("owl",ret,"while opening port");
26    return(1);
27  }
28  owl_zephyr_process_events(NULL);
29#endif
30  return(0);
31}
32
33
34int owl_zephyr_shutdown()
35{
36#ifdef HAVE_LIBZEPHYR
37  unsuball();
38  owl_zephyr_process_events(NULL);
39  ZClosePort();
40#endif
41  return(0);
42}
43
44int owl_zephyr_zpending()
45{
46#ifdef HAVE_LIBZEPHYR
47  return(ZPending());
48#else
49  return(0);
50#endif
51}
52
53char *owl_zephyr_get_realm()
54{
55#ifdef HAVE_LIBZEPHYR
56  return(ZGetRealm());
57#else
58  return("");
59#endif
60}
61
62char *owl_zephyr_get_sender()
63{
64#ifdef HAVE_LIBZEPHYR
65  return(ZGetSender());
66#else
67  return("");
68#endif
69}
70
71#ifdef HAVE_LIBZEPHYR
72int owl_zephyr_loadsubs_helper(ZSubscription_t subs[], int count)
73{
74  int i, ret = 0;
75  /* sub without defaults */
76  if (ZSubscribeToSansDefaults(subs,count,0) != ZERR_NONE) {
77    owl_function_error("Error subscribing to zephyr notifications.");
78    ret=-2;
79  }
80
81  /* free stuff */
82  for (i=0; i<count; i++) {
83    owl_free(subs[i].zsub_class);
84    owl_free(subs[i].zsub_classinst);
85    owl_free(subs[i].zsub_recipient);
86  }
87  owl_zephyr_process_events(NULL);
88  return ret;
89}
90#endif
91
92/* Load zephyr subscriptions form 'filename'.  If 'filename' is NULL,
93 * the default file $HOME/.zephyr.subs will be used.
94 *
95 * Returns 0 on success.  If the file does not exist, return -1 if
96 * 'error_on_nofile' is 1, otherwise return 0.  Return -1 if the file
97 * exists but can not be read.  Return -2 if there is a failure from
98 * zephyr to load the subscriptions.
99 */
100int owl_zephyr_loadsubs(char *filename, int error_on_nofile)
101{
102#ifdef HAVE_LIBZEPHYR
103  FILE *file;
104  char *tmp, *start;
105  char buffer[1024], subsfile[1024];
106  ZSubscription_t subs[3001];
107  int count, ret;
108  struct stat statbuff;
109
110  if (filename==NULL) {
111    sprintf(subsfile, "%s/%s", owl_global_get_homedir(&g), ".zephyr.subs");
112  } else {
113    strcpy(subsfile, filename);
114  }
115
116  ret=stat(subsfile, &statbuff);
117  if (ret) {
118    if (error_on_nofile==1) return(-1);
119    return(0);
120  }
121
122  ZResetAuthentication();
123  count=0;
124  file=fopen(subsfile, "r");
125  if (!file) return(-1);
126  while ( fgets(buffer, 1024, file)!=NULL ) {
127    if (buffer[0]=='#' || buffer[0]=='\n' || buffer[0]=='\n') continue;
128   
129    if (buffer[0]=='-') {
130      start=buffer+1;
131    } else {
132      start=buffer;
133    }
134   
135    if (count >= 3000) {
136      ret = owl_zephyr_loadsubs_helper(subs, count);
137      if (ret != 0) {
138        fclose(file);
139        return(ret);
140      }
141      count=0;
142    }
143   
144    /* add it to the list of subs */
145    if ((tmp=(char *) strtok(start, ",\n\r"))==NULL) continue;
146    subs[count].zsub_class=owl_strdup(tmp);
147    if ((tmp=(char *) strtok(NULL, ",\n\r"))==NULL) continue;
148    subs[count].zsub_classinst=owl_strdup(tmp);
149    if ((tmp=(char *) strtok(NULL, " \t\n\r"))==NULL) continue;
150    subs[count].zsub_recipient=owl_strdup(tmp);
151   
152    /* if it started with '-' then add it to the global punt list */
153    if (buffer[0]=='-') {
154      owl_function_zpunt(subs[count].zsub_class, subs[count].zsub_classinst, subs[count].zsub_recipient, 0);
155    }
156   
157    count++;
158  }
159  fclose(file);
160
161  ret=owl_zephyr_loadsubs_helper(subs, count);
162
163  return(ret);
164#else
165  return(0);
166#endif
167}
168
169int owl_zephyr_loaddefaultsubs()
170{
171#ifdef HAVE_LIBZEPHYR
172  ZSubscription_t subs[10];
173   
174  if (ZSubscribeTo(subs,0,0) != ZERR_NONE) {
175    owl_function_error("Error subscribing to default zephyr notifications.");
176    return(-1);
177  }
178  owl_zephyr_process_events(NULL);
179  return(0);
180#else
181  return(0);
182#endif
183}
184
185int owl_zephyr_loadloginsubs(char *filename)
186{
187#ifdef HAVE_LIBZEPHYR
188  FILE *file;
189  ZSubscription_t subs[3001];
190  char subsfile[1024], buffer[1024];
191  int count, ret, i;
192  struct stat statbuff;
193
194  if (filename==NULL) {
195    sprintf(subsfile, "%s/%s", owl_global_get_homedir(&g), ".anyone");
196  } else {
197    strcpy(subsfile, filename);
198  }
199 
200  ret=stat(subsfile, &statbuff);
201  if (ret) return(0);
202
203  ret=0;
204
205  ZResetAuthentication();
206  /* need to redo this to do chunks, not just bag out after 3000 */
207  count=0;
208  file=fopen(subsfile, "r");
209  if (file) {
210    while ( fgets(buffer, 1024, file)!=NULL ) {
211      if (buffer[0]=='#' || buffer[0]=='\n' || buffer[0]=='\n') continue;
212     
213      if (count >= 3000) break; /* also tell the user */
214
215      buffer[strlen(buffer)-1]='\0';
216      subs[count].zsub_class="login";
217      subs[count].zsub_recipient="*";
218      if (strchr(buffer, '@')) {
219        subs[count].zsub_classinst=owl_strdup(buffer);
220      } else {
221        subs[count].zsub_classinst=owl_sprintf("%s@%s", buffer, ZGetRealm());
222      }
223
224      count++;
225    }
226    fclose(file);
227  } else {
228    count=0;
229    ret=-1;
230  }
231
232  /* sub with defaults */
233  if (ZSubscribeToSansDefaults(subs,count,0) != ZERR_NONE) {
234    owl_function_error("Error subscribing to zephyr notifications.");
235    ret=-2;
236  }
237
238  /* free stuff */
239  for (i=0; i<count; i++) {
240    owl_free(subs[i].zsub_classinst);
241  }
242
243  owl_zephyr_process_events(NULL);
244
245  return(ret);
246#else
247  return(0);
248#endif
249}
250
251void unsuball()
252{
253#if HAVE_LIBZEPHYR
254  int ret;
255
256  ZResetAuthentication();
257  ret=ZCancelSubscriptions(0);
258  if (ret != ZERR_NONE) {
259    com_err("owl",ret,"while unsubscribing");
260  }
261#endif
262}
263
264int owl_zephyr_sub(char *class, char *inst, char *recip)
265{
266#ifdef HAVE_LIBZEPHYR
267  ZSubscription_t subs[5];
268
269  subs[0].zsub_class=class;
270  subs[0].zsub_classinst=inst;
271  subs[0].zsub_recipient=recip;
272
273  ZResetAuthentication();
274  if (ZSubscribeTo(subs,1,0) != ZERR_NONE) {
275    owl_function_error("Error subbing to <%s,%s,%s>", class, inst, recip);
276    return(-2);
277  }
278  owl_zephyr_process_events(NULL);
279  return(0);
280#else
281  return(0);
282#endif
283}
284
285
286int owl_zephyr_unsub(char *class, char *inst, char *recip)
287{
288#ifdef HAVE_LIBZEPHYR
289  ZSubscription_t subs[5];
290
291  subs[0].zsub_class=class;
292  subs[0].zsub_classinst=inst;
293  subs[0].zsub_recipient=recip;
294
295  ZResetAuthentication();
296  if (ZUnsubscribeTo(subs,1,0) != ZERR_NONE) {
297    owl_function_error("Error unsubbing from <%s,%s,%s>", class, inst, recip);
298    return(-2);
299  }
300  owl_zephyr_process_events(NULL);
301  return(0);
302#else
303  return(0);
304#endif
305}
306
307/* return a pointer to the data in the Jth field, (NULL terminated by
308 * definition).  Caller must free the return.
309 */
310#ifdef HAVE_LIBZEPHYR
311char *owl_zephyr_get_field(ZNotice_t *n, int j)
312{
313  int i, count, save;
314  char *out;
315
316  /* If there's no message here, just run along now */
317  if (n->z_message_len == 0)
318    return(owl_strdup(""));
319
320  count=save=0;
321  for (i=0; i<n->z_message_len; i++) {
322    if (n->z_message[i]=='\0') {
323      count++;
324      if (count==j) {
325        /* just found the end of the field we're looking for */
326        return(owl_strdup(n->z_message+save));
327      } else {
328        save=i+1;
329      }
330    }
331  }
332  /* catch the last field, which might not be null terminated */
333  if (count==j-1) {
334    out=owl_malloc(n->z_message_len-save+5);
335    memcpy(out, n->z_message+save, n->z_message_len-save);
336    out[n->z_message_len-save]='\0';
337    return(out);
338  }
339
340  return(owl_strdup(""));
341}
342#else
343char *owl_zephyr_get_field(void *n, int j)
344{
345  return(owl_strdup(""));
346}
347#endif
348
349
350#ifdef HAVE_LIBZEPHYR
351int owl_zephyr_get_num_fields(ZNotice_t *n)
352{
353  int i, fields;
354
355  if(n->z_message_len == 0)
356    return 0;
357
358  fields=1;
359  for (i=0; i<n->z_message_len; i++) {
360    if (n->z_message[i]=='\0') fields++;
361  }
362 
363  return(fields);
364}
365#else
366int owl_zephyr_get_num_fields(void *n)
367{
368  return(0);
369}
370#endif
371
372#ifdef HAVE_LIBZEPHYR
373/* return a pointer to the message, place the message length in k
374 * caller must free the return
375 */
376char *owl_zephyr_get_message(ZNotice_t *n)
377{
378  /* don't let ping messages have a body */
379  if (!strcasecmp(n->z_opcode, "ping")) {
380    return(owl_strdup(""));
381  }
382
383  /* deal with MIT Athena OLC messages */
384  if (!strcasecmp(n->z_sender, "olc.matisse@ATHENA.MIT.EDU")) {
385    return(owl_zephyr_get_field(n, 1));
386  }
387  /* deal with MIT NOC messages */
388  else if (!strcasecmp(n->z_sender, "rcmd.achilles@ATHENA.MIT.EDU")) {
389    /* $opcode service on $instance $3.\n$4 */
390    char *msg, *opcode, *instance, *field3, *field4;
391
392    opcode = n->z_opcode;
393    instance = n->z_class_inst;
394    field3 = owl_zephyr_get_field(n, 3);
395    field4 = owl_zephyr_get_field(n, 4);
396
397    msg = owl_sprintf("%s service on %s %s\n%s", opcode, instance, field3, field4);
398    if (msg) {
399      return msg;
400    }
401  }
402
403  return(owl_zephyr_get_field(n, 2));
404}
405#endif
406
407#ifdef HAVE_LIBZEPHYR
408char *owl_zephyr_get_zsig(ZNotice_t *n, int *k)
409{
410  /* return a pointer to the zsig if there is one */
411
412  /* message length 0? No zsig */
413  if (n->z_message_len==0) {
414    *k=0;
415    return("");
416  }
417
418  /* No zsig for OLC messages */
419  if (!strcasecmp(n->z_sender, "olc.matisse@ATHENA.MIT.EDU")) {
420    return("");
421  }
422
423  /* Everything else is field 1 */
424  *k=strlen(n->z_message);
425  return(n->z_message);
426}
427#else
428char *owl_zephyr_get_zsig(void *n, int *k)
429{
430  return("");
431}
432#endif
433
434int send_zephyr(char *opcode, char *zsig, char *class, char *instance, char *recipient, char *message)
435{
436#ifdef HAVE_LIBZEPHYR
437  int ret;
438  ZNotice_t notice;
439   
440  memset(&notice, 0, sizeof(notice));
441
442  ZResetAuthentication();
443
444  if (!zsig) zsig="";
445 
446  notice.z_kind=ACKED;
447  notice.z_port=0;
448  notice.z_class=class;
449  notice.z_class_inst=instance;
450  if (!strcmp(recipient, "*") || !strcmp(recipient, "@")) {
451    notice.z_recipient="";
452  } else {
453    notice.z_recipient=recipient;
454  }
455  notice.z_default_format="Class $class, Instance $instance:\nTo: @bold($recipient) at $time $date\nFrom: @bold{$1 <$sender>}\n\n$2";
456  notice.z_sender=NULL;
457  if (opcode) notice.z_opcode=opcode;
458
459  notice.z_message_len=strlen(zsig)+1+strlen(message);
460  notice.z_message=owl_malloc(notice.z_message_len+10);
461  strcpy(notice.z_message, zsig);
462  memcpy(notice.z_message+strlen(zsig)+1, message, strlen(message));
463
464  /* ret=ZSendNotice(&notice, ZAUTH); */
465  ret=ZSrvSendNotice(&notice, ZAUTH, send_zephyr_helper);
466 
467  /* free then check the return */
468  owl_free(notice.z_message);
469  ZFreeNotice(&notice);
470  owl_zephyr_process_events(NULL);
471  if (ret!=ZERR_NONE) {
472    owl_function_error("Error sending zephyr");
473    return(ret);
474  }
475  return(0);
476#else
477  return(0);
478#endif
479}
480
481#ifdef HAVE_LIBZEPHYR
482Code_t send_zephyr_helper(ZNotice_t *notice, char *buf, int len, int wait)
483{
484  int ret;
485  ret=ZSendPacket(buf, len, 0);
486  owl_zephyr_process_events(NULL);
487  return(ret);
488}
489#endif
490
491void send_ping(char *to)
492{
493#ifdef HAVE_LIBZEPHYR
494  send_zephyr("PING", "", "MESSAGE", "PERSONAL", to, "");
495  owl_zephyr_process_events(NULL);
496#endif
497}
498
499#ifdef HAVE_LIBZEPHYR
500void owl_zephyr_handle_ack(ZNotice_t *retnotice)
501{
502  char *tmp, *buff;
503 
504  /* if it's an HMACK ignore it */
505  if (retnotice->z_kind == HMACK) return;
506
507  if (retnotice->z_kind == SERVNAK) {
508    owl_function_error("Authorization failure sending zephyr");
509  } else if ((retnotice->z_kind != SERVACK) || !retnotice->z_message_len) {
510    owl_function_error("Detected server failure while receiving acknowledgement");
511  } else if (!strcmp(retnotice->z_message, ZSRVACK_SENT)) {
512    if (!strcasecmp(retnotice->z_opcode, "ping")) {
513      return;
514    } else if (!strcasecmp(retnotice->z_class, "message") &&
515               !strcasecmp(retnotice->z_class_inst, "personal")) {
516      tmp=short_zuser(retnotice->z_recipient);
517      owl_function_makemsg("Message sent to %s.", tmp);
518      free(tmp);
519    } else {
520      owl_function_makemsg("Message sent to -c %s -i %s\n", retnotice->z_class, retnotice->z_class_inst);
521    }
522  } else if (!strcmp(retnotice->z_message, ZSRVACK_NOTSENT)) {
523    if (strcasecmp(retnotice->z_class, "message")) {
524      owl_function_error("No one subscribed to class class %s", retnotice->z_class);
525      buff=owl_sprintf("Could not send message to class %s: no one subscribed.\n",
526                       retnotice->z_class);
527      owl_function_adminmsg("", buff);
528      owl_free(buff);
529    } else {
530      tmp = short_zuser(retnotice->z_recipient);
531      owl_function_error("%s: Not logged in or subscribing to messages.", tmp);
532      buff=owl_sprintf("Could not send message to %s: not logged in or subscribing to messages.\n",
533                       tmp);
534      owl_function_adminmsg("", buff);
535      owl_log_outgoing_zephyr_error(tmp, buff);
536      owl_free(tmp);
537      owl_free(buff);
538    }
539  } else {
540    owl_function_error("Internal error on ack (%s)", retnotice->z_message);
541  }
542  owl_zephyr_process_events(NULL);
543}
544#else
545void owl_zephyr_handle_ack(void *retnotice)
546{
547}
548#endif
549
550#ifdef HAVE_LIBZEPHYR
551int owl_zephyr_notice_is_ack(ZNotice_t *n)
552{
553  if (n->z_kind == SERVNAK || n->z_kind == SERVACK || n->z_kind == HMACK) {
554    if (!strcasecmp(n->z_class, LOGIN_CLASS)) return(0);
555    return(1);
556  }
557  return(0);
558}
559#else
560int owl_zephyr_notice_is_ack(void *n)
561{
562  return(0);
563}
564#endif
565 
566void owl_zephyr_zaway(owl_message *m)
567{
568#ifdef HAVE_LIBZEPHYR
569  char *tmpbuff, *myuser, *to;
570  owl_message *mout;
571 
572  /* bail if it doesn't look like a message we should reply to.  Some
573   * of this defined by the way zaway(1) works
574   */
575  if (strcasecmp(owl_message_get_class(m), "message")) return;
576  if (strcasecmp(owl_message_get_recipient(m), ZGetSender())) return;
577  if (!strcasecmp(owl_message_get_sender(m), "")) return;
578  if (!strcasecmp(owl_message_get_opcode(m), "ping")) return;
579  if (!strcasecmp(owl_message_get_opcode(m), "auto")) return;
580  if (!strcasecmp(owl_message_get_zsig(m), "Automated reply:")) return;
581  if (!strcasecmp(owl_message_get_sender(m), ZGetSender())) return;
582  if (owl_message_get_attribute_value(m, "isauto")) return;
583
584  if (owl_global_is_smartstrip(&g)) {
585    to=owl_zephyr_smartstripped_user(owl_message_get_sender(m));
586  } else {
587    to=owl_strdup(owl_message_get_sender(m));
588  }
589
590  send_zephyr("",
591              "Automated reply:",
592              owl_message_get_class(m),
593              owl_message_get_instance(m),
594              to,
595              owl_global_get_zaway_msg(&g));
596
597  myuser=short_zuser(to);
598  if (!strcasecmp(owl_message_get_instance(m), "personal")) {
599    tmpbuff = owl_sprintf("zwrite %s", myuser);
600  } else {
601    tmpbuff = owl_sprintf("zwrite -i %s %s", owl_message_get_instance(m), myuser);
602  }
603  owl_free(myuser);
604  owl_free(to);
605
606  /* display the message as an admin message in the receive window */
607  mout=owl_function_make_outgoing_zephyr(owl_global_get_zaway_msg(&g), tmpbuff, "Automated reply:");
608  owl_function_add_message(mout);
609  owl_free(tmpbuff);
610#endif
611}
612
613#ifdef HAVE_LIBZEPHYR
614void owl_zephyr_hackaway_cr(ZNotice_t *n)
615{
616  /* replace \r's with ' '.  Gross-ish */
617  int i;
618
619  for (i=0; i<n->z_message_len; i++) {
620    if (n->z_message[i]=='\r') {
621      n->z_message[i]=' ';
622    }
623  }
624}
625#endif
626
627char *owl_zephyr_zlocate(char *user, int auth)
628{
629#ifdef HAVE_LIBZEPHYR
630  int ret, numlocs;
631  int one = 1;
632  ZLocations_t locations;
633  char *myuser, *out, *tmp;
634 
635  ZResetAuthentication();
636  ret=ZLocateUser(user,&numlocs,auth?ZAUTH:ZNOAUTH);
637  owl_zephyr_process_events(NULL);
638  if (ret != ZERR_NONE) {
639    return(owl_sprintf("Error locating user %s\n", user));
640  }
641
642  if (numlocs==0) {
643    myuser=short_zuser(user);
644    out=owl_sprintf("%s: Hidden or not logged in\n", myuser);
645    owl_free(myuser);
646    return(out);
647  }
648
649  out=strdup("");
650  for (;numlocs;numlocs--) {
651    ZGetLocations(&locations,&one);
652    myuser=short_zuser(user);
653    tmp=owl_sprintf("%s%s: %s\t%s\t%s\n",
654                    out,
655                    myuser,
656                    locations.host ? locations.host : "?",
657                    locations.tty ? locations.tty : "?",
658                    locations.time ? locations.time : "?");
659    owl_free(out);
660    out=tmp;
661    owl_free(myuser);
662  }
663  owl_zephyr_process_events(NULL);
664  return(out);
665#else
666  return(owl_strdup("Zephyr not available"));
667#endif
668}
669
670void owl_zephyr_addsub(char *filename, char *class, char *inst, char *recip)
671{
672#ifdef HAVE_LIBZEPHYR
673  char *line, subsfile[LINE], buff[LINE];
674  FILE *file;
675
676  line=owl_zephyr_makesubline(class, inst, recip);
677
678  if (filename==NULL) {
679    sprintf(subsfile, "%s/%s", owl_global_get_homedir(&g), ".zephyr.subs");
680  } else {
681    strcpy(subsfile, filename);
682  }
683
684  /* if the file already exists, check to see if the sub is already there */
685  file=fopen(subsfile, "r");
686  if (file) {
687    while (fgets(buff, LINE, file)!=NULL) {
688      if (!strcasecmp(buff, line)) {
689        owl_function_error("Subscription already present in %s", subsfile);
690        owl_free(line);
691        fclose(file);
692        return;
693      }
694    }
695    fclose(file);
696  }
697
698  /* if we get here then we didn't find it */
699  file=fopen(subsfile, "a");
700  if (!file) {
701    owl_function_error("Error opening file %s for writing", subsfile);
702    owl_free(line);
703    return;
704  }
705  fputs(line, file);
706  fclose(file);
707  owl_function_makemsg("Subscription added");
708 
709  owl_free(line);
710#endif
711}
712
713void owl_zephyr_delsub(char *filename, char *class, char *inst, char *recip)
714{
715#ifdef HAVE_LIBZEPHYR
716  char *line, *subsfile;
717 
718  line=owl_zephyr_makesubline(class, inst, recip);
719  line[strlen(line)-1]='\0';
720
721  if (!filename) {
722    subsfile=owl_sprintf("%s/.zephyr.subs", owl_global_get_homedir(&g));
723  } else {
724    subsfile=owl_strdup(filename);
725  }
726 
727  owl_util_file_deleteline(subsfile, line, 1);
728  owl_free(subsfile);
729  owl_free(line);
730  owl_function_makemsg("Subscription removed");
731#endif
732}
733
734/* caller must free the return */
735char *owl_zephyr_makesubline(char *class, char *inst, char *recip)
736{
737  return(owl_sprintf("%s,%s,%s\n", class, inst, !strcmp(recip, "") ? "*" : recip));
738}
739
740void owl_zephyr_zlog_in(void)
741{
742#ifdef HAVE_LIBZEPHYR
743  char *exposure, *eset;
744  int ret;
745
746  ZResetAuthentication();
747   
748  eset=EXPOSE_REALMVIS;
749  exposure=ZGetVariable("exposure");
750  if (exposure==NULL) {
751    eset=EXPOSE_REALMVIS;
752  } else if (!strcasecmp(exposure,EXPOSE_NONE)) {
753    eset = EXPOSE_NONE;
754  } else if (!strcasecmp(exposure,EXPOSE_OPSTAFF)) {
755    eset = EXPOSE_OPSTAFF;
756  } else if (!strcasecmp(exposure,EXPOSE_REALMVIS)) {
757    eset = EXPOSE_REALMVIS;
758  } else if (!strcasecmp(exposure,EXPOSE_REALMANN)) {
759    eset = EXPOSE_REALMANN;
760  } else if (!strcasecmp(exposure,EXPOSE_NETVIS)) {
761    eset = EXPOSE_NETVIS;
762  } else if (!strcasecmp(exposure,EXPOSE_NETANN)) {
763    eset = EXPOSE_NETANN;
764  }
765   
766  ret=ZSetLocation(eset);
767  owl_zephyr_process_events(NULL);
768  if (ret != ZERR_NONE) {
769    /* owl_function_makemsg("Error setting location: %s", error_message(ret)); */
770  }
771#endif
772}
773
774void owl_zephyr_zlog_out(void)
775{
776#ifdef HAVE_LIBZEPHYR
777  int ret;
778
779  ZResetAuthentication();
780  ret=ZUnsetLocation();
781  owl_zephyr_process_events(NULL);
782  if (ret != ZERR_NONE) {
783    /* owl_function_makemsg("Error unsetting location: %s", error_message(ret)); */
784  }
785#endif
786}
787
788void owl_zephyr_addbuddy(char *name)
789{
790  char *filename;
791  FILE *file;
792 
793  filename=owl_sprintf("%s/.anyone", owl_global_get_homedir(&g));
794  file=fopen(filename, "a");
795  owl_free(filename);
796  if (!file) {
797    owl_function_error("Error opening zephyr buddy file for append");
798    return;
799  }
800  fprintf(file, "%s\n", name);
801  fclose(file);
802}
803
804void owl_zephyr_delbuddy(char *name)
805{
806  char *filename;
807
808  filename=owl_sprintf("%s/.anyone", owl_global_get_homedir(&g));
809  owl_util_file_deleteline(filename, name, 0);
810  owl_free(filename);
811}
812
813/* return auth string */
814#ifdef HAVE_LIBZEPHYR
815char *owl_zephyr_get_authstr(ZNotice_t *n)
816{
817
818  if (!n) return("UNKNOWN");
819
820  if (n->z_auth == ZAUTH_FAILED) {
821    return ("FAILED");
822  } else if (n->z_auth == ZAUTH_NO) {
823    return ("NO");
824  } else if (n->z_auth == ZAUTH_YES) {
825    return ("YES");
826  } else {
827    return ("UNKNOWN");
828  }           
829}
830#else
831char *owl_zephyr_get_authstr(void *n)
832{
833  return("");
834}
835#endif
836
837/* Returns a buffer of subscriptions or an error message.  Caller must
838 * free the return.
839 */
840char *owl_zephyr_getsubs()
841{
842#ifdef HAVE_LIBZEPHYR
843  int ret, num, i, one;
844  int buffsize;
845  ZSubscription_t sub;
846  char *out;
847  one=1;
848
849  ret=ZRetrieveSubscriptions(0, &num);
850  if (ret==ZERR_TOOMANYSUBS) {
851    return(owl_strdup("Zephyr: too many subscriptions\n"));
852  } else if (ret || (num <= 0)) {
853    return(owl_strdup("Zephyr: error retriving subscriptions\n"));
854  }
855
856  buffsize = (num + 1) * 50;
857  out=owl_malloc(buffsize);
858  strcpy(out, "");
859  for (i=0; i<num; i++) {
860    if ((ret = ZGetSubscriptions(&sub, &one)) != ZERR_NONE) {
861      owl_free(out);
862      ZFlushSubscriptions();
863      out=owl_strdup("Error while getting subscriptions\n");
864      return(out);
865    } else {
866      int tmpbufflen;
867      char *tmpbuff;
868      owl_zephyr_process_events(NULL);
869      tmpbuff = owl_sprintf("<%s,%s,%s>\n%s", sub.zsub_class, sub.zsub_classinst, sub.zsub_recipient, out);
870      tmpbufflen = strlen(tmpbuff) + 1;
871      if (tmpbufflen > buffsize) {
872        char *out2;
873        buffsize = tmpbufflen * 2;
874        out2 = owl_realloc(out, buffsize);
875        if (out2 == NULL) {
876          owl_free(out);
877          owl_free(tmpbuff);
878          ZFlushSubscriptions();
879          out=owl_strdup("Realloc error while getting subscriptions\n");
880          return(out);   
881        }
882        out = out2;
883      }
884      strcpy(out, tmpbuff);
885      owl_free(tmpbuff);
886    }
887  }
888
889  ZFlushSubscriptions();
890  owl_zephyr_process_events(NULL);
891  return(out);
892#else
893  return(owl_strdup("Zephyr not available"));
894#endif
895}
896
897char *owl_zephyr_get_variable(char *var)
898{
899#ifdef HAVE_LIBZEPHYR
900  return(ZGetVariable(var));
901#else
902  return("");
903#endif
904}
905
906void owl_zephyr_set_locationinfo(char *host, char *val)
907{
908#ifdef HAVE_LIBZEPHYR
909  ZInitLocationInfo(host, val);
910  owl_zephyr_process_events(NULL);
911#endif
912}
913 
914/* Strip a local realm fron the zephyr user name.
915 * The caller must free the return
916 */
917char *short_zuser(char *in)
918{
919  char *out, *ptr;
920
921  out=owl_strdup(in);
922  ptr=strchr(out, '@');
923  if (ptr) {
924    if (!strcasecmp(ptr+1, owl_zephyr_get_realm())) {
925      *ptr='\0';
926    }
927  }
928  return(out);
929}
930
931/* Append a local realm to the zephyr user name if necessary.
932 * The caller must free the return.
933 */
934char *long_zuser(char *in)
935{
936  if (strchr(in, '@')) {
937    return(owl_strdup(in));
938  }
939  return(owl_sprintf("%s@%s", in, owl_zephyr_get_realm()));
940}
941
942/* strip out the instance from a zsender's principal.  Preserves the
943 * realm if present.  daemon.webzephyr is a special case.  The
944 * caller must free the return
945 */
946char *owl_zephyr_smartstripped_user(char *in)
947{
948  char *ptr, *realm, *out;
949
950  out=owl_strdup(in);
951
952  /* bail immeaditly if we don't have to do any work */
953  ptr=strchr(in, '.');
954  if (!strchr(in, '/') && !ptr) {
955    /* no '/' and no '.' */
956    return(out);
957  }
958  if (ptr && strchr(in, '@') && (ptr > strchr(in, '@'))) {
959    /* There's a '.' but it's in the realm */
960    return(out);
961  }
962  if (!strncasecmp(in, OWL_WEBZEPHYR_PRINCIPAL, strlen(OWL_WEBZEPHYR_PRINCIPAL))) {
963    return(out);
964  }
965
966  /* remove the realm from ptr, but hold on to it */
967  realm=strchr(out, '@');
968  if (realm) realm[0]='\0';
969
970  /* strip */
971  ptr=strchr(out, '.');
972  if (!ptr) ptr=strchr(out, '/');
973  ptr[0]='\0';
974
975  /* reattach the realm if we had one */
976  if (realm) {
977    strcat(out, "@");
978    strcat(out, realm+1);
979  }
980
981  return(out);
982}
983
984/* read the list of users in 'filename' as a .anyone file, and put the
985 * names of the zephyr users in the list 'in'.  If 'filename' is NULL,
986 * use the default .anyone file in the users home directory.  Returns
987 * -1 on failure, 0 on success.
988 */
989int owl_zephyr_get_anyone_list(owl_list *in, char *filename)
990{
991#ifdef HAVE_LIBZEPHYR
992  char *ourfile, *tmp, buff[LINE];
993  FILE *f;
994
995  if (filename==NULL) {
996    tmp=owl_global_get_homedir(&g);
997    ourfile=owl_sprintf("%s/.anyone", owl_global_get_homedir(&g));
998  } else {
999    ourfile=owl_strdup(filename);
1000  }
1001 
1002  f=fopen(ourfile, "r");
1003  if (!f) {
1004    owl_function_error("Error opening file %s: %s", ourfile, strerror(errno) ? strerror(errno) : "");
1005    owl_free(ourfile);
1006    return(-1);
1007  }
1008
1009  while (fgets(buff, LINE, f)!=NULL) {
1010    /* ignore comments, blank lines etc. */
1011    if (buff[0]=='#') continue;
1012    if (buff[0]=='\n') continue;
1013    if (buff[0]=='\0') continue;
1014   
1015    /* strip the \n */
1016    buff[strlen(buff)-1]='\0';
1017   
1018    /* ingore from # on */
1019    tmp=strchr(buff, '#');
1020    if (tmp) tmp[0]='\0';
1021   
1022    /* ingore from SPC */
1023    tmp=strchr(buff, ' ');
1024    if (tmp) tmp[0]='\0';
1025   
1026    /* stick on the local realm. */
1027    if (!strchr(buff, '@')) {
1028      strcat(buff, "@");
1029      strcat(buff, ZGetRealm());
1030    }
1031    owl_list_append_element(in, owl_strdup(buff));
1032  }
1033  fclose(f);
1034  owl_free(ourfile);
1035  return(0);
1036#else
1037  return(-1);
1038#endif
1039}
1040
1041
1042#ifdef HAVE_LIBZEPHYR
1043void owl_zephyr_process_events(owl_dispatch *d) {
1044  int zpendcount=0;
1045  ZNotice_t notice;
1046  struct sockaddr_in from;
1047  owl_message *m=NULL;
1048
1049  while(owl_zephyr_zpending() && zpendcount < 20) {
1050    if (owl_zephyr_zpending()) {
1051      ZReceiveNotice(&notice, &from);
1052      zpendcount++;
1053
1054      /* is this an ack from a zephyr we sent? */
1055      if (owl_zephyr_notice_is_ack(&notice)) {
1056        owl_zephyr_handle_ack(&notice);
1057        continue;
1058      }
1059
1060      /* if it's a ping and we're not viewing pings then skip it */
1061      if (!owl_global_is_rxping(&g) && !strcasecmp(notice.z_opcode, "ping")) {
1062        continue;
1063      }
1064
1065      /* create the new message */
1066      m=owl_malloc(sizeof(owl_message));
1067      owl_message_create_from_znotice(m, &notice);
1068
1069      owl_global_messagequeue_addmsg(&g, m);
1070    }
1071  }
1072}
1073
1074#else
1075void owl_zephyr_process_events(owl_dispatch *d) {
1076 
1077}
1078#endif
Note: See TracBrowser for help on using the repository browser.