source: zephyr.c @ 07bfbc2

barnowl_perlaimdebianrelease-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since 07bfbc2 was 5f3168a, checked in by Alejandro R. Sedeño <asedeno@mit.edu>, 14 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
Line 
1#include <stdlib.h>
2#include <unistd.h>
3#include <sys/types.h>
4#include <sys/wait.h>
5#include <sys/stat.h>
6#include <string.h>
7#include "owl.h"
8
9static const char fileIdent[] = "$Id$";
10
11#ifdef HAVE_LIBZEPHYR
12Code_t ZResetAuthentication();
13#endif
14
15int owl_zephyr_initialize()
16{
17#ifdef HAVE_LIBZEPHYR
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  }
28#endif
29  return(0);
30}
31
32int owl_zephyr_shutdown()
33{
34#ifdef HAVE_LIBZEPHYR
35  if(owl_global_is_havezephyr(&g)) {
36    unsuball();
37    ZClosePort();
38  }
39#endif
40  return(0);
41}
42
43int owl_zephyr_zpending()
44{
45#ifdef HAVE_LIBZEPHYR
46  if(owl_global_is_havezephyr(&g))
47    return(ZPending());
48  else
49    return 0;
50#else
51  return(0);
52#endif
53}
54
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
73#ifdef HAVE_LIBZEPHYR
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  }
89
90  return ret;
91}
92#endif
93
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.
101 */
102int owl_zephyr_loadsubs(char *filename, int error_on_nofile)
103{
104#ifdef HAVE_LIBZEPHYR
105  FILE *file;
106  char *tmp, *start;
107  char buffer[1024], subsfile[1024];
108  ZSubscription_t *subs;
109  int subSize = 1024;
110  int count, ret;
111  struct stat statbuff;
112
113  subs = owl_malloc(sizeof(ZSubscription_t) * subSize);
114  if (filename==NULL) {
115    sprintf(subsfile, "%s/%s", owl_global_get_homedir(&g), ".zephyr.subs");
116  } else {
117    strcpy(subsfile, filename);
118  }
119
120  ret=stat(subsfile, &statbuff);
121  if (ret) {
122    if (error_on_nofile==1) return(-1);
123    return(0);
124  }
125
126  ZResetAuthentication();
127  count=0;
128  file=fopen(subsfile, "r");
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;
137    }
138   
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;
154      }
155    }
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   
165    /* if it started with '-' then add it to the global punt list, and
166     * remove it from the list of subs. */
167    if (buffer[0]=='-') {
168      owl_function_zpunt(subs[count].zsub_class, subs[count].zsub_classinst, subs[count].zsub_recipient, 0);
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++;
175    }
176  }
177  fclose(file);
178
179  ret=owl_zephyr_loadsubs_helper(subs, count);
180  owl_free(subs);
181  return(ret);
182#else
183  return(0);
184#endif
185}
186
187int owl_zephyr_loaddefaultsubs()
188{
189#ifdef HAVE_LIBZEPHYR
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);
197#else
198  return(0);
199#endif
200}
201
202int owl_zephyr_loadloginsubs(char *filename)
203{
204#ifdef HAVE_LIBZEPHYR
205  FILE *file;
206  ZSubscription_t subs[3001];
207  char subsfile[1024], buffer[1024];
208  int count, ret, i;
209  struct stat statbuff;
210
211  if (filename==NULL) {
212    sprintf(subsfile, "%s/%s", owl_global_get_homedir(&g), ".anyone");
213  } else {
214    strcpy(subsfile, filename);
215  }
216 
217  ret=stat(subsfile, &statbuff);
218  if (ret) return(0);
219
220  ret=0;
221
222  ZResetAuthentication();
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 {
238        subs[count].zsub_classinst=owl_sprintf("%s@%s", buffer, ZGetRealm());
239      }
240
241      count++;
242    }
243    fclose(file);
244  } else {
245    count=0;
246    ret=-1;
247  }
248
249  /* sub with defaults */
250  if (ZSubscribeToSansDefaults(subs,count,0) != ZERR_NONE) {
251    owl_function_error("Error subscribing to zephyr notifications.");
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);
261#else
262  return(0);
263#endif
264}
265
266void unsuball()
267{
268#if HAVE_LIBZEPHYR
269  int ret;
270
271  ZResetAuthentication();
272  ret=ZCancelSubscriptions(0);
273  if (ret != ZERR_NONE) {
274    com_err("owl",ret,"while unsubscribing");
275  }
276#endif
277}
278
279int owl_zephyr_sub(char *class, char *inst, char *recip)
280{
281#ifdef HAVE_LIBZEPHYR
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
288  ZResetAuthentication();
289  if (ZSubscribeTo(subs,1,0) != ZERR_NONE) {
290    owl_function_error("Error subbing to <%s,%s,%s>", class, inst, recip);
291    return(-2);
292  }
293  return(0);
294#else
295  return(0);
296#endif
297}
298
299
300int owl_zephyr_unsub(char *class, char *inst, char *recip)
301{
302#ifdef HAVE_LIBZEPHYR
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
309  ZResetAuthentication();
310  if (ZUnsubscribeTo(subs,1,0) != ZERR_NONE) {
311    owl_function_error("Error unsubbing from <%s,%s,%s>", class, inst, recip);
312    return(-2);
313  }
314  return(0);
315#else
316  return(0);
317#endif
318}
319
320/* return a pointer to the data in the Jth field, (NULL terminated by
321 * definition).  Caller must free the return.
322 */
323#ifdef HAVE_LIBZEPHYR
324char *owl_zephyr_get_field(ZNotice_t *n, int j)
325{
326  int i, count, save;
327  char *out;
328
329  /* If there's no message here, just run along now */
330  if (n->z_message_len == 0)
331    return(owl_strdup(""));
332
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 */
339        return(owl_strdup(n->z_message+save));
340      } else {
341        save=i+1;
342      }
343    }
344  }
345  /* catch the last field, which might not be null terminated */
346  if (count==j-1) {
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);
351  }
352
353  return(owl_strdup(""));
354}
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 */
370        return(owl_validate_or_convert(n->z_message + save));
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) {
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;
385  }
386
387  return(owl_strdup(""));
388}
389#else
390char *owl_zephyr_get_field(void *n, int j)
391{
392  return(owl_strdup(""));
393}
394char *owl_zephyr_get_field_as_utf8(ZNotice_t *n, int j)
395{
396  return owl_zephyr_get_field(n, j);
397}
398#endif
399
400
401#ifdef HAVE_LIBZEPHYR
402int owl_zephyr_get_num_fields(ZNotice_t *n)
403{
404  int i, fields;
405
406  if(n->z_message_len == 0)
407    return 0;
408
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}
416#else
417int owl_zephyr_get_num_fields(void *n)
418{
419  return(0);
420}
421#endif
422
423#ifdef HAVE_LIBZEPHYR
424/* return a pointer to the message, place the message length in k
425 * caller must free the return
426 */
427char *owl_zephyr_get_message(ZNotice_t *n, owl_message *m)
428{
429  /* don't let ping messages have a body */
430  if (!strcasecmp(n->z_opcode, "ping")) {
431    return(owl_strdup(""));
432  }
433
434  /* deal with MIT NOC messages */
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;
437
438    field3 = owl_zephyr_get_field(n, 3);
439    field4 = owl_zephyr_get_field(n, 4);
440
441    msg = owl_sprintf("%s service on %s %s\n%s", n->z_opcode, n->z_class_inst, field3, field4);
442    owl_free(field3);
443    owl_free(field4);
444    if (msg) {
445      return msg;
446    }
447  }
448  /* deal with MIT Discuss messages */
449  else if (!strcasecmp(n->z_default_format, "New transaction [$1] entered in $2\nFrom: $3 ($5)\nSubject: $4")) {
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  }
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  }
480
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  }
487}
488#endif
489
490#ifdef HAVE_LIBZEPHYR
491char *owl_zephyr_get_zsig(ZNotice_t *n, int *k)
492{
493  /* return a pointer to the zsig if there is one */
494
495  /* message length 0? No zsig */
496  if (n->z_message_len==0) {
497    *k=0;
498    return("");
499  }
500
501  /* If there's only one field, no zsig */
502  if (owl_zephyr_get_num_fields(n) == 1) {
503    *k=0;
504    return("");
505  }
506
507  /* Everything else is field 1 */
508  *k=strlen(n->z_message);
509  return(n->z_message);
510}
511#else
512char *owl_zephyr_get_zsig(void *n, int *k)
513{
514  return("");
515}
516#endif
517
518int send_zephyr(char *opcode, char *zsig, char *class, char *instance, char *recipient, char *message)
519{
520#ifdef HAVE_LIBZEPHYR
521  int ret;
522  ZNotice_t notice;
523   
524  memset(&notice, 0, sizeof(notice));
525
526  ZResetAuthentication();
527
528  if (!zsig) zsig="";
529 
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";
540  if (*owl_global_get_zsender(&g))
541      notice.z_sender=owl_global_get_zsender(&g);
542  else
543      notice.z_sender=NULL;
544  if (opcode) notice.z_opcode=opcode;
545
546  notice.z_message_len=strlen(zsig)+1+strlen(message);
547  notice.z_message=owl_malloc(notice.z_message_len+10);
548  strcpy(notice.z_message, zsig);
549  memcpy(notice.z_message+strlen(zsig)+1, message, strlen(message));
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) {
558    owl_function_error("Error sending zephyr");
559    return(ret);
560  }
561  return(0);
562#else
563  return(0);
564#endif
565}
566
567#ifdef HAVE_LIBZEPHYR
568Code_t send_zephyr_helper(ZNotice_t *notice, char *buf, int len, int wait)
569{
570  return(ZSendPacket(buf, len, 0));
571}
572#endif
573
574void send_ping(char *to)
575{
576#ifdef HAVE_LIBZEPHYR
577  send_zephyr("PING", "", "MESSAGE", "PERSONAL", to, "");
578#endif
579}
580
581#ifdef HAVE_LIBZEPHYR
582void owl_zephyr_handle_ack(ZNotice_t *retnotice)
583{
584  char *tmp;
585 
586  /* if it's an HMACK ignore it */
587  if (retnotice->z_kind == HMACK) return;
588
589  if (retnotice->z_kind == SERVNAK) {
590    owl_function_error("Authorization failure sending zephyr");
591  } else if ((retnotice->z_kind != SERVACK) || !retnotice->z_message_len) {
592    owl_function_error("Detected server failure while receiving acknowledgement");
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")) {
598      tmp=short_zuser(retnotice->z_recipient);
599      owl_function_makemsg("Message sent to %s.", tmp);
600      free(tmp);
601    } else {
602      owl_function_makemsg("Message sent to -c %s -i %s\n", retnotice->z_class, retnotice->z_class_inst);
603    }
604  } else if (!strcmp(retnotice->z_message, ZSRVACK_NOTSENT)) {
605    #define BUFFLEN 1024
606    if (retnotice->z_recipient == NULL
607        || *retnotice->z_recipient == 0
608        || *retnotice->z_recipient == '@') {
609      char buff[BUFFLEN];
610      owl_function_error("No one subscribed to class %s", retnotice->z_class);
611      snprintf(buff, BUFFLEN, "Could not send message to class %s: no one subscribed.\n", retnotice->z_class);
612      owl_function_adminmsg("", buff);
613    } else {
614      char buff[BUFFLEN];
615      tmp = short_zuser(retnotice->z_recipient);
616      owl_function_error("%s: Not logged in or subscribing.", tmp);
617      if(strcmp(retnotice->z_class, "message")) {
618        snprintf(buff, BUFFLEN,
619                 "Could not send message to %s: "
620                 "not logged in or subscribing to class %s, instance %s.\n", 
621                 tmp,
622                 retnotice->z_class,
623                 retnotice->z_class_inst);
624      } else {
625        snprintf(buff, BUFFLEN,
626                 "Could not send message to %s: "
627                 "not logged in or subscribing to messages.\n",
628                 tmp);
629      }
630      owl_function_adminmsg("", buff);
631      owl_log_outgoing_zephyr_error(tmp, buff);
632      owl_free(tmp);
633    }
634  } else {
635    owl_function_error("Internal error on ack (%s)", retnotice->z_message);
636  }
637}
638#else
639void owl_zephyr_handle_ack(void *retnotice)
640{
641}
642#endif
643
644#ifdef HAVE_LIBZEPHYR
645int owl_zephyr_notice_is_ack(ZNotice_t *n)
646{
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}
653#else
654int owl_zephyr_notice_is_ack(void *n)
655{
656  return(0);
657}
658#endif
659 
660void owl_zephyr_zaway(owl_message *m)
661{
662#ifdef HAVE_LIBZEPHYR
663  char *tmpbuff, *myuser, *to;
664  owl_message *mout;
665 
666  /* bail if it doesn't look like a message we should reply to.  Some
667   * of this defined by the way zaway(1) works
668   */
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;
674  if (!strcasecmp(owl_message_get_zsig(m), "Automated reply:")) return;
675  if (!strcasecmp(owl_message_get_sender(m), ZGetSender())) return;
676  if (owl_message_get_attribute_value(m, "isauto")) return;
677
678  if (owl_global_is_smartstrip(&g)) {
679    to=owl_zephyr_smartstripped_user(owl_message_get_sender(m));
680  } else {
681    to=owl_strdup(owl_message_get_sender(m));
682  }
683
684  send_zephyr("",
685              "Automated reply:",
686              owl_message_get_class(m),
687              owl_message_get_instance(m),
688              to,
689              owl_global_get_zaway_msg(&g));
690
691  myuser=short_zuser(to);
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);
698  owl_free(to);
699
700  /* display the message as an admin message in the receive window */
701  mout=owl_function_make_outgoing_zephyr(owl_global_get_zaway_msg(&g), tmpbuff, "Automated reply:");
702  owl_global_messagequeue_addmsg(&g, mout);
703  owl_free(tmpbuff);
704#endif
705}
706
707#ifdef HAVE_LIBZEPHYR
708void owl_zephyr_hackaway_cr(ZNotice_t *n)
709{
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}
719#endif
720
721void owl_zephyr_zlocate(char *user, char *out, int auth)
722{
723#ifdef HAVE_LIBZEPHYR
724  int ret, numlocs;
725  int one = 1;
726  ZLocations_t locations;
727  char *myuser;
728 
729  strcpy(out, "");
730  ZResetAuthentication();
731  ret=ZLocateUser(user,&numlocs,auth?ZAUTH:ZNOAUTH);
732  if (ret != ZERR_NONE) {
733    sprintf(out, "Error locating user %s\n", user);
734    return;
735  }
736
737  if (numlocs==0) {
738    myuser=short_zuser(user);
739    sprintf(out, "%s: Hidden or not logged-in\n", myuser);
740    owl_free(myuser);
741    return;
742  }
743   
744  for (;numlocs;numlocs--) {
745    ZGetLocations(&locations,&one);
746    myuser=short_zuser(user);
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 : "?");
751    owl_free(myuser);
752  }
753#endif
754}
755
756void owl_zephyr_addsub(char *filename, char *class, char *inst, char *recip)
757{
758#ifdef HAVE_LIBZEPHYR
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
770  /* if the file already exists, check to see if the sub is already there */
771  file=fopen(subsfile, "r");
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);
777        fclose(file);
778        return;
779      }
780    }
781    fclose(file);
782  }
783
784  /* if we get here then we didn't find it */
785  file=fopen(subsfile, "a");
786  if (!file) {
787    owl_function_error("Error opening file %s for writing", subsfile);
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);
796#endif
797}
798
799void owl_zephyr_delsub(char *filename, char *class, char *inst, char *recip)
800{
801#ifdef HAVE_LIBZEPHYR
802  char *line, *subsfile;
803 
804  line=owl_zephyr_makesubline(class, inst, recip);
805  line[strlen(line)-1]='\0';
806
807  if (!filename) {
808    subsfile=owl_sprintf("%s/.zephyr.subs", owl_global_get_homedir(&g));
809  } else {
810    subsfile=owl_strdup(filename);
811  }
812 
813  owl_util_file_deleteline(subsfile, line, 1);
814  owl_free(subsfile);
815  owl_free(line);
816  owl_function_makemsg("Subscription removed");
817#endif
818}
819
820/* caller must free the return */
821char *owl_zephyr_makesubline(char *class, char *inst, char *recip)
822{
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}
829
830
831void owl_zephyr_zlog_in(void)
832{
833#ifdef HAVE_LIBZEPHYR
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  }
865#endif
866}
867
868void owl_zephyr_zlog_out(void)
869{
870#ifdef HAVE_LIBZEPHYR
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  }
882#endif
883}
884
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) {
894    owl_function_error("Error opening zephyr buddy file for append");
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}
909
910/* return auth string */
911#ifdef HAVE_LIBZEPHYR
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}
927#else
928char *owl_zephyr_get_authstr(void *n)
929{
930  return("");
931}
932#endif
933
934/* Returns a buffer of subscriptions or an error message.  Caller must
935 * free the return.
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) {
947    return(owl_strdup("Zephyr: too many subscriptions\n"));
948  } else if (ret) {
949    return(owl_strdup("Zephyr: error retriving subscriptions\n"));
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 {
963      sprintf(tmpbuff, "<%s,%s,%s>\n%s", sub.zsub_class, sub.zsub_classinst, sub.zsub_recipient, out);
964      strcpy(out, tmpbuff);
965    }
966  }
967
968  owl_free(tmpbuff);
969  ZFlushSubscriptions();
970  return(out);
971#else
972  return(owl_strdup("Zephyr not available"));
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 
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{
1014  if (strchr(in, '@')) {
1015    return(owl_strdup(in));
1016  }
1017  return(owl_sprintf("%s@%s", in, owl_zephyr_get_realm()));
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}
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}
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.