source: zephyr.c @ 765fa34

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