source: zephyr.c @ f8d9df1

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