source: zephyr.c @ 3baf77f

barnowl_perlaimdebianrelease-1.10release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since 3baf77f was 60c2e1e, checked in by Nelson Elhage <nelhage@mit.edu>, 17 years ago
Nuke a warning
  • Property mode set to 100644
File size: 25.8 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
[50e29e3]369  if(n->z_message_len == 0)
370    return 0;
371
[7d4fbcd]372  fields=1;
373  for (i=0; i<n->z_message_len; i++) {
374    if (n->z_message[i]=='\0') fields++;
375  }
376 
377  return(fields);
378}
[09489b89]379#else
380int owl_zephyr_get_num_fields(void *n)
381{
382  return(0);
383}
[be0a79f]384#endif
[7d4fbcd]385
[be0a79f]386#ifdef HAVE_LIBZEPHYR
[bf73bdd]387/* return a pointer to the message, place the message length in k
388 * caller must free the return
389 */
[85d1795]390char *owl_zephyr_get_message(ZNotice_t *n, owl_message *m)
[31e48a3]391{
[405d5e6]392  /* don't let ping messages have a body */
[7d4fbcd]393  if (!strcasecmp(n->z_opcode, "ping")) {
[bf73bdd]394    return(owl_strdup(""));
[7d4fbcd]395  }
396
[a1bb198]397  /* deal with MIT NOC messages */
[85d1795]398  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")) {
399    char *msg, *field3, *field4;
[a1bb198]400
401    field3 = owl_zephyr_get_field(n, 3);
402    field4 = owl_zephyr_get_field(n, 4);
403
[85d1795]404    msg = owl_sprintf("%s service on %s %s\n%s", n->z_opcode, n->z_class_inst, field3, field4);
[272a4a0]405    owl_free(field3);
406    owl_free(field4);
[a1bb198]407    if (msg) {
408      return msg;
409    }
410  }
[fba0f96]411  /* deal with MIT Discuss messages */
[85d1795]412  else if (!strcasecmp(n->z_default_format, "New transaction [$1] entered in $2\nFrom: $3 ($5)\nSubject: $4")) {
[fba0f96]413    char *msg, *field1, *field2, *field3, *field4, *field5;
414   
415    field1 = owl_zephyr_get_field(n, 1);
416    field2 = owl_zephyr_get_field(n, 2);
417    field3 = owl_zephyr_get_field(n, 3);
418    field4 = owl_zephyr_get_field(n, 4);
419    field5 = owl_zephyr_get_field(n, 5);
420   
421    msg = owl_sprintf("New transaction [%s] entered in %s\nFrom: %s (%s)\nSubject: %s", field1, field2, field3, field5, field4);
422    owl_free(field1);
423    owl_free(field2);
424    owl_free(field3);
425    owl_free(field4);
426    owl_free(field5);
427    if (msg) {
428      return msg;
429    }
430  }
[85d1795]431  /* deal with MIT Moira messages */
432  else if (!strcasecmp(n->z_default_format, "MOIRA $instance on $fromhost:\n $message\n")) {
433    char *msg, *field1;
434   
435    field1 = owl_zephyr_get_field(n, 1);
436   
437    msg = owl_sprintf("MOIRA %s on %s: %s", n->z_class_inst, owl_message_get_hostname(m), field1);
438    owl_free(field1);
439    if (msg) {
440      return msg;
441    }
442  }
[405d5e6]443
[85d1795]444  if (owl_zephyr_get_num_fields(n) == 1) {
445    return(owl_zephyr_get_field(n, 1));
446  }
447  else {
448    return(owl_zephyr_get_field(n, 2));
449  }
[7d4fbcd]450}
[be0a79f]451#endif
[7d4fbcd]452
[be0a79f]453#ifdef HAVE_LIBZEPHYR
[31e48a3]454char *owl_zephyr_get_zsig(ZNotice_t *n, int *k)
455{
[7d4fbcd]456  /* return a pointer to the zsig if there is one */
457
[405d5e6]458  /* message length 0? No zsig */
[7d4fbcd]459  if (n->z_message_len==0) {
460    *k=0;
461    return("");
462  }
[405d5e6]463
[85d1795]464  /* If there's only one field, no zsig */
465  if (owl_zephyr_get_num_fields(n) == 1) {
466    *k=0;
[405d5e6]467    return("");
468  }
469
470  /* Everything else is field 1 */
[7d4fbcd]471  *k=strlen(n->z_message);
472  return(n->z_message);
473}
[09489b89]474#else
475char *owl_zephyr_get_zsig(void *n, int *k)
476{
477  return("");
478}
[be0a79f]479#endif
[7d4fbcd]480
[31e48a3]481int send_zephyr(char *opcode, char *zsig, char *class, char *instance, char *recipient, char *message)
482{
[be0a79f]483#ifdef HAVE_LIBZEPHYR
[7d4fbcd]484  int ret;
485  ZNotice_t notice;
486   
487  memset(&notice, 0, sizeof(notice));
488
[8262340]489  ZResetAuthentication();
[8ba37ec]490
491  if (!zsig) zsig="";
[8262340]492 
[7d4fbcd]493  notice.z_kind=ACKED;
494  notice.z_port=0;
495  notice.z_class=class;
496  notice.z_class_inst=instance;
497  if (!strcmp(recipient, "*") || !strcmp(recipient, "@")) {
498    notice.z_recipient="";
499  } else {
500    notice.z_recipient=recipient;
501  }
502  notice.z_default_format="Class $class, Instance $instance:\nTo: @bold($recipient) at $time $date\nFrom: @bold{$1 <$sender>}\n\n$2";
503  notice.z_sender=NULL;
504  if (opcode) notice.z_opcode=opcode;
505
[56330ff]506  notice.z_message_len=strlen(zsig)+1+strlen(message);
[7d4fbcd]507  notice.z_message=owl_malloc(notice.z_message_len+10);
[56330ff]508  strcpy(notice.z_message, zsig);
509  memcpy(notice.z_message+strlen(zsig)+1, message, strlen(message));
[7d4fbcd]510
511  /* ret=ZSendNotice(&notice, ZAUTH); */
512  ret=ZSrvSendNotice(&notice, ZAUTH, send_zephyr_helper);
513 
514  /* free then check the return */
515  owl_free(notice.z_message);
516  ZFreeNotice(&notice);
517  if (ret!=ZERR_NONE) {
[ec6ff52]518    owl_function_error("Error sending zephyr");
[7d4fbcd]519    return(ret);
520  }
521  return(0);
[be0a79f]522#else
523  return(0);
524#endif
[7d4fbcd]525}
526
[be0a79f]527#ifdef HAVE_LIBZEPHYR
[31e48a3]528Code_t send_zephyr_helper(ZNotice_t *notice, char *buf, int len, int wait)
529{
[7d4fbcd]530  return(ZSendPacket(buf, len, 0));
531}
[be0a79f]532#endif
[7d4fbcd]533
[31e48a3]534void send_ping(char *to)
535{
[be0a79f]536#ifdef HAVE_LIBZEPHYR
[7d4fbcd]537  send_zephyr("PING", "", "MESSAGE", "PERSONAL", to, "");
[be0a79f]538#endif
[7d4fbcd]539}
540
[be0a79f]541#ifdef HAVE_LIBZEPHYR
[31e48a3]542void owl_zephyr_handle_ack(ZNotice_t *retnotice)
543{
[7d4fbcd]544  char *tmp;
545 
546  /* if it's an HMACK ignore it */
547  if (retnotice->z_kind == HMACK) return;
[aecf3e6]548
[7d4fbcd]549  if (retnotice->z_kind == SERVNAK) {
[ec6ff52]550    owl_function_error("Authorization failure sending zephyr");
[7d4fbcd]551  } else if ((retnotice->z_kind != SERVACK) || !retnotice->z_message_len) {
[ec6ff52]552    owl_function_error("Detected server failure while receiving acknowledgement");
[7d4fbcd]553  } else if (!strcmp(retnotice->z_message, ZSRVACK_SENT)) {
554    if (!strcasecmp(retnotice->z_opcode, "ping")) {
555      return;
556    } else if (!strcasecmp(retnotice->z_class, "message") &&
557               !strcasecmp(retnotice->z_class_inst, "personal")) {
[4b464a4]558      tmp=short_zuser(retnotice->z_recipient);
[b4db911]559      owl_function_makemsg("Message sent to %s.", tmp);
[7d4fbcd]560      free(tmp);
561    } else {
[b4db911]562      owl_function_makemsg("Message sent to -c %s -i %s\n", retnotice->z_class, retnotice->z_class_inst);
[7d4fbcd]563    }
564  } else if (!strcmp(retnotice->z_message, ZSRVACK_NOTSENT)) {
[1151f0b]565    #define BUFFLEN 1024
[44f32fb]566    if (retnotice->z_recipient == NULL
[60c2e1e]567        || *retnotice->z_recipient == 0
[44f32fb]568        || *retnotice->z_recipient == '@') {
[1151f0b]569      char buff[BUFFLEN];
[44f32fb]570      owl_function_error("No one subscribed to class %s", retnotice->z_class);
[1151f0b]571      snprintf(buff, BUFFLEN, "Could not send message to class %s: no one subscribed.\n", retnotice->z_class);
[9119a47]572      owl_function_adminmsg("", buff);
[7d4fbcd]573    } else {
[1151f0b]574      char buff[BUFFLEN];
[4b464a4]575      tmp = short_zuser(retnotice->z_recipient);
[44f32fb]576      owl_function_error("%s: Not logged in or subscribing.", tmp);
[1151f0b]577      snprintf(buff, BUFFLEN, "Could not send message to %s: not logged in or subscribing to", tmp);
[44f32fb]578      if(strcmp(retnotice->z_class, "message")) {
[1151f0b]579        snprintf(buff, BUFFLEN,
580                 "%s class %s, instance %s.\n", buff,
581                 retnotice->z_class,
582                 retnotice->z_class_inst);
[44f32fb]583      } else {
[1151f0b]584        snprintf(buff, BUFFLEN,
585                 "%s messages.\n", buff);
[44f32fb]586      }
[9119a47]587      owl_function_adminmsg("", buff);
[180cd15]588      owl_log_outgoing_zephyr_error(tmp, buff);
[1c6c4d3]589      owl_free(tmp);
[7d4fbcd]590    }
591  } else {
[ec6ff52]592    owl_function_error("Internal error on ack (%s)", retnotice->z_message);
[7d4fbcd]593  }
594}
[09489b89]595#else
596void owl_zephyr_handle_ack(void *retnotice)
597{
598}
[be0a79f]599#endif
[7d4fbcd]600
[be0a79f]601#ifdef HAVE_LIBZEPHYR
[31e48a3]602int owl_zephyr_notice_is_ack(ZNotice_t *n)
603{
[7d4fbcd]604  if (n->z_kind == SERVNAK || n->z_kind == SERVACK || n->z_kind == HMACK) {
605    if (!strcasecmp(n->z_class, LOGIN_CLASS)) return(0);
606    return(1);
607  }
608  return(0);
609}
[09489b89]610#else
611int owl_zephyr_notice_is_ack(void *n)
612{
613  return(0);
614}
[be0a79f]615#endif
[7d4fbcd]616 
[31e48a3]617void owl_zephyr_zaway(owl_message *m)
618{
[be0a79f]619#ifdef HAVE_LIBZEPHYR
[7c8060d0]620  char *tmpbuff, *myuser, *to;
[15b34fd]621  owl_message *mout;
[7d4fbcd]622 
[aa2f6364]623  /* bail if it doesn't look like a message we should reply to.  Some
[2de4f20]624   * of this defined by the way zaway(1) works
625   */
[7d4fbcd]626  if (strcasecmp(owl_message_get_class(m), "message")) return;
627  if (strcasecmp(owl_message_get_recipient(m), ZGetSender())) return;
628  if (!strcasecmp(owl_message_get_sender(m), "")) return;
629  if (!strcasecmp(owl_message_get_opcode(m), "ping")) return;
630  if (!strcasecmp(owl_message_get_opcode(m), "auto")) return;
[d023c25]631  if (!strcasecmp(owl_message_get_zsig(m), "Automated reply:")) return;
[7d4fbcd]632  if (!strcasecmp(owl_message_get_sender(m), ZGetSender())) return;
[9854278]633  if (owl_message_get_attribute_value(m, "isauto")) return;
[7d4fbcd]634
[7c8060d0]635  if (owl_global_is_smartstrip(&g)) {
[e3d9c77]636    to=owl_zephyr_smartstripped_user(owl_message_get_sender(m));
[7c8060d0]637  } else {
638    to=owl_strdup(owl_message_get_sender(m));
639  }
640
[7d4fbcd]641  send_zephyr("",
642              "Automated reply:",
643              owl_message_get_class(m),
644              owl_message_get_instance(m),
[7c8060d0]645              to,
[7d4fbcd]646              owl_global_get_zaway_msg(&g));
647
[7c8060d0]648  myuser=short_zuser(to);
[aa2f6364]649  if (!strcasecmp(owl_message_get_instance(m), "personal")) {
650    tmpbuff = owl_sprintf("zwrite %s", myuser);
651  } else {
652    tmpbuff = owl_sprintf("zwrite -i %s %s", owl_message_get_instance(m), myuser);
653  }
654  owl_free(myuser);
[7c8060d0]655  owl_free(to);
[aa2f6364]656
[7d4fbcd]657  /* display the message as an admin message in the receive window */
[15b34fd]658  mout=owl_function_make_outgoing_zephyr(owl_global_get_zaway_msg(&g), tmpbuff, "Automated reply:");
[13a3c1db]659  owl_global_messagequeue_addmsg(&g, mout);
[7d4fbcd]660  owl_free(tmpbuff);
[be0a79f]661#endif
[7d4fbcd]662}
663
[be0a79f]664#ifdef HAVE_LIBZEPHYR
[31e48a3]665void owl_zephyr_hackaway_cr(ZNotice_t *n)
666{
[7d4fbcd]667  /* replace \r's with ' '.  Gross-ish */
668  int i;
669
670  for (i=0; i<n->z_message_len; i++) {
671    if (n->z_message[i]=='\r') {
672      n->z_message[i]=' ';
673    }
674  }
675}
[be0a79f]676#endif
[7d4fbcd]677
[31e48a3]678void owl_zephyr_zlocate(char *user, char *out, int auth)
679{
[be0a79f]680#ifdef HAVE_LIBZEPHYR
[7d4fbcd]681  int ret, numlocs;
682  int one = 1;
683  ZLocations_t locations;
684  char *myuser;
685 
686  strcpy(out, "");
[8262340]687  ZResetAuthentication();
[7d4fbcd]688  ret=ZLocateUser(user,&numlocs,auth?ZAUTH:ZNOAUTH);
689  if (ret != ZERR_NONE) {
[667a1b6]690    sprintf(out, "Error locating user %s\n", user);
691    return;
[7d4fbcd]692  }
693
694  if (numlocs==0) {
[2527615]695    myuser=short_zuser(user);
696    sprintf(out, "%s: Hidden or not logged-in\n", myuser);
697    owl_free(myuser);
[7d4fbcd]698    return;
699  }
700   
701  for (;numlocs;numlocs--) {
702    ZGetLocations(&locations,&one);
[4b464a4]703    myuser=short_zuser(user);
[667a1b6]704    sprintf(out, "%s%s: %s\t%s\t%s\n", out, myuser,
705            locations.host ? locations.host : "?",
706            locations.tty ? locations.tty : "?",
707            locations.time ? locations.time : "?");
[7d4fbcd]708    owl_free(myuser);
709  }
[be0a79f]710#endif
[7d4fbcd]711}
[bde7714]712
[31e48a3]713void owl_zephyr_addsub(char *filename, char *class, char *inst, char *recip)
714{
[be0a79f]715#ifdef HAVE_LIBZEPHYR
[bde7714]716  char *line, subsfile[LINE], buff[LINE];
717  FILE *file;
718
719  line=owl_zephyr_makesubline(class, inst, recip);
720
721  if (filename==NULL) {
722    sprintf(subsfile, "%s/%s", owl_global_get_homedir(&g), ".zephyr.subs");
723  } else {
724    strcpy(subsfile, filename);
725  }
726
[74037d9]727  /* if the file already exists, check to see if the sub is already there */
[bde7714]728  file=fopen(subsfile, "r");
[74037d9]729  if (file) {
730    while (fgets(buff, LINE, file)!=NULL) {
731      if (!strcasecmp(buff, line)) {
732        owl_function_error("Subscription already present in %s", subsfile);
733        owl_free(line);
[e78397d]734        fclose(file);
[74037d9]735        return;
736      }
[bde7714]737    }
[99dabee]738    fclose(file);
[bde7714]739  }
740
741  /* if we get here then we didn't find it */
742  file=fopen(subsfile, "a");
743  if (!file) {
[ec6ff52]744    owl_function_error("Error opening file %s for writing", subsfile);
[bde7714]745    owl_free(line);
746    return;
747  }
748  fputs(line, file);
749  fclose(file);
750  owl_function_makemsg("Subscription added");
751 
752  owl_free(line);
[be0a79f]753#endif
[bde7714]754}
755
[31e48a3]756void owl_zephyr_delsub(char *filename, char *class, char *inst, char *recip)
757{
[be0a79f]758#ifdef HAVE_LIBZEPHYR
[b2a91b6]759  char *line, *subsfile;
[bde7714]760 
761  line=owl_zephyr_makesubline(class, inst, recip);
[b2a91b6]762  line[strlen(line)-1]='\0';
[bde7714]763
[b2a91b6]764  if (!filename) {
765    subsfile=owl_sprintf("%s/.zephyr.subs", owl_global_get_homedir(&g));
[bde7714]766  } else {
[b2a91b6]767    subsfile=owl_strdup(filename);
[bde7714]768  }
[b2a91b6]769 
770  owl_util_file_deleteline(subsfile, line, 1);
771  owl_free(subsfile);
[bde7714]772  owl_free(line);
773  owl_function_makemsg("Subscription removed");
[be0a79f]774#endif
[bde7714]775}
776
[b2a91b6]777/* caller must free the return */
[31e48a3]778char *owl_zephyr_makesubline(char *class, char *inst, char *recip)
779{
[bde7714]780  char *out;
781
782  out=owl_malloc(strlen(class)+strlen(inst)+strlen(recip)+30);
783  sprintf(out, "%s,%s,%s\n", class, inst, !strcmp(recip, "") ? "*" : recip);
784  return(out);
785}
[31e48a3]786
787
788void owl_zephyr_zlog_in(void)
789{
[be0a79f]790#ifdef HAVE_LIBZEPHYR
[31e48a3]791  char *exposure, *eset;
792  int ret;
793
794  ZResetAuthentication();
795   
796  eset=EXPOSE_REALMVIS;
797  exposure=ZGetVariable("exposure");
798  if (exposure==NULL) {
799    eset=EXPOSE_REALMVIS;
800  } else if (!strcasecmp(exposure,EXPOSE_NONE)) {
801    eset = EXPOSE_NONE;
802  } else if (!strcasecmp(exposure,EXPOSE_OPSTAFF)) {
803    eset = EXPOSE_OPSTAFF;
804  } else if (!strcasecmp(exposure,EXPOSE_REALMVIS)) {
805    eset = EXPOSE_REALMVIS;
806  } else if (!strcasecmp(exposure,EXPOSE_REALMANN)) {
807    eset = EXPOSE_REALMANN;
808  } else if (!strcasecmp(exposure,EXPOSE_NETVIS)) {
809    eset = EXPOSE_NETVIS;
810  } else if (!strcasecmp(exposure,EXPOSE_NETANN)) {
811    eset = EXPOSE_NETANN;
812  }
813   
814  ret=ZSetLocation(eset);
815  if (ret != ZERR_NONE) {
816    /*
817      char buff[LINE];
818      sprintf(buff, "Error setting location: %s", error_message(ret));
819      owl_function_makemsg(buff);
820    */
821  }
[be0a79f]822#endif
[31e48a3]823}
824
825void owl_zephyr_zlog_out(void)
826{
[be0a79f]827#ifdef HAVE_LIBZEPHYR
[31e48a3]828  int ret;
829
830  ZResetAuthentication();
831  ret=ZUnsetLocation();
832  if (ret != ZERR_NONE) {
833    /*
834      char buff[LINE];
835      sprintf(buff, "Error unsetting location: %s", error_message(ret));
836      owl_function_makemsg(buff);
837    */
838  }
[be0a79f]839#endif
[31e48a3]840}
841
[65ad073]842void owl_zephyr_addbuddy(char *name)
843{
844  char *filename;
845  FILE *file;
846 
847  filename=owl_sprintf("%s/.anyone", owl_global_get_homedir(&g));
848  file=fopen(filename, "a");
849  owl_free(filename);
850  if (!file) {
[ec6ff52]851    owl_function_error("Error opening zephyr buddy file for append");
[65ad073]852    return;
853  }
854  fprintf(file, "%s\n", name);
855  fclose(file);
856}
857
858void owl_zephyr_delbuddy(char *name)
859{
860  char *filename;
861
862  filename=owl_sprintf("%s/.anyone", owl_global_get_homedir(&g));
863  owl_util_file_deleteline(filename, name, 0);
864  owl_free(filename);
865}
[9381782]866
867/* return auth string */
[09489b89]868#ifdef HAVE_LIBZEPHYR
[9381782]869char *owl_zephyr_get_authstr(ZNotice_t *n)
870{
871
872  if (!n) return("UNKNOWN");
873
874  if (n->z_auth == ZAUTH_FAILED) {
875    return ("FAILED");
876  } else if (n->z_auth == ZAUTH_NO) {
877    return ("NO");
878  } else if (n->z_auth == ZAUTH_YES) {
879    return ("YES");
880  } else {
881    return ("UNKNOWN");
882  }           
883}
[09489b89]884#else
885char *owl_zephyr_get_authstr(void *n)
886{
887  return("");
888}
889#endif
[9381782]890
[2de4f20]891/* Returns a buffer of subscriptions or an error message.  Caller must
892 * free the return.
[09489b89]893 */
894char *owl_zephyr_getsubs()
895{
896#ifdef HAVE_LIBZEPHYR
897  int ret, num, i, one;
898  ZSubscription_t sub;
899  char *out, *tmpbuff;
900  one=1;
901
902  ret=ZRetrieveSubscriptions(0, &num);
903  if (ret==ZERR_TOOMANYSUBS) {
[c8735aa]904    return(owl_strdup("Zephyr: too many subscriptions\n"));
905  } else if (ret) {
906    return(owl_strdup("Zephyr: error retriving subscriptions\n"));
[09489b89]907  }
908
909  out=owl_malloc(num*500);
910  tmpbuff=owl_malloc(num*500);
911  strcpy(out, "");
912  for (i=0; i<num; i++) {
913    if ((ret = ZGetSubscriptions(&sub, &one)) != ZERR_NONE) {
914      owl_free(out);
915      owl_free(tmpbuff);
916      ZFlushSubscriptions();
917      out=owl_strdup("Error while getting subscriptions\n");
918      return(out);
919    } else {
[03955f3]920      sprintf(tmpbuff, "<%s,%s,%s>\n%s", sub.zsub_class, sub.zsub_classinst, sub.zsub_recipient, out);
[09489b89]921      strcpy(out, tmpbuff);
922    }
923  }
924
925  owl_free(tmpbuff);
926  ZFlushSubscriptions();
927  return(out);
928#else
[12c35df]929  return(owl_strdup("Zephyr not available"));
[09489b89]930#endif
931}
932
933char *owl_zephyr_get_variable(char *var)
934{
935#ifdef HAVE_LIBZEPHYR
936  return(ZGetVariable(var));
937#else
938  return("");
939#endif
940}
941
942void owl_zephyr_set_locationinfo(char *host, char *val)
943{
944#ifdef HAVE_LIBZEPHYR
945  ZInitLocationInfo(host, val);
946#endif
947}
948 
[e3d9c77]949/* Strip a local realm fron the zephyr user name.
950 * The caller must free the return
951 */
952char *short_zuser(char *in)
953{
954  char *out, *ptr;
955
956  out=owl_strdup(in);
957  ptr=strchr(out, '@');
958  if (ptr) {
959    if (!strcasecmp(ptr+1, owl_zephyr_get_realm())) {
960      *ptr='\0';
961    }
962  }
963  return(out);
964}
965
966/* Append a local realm to the zephyr user name if necessary.
967 * The caller must free the return.
968 */
969char *long_zuser(char *in)
970{
[1971b59]971  if (strchr(in, '@')) {
972    return(owl_strdup(in));
[e3d9c77]973  }
[1971b59]974  return(owl_sprintf("%s@%s", in, owl_zephyr_get_realm()));
[e3d9c77]975}
976
977/* strip out the instance from a zsender's principal.  Preserves the
978 * realm if present.  daemon.webzephyr is a special case.  The
979 * caller must free the return
980 */
981char *owl_zephyr_smartstripped_user(char *in)
982{
983  char *ptr, *realm, *out;
984
985  out=owl_strdup(in);
986
987  /* bail immeaditly if we don't have to do any work */
988  ptr=strchr(in, '.');
989  if (!strchr(in, '/') && !ptr) {
990    /* no '/' and no '.' */
991    return(out);
992  }
993  if (ptr && strchr(in, '@') && (ptr > strchr(in, '@'))) {
994    /* There's a '.' but it's in the realm */
995    return(out);
996  }
997  if (!strncasecmp(in, OWL_WEBZEPHYR_PRINCIPAL, strlen(OWL_WEBZEPHYR_PRINCIPAL))) {
998    return(out);
999  }
1000
1001  /* remove the realm from ptr, but hold on to it */
1002  realm=strchr(out, '@');
1003  if (realm) realm[0]='\0';
1004
1005  /* strip */
1006  ptr=strchr(out, '.');
1007  if (!ptr) ptr=strchr(out, '/');
1008  ptr[0]='\0';
1009
1010  /* reattach the realm if we had one */
1011  if (realm) {
1012    strcat(out, "@");
1013    strcat(out, realm+1);
1014  }
1015
1016  return(out);
1017}
[5a95b69]1018
1019/* read the list of users in 'filename' as a .anyone file, and put the
1020 * names of the zephyr users in the list 'in'.  If 'filename' is NULL,
1021 * use the default .anyone file in the users home directory.  Returns
1022 * -1 on failure, 0 on success.
1023 */
1024int owl_zephyr_get_anyone_list(owl_list *in, char *filename)
1025{
1026#ifdef HAVE_LIBZEPHYR
1027  char *ourfile, *tmp, buff[LINE];
1028  FILE *f;
1029
1030  if (filename==NULL) {
1031    tmp=owl_global_get_homedir(&g);
1032    ourfile=owl_sprintf("%s/.anyone", owl_global_get_homedir(&g));
1033  } else {
1034    ourfile=owl_strdup(filename);
1035  }
1036 
1037  f=fopen(ourfile, "r");
1038  if (!f) {
1039    owl_function_error("Error opening file %s: %s", ourfile, strerror(errno) ? strerror(errno) : "");
1040    owl_free(ourfile);
1041    return(-1);
1042  }
1043
1044  while (fgets(buff, LINE, f)!=NULL) {
1045    /* ignore comments, blank lines etc. */
1046    if (buff[0]=='#') continue;
1047    if (buff[0]=='\n') continue;
1048    if (buff[0]=='\0') continue;
1049   
1050    /* strip the \n */
1051    buff[strlen(buff)-1]='\0';
1052   
1053    /* ingore from # on */
1054    tmp=strchr(buff, '#');
1055    if (tmp) tmp[0]='\0';
1056   
1057    /* ingore from SPC */
1058    tmp=strchr(buff, ' ');
1059    if (tmp) tmp[0]='\0';
1060   
1061    /* stick on the local realm. */
1062    if (!strchr(buff, '@')) {
1063      strcat(buff, "@");
1064      strcat(buff, ZGetRealm());
1065    }
1066    owl_list_append_element(in, owl_strdup(buff));
1067  }
1068  fclose(f);
1069  owl_free(ourfile);
1070  return(0);
1071#else
1072  return(-1);
1073#endif
1074}
[13a3c1db]1075
1076#ifdef HAVE_LIBZEPHYR
1077void owl_zephyr_process_events() {
1078  int zpendcount=0;
1079  ZNotice_t notice;
1080  struct sockaddr_in from;
1081  owl_message *m=NULL;
1082
1083  while(owl_zephyr_zpending() && zpendcount < 20) {
1084    if (owl_zephyr_zpending()) {
1085      ZReceiveNotice(&notice, &from);
1086      zpendcount++;
1087
1088      /* is this an ack from a zephyr we sent? */
1089      if (owl_zephyr_notice_is_ack(&notice)) {
1090        owl_zephyr_handle_ack(&notice);
1091        continue;
1092      }
1093
1094      /* if it's a ping and we're not viewing pings then skip it */
1095      if (!owl_global_is_rxping(&g) && !strcasecmp(notice.z_opcode, "ping")) {
1096        continue;
1097      }
1098
1099      /* create the new message */
1100      m=owl_malloc(sizeof(owl_message));
1101      owl_message_create_from_znotice(m, &notice);
1102
1103      owl_global_messagequeue_addmsg(&g, m);
1104    }
1105  }
1106}
1107
1108#else
1109void owl_zephyr_process_events() {
1110 
1111}
1112#endif
Note: See TracBrowser for help on using the repository browser.