source: zephyr.c @ ae47efb

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