source: zephyr.c @ b7ee89b

release-1.10release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since b7ee89b was b7ee89b, checked in by Karl Ramm <kcr@1ts.org>, 15 years ago
Cleanup and use owl_getline{,_chomp} and owl_slurp
  • Property mode set to 100644
File size: 33.3 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
[fea7992]9#ifdef HAVE_LIBZEPHYR
[a5e7ed6]10static GList *deferred_subs = NULL;
[79d7d98]11
[a5e7ed6]12typedef struct _owl_sub_list {                            /* noproto */
13  ZSubscription_t *subs;
14  int nsubs;
15} owl_sub_list;
16
[c79a047]17Code_t ZResetAuthentication(void);
[09489b89]18#endif
[8262340]19
[02f55dc]20#define HM_SVC_FALLBACK         htons((unsigned short) 2104)
21
[52a0f14]22#ifdef HAVE_LIBZEPHYR
[c79a047]23void owl_zephyr_initialize(void)
[52a0f14]24{
25  int ret;
[02f55dc]26  struct servent *sp;
27  struct sockaddr_in sin;
28  ZNotice_t req;
29  Code_t code;
[52a0f14]30  owl_dispatch *dispatch;
31
32  /*
33   * Code modified from libzephyr's ZhmStat.c
34   *
35   * Modified to add the fd to our select loop, rather than hanging
36   * until we get an ack.
37   */
38
39  if ((ret = ZOpenPort(NULL)) != ZERR_NONE) {
40    owl_function_error("Error opening Zephyr port: %s", error_message(ret));
41    return;
42  }
[02f55dc]43
[4d86e06]44  (void) memset(&sin, 0, sizeof(struct sockaddr_in));
[02f55dc]45
46  sp = getservbyname(HM_SVCNAME, "udp");
47
48  sin.sin_port = (sp) ? sp->s_port : HM_SVC_FALLBACK;
49  sin.sin_family = AF_INET;
50
51  sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
52
[4d86e06]53  (void) memset(&req, 0, sizeof(req));
[02f55dc]54  req.z_kind = STAT;
55  req.z_port = 0;
[712caac]56  req.z_class = zstr(HM_STAT_CLASS);
57  req.z_class_inst = zstr(HM_STAT_CLIENT);
58  req.z_opcode = zstr(HM_GIMMESTATS);
59  req.z_sender = zstr("");
60  req.z_recipient = zstr("");
61  req.z_default_format = zstr("");
[02f55dc]62  req.z_message_len = 0;
[52a0f14]63
64  if ((code = ZSetDestAddr(&sin)) != ZERR_NONE) {
65    owl_function_error("Initializing Zephyr: %s", error_message(code));
66    return;
67  }
68
69  if ((code = ZSendNotice(&req, ZNOAUTH)) != ZERR_NONE) {
70    owl_function_error("Initializing Zephyr: %s", error_message(code));
71    return;
72  }
73
74  dispatch = owl_malloc(sizeof(*dispatch));
75  dispatch->fd = ZGetFD();
76  dispatch->cfunc = owl_zephyr_finish_initialization;
77  dispatch->destroy = (void(*)(owl_dispatch*))owl_free;
78
79  owl_select_add_dispatch(dispatch);
[02f55dc]80}
81
[52a0f14]82void owl_zephyr_finish_initialization(owl_dispatch *d) {
83  Code_t code;
[df569c5]84  char *perl;
[52a0f14]85
86  owl_select_remove_dispatch(d->fd);
87
88  ZClosePort();
89
90  if ((code = ZInitialize()) != ZERR_NONE) {
91    owl_function_error("Initializing Zephyr: %s", error_message(code));
92    return;
93  }
94
95  if ((code = ZOpenPort(NULL)) != ZERR_NONE) {
96    owl_function_error("Initializing Zephyr: %s", error_message(code));
97    return;
98  }
99
100  d = owl_malloc(sizeof(owl_dispatch));
101  d->fd = ZGetFD();
102  d->cfunc = &owl_zephyr_process_events;
103  d->destroy = NULL;
104  owl_select_add_dispatch(d);
105  owl_global_set_havezephyr(&g);
106
107  if(g.load_initial_subs) {
108    owl_zephyr_load_initial_subs();
109  }
[a5e7ed6]110  while(deferred_subs != NULL) {
111    owl_sub_list *subs = deferred_subs->data;
112    owl_function_debugmsg("Loading %d deferred subs.", subs->nsubs);
113    owl_zephyr_loadsubs_helper(subs->subs, subs->nsubs);
114    deferred_subs = g_list_delete_link(deferred_subs, deferred_subs);
115    owl_free(subs);
116  }
[619d864]117
118  /* zlog in if we need to */
119  if (owl_global_is_startuplogin(&g)) {
120    owl_function_debugmsg("startup: doing zlog in");
121    owl_zephyr_zlog_in();
122  }
[27964fe]123  /* check pseudo-logins if we need to */
124  if (owl_global_is_pseudologins(&g)) {
125    owl_function_debugmsg("startup: checking pseudo-logins");
126    owl_function_zephyr_buddy_check(0);
127  }
[df569c5]128
129 perl = owl_perlconfig_execute("BarnOwl::Zephyr::_zephyr_startup()");
130 owl_free(perl);
[52a0f14]131}
132
[c79a047]133void owl_zephyr_load_initial_subs(void) {
[7451af9]134  int ret_sd, ret_bd, ret_u;
[52a0f14]135
136  owl_function_debugmsg("startup: loading initial zephyr subs");
137
138  /* load default subscriptions */
[7451af9]139  ret_sd = owl_zephyr_loaddefaultsubs();
140
141  /* load Barnowl default subscriptions */
142  ret_bd = owl_zephyr_loadbarnowldefaultsubs();
[52a0f14]143
144  /* load subscriptions from subs file */
[7451af9]145  ret_u = owl_zephyr_loadsubs(NULL, 0);
[52a0f14]146
[7451af9]147  if (ret_sd || ret_bd || ret_u) {
[52a0f14]148    owl_function_error("Error loading zephyr subscriptions");
[7451af9]149  } else if (ret_u!=-1) {
[52a0f14]150    owl_global_add_userclue(&g, OWL_USERCLUE_CLASSES);
151  }
152
153  /* load login subscriptions */
154  if (owl_global_is_loginsubs(&g)) {
155    owl_function_debugmsg("startup: loading login subs");
156    owl_function_loadloginsubs(NULL);
157  }
158}
159#else
[c79a047]160void owl_zephyr_initialize(void)
[52a0f14]161{
162}
[02f55dc]163#endif
164
[52a0f14]165
[c79a047]166int owl_zephyr_shutdown(void)
[09489b89]167{
168#ifdef HAVE_LIBZEPHYR
[bfbf590]169  if(owl_global_is_havezephyr(&g)) {
170    unsuball();
171    ZClosePort();
172  }
[09489b89]173#endif
[32ad44d]174  return 0;
[be0a79f]175}
176
[c79a047]177int owl_zephyr_zpending(void)
[be0a79f]178{
179#ifdef HAVE_LIBZEPHYR
[bfbf590]180  if(owl_global_is_havezephyr(&g))
181    return(ZPending());
[be0a79f]182#endif
[32ad44d]183  return 0;
[be0a79f]184}
185
[c79a047]186const char *owl_zephyr_get_realm(void)
[09489b89]187{
188#ifdef HAVE_LIBZEPHYR
[32ad44d]189  if (owl_global_is_havezephyr(&g))
190    return(ZGetRealm());
[09489b89]191#endif
[32ad44d]192  return "";
[09489b89]193}
194
[c79a047]195const char *owl_zephyr_get_sender(void)
[09489b89]196{
197#ifdef HAVE_LIBZEPHYR
[32ad44d]198  if (owl_global_is_havezephyr(&g))
199    return(ZGetSender());
[09489b89]200#endif
[32ad44d]201  return "";
[09489b89]202}
203
[f6050ee]204#ifdef HAVE_LIBZEPHYR
[93e883d]205int owl_zephyr_loadsubs_helper(ZSubscription_t subs[], int count)
206{
[18105584]207  int ret = 0;
[a5e7ed6]208  if (owl_global_is_havezephyr(&g)) {
209    int i;
210    /* sub without defaults */
211    if (ZSubscribeToSansDefaults(subs,count,0) != ZERR_NONE) {
212      owl_function_error("Error subscribing to zephyr notifications.");
213      ret=-2;
214    }
[93e883d]215
[a5e7ed6]216    /* free stuff */
217    for (i=0; i<count; i++) {
218      owl_free(subs[i].zsub_class);
219      owl_free(subs[i].zsub_classinst);
220      owl_free(subs[i].zsub_recipient);
221    }
[bb2c60d]222
[a5e7ed6]223    owl_free(subs);
224  } else {
225    owl_sub_list *s = owl_malloc(sizeof(owl_sub_list));
226    s->subs = subs;
227    s->nsubs = count;
228    deferred_subs = g_list_append(deferred_subs, s);
229  }
[d21efbc]230
[93e883d]231  return ret;
232}
[f6050ee]233#endif
[93e883d]234
[7451af9]235/* Load zephyr subscriptions from 'filename'.  If 'filename' is NULL,
[95474d7]236 * the default file $HOME/.zephyr.subs will be used.
237 *
238 * Returns 0 on success.  If the file does not exist, return -1 if
239 * 'error_on_nofile' is 1, otherwise return 0.  Return -1 if the file
240 * exists but can not be read.  Return -2 if there is a failure from
241 * zephyr to load the subscriptions.
[2de4f20]242 */
[e19eb97]243int owl_zephyr_loadsubs(const char *filename, int error_on_nofile)
[31e48a3]244{
[be0a79f]245#ifdef HAVE_LIBZEPHYR
[7d4fbcd]246  FILE *file;
247  char *tmp, *start;
[b7ee89b]248  char *buffer = NULL;
249  char *subsfile;
[bb2c60d]250  ZSubscription_t *subs;
251  int subSize = 1024;
[b7ee89b]252  int count;
[4357be8]253  struct stat statbuff;
[7d4fbcd]254
[bb2c60d]255  subs = owl_malloc(sizeof(ZSubscription_t) * subSize);
[b7ee89b]256  if (filename == NULL)
257    subsfile = owl_sprintf("%s/.zephyr.subs", owl_global_get_homedir(&g));
258  else
259    subsfile = owl_strdup(filename);
260
261  if (stat(subsfile, &statbuff) != 0) {
262    if (error_on_nofile == 1)
263      return -1;
264    return 0;
[95474d7]265  }
[4357be8]266
[8262340]267  ZResetAuthentication();
[b7ee89b]268  count = 0;
269  file = fopen(subsfile, "r");
270  owl_free(subsfile);
271  if (!file)
272    return -1;
273  while (owl_getline(&buffer, file)) {
274    if (buffer[0] == '#' || buffer[0] == '\n')
275        continue;
276
277    if (buffer[0] == '-')
278      start = buffer + 1;
279    else
280      start = buffer;
281
[bb2c60d]282    if (count >= subSize) {
[d21efbc]283      subSize *= 2;
284      subs = owl_realloc(subs, sizeof(ZSubscription_t) * subSize);
[93e883d]285    }
[95474d7]286   
287    /* add it to the list of subs */
[b7ee89b]288    if ((tmp = strtok(start, ",\n\r")) == NULL)
289      continue;
290    subs[count].zsub_class = owl_strdup(tmp);
291    if ((tmp=strtok(NULL, ",\n\r")) == NULL)
292      continue;
293    subs[count].zsub_classinst = owl_strdup(tmp);
294    if ((tmp = strtok(NULL, " \t\n\r")) == NULL)
295      continue;
296    subs[count].zsub_recipient = owl_strdup(tmp);
297
[bb2c60d]298    /* if it started with '-' then add it to the global punt list, and
299     * remove it from the list of subs. */
[b7ee89b]300    if (buffer[0] == '-') {
[95474d7]301      owl_function_zpunt(subs[count].zsub_class, subs[count].zsub_classinst, subs[count].zsub_recipient, 0);
[bb2c60d]302      owl_free(subs[count].zsub_class);
303      owl_free(subs[count].zsub_classinst);
304      owl_free(subs[count].zsub_recipient);
[b7ee89b]305    } else {
[bb2c60d]306      count++;
[95474d7]307    }
[7d4fbcd]308  }
[95474d7]309  fclose(file);
[b7ee89b]310  if (buffer)
311    owl_free(buffer);
[7d4fbcd]312
[b7ee89b]313  return owl_zephyr_loadsubs_helper(subs, count);
[7451af9]314#else
[b7ee89b]315  return 0;
[7451af9]316#endif
317}
318
319/* Load default Barnowl subscriptions
320 *
321 * Returns 0 on success.
322 * Return -2 if there is a failure from zephyr to load the subscriptions.
323 */
[c79a047]324int owl_zephyr_loadbarnowldefaultsubs(void)
[7451af9]325{
326#ifdef HAVE_LIBZEPHYR
327  ZSubscription_t *subs;
328  int subSize = 10; /* Max Barnowl default subs we allow */
329  int count, ret;
330
331  subs = owl_malloc(sizeof(ZSubscription_t) * subSize);
332  ret = 0;
333  ZResetAuthentication();
334  count=0;
335
336  subs[count].zsub_class=owl_strdup("message");
337  subs[count].zsub_classinst=owl_strdup("*");
338  subs[count].zsub_recipient=owl_strdup("%me%");
339  count++;
340
341  ret = owl_zephyr_loadsubs_helper(subs, count);
[7d4fbcd]342  return(ret);
[be0a79f]343#else
344  return(0);
345#endif
[7d4fbcd]346}
347
[c79a047]348int owl_zephyr_loaddefaultsubs(void)
[4357be8]349{
[40d834a]350#ifdef HAVE_LIBZEPHYR
[4357be8]351  ZSubscription_t subs[10];
352   
353  if (ZSubscribeTo(subs,0,0) != ZERR_NONE) {
354    owl_function_error("Error subscribing to default zephyr notifications.");
355    return(-1);
356  }
357  return(0);
[40d834a]358#else
359  return(0);
360#endif
[4357be8]361}
362
[e19eb97]363int owl_zephyr_loadloginsubs(const char *filename)
[31e48a3]364{
[be0a79f]365#ifdef HAVE_LIBZEPHYR
[7d4fbcd]366  FILE *file;
[d21efbc]367  ZSubscription_t *subs;
368  int numSubs = 100;
[b7ee89b]369  char *subsfile;
370  char *buffer = NULL;
371  int count;
[4357be8]372  struct stat statbuff;
[7d4fbcd]373
[d21efbc]374  subs = owl_malloc(numSubs * sizeof(ZSubscription_t));
375
[b7ee89b]376  if (filename==NULL)
377    subsfile = owl_sprintf("%s/%s", owl_global_get_homedir(&g), ".anyone");
378  else
379    subsfile = owl_strdup(filename);
[4357be8]380
[b7ee89b]381  if (stat(subsfile, &statbuff) == -1)
382    return 0;
[7d4fbcd]383
[8262340]384  ZResetAuthentication();
[b7ee89b]385  count = 0;
386  file = fopen(subsfile, "r");
387  owl_free(subsfile);
[7d4fbcd]388  if (file) {
[b7ee89b]389    while (owl_getline_chomp(&buffer, file)) {
390      if (buffer[0] == '\0' || buffer[0] == '#')
391        continue;
392
[d21efbc]393      if (count == numSubs) {
394        numSubs *= 2;
395        subs = owl_realloc(subs, numSubs * sizeof(ZSubscription_t));
396      }
[7d4fbcd]397
[b7ee89b]398      subs[count].zsub_class = owl_strdup("login");
399      subs[count].zsub_recipient = owl_strdup("*");
400      subs[count].zsub_classinst = long_zuser(buffer);
[7d4fbcd]401
402      count++;
403    }
404    fclose(file);
405  } else {
[b7ee89b]406    return 0;
[7d4fbcd]407  }
[b7ee89b]408  if (buffer)
409    owl_free(buffer);
[7d4fbcd]410
[b7ee89b]411  return owl_zephyr_loadsubs_helper(subs, count);
[be0a79f]412#else
[b7ee89b]413  return 0;
[be0a79f]414#endif
[7d4fbcd]415}
416
[c79a047]417void unsuball(void)
[31e48a3]418{
[be0a79f]419#if HAVE_LIBZEPHYR
[7d4fbcd]420  int ret;
[8262340]421
422  ZResetAuthentication();
[7d4fbcd]423  ret=ZCancelSubscriptions(0);
424  if (ret != ZERR_NONE) {
425    com_err("owl",ret,"while unsubscribing");
426  }
[be0a79f]427#endif
[7d4fbcd]428}
429
[e19eb97]430int owl_zephyr_sub(const char *class, const char *inst, const char *recip)
[31e48a3]431{
[be0a79f]432#ifdef HAVE_LIBZEPHYR
[7d4fbcd]433  ZSubscription_t subs[5];
434
[712caac]435  subs[0].zsub_class=zstr(class);
436  subs[0].zsub_classinst=zstr(inst);
437  subs[0].zsub_recipient=zstr(recip);
[7d4fbcd]438
[8262340]439  ZResetAuthentication();
[7d4fbcd]440  if (ZSubscribeTo(subs,1,0) != ZERR_NONE) {
[97cd00be]441    owl_function_error("Error subbing to <%s,%s,%s>", class, inst, recip);
442    return(-2);
[7d4fbcd]443  }
444  return(0);
[be0a79f]445#else
446  return(0);
447#endif
[7d4fbcd]448}
449
450
[e19eb97]451int owl_zephyr_unsub(const char *class, const char *inst, const char *recip)
[31e48a3]452{
[be0a79f]453#ifdef HAVE_LIBZEPHYR
[7d4fbcd]454  ZSubscription_t subs[5];
455
[712caac]456  subs[0].zsub_class=zstr(class);
457  subs[0].zsub_classinst=zstr(inst);
458  subs[0].zsub_recipient=zstr(recip);
[7d4fbcd]459
[8262340]460  ZResetAuthentication();
[7d4fbcd]461  if (ZUnsubscribeTo(subs,1,0) != ZERR_NONE) {
[97cd00be]462    owl_function_error("Error unsubbing from <%s,%s,%s>", class, inst, recip);
463    return(-2);
[7d4fbcd]464  }
465  return(0);
[be0a79f]466#else
467  return(0);
468#endif
[7d4fbcd]469}
470
[b0430a6]471/* return a pointer to the data in the Jth field, (NULL terminated by
472 * definition).  Caller must free the return.
473 */
[be0a79f]474#ifdef HAVE_LIBZEPHYR
[1077891a]475char *owl_zephyr_get_field(const ZNotice_t *n, int j)
[31e48a3]476{
[7d4fbcd]477  int i, count, save;
[b0430a6]478  char *out;
[7d4fbcd]479
[128171a]480  /* If there's no message here, just run along now */
481  if (n->z_message_len == 0)
482    return(owl_strdup(""));
483
[7d4fbcd]484  count=save=0;
485  for (i=0; i<n->z_message_len; i++) {
486    if (n->z_message[i]=='\0') {
487      count++;
488      if (count==j) {
489        /* just found the end of the field we're looking for */
[b0430a6]490        return(owl_strdup(n->z_message+save));
[7d4fbcd]491      } else {
492        save=i+1;
493      }
494    }
495  }
[b0430a6]496  /* catch the last field, which might not be null terminated */
[7d4fbcd]497  if (count==j-1) {
[b0430a6]498    out=owl_malloc(n->z_message_len-save+5);
499    memcpy(out, n->z_message+save, n->z_message_len-save);
500    out[n->z_message_len-save]='\0';
501    return(out);
[7d4fbcd]502  }
[b0430a6]503
504  return(owl_strdup(""));
[7d4fbcd]505}
[5376a95]506
[1077891a]507char *owl_zephyr_get_field_as_utf8(const ZNotice_t *n, int j)
[5376a95]508{
509  int i, count, save;
510
511  /* If there's no message here, just run along now */
512  if (n->z_message_len == 0)
513    return(owl_strdup(""));
514
515  count=save=0;
516  for (i = 0; i < n->z_message_len; i++) {
517    if (n->z_message[i]=='\0') {
518      count++;
519      if (count == j) {
520        /* just found the end of the field we're looking for */
[6201646]521        return(owl_validate_or_convert(n->z_message + save));
[5376a95]522      } else {
523        save = i + 1;
524      }
525    }
526  }
527  /* catch the last field, which might not be null terminated */
528  if (count == j - 1) {
[6201646]529    char *tmp, *out;
530    tmp = owl_malloc(n->z_message_len-save+5);
531    memcpy(tmp, n->z_message+save, n->z_message_len-save);
532    tmp[n->z_message_len-save]='\0';
533    out = owl_validate_or_convert(tmp);
534    owl_free(tmp);
535    return out;
[5376a95]536  }
537
538  return(owl_strdup(""));
539}
[09489b89]540#else
[701a184]541char *owl_zephyr_get_field(void *n, int j)
[09489b89]542{
[b0430a6]543  return(owl_strdup(""));
[09489b89]544}
[5577606]545char *owl_zephyr_get_field_as_utf8(void *n, int j)
[5376a95]546{
547  return owl_zephyr_get_field(n, j);
548}
[be0a79f]549#endif
[7d4fbcd]550
[b0430a6]551
[be0a79f]552#ifdef HAVE_LIBZEPHYR
[1077891a]553int owl_zephyr_get_num_fields(const ZNotice_t *n)
[31e48a3]554{
[7d4fbcd]555  int i, fields;
556
[50e29e3]557  if(n->z_message_len == 0)
558    return 0;
559
[7d4fbcd]560  fields=1;
561  for (i=0; i<n->z_message_len; i++) {
562    if (n->z_message[i]=='\0') fields++;
563  }
564 
565  return(fields);
566}
[09489b89]567#else
[1077891a]568int owl_zephyr_get_num_fields(const void *n)
[09489b89]569{
570  return(0);
571}
[be0a79f]572#endif
[7d4fbcd]573
[be0a79f]574#ifdef HAVE_LIBZEPHYR
[bf73bdd]575/* return a pointer to the message, place the message length in k
576 * caller must free the return
577 */
[c08c70a]578char *owl_zephyr_get_message(const ZNotice_t *n, const owl_message *m)
[31e48a3]579{
[405d5e6]580  /* don't let ping messages have a body */
[7d4fbcd]581  if (!strcasecmp(n->z_opcode, "ping")) {
[bf73bdd]582    return(owl_strdup(""));
[7d4fbcd]583  }
584
[a1bb198]585  /* deal with MIT NOC messages */
[85d1795]586  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")) {
587    char *msg, *field3, *field4;
[a1bb198]588
589    field3 = owl_zephyr_get_field(n, 3);
590    field4 = owl_zephyr_get_field(n, 4);
591
[85d1795]592    msg = owl_sprintf("%s service on %s %s\n%s", n->z_opcode, n->z_class_inst, field3, field4);
[272a4a0]593    owl_free(field3);
594    owl_free(field4);
[a1bb198]595    if (msg) {
596      return msg;
597    }
598  }
[fba0f96]599  /* deal with MIT Discuss messages */
[e2a620b]600  else if (!strcasecmp(n->z_default_format, "New transaction [$1] entered in $2\nFrom: $3 ($5)\nSubject: $4") ||
601           !strcasecmp(n->z_default_format, "New transaction [$1] entered in $2\nFrom: $3\nSubject: $4")) {
[fba0f96]602    char *msg, *field1, *field2, *field3, *field4, *field5;
603   
604    field1 = owl_zephyr_get_field(n, 1);
605    field2 = owl_zephyr_get_field(n, 2);
606    field3 = owl_zephyr_get_field(n, 3);
607    field4 = owl_zephyr_get_field(n, 4);
608    field5 = owl_zephyr_get_field(n, 5);
609   
610    msg = owl_sprintf("New transaction [%s] entered in %s\nFrom: %s (%s)\nSubject: %s", field1, field2, field3, field5, field4);
611    owl_free(field1);
612    owl_free(field2);
613    owl_free(field3);
614    owl_free(field4);
615    owl_free(field5);
616    if (msg) {
617      return msg;
618    }
619  }
[85d1795]620  /* deal with MIT Moira messages */
621  else if (!strcasecmp(n->z_default_format, "MOIRA $instance on $fromhost:\n $message\n")) {
622    char *msg, *field1;
623   
624    field1 = owl_zephyr_get_field(n, 1);
625   
626    msg = owl_sprintf("MOIRA %s on %s: %s", n->z_class_inst, owl_message_get_hostname(m), field1);
627    owl_free(field1);
628    if (msg) {
629      return msg;
630    }
631  }
[405d5e6]632
[85d1795]633  if (owl_zephyr_get_num_fields(n) == 1) {
634    return(owl_zephyr_get_field(n, 1));
635  }
636  else {
637    return(owl_zephyr_get_field(n, 2));
638  }
[7d4fbcd]639}
[be0a79f]640#endif
[7d4fbcd]641
[be0a79f]642#ifdef HAVE_LIBZEPHYR
[1077891a]643const char *owl_zephyr_get_zsig(const ZNotice_t *n, int *k)
[31e48a3]644{
[7d4fbcd]645  /* return a pointer to the zsig if there is one */
646
[405d5e6]647  /* message length 0? No zsig */
[7d4fbcd]648  if (n->z_message_len==0) {
649    *k=0;
650    return("");
651  }
[405d5e6]652
[85d1795]653  /* If there's only one field, no zsig */
654  if (owl_zephyr_get_num_fields(n) == 1) {
655    *k=0;
[405d5e6]656    return("");
657  }
658
659  /* Everything else is field 1 */
[7d4fbcd]660  *k=strlen(n->z_message);
661  return(n->z_message);
662}
[09489b89]663#else
[1077891a]664const char *owl_zephyr_get_zsig(const void *n, int *k)
[09489b89]665{
666  return("");
667}
[be0a79f]668#endif
[7d4fbcd]669
[e19eb97]670int send_zephyr(const char *opcode, const char *zsig, const char *class, const char *instance, const char *recipient, const char *message)
[31e48a3]671{
[be0a79f]672#ifdef HAVE_LIBZEPHYR
[7d4fbcd]673  int ret;
674  ZNotice_t notice;
675   
676  memset(&notice, 0, sizeof(notice));
677
[8262340]678  ZResetAuthentication();
[8ba37ec]679
680  if (!zsig) zsig="";
[8262340]681 
[7d4fbcd]682  notice.z_kind=ACKED;
683  notice.z_port=0;
[712caac]684  notice.z_class=zstr(class);
685  notice.z_class_inst=zstr(instance);
[21882032]686  notice.z_sender=NULL;
[7d4fbcd]687  if (!strcmp(recipient, "*") || !strcmp(recipient, "@")) {
[712caac]688    notice.z_recipient=zstr("");
[21882032]689    if (*owl_global_get_zsender(&g))
[712caac]690        notice.z_sender=zstr(owl_global_get_zsender(&g));
[7d4fbcd]691  } else {
[712caac]692    notice.z_recipient=zstr(recipient);
[7d4fbcd]693  }
[712caac]694  notice.z_default_format=zstr("Class $class, Instance $instance:\nTo: @bold($recipient) at $time $date\nFrom: @bold{$1 <$sender>}\n\n$2");
695  if (opcode) notice.z_opcode=zstr(opcode);
[7d4fbcd]696
[56330ff]697  notice.z_message_len=strlen(zsig)+1+strlen(message);
[7d4fbcd]698  notice.z_message=owl_malloc(notice.z_message_len+10);
[56330ff]699  strcpy(notice.z_message, zsig);
700  memcpy(notice.z_message+strlen(zsig)+1, message, strlen(message));
[7d4fbcd]701
702  /* ret=ZSendNotice(&notice, ZAUTH); */
703  ret=ZSrvSendNotice(&notice, ZAUTH, send_zephyr_helper);
704 
705  /* free then check the return */
706  owl_free(notice.z_message);
707  ZFreeNotice(&notice);
708  if (ret!=ZERR_NONE) {
[ec6ff52]709    owl_function_error("Error sending zephyr");
[7d4fbcd]710    return(ret);
711  }
712  return(0);
[be0a79f]713#else
714  return(0);
715#endif
[7d4fbcd]716}
717
[be0a79f]718#ifdef HAVE_LIBZEPHYR
[31e48a3]719Code_t send_zephyr_helper(ZNotice_t *notice, char *buf, int len, int wait)
720{
[7d4fbcd]721  return(ZSendPacket(buf, len, 0));
722}
[be0a79f]723#endif
[7d4fbcd]724
[e19eb97]725void send_ping(const char *to, const char *zclass, const char *zinstance)
[31e48a3]726{
[be0a79f]727#ifdef HAVE_LIBZEPHYR
[3ef779b]728  send_zephyr("PING", "", zclass, zinstance, to, "");
[be0a79f]729#endif
[7d4fbcd]730}
731
[be0a79f]732#ifdef HAVE_LIBZEPHYR
[1077891a]733void owl_zephyr_handle_ack(const ZNotice_t *retnotice)
[31e48a3]734{
[7d4fbcd]735  char *tmp;
736 
737  /* if it's an HMACK ignore it */
738  if (retnotice->z_kind == HMACK) return;
[aecf3e6]739
[7d4fbcd]740  if (retnotice->z_kind == SERVNAK) {
[ec6ff52]741    owl_function_error("Authorization failure sending zephyr");
[7d4fbcd]742  } else if ((retnotice->z_kind != SERVACK) || !retnotice->z_message_len) {
[ec6ff52]743    owl_function_error("Detected server failure while receiving acknowledgement");
[7d4fbcd]744  } else if (!strcmp(retnotice->z_message, ZSRVACK_SENT)) {
745    if (!strcasecmp(retnotice->z_opcode, "ping")) {
746      return;
747    } else {
[d73e3af]748      if (strcasecmp(retnotice->z_recipient, ""))
[1e550b2]749      { /* personal */
[d73e3af]750        tmp=short_zuser(retnotice->z_recipient);
751        if(!strcasecmp(retnotice->z_class, "message") &&
752           !strcasecmp(retnotice->z_class_inst, "personal")) {
753          owl_function_makemsg("Message sent to %s.", tmp);
[1e550b2]754        } else if(!strcasecmp(retnotice->z_class, "message")) { /* instanced, but not classed, personal */
[d73e3af]755          owl_function_makemsg("Message sent to %s on -i %s\n", tmp, retnotice->z_class_inst);
[1e550b2]756        } else { /* classed personal */
[d73e3af]757          owl_function_makemsg("Message sent to %s on -c %s -i %s\n", tmp, retnotice->z_class, retnotice->z_class_inst);
758        }
[27f6487]759        owl_free(tmp);
[d73e3af]760      } else {
[1e550b2]761        /* class / instance message */
[d73e3af]762          owl_function_makemsg("Message sent to -c %s -i %s\n", retnotice->z_class, retnotice->z_class_inst);
763      }
[7d4fbcd]764    }
765  } else if (!strcmp(retnotice->z_message, ZSRVACK_NOTSENT)) {
[1151f0b]766    #define BUFFLEN 1024
[44f32fb]767    if (retnotice->z_recipient == NULL
[60c2e1e]768        || *retnotice->z_recipient == 0
[44f32fb]769        || *retnotice->z_recipient == '@') {
[1151f0b]770      char buff[BUFFLEN];
[44f32fb]771      owl_function_error("No one subscribed to class %s", retnotice->z_class);
[1151f0b]772      snprintf(buff, BUFFLEN, "Could not send message to class %s: no one subscribed.\n", retnotice->z_class);
[9119a47]773      owl_function_adminmsg("", buff);
[7d4fbcd]774    } else {
[1151f0b]775      char buff[BUFFLEN];
[24ccc01]776      owl_zwrite zw;
777      char *realm;
778
[4b464a4]779      tmp = short_zuser(retnotice->z_recipient);
[44f32fb]780      owl_function_error("%s: Not logged in or subscribing.", tmp);
[3ef779b]781      /*
782       * These error messages are often over 80 chars, but users who want to
783       * see the whole thing can scroll to the side, and for those with wide
784       * terminals or who don't care, not splitting saves a line in the UI
785       */
786      if(strcasecmp(retnotice->z_class, "message")) {
[1151f0b]787        snprintf(buff, BUFFLEN,
[10e3963]788                 "Could not send message to %s: "
[3ef779b]789                 "not logged in or subscribing to class %s, instance %s.\n",
[10e3963]790                 tmp,
[1151f0b]791                 retnotice->z_class,
792                 retnotice->z_class_inst);
[3ef779b]793      } else if(strcasecmp(retnotice->z_class_inst, "personal")) {
794        snprintf(buff, BUFFLEN,
795                 "Could not send message to %s: "
796                 "not logged in or subscribing to instance %s.\n",
797                 tmp,
798                 retnotice->z_class_inst);
[44f32fb]799      } else {
[1151f0b]800        snprintf(buff, BUFFLEN,
[10e3963]801                 "Could not send message to %s: "
802                 "not logged in or subscribing to messages.\n",
803                 tmp);
[44f32fb]804      }
[9119a47]805      owl_function_adminmsg("", buff);
[24ccc01]806
807      memset(&zw, 0, sizeof(zw));
808      zw.class = owl_strdup(retnotice->z_class);
809      zw.inst  = owl_strdup(retnotice->z_class_inst);
810      realm = strchr(retnotice->z_recipient, '@');
811      if(realm) {
812        zw.realm = owl_strdup(realm + 1);
813      } else {
814        zw.realm = owl_strdup(owl_zephyr_get_realm());
815      }
816      zw.opcode = owl_strdup(retnotice->z_opcode);
817      zw.zsig   = owl_strdup("");
818      owl_list_create(&(zw.recips));
819      owl_list_append_element(&(zw.recips), owl_strdup(tmp));
820
821      owl_log_outgoing_zephyr_error(&zw, buff);
822
823      owl_zwrite_free(&zw);
[1c6c4d3]824      owl_free(tmp);
[7d4fbcd]825    }
826  } else {
[ec6ff52]827    owl_function_error("Internal error on ack (%s)", retnotice->z_message);
[7d4fbcd]828  }
829}
[09489b89]830#else
[1077891a]831void owl_zephyr_handle_ack(const void *retnotice)
[09489b89]832{
833}
[be0a79f]834#endif
[7d4fbcd]835
[be0a79f]836#ifdef HAVE_LIBZEPHYR
[1077891a]837int owl_zephyr_notice_is_ack(const ZNotice_t *n)
[31e48a3]838{
[7d4fbcd]839  if (n->z_kind == SERVNAK || n->z_kind == SERVACK || n->z_kind == HMACK) {
840    if (!strcasecmp(n->z_class, LOGIN_CLASS)) return(0);
841    return(1);
842  }
843  return(0);
844}
[09489b89]845#else
[1077891a]846int owl_zephyr_notice_is_ack(const void *n)
[09489b89]847{
848  return(0);
849}
[be0a79f]850#endif
[7d4fbcd]851 
[c08c70a]852void owl_zephyr_zaway(const owl_message *m)
[31e48a3]853{
[be0a79f]854#ifdef HAVE_LIBZEPHYR
[7c8060d0]855  char *tmpbuff, *myuser, *to;
[15b34fd]856  owl_message *mout;
[7d4fbcd]857 
[aa2f6364]858  /* bail if it doesn't look like a message we should reply to.  Some
[2de4f20]859   * of this defined by the way zaway(1) works
860   */
[7d4fbcd]861  if (strcasecmp(owl_message_get_class(m), "message")) return;
862  if (strcasecmp(owl_message_get_recipient(m), ZGetSender())) return;
863  if (!strcasecmp(owl_message_get_sender(m), "")) return;
864  if (!strcasecmp(owl_message_get_opcode(m), "ping")) return;
865  if (!strcasecmp(owl_message_get_opcode(m), "auto")) return;
[d023c25]866  if (!strcasecmp(owl_message_get_zsig(m), "Automated reply:")) return;
[7d4fbcd]867  if (!strcasecmp(owl_message_get_sender(m), ZGetSender())) return;
[9854278]868  if (owl_message_get_attribute_value(m, "isauto")) return;
[7d4fbcd]869
[7c8060d0]870  if (owl_global_is_smartstrip(&g)) {
[e3d9c77]871    to=owl_zephyr_smartstripped_user(owl_message_get_sender(m));
[7c8060d0]872  } else {
873    to=owl_strdup(owl_message_get_sender(m));
874  }
875
[7d4fbcd]876  send_zephyr("",
877              "Automated reply:",
878              owl_message_get_class(m),
879              owl_message_get_instance(m),
[7c8060d0]880              to,
[7d4fbcd]881              owl_global_get_zaway_msg(&g));
882
[7c8060d0]883  myuser=short_zuser(to);
[aa2f6364]884  if (!strcasecmp(owl_message_get_instance(m), "personal")) {
885    tmpbuff = owl_sprintf("zwrite %s", myuser);
886  } else {
887    tmpbuff = owl_sprintf("zwrite -i %s %s", owl_message_get_instance(m), myuser);
888  }
889  owl_free(myuser);
[7c8060d0]890  owl_free(to);
[aa2f6364]891
[7d4fbcd]892  /* display the message as an admin message in the receive window */
[15b34fd]893  mout=owl_function_make_outgoing_zephyr(owl_global_get_zaway_msg(&g), tmpbuff, "Automated reply:");
[13a3c1db]894  owl_global_messagequeue_addmsg(&g, mout);
[7d4fbcd]895  owl_free(tmpbuff);
[be0a79f]896#endif
[7d4fbcd]897}
898
[be0a79f]899#ifdef HAVE_LIBZEPHYR
[31e48a3]900void owl_zephyr_hackaway_cr(ZNotice_t *n)
901{
[7d4fbcd]902  /* replace \r's with ' '.  Gross-ish */
903  int i;
904
905  for (i=0; i<n->z_message_len; i++) {
906    if (n->z_message[i]=='\r') {
907      n->z_message[i]=' ';
908    }
909  }
910}
[be0a79f]911#endif
[7d4fbcd]912
[dca3b27]913char *owl_zephyr_zlocate(const char *user, int auth)
[31e48a3]914{
[be0a79f]915#ifdef HAVE_LIBZEPHYR
[7d4fbcd]916  int ret, numlocs;
917  int one = 1;
918  ZLocations_t locations;
919  char *myuser;
[dca3b27]920  char *p, *result;
921
[8262340]922  ZResetAuthentication();
[dca3b27]923  ret = ZLocateUser(zstr(user), &numlocs, auth ? ZAUTH : ZNOAUTH);
924  if (ret != ZERR_NONE)
925    return owl_sprintf("Error locating user %s: %s\n",
926                       user, error_message(ret));
927
928  myuser = short_zuser(user);
929  if (numlocs == 0) {
930    result = owl_sprintf("%s: Hidden or not logged in\n", myuser);
931  } else {
932    result = owl_strdup("");
933    for (; numlocs; numlocs--) {
934      ZGetLocations(&locations, &one);
935      p = owl_sprintf("%s%s: %s\t%s\t%s\n",
936                          result, myuser,
937                          locations.host ? locations.host : "?",
938                          locations.tty ? locations.tty : "?",
939                          locations.time ? locations.time : "?");
940      owl_free(result);
941      result = p;
942    }
[7d4fbcd]943  }
944
[dca3b27]945  return result;
946#else
947  return owl_strdup("");
[be0a79f]948#endif
[7d4fbcd]949}
[bde7714]950
[e19eb97]951void owl_zephyr_addsub(const char *filename, const char *class, const char *inst, const char *recip)
[31e48a3]952{
[be0a79f]953#ifdef HAVE_LIBZEPHYR
[b7ee89b]954  char *line, *subsfile, *s = NULL;
[bde7714]955  FILE *file;
[b7ee89b]956  int duplicate = 0;
[bde7714]957
[b7ee89b]958  line = owl_zephyr_makesubline(class, inst, recip);
[bde7714]959
[b7ee89b]960  if (filename == NULL)
961    subsfile = owl_sprintf("%s/.zephyr.subs", owl_global_get_homedir(&g));
962  else
963    subsfile = owl_strdup(filename);
[bde7714]964
[74037d9]965  /* if the file already exists, check to see if the sub is already there */
[b7ee89b]966  file = fopen(subsfile, "r");
[74037d9]967  if (file) {
[b7ee89b]968    while (owl_getline(&s, file)) {
969      if (strcasecmp(s, line) == 0) {
[74037d9]970        owl_function_error("Subscription already present in %s", subsfile);
[b7ee89b]971        duplicate++;
[74037d9]972      }
[bde7714]973    }
[99dabee]974    fclose(file);
[b7ee89b]975    owl_free(s);
[bde7714]976  }
977
[b7ee89b]978  if (!duplicate) {
979    file = fopen(subsfile, "a");
980    if (file) {
981      fputs(line, file);
982      fclose(file);
983      owl_function_makemsg("Subscription added");
984    } else {
985      owl_function_error("Error opening file %s for writing", subsfile);
986    }
[bde7714]987  }
[b7ee89b]988
[bde7714]989  owl_free(line);
[be0a79f]990#endif
[bde7714]991}
992
[e19eb97]993void owl_zephyr_delsub(const char *filename, const char *class, const char *inst, const char *recip)
[31e48a3]994{
[be0a79f]995#ifdef HAVE_LIBZEPHYR
[b2a91b6]996  char *line, *subsfile;
[da60ba9]997  int linesdeleted;
[bde7714]998 
999  line=owl_zephyr_makesubline(class, inst, recip);
[b2a91b6]1000  line[strlen(line)-1]='\0';
[bde7714]1001
[b2a91b6]1002  if (!filename) {
1003    subsfile=owl_sprintf("%s/.zephyr.subs", owl_global_get_homedir(&g));
[bde7714]1004  } else {
[b2a91b6]1005    subsfile=owl_strdup(filename);
[bde7714]1006  }
[b2a91b6]1007 
[da60ba9]1008  linesdeleted = owl_util_file_deleteline(subsfile, line, 1);
1009  if (linesdeleted > 0) {
1010    owl_function_makemsg("Subscription removed");
1011  } else {
1012    owl_function_error("No subscription present in %s", subsfile);
1013  }
[b2a91b6]1014  owl_free(subsfile);
[bde7714]1015  owl_free(line);
[be0a79f]1016#endif
[bde7714]1017}
1018
[b2a91b6]1019/* caller must free the return */
[e19eb97]1020char *owl_zephyr_makesubline(const char *class, const char *inst, const char *recip)
[31e48a3]1021{
[36486be]1022  return owl_sprintf("%s,%s,%s\n", class, inst, !strcmp(recip, "") ? "*" : recip);
[bde7714]1023}
[31e48a3]1024
1025
1026void owl_zephyr_zlog_in(void)
1027{
[be0a79f]1028#ifdef HAVE_LIBZEPHYR
[e19eb97]1029  const char *exposure, *eset;
[31e48a3]1030  int ret;
1031
1032  ZResetAuthentication();
1033   
1034  eset=EXPOSE_REALMVIS;
[712caac]1035  exposure=ZGetVariable(zstr("exposure"));
[31e48a3]1036  if (exposure==NULL) {
1037    eset=EXPOSE_REALMVIS;
1038  } else if (!strcasecmp(exposure,EXPOSE_NONE)) {
1039    eset = EXPOSE_NONE;
1040  } else if (!strcasecmp(exposure,EXPOSE_OPSTAFF)) {
1041    eset = EXPOSE_OPSTAFF;
1042  } else if (!strcasecmp(exposure,EXPOSE_REALMVIS)) {
1043    eset = EXPOSE_REALMVIS;
1044  } else if (!strcasecmp(exposure,EXPOSE_REALMANN)) {
1045    eset = EXPOSE_REALMANN;
1046  } else if (!strcasecmp(exposure,EXPOSE_NETVIS)) {
1047    eset = EXPOSE_NETVIS;
1048  } else if (!strcasecmp(exposure,EXPOSE_NETANN)) {
1049    eset = EXPOSE_NETANN;
1050  }
1051   
[712caac]1052  ret=ZSetLocation(zstr(eset));
[31e48a3]1053  if (ret != ZERR_NONE) {
1054    /*
[e440602]1055      owl_function_makemsg("Error setting location: %s", error_message(ret));
[31e48a3]1056    */
1057  }
[be0a79f]1058#endif
[31e48a3]1059}
1060
1061void owl_zephyr_zlog_out(void)
1062{
[be0a79f]1063#ifdef HAVE_LIBZEPHYR
[31e48a3]1064  int ret;
1065
1066  ZResetAuthentication();
1067  ret=ZUnsetLocation();
1068  if (ret != ZERR_NONE) {
1069    /*
[e440602]1070      owl_function_makemsg("Error unsetting location: %s", error_message(ret));
[31e48a3]1071    */
1072  }
[be0a79f]1073#endif
[31e48a3]1074}
1075
[e19eb97]1076void owl_zephyr_addbuddy(const char *name)
[65ad073]1077{
1078  char *filename;
1079  FILE *file;
1080 
1081  filename=owl_sprintf("%s/.anyone", owl_global_get_homedir(&g));
1082  file=fopen(filename, "a");
1083  owl_free(filename);
1084  if (!file) {
[ec6ff52]1085    owl_function_error("Error opening zephyr buddy file for append");
[65ad073]1086    return;
1087  }
1088  fprintf(file, "%s\n", name);
1089  fclose(file);
1090}
1091
[e19eb97]1092void owl_zephyr_delbuddy(const char *name)
[65ad073]1093{
1094  char *filename;
1095
1096  filename=owl_sprintf("%s/.anyone", owl_global_get_homedir(&g));
1097  owl_util_file_deleteline(filename, name, 0);
1098  owl_free(filename);
1099}
[9381782]1100
1101/* return auth string */
[09489b89]1102#ifdef HAVE_LIBZEPHYR
[1077891a]1103const char *owl_zephyr_get_authstr(const ZNotice_t *n)
[9381782]1104{
1105
[f12d199]1106  if (!n) return("UNKNOWN");
1107
1108  if (n->z_auth == ZAUTH_FAILED) {
[9381782]1109    return ("FAILED");
[f12d199]1110  } else if (n->z_auth == ZAUTH_NO) {
[9381782]1111    return ("NO");
[f12d199]1112  } else if (n->z_auth == ZAUTH_YES) {
[9381782]1113    return ("YES");
1114  } else {
1115    return ("UNKNOWN");
[f12d199]1116  }           
[9381782]1117}
[09489b89]1118#else
[1077891a]1119const char *owl_zephyr_get_authstr(const void *n)
[09489b89]1120{
1121  return("");
1122}
1123#endif
[9381782]1124
[2de4f20]1125/* Returns a buffer of subscriptions or an error message.  Caller must
1126 * free the return.
[09489b89]1127 */
[c79a047]1128char *owl_zephyr_getsubs(void)
[09489b89]1129{
1130#ifdef HAVE_LIBZEPHYR
1131  int ret, num, i, one;
[ddacb9c]1132  int buffsize;
[09489b89]1133  ZSubscription_t sub;
[ddacb9c]1134  char *out;
[09489b89]1135  one=1;
1136
1137  ret=ZRetrieveSubscriptions(0, &num);
1138  if (ret==ZERR_TOOMANYSUBS) {
[c8735aa]1139    return(owl_strdup("Zephyr: too many subscriptions\n"));
[ddacb9c]1140  } else if (ret || (num <= 0)) {
[c8735aa]1141    return(owl_strdup("Zephyr: error retriving subscriptions\n"));
[09489b89]1142  }
1143
[ddacb9c]1144  buffsize = (num + 1) * 50;
1145  out=owl_malloc(buffsize);
[09489b89]1146  strcpy(out, "");
1147  for (i=0; i<num; i++) {
1148    if ((ret = ZGetSubscriptions(&sub, &one)) != ZERR_NONE) {
1149      owl_free(out);
1150      ZFlushSubscriptions();
1151      out=owl_strdup("Error while getting subscriptions\n");
1152      return(out);
1153    } else {
[ddacb9c]1154      int tmpbufflen;
1155      char *tmpbuff;
1156      tmpbuff = owl_sprintf("<%s,%s,%s>\n%s", sub.zsub_class, sub.zsub_classinst, sub.zsub_recipient, out);
1157      tmpbufflen = strlen(tmpbuff) + 1;
1158      if (tmpbufflen > buffsize) {
1159        char *out2;
1160        buffsize = tmpbufflen * 2;
1161        out2 = owl_realloc(out, buffsize);
1162        if (out2 == NULL) {
1163          owl_free(out);
1164          owl_free(tmpbuff);
1165          ZFlushSubscriptions();
1166          out=owl_strdup("Realloc error while getting subscriptions\n");
1167          return(out);   
1168        }
1169        out = out2;
1170      }
[09489b89]1171      strcpy(out, tmpbuff);
[ddacb9c]1172      owl_free(tmpbuff);
[09489b89]1173    }
1174  }
1175
1176  ZFlushSubscriptions();
1177  return(out);
1178#else
[12c35df]1179  return(owl_strdup("Zephyr not available"));
[09489b89]1180#endif
1181}
1182
[e19eb97]1183const char *owl_zephyr_get_variable(const char *var)
[09489b89]1184{
1185#ifdef HAVE_LIBZEPHYR
[712caac]1186  return(ZGetVariable(zstr(var)));
[09489b89]1187#else
1188  return("");
1189#endif
1190}
1191
[e19eb97]1192void owl_zephyr_set_locationinfo(const char *host, const char *val)
[09489b89]1193{
1194#ifdef HAVE_LIBZEPHYR
[712caac]1195  ZInitLocationInfo(zstr(host), zstr(val));
[09489b89]1196#endif
1197}
1198 
[e3d9c77]1199/* Strip a local realm fron the zephyr user name.
1200 * The caller must free the return
1201 */
[e19eb97]1202char *short_zuser(const char *in)
[e3d9c77]1203{
1204  char *out, *ptr;
1205
1206  out=owl_strdup(in);
1207  ptr=strchr(out, '@');
1208  if (ptr) {
1209    if (!strcasecmp(ptr+1, owl_zephyr_get_realm())) {
1210      *ptr='\0';
1211    }
1212  }
1213  return(out);
1214}
1215
1216/* Append a local realm to the zephyr user name if necessary.
1217 * The caller must free the return.
1218 */
[e19eb97]1219char *long_zuser(const char *in)
[e3d9c77]1220{
[1971b59]1221  if (strchr(in, '@')) {
1222    return(owl_strdup(in));
[e3d9c77]1223  }
[1971b59]1224  return(owl_sprintf("%s@%s", in, owl_zephyr_get_realm()));
[e3d9c77]1225}
1226
1227/* strip out the instance from a zsender's principal.  Preserves the
1228 * realm if present.  daemon.webzephyr is a special case.  The
1229 * caller must free the return
1230 */
[e19eb97]1231char *owl_zephyr_smartstripped_user(const char *in)
[e3d9c77]1232{
1233  char *ptr, *realm, *out;
1234
1235  out=owl_strdup(in);
1236
1237  /* bail immeaditly if we don't have to do any work */
[fa4562c]1238  ptr=strchr(out, '.');
1239  if (!strchr(out, '/') && !ptr) {
[e3d9c77]1240    /* no '/' and no '.' */
1241    return(out);
1242  }
[fa4562c]1243  if (ptr && strchr(out, '@') && (ptr > strchr(out, '@'))) {
[e3d9c77]1244    /* There's a '.' but it's in the realm */
1245    return(out);
1246  }
[fa4562c]1247  if (!strncasecmp(out, OWL_WEBZEPHYR_PRINCIPAL, strlen(OWL_WEBZEPHYR_PRINCIPAL))) {
[e3d9c77]1248    return(out);
1249  }
1250
[fa4562c]1251  /* remove the realm from out, but hold on to it */
[e3d9c77]1252  realm=strchr(out, '@');
1253  if (realm) realm[0]='\0';
1254
1255  /* strip */
1256  ptr=strchr(out, '.');
1257  if (!ptr) ptr=strchr(out, '/');
1258  ptr[0]='\0';
1259
1260  /* reattach the realm if we had one */
1261  if (realm) {
1262    strcat(out, "@");
1263    strcat(out, realm+1);
1264  }
1265
1266  return(out);
1267}
[5a95b69]1268
1269/* read the list of users in 'filename' as a .anyone file, and put the
1270 * names of the zephyr users in the list 'in'.  If 'filename' is NULL,
1271 * use the default .anyone file in the users home directory.  Returns
1272 * -1 on failure, 0 on success.
1273 */
[e19eb97]1274int owl_zephyr_get_anyone_list(owl_list *in, const char *filename)
[5a95b69]1275{
1276#ifdef HAVE_LIBZEPHYR
[b7ee89b]1277  char *ourfile, *tmp, *s = NULL;
[5a95b69]1278  FILE *f;
1279
[b7ee89b]1280  if (filename == NULL)
1281    ourfile = owl_sprintf("%s/.anyone", owl_global_get_homedir(&g));
1282  else
1283    ourfile = owl_strdup(filename);
1284
1285  f = fopen(ourfile, "r");
[5a95b69]1286  if (!f) {
1287    owl_function_error("Error opening file %s: %s", ourfile, strerror(errno) ? strerror(errno) : "");
1288    owl_free(ourfile);
[b7ee89b]1289    return -1;
[5a95b69]1290  }
[b7ee89b]1291  owl_free(ourfile);
[5a95b69]1292
[b7ee89b]1293  while (owl_getline_chomp(&s, f)) {
[5a95b69]1294    /* ignore comments, blank lines etc. */
[b7ee89b]1295    if (s[0] == '#' || s[0] == '\0')
1296      continue;
1297
1298    /* ignore from # on */
1299    tmp = strchr(s, '#');
1300    if (tmp)
1301      tmp[0] = '\0';
1302
1303    /* ignore from SPC */
1304    tmp = strchr(s, ' ');
1305    if (tmp)
1306      tmp[0] = '\0';
1307
1308    owl_list_append_element(in, long_zuser(s));
[5a95b69]1309  }
1310  fclose(f);
[b7ee89b]1311  return 0;
[5a95b69]1312#else
[b7ee89b]1313  return -1;
[5a95b69]1314#endif
1315}
[13a3c1db]1316
1317#ifdef HAVE_LIBZEPHYR
[f36cd97]1318void owl_zephyr_process_events(owl_dispatch *d) {
[13a3c1db]1319  int zpendcount=0;
1320  ZNotice_t notice;
1321  struct sockaddr_in from;
1322  owl_message *m=NULL;
1323
1324  while(owl_zephyr_zpending() && zpendcount < 20) {
1325    if (owl_zephyr_zpending()) {
1326      ZReceiveNotice(&notice, &from);
1327      zpendcount++;
1328
1329      /* is this an ack from a zephyr we sent? */
1330      if (owl_zephyr_notice_is_ack(&notice)) {
1331        owl_zephyr_handle_ack(&notice);
1332        continue;
1333      }
1334
1335      /* if it's a ping and we're not viewing pings then skip it */
1336      if (!owl_global_is_rxping(&g) && !strcasecmp(notice.z_opcode, "ping")) {
1337        continue;
1338      }
1339
1340      /* create the new message */
1341      m=owl_malloc(sizeof(owl_message));
1342      owl_message_create_from_znotice(m, &notice);
1343
1344      owl_global_messagequeue_addmsg(&g, m);
1345    }
1346  }
1347}
1348
1349#else
[f36cd97]1350void owl_zephyr_process_events(owl_dispatch *d) {
[13a3c1db]1351 
1352}
1353#endif
Note: See TracBrowser for help on using the repository browser.