source: zephyr.c @ 4c7c21f

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