source: zephyr.c @ b72a352

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