source: zephyr.c @ cd5adec

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