source: zephyr.c @ fba0f96

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