source: zephyr.c @ fa3290d

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