source: zephyr.c @ 3cc1582

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