source: zephyr.c @ 64b6449

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