source: zephyr.c @ 93e883d

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