source: message.c @ 8f5afcf

Last change on this file since 8f5afcf was 06af663, checked in by David Benjamin <davidben@mit.edu>, 13 years ago
Use g_build_filename instead of g_strdup_printf to build paths Saves us a duplicate slash in a few places, although most of the time it doesn't do anything. Eh, may as well use it and be more explicit or something. Also change the one existing call to g_build_path to g_build_filename instead. It's much less of a pain to use.
  • Property mode set to 100644
File size: 24.6 KB
Line 
1#include <stdlib.h>
2#include <unistd.h>
3#include <string.h>
4#include <sys/socket.h>
5#include <netdb.h>
6#include <sys/types.h>
7#include <sys/socket.h>
8#include <netinet/in.h>
9#include <arpa/inet.h>
10#include <time.h>
11#include "owl.h"
12#include "filterproc.h"
13
14static owl_fmtext_cache fmtext_cache[OWL_FMTEXT_CACHE_SIZE];
15static owl_fmtext_cache * fmtext_cache_next = fmtext_cache;
16
17void owl_message_init_fmtext_cache(void)
18{
19    int i;
20    for(i = 0; i < OWL_FMTEXT_CACHE_SIZE; i++) {
21        owl_fmtext_init_null(&(fmtext_cache[i].fmtext));
22        fmtext_cache[i].message = NULL;
23    }
24}
25
26static owl_fmtext_cache *owl_message_next_fmtext(void)
27{
28    owl_fmtext_cache * f = fmtext_cache_next;
29    if(fmtext_cache_next->message != NULL) {
30        owl_message_invalidate_format(fmtext_cache_next->message);
31    }
32    fmtext_cache_next++;
33    if(fmtext_cache_next - fmtext_cache == OWL_FMTEXT_CACHE_SIZE)
34        fmtext_cache_next = fmtext_cache;
35    return f;
36}
37
38void owl_message_init(owl_message *m)
39{
40  m->id=owl_global_get_nextmsgid(&g);
41  owl_message_set_direction_none(m);
42  m->delete=0;
43
44  owl_message_set_hostname(m, "");
45  m->attributes = g_ptr_array_new();
46 
47  /* save the time */
48  m->time=time(NULL);
49  m->timestr=g_strdup(ctime(&(m->time)));
50  m->timestr[strlen(m->timestr)-1]='\0';
51
52  m->fmtext = NULL;
53}
54
55/* add the named attribute to the message.  If an attribute with the
56 * name already exists, replace the old value with the new value
57 */
58void owl_message_set_attribute(owl_message *m, const char *attrname, const char *attrvalue)
59{
60  int i;
61  owl_pair *p = NULL, *pair = NULL;
62
63  attrname = g_intern_string(attrname);
64
65  /* look for an existing pair with this key, */
66  for (i = 0; i < m->attributes->len; i++) {
67    p = m->attributes->pdata[i];
68    if (owl_pair_get_key(p) == attrname) {
69      g_free(owl_pair_get_value(p));
70      pair = p;
71      break;
72    }
73  }
74
75  if(pair ==  NULL) {
76    pair = g_new(owl_pair, 1);
77    owl_pair_create(pair, attrname, NULL);
78    g_ptr_array_add(m->attributes, pair);
79  }
80  owl_pair_set_value(pair, owl_validate_or_convert(attrvalue));
81}
82
83/* return the value associated with the named attribute, or NULL if
84 * the attribute does not exist
85 */
86const char *owl_message_get_attribute_value(const owl_message *m, const char *attrname)
87{
88  int i;
89  owl_pair *p;
90  GQuark quark;
91
92  quark = g_quark_try_string(attrname);
93  if (quark == 0)
94    /* don't bother inserting into string table */
95    return NULL;
96  attrname = g_quark_to_string(quark);
97
98  for (i = 0; i < m->attributes->len; i++) {
99    p = m->attributes->pdata[i];
100    if (owl_pair_get_key(p) == attrname) {
101      return(owl_pair_get_value(p));
102    }
103  }
104
105  /*
106  owl_function_debugmsg("No attribute %s found for message %i",
107                        attrname,
108                        owl_message_get_id(m));
109  */
110  return(NULL);
111}
112
113/* We cheat and indent it for now, since we really want this for
114 * the 'info' function.  Later there should just be a generic
115 * function to indent fmtext.
116 */
117void owl_message_attributes_tofmtext(const owl_message *m, owl_fmtext *fm) {
118  int i;
119  owl_pair *p;
120  char *buff, *tmpbuff;
121
122  owl_fmtext_init_null(fm);
123
124  for (i = 0; i < m->attributes->len; i++) {
125    p = m->attributes->pdata[i];
126
127    tmpbuff = g_strdup(owl_pair_get_value(p));
128    g_strdelimit(tmpbuff, "\n", '~');
129    g_strdelimit(tmpbuff, "\r", '!');
130    buff = g_strdup_printf("  %-15.15s: %s\n", owl_pair_get_key(p), tmpbuff);
131    g_free(tmpbuff);
132
133    if(buff == NULL) {
134      buff = g_strdup_printf("  %-15.15s: %s\n", owl_pair_get_key(p), "<error>");
135      if(buff == NULL)
136        buff=g_strdup("   <error>\n");
137    }
138    owl_fmtext_append_normal(fm, buff);
139    g_free(buff);
140  }
141}
142
143void owl_message_invalidate_format(owl_message *m)
144{
145  if(m->fmtext) {
146    m->fmtext->message = NULL;
147    owl_fmtext_clear(&(m->fmtext->fmtext));
148    m->fmtext=NULL;
149  }
150}
151
152owl_fmtext *owl_message_get_fmtext(owl_message *m)
153{
154  owl_message_format(m);
155  return(&(m->fmtext->fmtext));
156}
157
158void owl_message_format(owl_message *m)
159{
160  const owl_style *s;
161  const owl_view *v;
162
163  if (!m->fmtext) {
164    m->fmtext = owl_message_next_fmtext();
165    m->fmtext->message = m;
166    /* for now we assume there's just the one view and use that style */
167    v=owl_global_get_current_view(&g);
168    s=owl_view_get_style(v);
169
170    owl_style_get_formattext(s, &(m->fmtext->fmtext), m);
171  }
172}
173
174void owl_message_set_class(owl_message *m, const char *class)
175{
176  owl_message_set_attribute(m, "class", class);
177}
178
179const char *owl_message_get_class(const owl_message *m)
180{
181  const char *class;
182
183  class=owl_message_get_attribute_value(m, "class");
184  if (!class) return("");
185  return(class);
186}
187
188void owl_message_set_instance(owl_message *m, const char *inst)
189{
190  owl_message_set_attribute(m, "instance", inst);
191}
192
193const char *owl_message_get_instance(const owl_message *m)
194{
195  const char *instance;
196
197  instance=owl_message_get_attribute_value(m, "instance");
198  if (!instance) return("");
199  return(instance);
200}
201
202void owl_message_set_sender(owl_message *m, const char *sender)
203{
204  owl_message_set_attribute(m, "sender", sender);
205}
206
207const char *owl_message_get_sender(const owl_message *m)
208{
209  const char *sender;
210
211  sender=owl_message_get_attribute_value(m, "sender");
212  if (!sender) return("");
213  return(sender);
214}
215
216void owl_message_set_zsig(owl_message *m, const char *zsig)
217{
218  owl_message_set_attribute(m, "zsig", zsig);
219}
220
221const char *owl_message_get_zsig(const owl_message *m)
222{
223  const char *zsig;
224
225  zsig=owl_message_get_attribute_value(m, "zsig");
226  if (!zsig) return("");
227  return(zsig);
228}
229
230void owl_message_set_recipient(owl_message *m, const char *recip)
231{
232  owl_message_set_attribute(m, "recipient", recip);
233}
234
235const char *owl_message_get_recipient(const owl_message *m)
236{
237  /* this is stupid for outgoing messages, we need to fix it. */
238
239  const char *recip;
240
241  recip=owl_message_get_attribute_value(m, "recipient");
242  if (!recip) return("");
243  return(recip);
244}
245
246void owl_message_set_realm(owl_message *m, const char *realm)
247{
248  owl_message_set_attribute(m, "realm", realm);
249}
250
251const char *owl_message_get_realm(const owl_message *m)
252{
253  const char *realm;
254 
255  realm=owl_message_get_attribute_value(m, "realm");
256  if (!realm) return("");
257  return(realm);
258}
259
260void owl_message_set_body(owl_message *m, const char *body)
261{
262  owl_message_set_attribute(m, "body", body);
263}
264
265const char *owl_message_get_body(const owl_message *m)
266{
267  const char *body;
268
269  body=owl_message_get_attribute_value(m, "body");
270  if (!body) return("");
271  return(body);
272}
273
274
275void owl_message_set_opcode(owl_message *m, const char *opcode)
276{
277  owl_message_set_attribute(m, "opcode", opcode);
278}
279
280const char *owl_message_get_opcode(const owl_message *m)
281{
282  const char *opcode;
283
284  opcode=owl_message_get_attribute_value(m, "opcode");
285  if (!opcode) return("");
286  return(opcode);
287}
288
289
290void owl_message_set_islogin(owl_message *m)
291{
292  owl_message_set_attribute(m, "loginout", "login");
293}
294
295
296void owl_message_set_islogout(owl_message *m)
297{
298  owl_message_set_attribute(m, "loginout", "logout");
299}
300
301int owl_message_is_loginout(const owl_message *m)
302{
303  const char *res;
304
305  res=owl_message_get_attribute_value(m, "loginout");
306  if (!res) return(0);
307  return(1);
308}
309
310int owl_message_is_login(const owl_message *m)
311{
312  const char *res;
313
314  res=owl_message_get_attribute_value(m, "loginout");
315  if (!res) return(0);
316  if (!strcmp(res, "login")) return(1);
317  return(0);
318}
319
320
321int owl_message_is_logout(const owl_message *m)
322{
323  const char *res;
324
325  res=owl_message_get_attribute_value(m, "loginout");
326  if (!res) return(0);
327  if (!strcmp(res, "logout")) return(1);
328  return(0);
329}
330
331void owl_message_set_isprivate(owl_message *m)
332{
333  owl_message_set_attribute(m, "isprivate", "true");
334}
335
336int owl_message_is_private(const owl_message *m)
337{
338  const char *res;
339
340  res=owl_message_get_attribute_value(m, "isprivate");
341  if (!res) return(0);
342  return !strcmp(res, "true");
343}
344
345const char *owl_message_get_timestr(const owl_message *m)
346{
347  if (m->timestr) return(m->timestr);
348  return("");
349}
350
351void owl_message_set_type_admin(owl_message *m)
352{
353  owl_message_set_attribute(m, "type", "admin");
354}
355
356void owl_message_set_type_loopback(owl_message *m)
357{
358  owl_message_set_attribute(m, "type", "loopback");
359}
360
361void owl_message_set_type_zephyr(owl_message *m)
362{
363  owl_message_set_attribute(m, "type", "zephyr");
364}
365
366void owl_message_set_type_aim(owl_message *m)
367{
368  owl_message_set_attribute(m, "type", "AIM");
369}
370
371void owl_message_set_type(owl_message *m, const char* type)
372{
373  owl_message_set_attribute(m, "type", type);
374}
375
376int owl_message_is_type(const owl_message *m, const char *type) {
377  const char * t = owl_message_get_attribute_value(m, "type");
378  if(!t) return 0;
379  return !strcasecmp(t, type);
380}
381                                               
382int owl_message_is_type_admin(const owl_message *m)
383{
384  return owl_message_is_type(m, "admin");
385}
386
387int owl_message_is_type_zephyr(const owl_message *m)
388{
389  return owl_message_is_type(m, "zephyr");
390}
391
392int owl_message_is_type_aim(const owl_message *m)
393{
394  return owl_message_is_type(m, "aim");
395}
396
397/* XXX TODO: deprecate this */
398int owl_message_is_type_jabber(const owl_message *m)
399{
400  return owl_message_is_type(m, "jabber");
401}
402
403int owl_message_is_type_loopback(const owl_message *m)
404{
405  return owl_message_is_type(m, "loopback");
406}
407
408int owl_message_is_pseudo(const owl_message *m)
409{
410  if (owl_message_get_attribute_value(m, "pseudo")) return(1);
411  return(0);
412}
413
414const char *owl_message_get_text(owl_message *m)
415{
416  owl_message_format(m);
417  return(owl_fmtext_get_text(&(m->fmtext->fmtext)));
418}
419
420void owl_message_set_direction_in(owl_message *m)
421{
422  m->direction=OWL_MESSAGE_DIRECTION_IN;
423}
424
425void owl_message_set_direction_out(owl_message *m)
426{
427  m->direction=OWL_MESSAGE_DIRECTION_OUT;
428}
429
430void owl_message_set_direction_none(owl_message *m)
431{
432  m->direction=OWL_MESSAGE_DIRECTION_NONE;
433}
434
435void owl_message_set_direction(owl_message *m, int direction)
436{
437  m->direction=direction;
438}
439
440int owl_message_is_direction_in(const owl_message *m)
441{
442  if (m->direction==OWL_MESSAGE_DIRECTION_IN) return(1);
443  return(0);
444}
445
446int owl_message_is_direction_out(const owl_message *m)
447{
448  if (m->direction==OWL_MESSAGE_DIRECTION_OUT) return(1);
449  return(0);
450}
451
452int owl_message_is_direction_none(const owl_message *m)
453{
454  if (m->direction==OWL_MESSAGE_DIRECTION_NONE) return(1);
455  return(0);
456}
457
458int owl_message_get_numlines(owl_message *m)
459{
460  if (m == NULL) return(0);
461  owl_message_format(m);
462  return(owl_fmtext_num_lines(&(m->fmtext->fmtext)));
463}
464
465void owl_message_mark_delete(owl_message *m)
466{
467  if (m == NULL) return;
468  m->delete=1;
469}
470
471void owl_message_unmark_delete(owl_message *m)
472{
473  if (m == NULL) return;
474  m->delete=0;
475}
476
477const char *owl_message_get_zwriteline(const owl_message *m)
478{
479  const char *z = owl_message_get_attribute_value(m, "zwriteline");
480  if (!z) return "";
481  return z;
482}
483
484void owl_message_set_zwriteline(owl_message *m, const char *line)
485{
486  owl_message_set_attribute(m, "zwriteline", line);
487}
488
489int owl_message_is_delete(const owl_message *m)
490{
491  if (m == NULL) return(0);
492  if (m->delete==1) return(1);
493  return(0);
494}
495
496#ifdef HAVE_LIBZEPHYR
497const ZNotice_t *owl_message_get_notice(const owl_message *m)
498{
499  return(&(m->notice));
500}
501#else
502void *owl_message_get_notice(const owl_message *m)
503{
504  return(NULL);
505}
506#endif
507
508void owl_message_set_hostname(owl_message *m, const char *hostname)
509{
510  m->hostname = g_intern_string(hostname);
511}
512
513const char *owl_message_get_hostname(const owl_message *m)
514{
515  return(m->hostname);
516}
517
518void owl_message_curs_waddstr(owl_message *m, WINDOW *win, int aline, int bline, int acol, int bcol, int fgcolor, int bgcolor)
519{
520  owl_fmtext a, b;
521
522  /* this will ensure that our cached copy is up to date */
523  owl_message_format(m);
524
525  owl_fmtext_init_null(&a);
526  owl_fmtext_init_null(&b);
527 
528  owl_fmtext_truncate_lines(&(m->fmtext->fmtext), aline, bline-aline, &a);
529  owl_fmtext_truncate_cols(&a, acol, bcol, &b);
530
531  owl_fmtext_curs_waddstr(&b, win, OWL_FMTEXT_ATTR_NONE, fgcolor, bgcolor);
532
533  owl_fmtext_cleanup(&a);
534  owl_fmtext_cleanup(&b);
535}
536
537int owl_message_is_personal(const owl_message *m)
538{
539  const owl_filter * f = owl_global_get_filter(&g, "personal");
540  if(!f) {
541      owl_function_error("personal filter is not defined");
542      return (0);
543  }
544  return owl_filter_message_match(f, m);
545}
546
547int owl_message_is_question(const owl_message *m)
548{
549  if(!owl_message_is_type_admin(m)) return 0;
550  if(owl_message_get_attribute_value(m, "question") != NULL) return 1;
551  return 0;
552}
553
554int owl_message_is_answered(const owl_message *m) {
555  const char *q;
556  if(!owl_message_is_question(m)) return 0;
557  q = owl_message_get_attribute_value(m, "question");
558  if(!q) return 0;
559  return !strcmp(q, "answered");
560}
561
562void owl_message_set_isanswered(owl_message *m) {
563  owl_message_set_attribute(m, "question", "answered");
564}
565
566int owl_message_is_mail(const owl_message *m)
567{
568  if (owl_message_is_type_zephyr(m)) {
569    if (!strcasecmp(owl_message_get_class(m), "mail") && owl_message_is_private(m)) {
570      return(1);
571    } else {
572      return(0);
573    }
574  }
575  return(0);
576}
577
578/* caller must free return value. */
579CALLER_OWN char *owl_message_get_cc(const owl_message *m)
580{
581  const char *cur;
582  char *out, *end;
583
584  cur = owl_message_get_body(m);
585  while (*cur && *cur==' ') cur++;
586  if (strncasecmp(cur, "cc:", 3)) return(NULL);
587  cur+=3;
588  while (*cur && *cur==' ') cur++;
589  out = g_strdup(cur);
590  end = strchr(out, '\n');
591  if (end) end[0] = '\0';
592  return(out);
593}
594
595/* caller must free return value */
596CALLER_OWN GList *owl_message_get_cc_without_recipient(const owl_message *m)
597{
598  char *cc, *shortuser, *recip;
599  const char *user;
600  GList *out = NULL;
601
602  cc = owl_message_get_cc(m);
603  if (cc == NULL)
604    return NULL;
605
606  recip = short_zuser(owl_message_get_recipient(m));
607
608  user = strtok(cc, " ");
609  while (user != NULL) {
610    shortuser = short_zuser(user);
611    if (strcasecmp(shortuser, recip) != 0) {
612      out = g_list_prepend(out, g_strdup(user));
613    }
614    g_free(shortuser);
615    user = strtok(NULL, " ");
616  }
617
618  g_free(recip);
619  g_free(cc);
620
621  return(out);
622}
623
624int owl_message_get_id(const owl_message *m)
625{
626  return(m->id);
627}
628
629const char *owl_message_get_type(const owl_message *m) {
630  const char * type = owl_message_get_attribute_value(m, "type");
631  if(!type) {
632    return "generic";
633  }
634  return type;
635}
636
637const char *owl_message_get_direction(const owl_message *m) {
638  switch (m->direction) {
639  case OWL_MESSAGE_DIRECTION_IN:
640    return("in");
641  case OWL_MESSAGE_DIRECTION_OUT:
642    return("out");
643  case OWL_MESSAGE_DIRECTION_NONE:
644    return("none");
645  default:
646    return("unknown");
647  }
648}
649
650int owl_message_parse_direction(const char *d) {
651  if(!strcmp(d, "in")) {
652    return OWL_MESSAGE_DIRECTION_IN;
653  } else if(!strcmp(d, "out")) {
654    return OWL_MESSAGE_DIRECTION_OUT;
655  } else {
656    return OWL_MESSAGE_DIRECTION_NONE;
657  }
658}
659
660
661const char *owl_message_get_login(const owl_message *m) {
662  if (owl_message_is_login(m)) {
663    return "login";
664  } else if (owl_message_is_logout(m)) {
665    return "logout";
666  } else {
667    return "none";
668  }
669}
670
671
672const char *owl_message_get_header(const owl_message *m) {
673  return owl_message_get_attribute_value(m, "adminheader");
674}
675
676/* return 1 if the message contains "string", 0 otherwise.  This is
677 * case insensitive because the functions it uses are
678 */
679int owl_message_search(owl_message *m, const owl_regex *re)
680{
681
682  owl_message_format(m); /* is this necessary? */
683 
684  return owl_fmtext_search(&(m->fmtext->fmtext), re, 0) >= 0;
685}
686
687
688/* if loginout == -1 it's a logout message
689 *                 0 it's not a login/logout message
690 *                 1 it's a login message
691 */
692void owl_message_create_aim(owl_message *m, const char *sender, const char *recipient, const char *text, int direction, int loginout)
693{
694  owl_message_init(m);
695  owl_message_set_body(m, text);
696  owl_message_set_sender(m, sender);
697  owl_message_set_recipient(m, recipient);
698  owl_message_set_type_aim(m);
699
700  if (direction==OWL_MESSAGE_DIRECTION_IN) {
701    owl_message_set_direction_in(m);
702  } else if (direction==OWL_MESSAGE_DIRECTION_OUT) {
703    owl_message_set_direction_out(m);
704  }
705
706  /* for now all messages that aren't loginout are private */
707  if (!loginout) {
708    owl_message_set_isprivate(m);
709  }
710
711  if (loginout==-1) {
712    owl_message_set_islogout(m);
713  } else if (loginout==1) {
714    owl_message_set_islogin(m);
715  }
716}
717
718void owl_message_create_admin(owl_message *m, const char *header, const char *text)
719{
720  owl_message_init(m);
721  owl_message_set_type_admin(m);
722  owl_message_set_body(m, text);
723  owl_message_set_attribute(m, "adminheader", header); /* just a hack for now */
724}
725
726/* caller should set the direction */
727void owl_message_create_loopback(owl_message *m, const char *text)
728{
729  owl_message_init(m);
730  owl_message_set_type_loopback(m);
731  owl_message_set_body(m, text);
732  owl_message_set_sender(m, "loopsender");
733  owl_message_set_recipient(m, "looprecip");
734  owl_message_set_isprivate(m);
735}
736
737void owl_message_save_ccs(owl_message *m) {
738  GList *cc;
739  char *tmp;
740
741  cc = owl_message_get_cc_without_recipient(m);
742
743  if (cc != NULL) {
744    GString *recips = g_string_new("");
745    cc = g_list_prepend(cc, short_zuser(owl_message_get_sender(m)));
746    cc = g_list_prepend(cc, short_zuser(owl_message_get_recipient(m)));
747    cc = g_list_sort(cc, (GCompareFunc)strcasecmp);
748
749    while(cc != NULL) {
750      /* Collapse any identical entries */
751      while (cc->next && strcasecmp(cc->data, cc->next->data) == 0) {
752        g_free(cc->data);
753        cc = g_list_delete_link(cc, cc);
754      }
755
756      tmp = short_zuser(cc->data);
757      g_string_append(recips, tmp);
758
759      g_free(tmp);
760      g_free(cc->data);
761      cc = g_list_delete_link(cc, cc);
762
763      if (cc)
764        g_string_append_c(recips, ' ');
765    }
766
767    owl_message_set_attribute(m, "zephyr_ccs", recips->str);
768    g_string_free(recips, true);
769  }
770}
771
772#ifdef HAVE_LIBZEPHYR
773void owl_message_create_from_znotice(owl_message *m, const ZNotice_t *n)
774{
775#ifdef ZNOTICE_SOCKADDR
776  char hbuf[NI_MAXHOST];
777#else /* !ZNOTICE_SOCKADDR */
778  struct hostent *hent;
779#endif /* ZNOTICE_SOCKADDR */
780  char *tmp, *tmp2;
781  int len;
782
783  owl_message_init(m);
784 
785  owl_message_set_type_zephyr(m);
786  owl_message_set_direction_in(m);
787 
788  /* first save the full notice */
789  m->notice = *n;
790
791  /* a little gross, we'll replace \r's with ' ' for now */
792  owl_zephyr_hackaway_cr(&(m->notice));
793 
794  /* save the time, we need to nuke the string saved by message_init */
795  if (m->timestr) g_free(m->timestr);
796  m->time=n->z_time.tv_sec;
797  m->timestr=g_strdup(ctime(&(m->time)));
798  m->timestr[strlen(m->timestr)-1]='\0';
799
800  /* set other info */
801  owl_message_set_sender(m, n->z_sender);
802  owl_message_set_class(m, n->z_class);
803  owl_message_set_instance(m, n->z_class_inst);
804  owl_message_set_recipient(m, n->z_recipient);
805  if (n->z_opcode) {
806    owl_message_set_opcode(m, n->z_opcode);
807  } else {
808    owl_message_set_opcode(m, "");
809  }
810  owl_message_set_zsig(m, owl_zephyr_get_zsig(n, &len));
811
812  owl_message_set_realm(m, zuser_realm(n->z_recipient));
813
814  /* Set the "isloginout" attribute if it's a login message */
815  if (!strcasecmp(n->z_class, "login") || !strcasecmp(n->z_class, OWL_WEBZEPHYR_CLASS)) {
816    if (!strcasecmp(n->z_opcode, "user_login") || !strcasecmp(n->z_opcode, "user_logout")) {
817      tmp=owl_zephyr_get_field(n, 1);
818      owl_message_set_attribute(m, "loginhost", tmp);
819      g_free(tmp);
820
821      tmp=owl_zephyr_get_field(n, 3);
822      owl_message_set_attribute(m, "logintty", tmp);
823      g_free(tmp);
824    }
825
826    if (!strcasecmp(n->z_opcode, "user_login")) {
827      owl_message_set_islogin(m);
828    } else if (!strcasecmp(n->z_opcode, "user_logout")) {
829      owl_message_set_islogout(m);
830    }
831  }
832
833 
834  /* set the "isprivate" attribute if it's a private zephyr.
835   ``private'' means recipient is non-empty and doesn't start wit
836   `@' */
837  if (*n->z_recipient && *n->z_recipient != '@') {
838    owl_message_set_isprivate(m);
839  }
840
841  /* set the "isauto" attribute if it's an autoreply */
842  if (!strcasecmp(n->z_message, "Automated reply:") ||
843      !strcasecmp(n->z_opcode, "auto")) {
844    owl_message_set_attribute(m, "isauto", "");
845  }
846
847  /* save the hostname */
848#ifdef ZNOTICE_SOCKADDR
849  owl_function_debugmsg("About to do getnameinfo");
850  if (getnameinfo(&n->z_sender_sockaddr.sa, sizeof(n->z_sender_sockaddr), hbuf, sizeof(hbuf), NULL, 0, 0) == 0)
851    owl_message_set_hostname(m, hbuf);
852#else /* !ZNOTICE_SOCKADDR */
853  owl_function_debugmsg("About to do gethostbyaddr");
854  hent = gethostbyaddr(&n->z_uid.zuid_addr, sizeof(n->z_uid.zuid_addr), AF_INET);
855  if (hent && hent->h_name)
856    owl_message_set_hostname(m, hent->h_name);
857  else
858    owl_message_set_hostname(m, inet_ntoa(n->z_sender_addr));
859#endif /* ZNOTICE_SOCKADDR */
860
861  /* set the body */
862  tmp=owl_zephyr_get_message(n, m);
863  if (owl_global_is_newlinestrip(&g)) {
864    tmp2=owl_util_stripnewlines(tmp);
865    owl_message_set_body(m, tmp2);
866    g_free(tmp2);
867  } else {
868    owl_message_set_body(m, tmp);
869  }
870  g_free(tmp);
871
872  /* if zcrypt is enabled try to decrypt the message */
873  if (owl_global_is_zcrypt(&g) && !strcasecmp(n->z_opcode, "crypt")) {
874    const char *argv[] = {
875      "zcrypt",
876      "-D",
877      "-c", owl_message_get_class(m),
878      "-i", owl_message_get_instance(m),
879      NULL
880    };
881    char *out;
882    int rv;
883    int status;
884    char *zcrypt;
885
886    zcrypt = g_build_filename(owl_get_bindir(), "zcrypt", NULL);
887
888    rv = call_filter(zcrypt, argv, owl_message_get_body(m), &out, &status);
889    g_free(zcrypt);
890
891    if(!rv && !status) {
892      int len = strlen(out);
893      if(len >= 8 && !strcmp(out + len - 8, "**END**\n")) {
894        out[len - 8] = 0;
895      }
896      owl_message_set_body(m, out);
897      g_free(out);
898    } else if(out) {
899      g_free(out);
900    }
901  }
902
903  owl_message_save_ccs(m);
904}
905#else
906void owl_message_create_from_znotice(owl_message *m, const void *n)
907{
908}
909#endif
910
911/* If 'direction' is '0' it is a login message, '1' is a logout message. */
912void owl_message_create_pseudo_zlogin(owl_message *m, int direction, const char *user, const char *host, const char *time, const char *tty)
913{
914  char *longuser;
915
916#ifdef HAVE_LIBZEPHYR
917  memset(&(m->notice), 0, sizeof(ZNotice_t));
918#endif
919 
920  longuser=long_zuser(user);
921 
922  owl_message_init(m);
923 
924  owl_message_set_type_zephyr(m);
925  owl_message_set_direction_in(m);
926
927  owl_message_set_attribute(m, "pseudo", "");
928  owl_message_set_attribute(m, "loginhost", host ? host : "");
929  owl_message_set_attribute(m, "logintty", tty ? tty : "");
930
931  owl_message_set_sender(m, longuser);
932  owl_message_set_class(m, "LOGIN");
933  owl_message_set_instance(m, longuser);
934  owl_message_set_recipient(m, "");
935  if (direction==0) {
936    owl_message_set_opcode(m, "USER_LOGIN");
937    owl_message_set_islogin(m);
938  } else if (direction==1) {
939    owl_message_set_opcode(m, "USER_LOGOUT");
940    owl_message_set_islogout(m);
941  }
942
943  owl_message_set_realm(m, zuser_realm(longuser));
944
945  owl_message_set_body(m, "<uninitialized>");
946
947  /* save the hostname */
948  owl_function_debugmsg("create_pseudo_login: host is %s", host ? host : "");
949  owl_message_set_hostname(m, host ? host : "");
950  g_free(longuser);
951}
952
953void owl_message_create_from_zwrite(owl_message *m, const owl_zwrite *z, const char *body, int recip_index)
954{
955  char *replyline;
956 
957  owl_message_init(m);
958
959  /* set things */
960  owl_message_set_direction_out(m);
961  owl_message_set_type_zephyr(m);
962  owl_message_set_sender(m, owl_zephyr_get_sender());
963  owl_message_set_class(m, owl_zwrite_get_class(z));
964  owl_message_set_instance(m, owl_zwrite_get_instance(z));
965  if (recip_index < owl_zwrite_get_numrecips(z)) {
966    char *zuser = owl_zwrite_get_recip_n_with_realm(z, recip_index);
967    char *longzuser = long_zuser(zuser);
968    owl_message_set_recipient(m, longzuser);
969    owl_message_set_realm(m, zuser_realm(longzuser));
970    g_free(longzuser);
971    g_free(zuser);
972  } else {
973    /* TODO: We should maybe munge this into the case above, as that comparison
974     * is a little overly clever. It's also not clear this codepath ever runs
975     * anyway. */
976    const char *realm = owl_zwrite_get_realm(z);
977    owl_message_set_realm(m, realm[0] ? realm : owl_zephyr_get_realm());
978  }
979  owl_message_set_opcode(m, owl_zwrite_get_opcode(z));
980
981  /* Although not strictly the zwriteline, anyone using the unsantized version
982   * of it probably has a bug. */
983  replyline = owl_zwrite_get_replyline(z, recip_index);
984  owl_message_set_zwriteline(m, replyline);
985  g_free(replyline);
986
987  owl_message_set_body(m, body);
988  owl_message_set_zsig(m, owl_zwrite_get_zsig(z));
989 
990  /* save the hostname */
991  owl_message_set_hostname(m, g_get_host_name());
992
993  /* set the "isprivate" attribute if it's a private zephyr. */
994  if (owl_zwrite_is_personal(z)) {
995    owl_message_set_isprivate(m);
996  }
997
998  owl_message_save_ccs(m);
999}
1000
1001void owl_message_cleanup(owl_message *m)
1002{
1003  int i;
1004  owl_pair *p;
1005#ifdef HAVE_LIBZEPHYR   
1006  if (owl_message_is_type_zephyr(m) && owl_message_is_direction_in(m)) {
1007    ZFreeNotice(&(m->notice));
1008  }
1009#endif
1010  if (m->timestr) g_free(m->timestr);
1011
1012  /* free all the attributes */
1013  for (i = 0; i < m->attributes->len; i++) {
1014    p = m->attributes->pdata[i];
1015    g_free(owl_pair_get_value(p));
1016    g_free(p);
1017  }
1018
1019  g_ptr_array_free(m->attributes, true);
1020 
1021  owl_message_invalidate_format(m);
1022}
1023
1024void owl_message_delete(owl_message *m)
1025{
1026  owl_message_cleanup(m);
1027  g_free(m);
1028}
Note: See TracBrowser for help on using the repository browser.