source: zephyr.c @ 789462a

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