source: zephyr.c @ 128171a

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