source: zephyr.c @ 836e6263

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