source: zephyr.c @ d1b1cf6

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