source: zephyr.c @ 823671c

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