source: zephyr.c @ d73e3af

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