source: zephyr.c @ 21882032

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