source: zephyr.c @ 247cbc9

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