source: zephyr.c @ 3b4ba7d

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