source: zephyr.c @ 0d5d51b

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