source: zephyr.c @ d21efbc

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