source: zephyr.c @ ae47efb

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