source: zephyr.c @ 9b9e2d9c

owl
Last change on this file since 9b9e2d9c was 9b9e2d9c, checked in by James M. Kretchmar <kretch@mit.edu>, 15 years ago
zlocate return with no zephyr
  • 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  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#else
650  return(owl_strdup("Zephyr not available"));
651#endif
652}
653
654void owl_zephyr_addsub(char *filename, char *class, char *inst, char *recip)
655{
656#ifdef HAVE_LIBZEPHYR
657  char *line, subsfile[LINE], buff[LINE];
658  FILE *file;
659
660  line=owl_zephyr_makesubline(class, inst, recip);
661
662  if (filename==NULL) {
663    sprintf(subsfile, "%s/%s", owl_global_get_homedir(&g), ".zephyr.subs");
664  } else {
665    strcpy(subsfile, filename);
666  }
667
668  /* if the file already exists, check to see if the sub is already there */
669  file=fopen(subsfile, "r");
670  if (file) {
671    while (fgets(buff, LINE, file)!=NULL) {
672      if (!strcasecmp(buff, line)) {
673        owl_function_error("Subscription already present in %s", subsfile);
674        owl_free(line);
675        fclose(file);
676        return;
677      }
678    }
679    fclose(file);
680  }
681
682  /* if we get here then we didn't find it */
683  file=fopen(subsfile, "a");
684  if (!file) {
685    owl_function_error("Error opening file %s for writing", subsfile);
686    owl_free(line);
687    return;
688  }
689  fputs(line, file);
690  fclose(file);
691  owl_function_makemsg("Subscription added");
692 
693  owl_free(line);
694#endif
695}
696
697void owl_zephyr_delsub(char *filename, char *class, char *inst, char *recip)
698{
699#ifdef HAVE_LIBZEPHYR
700  char *line, *subsfile;
701 
702  line=owl_zephyr_makesubline(class, inst, recip);
703  line[strlen(line)-1]='\0';
704
705  if (!filename) {
706    subsfile=owl_sprintf("%s/.zephyr.subs", owl_global_get_homedir(&g));
707  } else {
708    subsfile=owl_strdup(filename);
709  }
710 
711  owl_util_file_deleteline(subsfile, line, 1);
712  owl_free(subsfile);
713  owl_free(line);
714  owl_function_makemsg("Subscription removed");
715#endif
716}
717
718/* caller must free the return */
719char *owl_zephyr_makesubline(char *class, char *inst, char *recip)
720{
721  return(owl_sprintf("%s,%s,%s\n", class, inst, !strcmp(recip, "") ? "*" : recip));
722}
723
724void owl_zephyr_zlog_in(void)
725{
726#ifdef HAVE_LIBZEPHYR
727  char *exposure, *eset;
728  int ret;
729
730  ZResetAuthentication();
731   
732  eset=EXPOSE_REALMVIS;
733  exposure=ZGetVariable("exposure");
734  if (exposure==NULL) {
735    eset=EXPOSE_REALMVIS;
736  } else if (!strcasecmp(exposure,EXPOSE_NONE)) {
737    eset = EXPOSE_NONE;
738  } else if (!strcasecmp(exposure,EXPOSE_OPSTAFF)) {
739    eset = EXPOSE_OPSTAFF;
740  } else if (!strcasecmp(exposure,EXPOSE_REALMVIS)) {
741    eset = EXPOSE_REALMVIS;
742  } else if (!strcasecmp(exposure,EXPOSE_REALMANN)) {
743    eset = EXPOSE_REALMANN;
744  } else if (!strcasecmp(exposure,EXPOSE_NETVIS)) {
745    eset = EXPOSE_NETVIS;
746  } else if (!strcasecmp(exposure,EXPOSE_NETANN)) {
747    eset = EXPOSE_NETANN;
748  }
749   
750  ret=ZSetLocation(eset);
751  if (ret != ZERR_NONE) {
752    /* owl_function_makemsg("Error setting location: %s", error_message(ret)); */
753  }
754#endif
755}
756
757void owl_zephyr_zlog_out(void)
758{
759#ifdef HAVE_LIBZEPHYR
760  int ret;
761
762  ZResetAuthentication();
763  ret=ZUnsetLocation();
764  if (ret != ZERR_NONE) {
765    /* owl_function_makemsg("Error unsetting location: %s", error_message(ret)); */
766  }
767#endif
768}
769
770void owl_zephyr_addbuddy(char *name)
771{
772  char *filename;
773  FILE *file;
774 
775  filename=owl_sprintf("%s/.anyone", owl_global_get_homedir(&g));
776  file=fopen(filename, "a");
777  owl_free(filename);
778  if (!file) {
779    owl_function_error("Error opening zephyr buddy file for append");
780    return;
781  }
782  fprintf(file, "%s\n", name);
783  fclose(file);
784}
785
786void owl_zephyr_delbuddy(char *name)
787{
788  char *filename;
789
790  filename=owl_sprintf("%s/.anyone", owl_global_get_homedir(&g));
791  owl_util_file_deleteline(filename, name, 0);
792  owl_free(filename);
793}
794
795/* return auth string */
796#ifdef HAVE_LIBZEPHYR
797char *owl_zephyr_get_authstr(ZNotice_t *n)
798{
799
800  if (!n) return("UNKNOWN");
801
802  if (n->z_auth == ZAUTH_FAILED) {
803    return ("FAILED");
804  } else if (n->z_auth == ZAUTH_NO) {
805    return ("NO");
806  } else if (n->z_auth == ZAUTH_YES) {
807    return ("YES");
808  } else {
809    return ("UNKNOWN");
810  }           
811}
812#else
813char *owl_zephyr_get_authstr(void *n)
814{
815  return("");
816}
817#endif
818
819/* Returns a buffer of subscriptions or an error message.  Caller must
820 * free the return.
821 */
822char *owl_zephyr_getsubs()
823{
824#ifdef HAVE_LIBZEPHYR
825  int ret, num, i, one;
826  int buffsize;
827  ZSubscription_t sub;
828  char *out;
829  one=1;
830
831  ret=ZRetrieveSubscriptions(0, &num);
832  if (ret==ZERR_TOOMANYSUBS) {
833    return(owl_strdup("Zephyr: too many subscriptions\n"));
834  } else if (ret || (num <= 0)) {
835    return(owl_strdup("Zephyr: error retriving subscriptions\n"));
836  }
837
838  buffsize = (num + 1) * 50;
839  out=owl_malloc(buffsize);
840  strcpy(out, "");
841  for (i=0; i<num; i++) {
842    if ((ret = ZGetSubscriptions(&sub, &one)) != ZERR_NONE) {
843      owl_free(out);
844      ZFlushSubscriptions();
845      out=owl_strdup("Error while getting subscriptions\n");
846      return(out);
847    } else {
848      int tmpbufflen;
849      char *tmpbuff;
850      tmpbuff = owl_sprintf("<%s,%s,%s>\n%s", sub.zsub_class, sub.zsub_classinst, sub.zsub_recipient, out);
851      tmpbufflen = strlen(tmpbuff) + 1;
852      if (tmpbufflen > buffsize) {
853        char *out2;
854        buffsize = tmpbufflen * 2;
855        out2 = owl_realloc(out, buffsize);
856        if (out2 == NULL) {
857          owl_free(out);
858          owl_free(tmpbuff);
859          ZFlushSubscriptions();
860          out=owl_strdup("Realloc error while getting subscriptions\n");
861          return(out);   
862        }
863        out = out2;
864      }
865      strcpy(out, tmpbuff);
866      owl_free(tmpbuff);
867    }
868  }
869
870  ZFlushSubscriptions();
871  return(out);
872#else
873  return(owl_strdup("Zephyr not available"));
874#endif
875}
876
877char *owl_zephyr_get_variable(char *var)
878{
879#ifdef HAVE_LIBZEPHYR
880  return(ZGetVariable(var));
881#else
882  return("");
883#endif
884}
885
886void owl_zephyr_set_locationinfo(char *host, char *val)
887{
888#ifdef HAVE_LIBZEPHYR
889  ZInitLocationInfo(host, val);
890#endif
891}
892 
893/* Strip a local realm fron the zephyr user name.
894 * The caller must free the return
895 */
896char *short_zuser(char *in)
897{
898  char *out, *ptr;
899
900  out=owl_strdup(in);
901  ptr=strchr(out, '@');
902  if (ptr) {
903    if (!strcasecmp(ptr+1, owl_zephyr_get_realm())) {
904      *ptr='\0';
905    }
906  }
907  return(out);
908}
909
910/* Append a local realm to the zephyr user name if necessary.
911 * The caller must free the return.
912 */
913char *long_zuser(char *in)
914{
915  if (strchr(in, '@')) {
916    return(owl_strdup(in));
917  }
918  return(owl_sprintf("%s@%s", in, owl_zephyr_get_realm()));
919}
920
921/* strip out the instance from a zsender's principal.  Preserves the
922 * realm if present.  daemon.webzephyr is a special case.  The
923 * caller must free the return
924 */
925char *owl_zephyr_smartstripped_user(char *in)
926{
927  char *ptr, *realm, *out;
928
929  out=owl_strdup(in);
930
931  /* bail immeaditly if we don't have to do any work */
932  ptr=strchr(in, '.');
933  if (!strchr(in, '/') && !ptr) {
934    /* no '/' and no '.' */
935    return(out);
936  }
937  if (ptr && strchr(in, '@') && (ptr > strchr(in, '@'))) {
938    /* There's a '.' but it's in the realm */
939    return(out);
940  }
941  if (!strncasecmp(in, OWL_WEBZEPHYR_PRINCIPAL, strlen(OWL_WEBZEPHYR_PRINCIPAL))) {
942    return(out);
943  }
944
945  /* remove the realm from ptr, but hold on to it */
946  realm=strchr(out, '@');
947  if (realm) realm[0]='\0';
948
949  /* strip */
950  ptr=strchr(out, '.');
951  if (!ptr) ptr=strchr(out, '/');
952  ptr[0]='\0';
953
954  /* reattach the realm if we had one */
955  if (realm) {
956    strcat(out, "@");
957    strcat(out, realm+1);
958  }
959
960  return(out);
961}
962
963/* read the list of users in 'filename' as a .anyone file, and put the
964 * names of the zephyr users in the list 'in'.  If 'filename' is NULL,
965 * use the default .anyone file in the users home directory.  Returns
966 * -1 on failure, 0 on success.
967 */
968int owl_zephyr_get_anyone_list(owl_list *in, char *filename)
969{
970#ifdef HAVE_LIBZEPHYR
971  char *ourfile, *tmp, buff[LINE];
972  FILE *f;
973
974  if (filename==NULL) {
975    tmp=owl_global_get_homedir(&g);
976    ourfile=owl_sprintf("%s/.anyone", owl_global_get_homedir(&g));
977  } else {
978    ourfile=owl_strdup(filename);
979  }
980 
981  f=fopen(ourfile, "r");
982  if (!f) {
983    owl_function_error("Error opening file %s: %s", ourfile, strerror(errno) ? strerror(errno) : "");
984    owl_free(ourfile);
985    return(-1);
986  }
987
988  while (fgets(buff, LINE, f)!=NULL) {
989    /* ignore comments, blank lines etc. */
990    if (buff[0]=='#') continue;
991    if (buff[0]=='\n') continue;
992    if (buff[0]=='\0') continue;
993   
994    /* strip the \n */
995    buff[strlen(buff)-1]='\0';
996   
997    /* ingore from # on */
998    tmp=strchr(buff, '#');
999    if (tmp) tmp[0]='\0';
1000   
1001    /* ingore from SPC */
1002    tmp=strchr(buff, ' ');
1003    if (tmp) tmp[0]='\0';
1004   
1005    /* stick on the local realm. */
1006    if (!strchr(buff, '@')) {
1007      strcat(buff, "@");
1008      strcat(buff, ZGetRealm());
1009    }
1010    owl_list_append_element(in, owl_strdup(buff));
1011  }
1012  fclose(f);
1013  owl_free(ourfile);
1014  return(0);
1015#else
1016  return(-1);
1017#endif
1018}
1019
1020
1021#ifdef HAVE_LIBZEPHYR
1022void owl_zephyr_process_events(owl_dispatch *d) {
1023  int zpendcount=0;
1024  ZNotice_t notice;
1025  struct sockaddr_in from;
1026  owl_message *m=NULL;
1027
1028  while(owl_zephyr_zpending() && zpendcount < 20) {
1029    if (owl_zephyr_zpending()) {
1030      ZReceiveNotice(&notice, &from);
1031      zpendcount++;
1032
1033      /* is this an ack from a zephyr we sent? */
1034      if (owl_zephyr_notice_is_ack(&notice)) {
1035        owl_zephyr_handle_ack(&notice);
1036        continue;
1037      }
1038
1039      /* if it's a ping and we're not viewing pings then skip it */
1040      if (!owl_global_is_rxping(&g) && !strcasecmp(notice.z_opcode, "ping")) {
1041        continue;
1042      }
1043
1044      /* create the new message */
1045      m=owl_malloc(sizeof(owl_message));
1046      owl_message_create_from_znotice(m, &notice);
1047
1048      owl_global_messagequeue_addmsg(&g, m);
1049    }
1050  }
1051}
1052
1053#else
1054void owl_zephyr_process_events(owl_dispatch *d) {
1055 
1056}
1057#endif
Note: See TracBrowser for help on using the repository browser.