source: zephyr.c @ f6050ee

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