source: zephyr.c @ 6e6ded7

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