source: zephyr.c @ e01449c

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