source: zephyr.c @ 1c2e0b3

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