source: zephyr.c @ 2b86d14

barnowl_perlaimdebianowlrelease-1.10release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since 2b86d14 was 2b86d14, checked in by James M. Kretchmar <kretch@mit.edu>, 20 years ago
Log when outgoing personal message fails
  • Property mode set to 100644
File size: 20.8 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 */
319char *owl_zephyr_get_message(ZNotice_t *n)
320{
321  if (!strcasecmp(n->z_opcode, "ping")) {
322    return("");
323  }
324
325  return(owl_zephyr_get_field(n, 2));
326}
327#endif
328
329#ifdef HAVE_LIBZEPHYR
330char *owl_zephyr_get_zsig(ZNotice_t *n, int *k)
331{
332  /* return a pointer to the zsig if there is one */
333
334  if (n->z_message_len==0) {
335    *k=0;
336    return("");
337  }
338  *k=strlen(n->z_message);
339  return(n->z_message);
340}
341#else
342char *owl_zephyr_get_zsig(void *n, int *k)
343{
344  return("");
345}
346#endif
347
348int send_zephyr(char *opcode, char *zsig, char *class, char *instance, char *recipient, char *message)
349{
350#ifdef HAVE_LIBZEPHYR
351  int ret;
352  ZNotice_t notice;
353   
354  memset(&notice, 0, sizeof(notice));
355
356  ZResetAuthentication();
357 
358  notice.z_kind=ACKED;
359  notice.z_port=0;
360  notice.z_class=class;
361  notice.z_class_inst=instance;
362  if (!strcmp(recipient, "*") || !strcmp(recipient, "@")) {
363    notice.z_recipient="";
364  } else {
365    notice.z_recipient=recipient;
366  }
367  notice.z_default_format="Class $class, Instance $instance:\nTo: @bold($recipient) at $time $date\nFrom: @bold{$1 <$sender>}\n\n$2";
368  notice.z_sender=NULL;
369  if (opcode) notice.z_opcode=opcode;
370
371  notice.z_message_len=strlen(zsig)+1+strlen(message);
372  notice.z_message=owl_malloc(notice.z_message_len+10);
373  strcpy(notice.z_message, zsig);
374  memcpy(notice.z_message+strlen(zsig)+1, message, strlen(message));
375
376  /* ret=ZSendNotice(&notice, ZAUTH); */
377  ret=ZSrvSendNotice(&notice, ZAUTH, send_zephyr_helper);
378 
379  /* free then check the return */
380  owl_free(notice.z_message);
381  ZFreeNotice(&notice);
382  if (ret!=ZERR_NONE) {
383    owl_function_error("Error sending zephyr");
384    return(ret);
385  }
386  return(0);
387#else
388  return(0);
389#endif
390}
391
392#ifdef HAVE_LIBZEPHYR
393Code_t send_zephyr_helper(ZNotice_t *notice, char *buf, int len, int wait)
394{
395  return(ZSendPacket(buf, len, 0));
396}
397#endif
398
399void send_ping(char *to)
400{
401#ifdef HAVE_LIBZEPHYR
402  send_zephyr("PING", "", "MESSAGE", "PERSONAL", to, "");
403#endif
404}
405
406#ifdef HAVE_LIBZEPHYR
407void owl_zephyr_handle_ack(ZNotice_t *retnotice)
408{
409  char *tmp;
410 
411  /* if it's an HMACK ignore it */
412  if (retnotice->z_kind == HMACK) return;
413
414  if (retnotice->z_kind == SERVNAK) {
415    owl_function_error("Authorization failure sending zephyr");
416  } else if ((retnotice->z_kind != SERVACK) || !retnotice->z_message_len) {
417    owl_function_error("Detected server failure while receiving acknowledgement");
418  } else if (!strcmp(retnotice->z_message, ZSRVACK_SENT)) {
419    if (!strcasecmp(retnotice->z_opcode, "ping")) {
420      return;
421    } else if (!strcasecmp(retnotice->z_class, "message") &&
422               !strcasecmp(retnotice->z_class_inst, "personal")) {
423      tmp=short_zuser(retnotice->z_recipient);
424      owl_function_makemsg("Message sent to %s.", tmp);
425      free(tmp);
426    } else {
427      owl_function_makemsg("Message sent to -c %s -i %s\n", retnotice->z_class, retnotice->z_class_inst);
428    }
429  } else if (!strcmp(retnotice->z_message, ZSRVACK_NOTSENT)) {
430    if (strcasecmp(retnotice->z_class, "message")) {
431      char buff[1024];
432      owl_function_error("No one subscribed to class class %s", retnotice->z_class);
433      sprintf(buff, "Could not send message to class %s: no one subscribed.\n", retnotice->z_class);
434      owl_function_adminmsg("", buff);
435    } else {
436      char buff[1024];
437      tmp = short_zuser(retnotice->z_recipient);
438      owl_function_error("%s: Not logged in or subscribing to messages.", 
439                           tmp);
440
441      sprintf(buff, "Could not send message to %s: not logged in or subscribing to messages.\n", tmp);
442      owl_function_adminmsg("", buff);
443      if (owl_global_is_logging(&g)) owl_log_outgoing_zephyr_error(tmp, buff);
444      owl_free(tmp);
445    }
446  } else {
447    owl_function_error("Internal error on ack (%s)", retnotice->z_message);
448  }
449}
450#else
451void owl_zephyr_handle_ack(void *retnotice)
452{
453}
454#endif
455
456#ifdef HAVE_LIBZEPHYR
457int owl_zephyr_notice_is_ack(ZNotice_t *n)
458{
459  if (n->z_kind == SERVNAK || n->z_kind == SERVACK || n->z_kind == HMACK) {
460    if (!strcasecmp(n->z_class, LOGIN_CLASS)) return(0);
461    return(1);
462  }
463  return(0);
464}
465#else
466int owl_zephyr_notice_is_ack(void *n)
467{
468  return(0);
469}
470#endif
471 
472void owl_zephyr_zaway(owl_message *m)
473{
474#ifdef HAVE_LIBZEPHYR
475  char *tmpbuff, *myuser, *to;
476 
477  /* bail if it doesn't look like a message we should reply to.  Some
478   * of this defined by the way zaway(1) works
479   */
480  if (strcasecmp(owl_message_get_class(m), "message")) return;
481  if (strcasecmp(owl_message_get_recipient(m), ZGetSender())) return;
482  if (!strcasecmp(owl_message_get_sender(m), "")) return;
483  if (!strcasecmp(owl_message_get_opcode(m), "ping")) return;
484  if (!strcasecmp(owl_message_get_opcode(m), "auto")) return;
485  if (!strcasecmp(owl_message_get_zsig(m), "Automated reply:")) return;
486  if (!strcasecmp(owl_message_get_sender(m), ZGetSender())) return;
487  if (owl_message_get_attribute_value(m, "isauto")) return;
488
489  if (owl_global_is_smartstrip(&g)) {
490    to=owl_zephyr_smartstripped_user(owl_message_get_sender(m));
491  } else {
492    to=owl_strdup(owl_message_get_sender(m));
493  }
494
495  send_zephyr("",
496              "Automated reply:",
497              owl_message_get_class(m),
498              owl_message_get_instance(m),
499              to,
500              owl_global_get_zaway_msg(&g));
501
502  myuser=short_zuser(to);
503  if (!strcasecmp(owl_message_get_instance(m), "personal")) {
504    tmpbuff = owl_sprintf("zwrite %s", myuser);
505  } else {
506    tmpbuff = owl_sprintf("zwrite -i %s %s", owl_message_get_instance(m), myuser);
507  }
508  owl_free(myuser);
509  owl_free(to);
510
511  /* display the message as an admin message in the receive window */
512  owl_function_make_outgoing_zephyr(owl_global_get_zaway_msg(&g), tmpbuff, "Automated reply:");
513  owl_free(tmpbuff);
514#endif
515}
516
517#ifdef HAVE_LIBZEPHYR
518void owl_zephyr_hackaway_cr(ZNotice_t *n)
519{
520  /* replace \r's with ' '.  Gross-ish */
521  int i;
522
523  for (i=0; i<n->z_message_len; i++) {
524    if (n->z_message[i]=='\r') {
525      n->z_message[i]=' ';
526    }
527  }
528}
529#endif
530
531void owl_zephyr_zlocate(char *user, char *out, int auth)
532{
533#ifdef HAVE_LIBZEPHYR
534  int ret, numlocs;
535  int one = 1;
536  ZLocations_t locations;
537  char *myuser;
538 
539  strcpy(out, "");
540  ZResetAuthentication();
541  ret=ZLocateUser(user,&numlocs,auth?ZAUTH:ZNOAUTH);
542  if (ret != ZERR_NONE) {
543    owl_function_error("Error locating user %s", user);
544  }
545
546  if (numlocs==0) {
547    myuser=short_zuser(user);
548    sprintf(out, "%s: Hidden or not logged-in\n", myuser);
549    owl_free(myuser);
550    return;
551  }
552   
553  for (;numlocs;numlocs--) {
554    ZGetLocations(&locations,&one);
555    myuser=short_zuser(user);
556    sprintf(out, "%s%s: %s\t%s\t%s\n", out, myuser, locations.host, locations.tty, locations.time);
557    owl_free(myuser);
558  }
559#endif
560}
561
562void owl_zephyr_addsub(char *filename, char *class, char *inst, char *recip)
563{
564#ifdef HAVE_LIBZEPHYR
565  char *line, subsfile[LINE], buff[LINE];
566  FILE *file;
567
568  line=owl_zephyr_makesubline(class, inst, recip);
569
570  if (filename==NULL) {
571    sprintf(subsfile, "%s/%s", owl_global_get_homedir(&g), ".zephyr.subs");
572  } else {
573    strcpy(subsfile, filename);
574  }
575
576  /* first check if it exists already */
577  file=fopen(subsfile, "r");
578  if (!file) {
579    owl_function_error("Error opening file %s", subsfile);
580    owl_free(line);
581    return;
582  }
583  while (fgets(buff, LINE, file)!=NULL) {
584    if (!strcasecmp(buff, line)) {
585      owl_function_error("Subscription already present in %s", subsfile);
586      owl_free(line);
587      return;
588    }
589  }
590
591  /* if we get here then we didn't find it */
592  fclose(file);
593  file=fopen(subsfile, "a");
594  if (!file) {
595    owl_function_error("Error opening file %s for writing", subsfile);
596    owl_free(line);
597    return;
598  }
599  fputs(line, file);
600  fclose(file);
601  owl_function_makemsg("Subscription added");
602 
603  owl_free(line);
604#endif
605}
606
607void owl_zephyr_delsub(char *filename, char *class, char *inst, char *recip)
608{
609#ifdef HAVE_LIBZEPHYR
610  char *line, *subsfile;
611 
612  line=owl_zephyr_makesubline(class, inst, recip);
613  line[strlen(line)-1]='\0';
614
615  if (!filename) {
616    subsfile=owl_sprintf("%s/.zephyr.subs", owl_global_get_homedir(&g));
617  } else {
618    subsfile=owl_strdup(filename);
619  }
620 
621  owl_util_file_deleteline(subsfile, line, 1);
622  owl_free(subsfile);
623  owl_free(line);
624  owl_function_makemsg("Subscription removed");
625#endif
626}
627
628/* caller must free the return */
629char *owl_zephyr_makesubline(char *class, char *inst, char *recip)
630{
631  char *out;
632
633  out=owl_malloc(strlen(class)+strlen(inst)+strlen(recip)+30);
634  sprintf(out, "%s,%s,%s\n", class, inst, !strcmp(recip, "") ? "*" : recip);
635  return(out);
636}
637
638
639void owl_zephyr_zlog_in(void)
640{
641#ifdef HAVE_LIBZEPHYR
642  char *exposure, *eset;
643  int ret;
644
645  ZResetAuthentication();
646   
647  eset=EXPOSE_REALMVIS;
648  exposure=ZGetVariable("exposure");
649  if (exposure==NULL) {
650    eset=EXPOSE_REALMVIS;
651  } else if (!strcasecmp(exposure,EXPOSE_NONE)) {
652    eset = EXPOSE_NONE;
653  } else if (!strcasecmp(exposure,EXPOSE_OPSTAFF)) {
654    eset = EXPOSE_OPSTAFF;
655  } else if (!strcasecmp(exposure,EXPOSE_REALMVIS)) {
656    eset = EXPOSE_REALMVIS;
657  } else if (!strcasecmp(exposure,EXPOSE_REALMANN)) {
658    eset = EXPOSE_REALMANN;
659  } else if (!strcasecmp(exposure,EXPOSE_NETVIS)) {
660    eset = EXPOSE_NETVIS;
661  } else if (!strcasecmp(exposure,EXPOSE_NETANN)) {
662    eset = EXPOSE_NETANN;
663  }
664   
665  ret=ZSetLocation(eset);
666  if (ret != ZERR_NONE) {
667    /*
668      char buff[LINE];
669      sprintf(buff, "Error setting location: %s", error_message(ret));
670      owl_function_makemsg(buff);
671    */
672  }
673#endif
674}
675
676void owl_zephyr_zlog_out(void)
677{
678#ifdef HAVE_LIBZEPHYR
679  int ret;
680
681  ZResetAuthentication();
682  ret=ZUnsetLocation();
683  if (ret != ZERR_NONE) {
684    /*
685      char buff[LINE];
686      sprintf(buff, "Error unsetting location: %s", error_message(ret));
687      owl_function_makemsg(buff);
688    */
689  }
690#endif
691}
692
693void owl_zephyr_addbuddy(char *name)
694{
695  char *filename;
696  FILE *file;
697 
698  filename=owl_sprintf("%s/.anyone", owl_global_get_homedir(&g));
699  file=fopen(filename, "a");
700  owl_free(filename);
701  if (!file) {
702    owl_function_error("Error opening zephyr buddy file for append");
703    return;
704  }
705  fprintf(file, "%s\n", name);
706  fclose(file);
707}
708
709void owl_zephyr_delbuddy(char *name)
710{
711  char *filename;
712
713  filename=owl_sprintf("%s/.anyone", owl_global_get_homedir(&g));
714  owl_util_file_deleteline(filename, name, 0);
715  owl_free(filename);
716}
717
718/* return auth string */
719#ifdef HAVE_LIBZEPHYR
720char *owl_zephyr_get_authstr(ZNotice_t *n)
721{
722
723  if (!n) return("UNKNOWN");
724
725  if (n->z_auth == ZAUTH_FAILED) {
726    return ("FAILED");
727  } else if (n->z_auth == ZAUTH_NO) {
728    return ("NO");
729  } else if (n->z_auth == ZAUTH_YES) {
730    return ("YES");
731  } else {
732    return ("UNKNOWN");
733  }           
734}
735#else
736char *owl_zephyr_get_authstr(void *n)
737{
738  return("");
739}
740#endif
741
742/* Returns a buffer of subscriptions or an error message.  Caller must
743 * free the return.
744 */
745char *owl_zephyr_getsubs()
746{
747#ifdef HAVE_LIBZEPHYR
748  int ret, num, i, one;
749  ZSubscription_t sub;
750  char *out, *tmpbuff;
751  one=1;
752
753  ret=ZRetrieveSubscriptions(0, &num);
754  if (ret==ZERR_TOOMANYSUBS) {
755    return(owl_strdup("Zephyr: too many subscriptions\n"));
756  } else if (ret) {
757    return(owl_strdup("Zephyr: error retriving subscriptions\n"));
758  }
759
760  out=owl_malloc(num*500);
761  tmpbuff=owl_malloc(num*500);
762  strcpy(out, "");
763  for (i=0; i<num; i++) {
764    if ((ret = ZGetSubscriptions(&sub, &one)) != ZERR_NONE) {
765      owl_free(out);
766      owl_free(tmpbuff);
767      ZFlushSubscriptions();
768      out=owl_strdup("Error while getting subscriptions\n");
769      return(out);
770    } else {
771      sprintf(tmpbuff, "<%s,%s,%s>\n%s", sub.zsub_class, sub.zsub_classinst, sub.zsub_recipient, out);
772      strcpy(out, tmpbuff);
773    }
774  }
775
776  owl_free(tmpbuff);
777  ZFlushSubscriptions();
778  return(out);
779#else
780  return(owl_strdup("Zephyr not available"));
781#endif
782}
783
784char *owl_zephyr_get_variable(char *var)
785{
786#ifdef HAVE_LIBZEPHYR
787  return(ZGetVariable(var));
788#else
789  return("");
790#endif
791}
792
793void owl_zephyr_set_locationinfo(char *host, char *val)
794{
795#ifdef HAVE_LIBZEPHYR
796  ZInitLocationInfo(host, val);
797#endif
798}
799 
800/* Strip a local realm fron the zephyr user name.
801 * The caller must free the return
802 */
803char *short_zuser(char *in)
804{
805  char *out, *ptr;
806
807  out=owl_strdup(in);
808  ptr=strchr(out, '@');
809  if (ptr) {
810    if (!strcasecmp(ptr+1, owl_zephyr_get_realm())) {
811      *ptr='\0';
812    }
813  }
814  return(out);
815}
816
817/* Append a local realm to the zephyr user name if necessary.
818 * The caller must free the return.
819 */
820char *long_zuser(char *in)
821{
822  char *ptr;
823
824  if (NULL != (ptr=strchr(in, '@'))) {
825    return owl_strdup(in);
826  } else {
827    return owl_sprintf("%s@%s", in, owl_zephyr_get_realm());
828  }
829}
830
831
832/* strip out the instance from a zsender's principal.  Preserves the
833 * realm if present.  daemon.webzephyr is a special case.  The
834 * caller must free the return
835 */
836char *owl_zephyr_smartstripped_user(char *in)
837{
838  char *ptr, *realm, *out;
839
840  out=owl_strdup(in);
841
842  /* bail immeaditly if we don't have to do any work */
843  ptr=strchr(in, '.');
844  if (!strchr(in, '/') && !ptr) {
845    /* no '/' and no '.' */
846    return(out);
847  }
848  if (ptr && strchr(in, '@') && (ptr > strchr(in, '@'))) {
849    /* There's a '.' but it's in the realm */
850    return(out);
851  }
852  if (!strncasecmp(in, OWL_WEBZEPHYR_PRINCIPAL, strlen(OWL_WEBZEPHYR_PRINCIPAL))) {
853    return(out);
854  }
855
856  /* remove the realm from ptr, but hold on to it */
857  realm=strchr(out, '@');
858  if (realm) realm[0]='\0';
859
860  /* strip */
861  ptr=strchr(out, '.');
862  if (!ptr) ptr=strchr(out, '/');
863  ptr[0]='\0';
864
865  /* reattach the realm if we had one */
866  if (realm) {
867    strcat(out, "@");
868    strcat(out, realm+1);
869  }
870
871  return(out);
872}
873
874/* read the list of users in 'filename' as a .anyone file, and put the
875 * names of the zephyr users in the list 'in'.  If 'filename' is NULL,
876 * use the default .anyone file in the users home directory.  Returns
877 * -1 on failure, 0 on success.
878 */
879int owl_zephyr_get_anyone_list(owl_list *in, char *filename)
880{
881#ifdef HAVE_LIBZEPHYR
882  char *ourfile, *tmp, buff[LINE];
883  FILE *f;
884
885  if (filename==NULL) {
886    tmp=owl_global_get_homedir(&g);
887    ourfile=owl_sprintf("%s/.anyone", owl_global_get_homedir(&g));
888  } else {
889    ourfile=owl_strdup(filename);
890  }
891 
892  f=fopen(ourfile, "r");
893  if (!f) {
894    owl_function_error("Error opening file %s: %s", ourfile, strerror(errno) ? strerror(errno) : "");
895    owl_free(ourfile);
896    return(-1);
897  }
898
899  while (fgets(buff, LINE, f)!=NULL) {
900    /* ignore comments, blank lines etc. */
901    if (buff[0]=='#') continue;
902    if (buff[0]=='\n') continue;
903    if (buff[0]=='\0') continue;
904   
905    /* strip the \n */
906    buff[strlen(buff)-1]='\0';
907   
908    /* ingore from # on */
909    tmp=strchr(buff, '#');
910    if (tmp) tmp[0]='\0';
911   
912    /* ingore from SPC */
913    tmp=strchr(buff, ' ');
914    if (tmp) tmp[0]='\0';
915   
916    /* stick on the local realm. */
917    if (!strchr(buff, '@')) {
918      strcat(buff, "@");
919      strcat(buff, ZGetRealm());
920    }
921    owl_list_append_element(in, owl_strdup(buff));
922  }
923  fclose(f);
924  owl_free(ourfile);
925  return(0);
926#else
927  return(-1);
928#endif
929}
Note: See TracBrowser for help on using the repository browser.