source: zephyr.c @ 9729dba

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