source: zephyr.c @ bf73bdd

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