source: zephyr.c @ 99dabee

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