source: message.c @ 7abfcf2

release-1.10release-1.9
Last change on this file since 7abfcf2 was 4ebbfbc, checked in by Jason Gross <jgross@mit.edu>, 13 years ago
Show the time zone in :info; replace ctime with strftime This fixes trac-#146. The new function owl_util_time_to_timestr takes a const struct tm * instead of a const time_t because, although we pass around time_t for just about everything else, this lets the caller decide whether to use localtime or gmtime (UTC). Note: I'm not sure that "%c" (which is locale-dependent) will always include the time zone. If not, or if we want a bit more consistency, we can switch to something like "%a %b %d %H:%M:%S %Y %Z" (like "Fri Jul 22 15:39:45 2011 EDT") or "%a %b %d, %Y %h:%M:%S %p %Z" (like "Fri Jul 22, 2011 03:39:45 PM 2011 EDT") or something. On linerva, "%c" seems to be equivalent to "%a %d %b %Y %h:%M:%S %p %Z" (like "Fri 22 Jul 2011 03:39:45 PM EDT").
  • 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 = owl_util_time_to_timestr(localtime(&m->time));
50
51  m->fmtext = NULL;
52}
53
54/* add the named attribute to the message.  If an attribute with the
55 * name already exists, replace the old value with the new value
56 */
57void owl_message_set_attribute(owl_message *m, const char *attrname, const char *attrvalue)
58{
59  int i;
60  owl_pair *p = NULL, *pair = NULL;
61
62  attrname = g_intern_string(attrname);
63
64  /* look for an existing pair with this key, */
65  for (i = 0; i < m->attributes->len; i++) {
66    p = m->attributes->pdata[i];
67    if (owl_pair_get_key(p) == attrname) {
68      g_free(owl_pair_get_value(p));
69      pair = p;
70      break;
71    }
72  }
73
74  if(pair ==  NULL) {
75    pair = g_new(owl_pair, 1);
76    owl_pair_create(pair, attrname, NULL);
77    g_ptr_array_add(m->attributes, pair);
78  }
79  owl_pair_set_value(pair, owl_validate_or_convert(attrvalue));
80}
81
82/* return the value associated with the named attribute, or NULL if
83 * the attribute does not exist
84 */
85const char *owl_message_get_attribute_value(const owl_message *m, const char *attrname)
86{
87  int i;
88  owl_pair *p;
89  GQuark quark;
90
91  quark = g_quark_try_string(attrname);
92  if (quark == 0)
93    /* don't bother inserting into string table */
94    return NULL;
95  attrname = g_quark_to_string(quark);
96
97  for (i = 0; i < m->attributes->len; i++) {
98    p = m->attributes->pdata[i];
99    if (owl_pair_get_key(p) == attrname) {
100      return(owl_pair_get_value(p));
101    }
102  }
103
104  /*
105  owl_function_debugmsg("No attribute %s found for message %i",
106                        attrname,
107                        owl_message_get_id(m));
108  */
109  return(NULL);
110}
111
112/* We cheat and indent it for now, since we really want this for
113 * the 'info' function.  Later there should just be a generic
114 * function to indent fmtext.
115 */
116void owl_message_attributes_tofmtext(const owl_message *m, owl_fmtext *fm) {
117  int i;
118  owl_pair *p;
119  char *buff, *tmpbuff;
120
121  owl_fmtext_init_null(fm);
122
123  for (i = 0; i < m->attributes->len; i++) {
124    p = m->attributes->pdata[i];
125
126    buff = g_strdup(owl_pair_get_value(p));
127    if (buff) {
128      tmpbuff = owl_text_indent(buff, 19, false);
129      g_free(buff);
130      buff = g_strdup_printf("  %-15.15s: %s\n", owl_pair_get_key(p), tmpbuff);
131      g_free(tmpbuff);
132    }
133
134    if(buff == NULL) {
135      buff = g_strdup_printf("  %-15.15s: %s\n", owl_pair_get_key(p), "<error>");
136      if(buff == NULL)
137        buff=g_strdup("   <error>\n");
138    }
139    owl_fmtext_append_normal(fm, buff);
140    g_free(buff);
141  }
142}
143
144void owl_message_invalidate_format(owl_message *m)
145{
146  if(m->fmtext) {
147    m->fmtext->message = NULL;
148    owl_fmtext_clear(&(m->fmtext->fmtext));
149    m->fmtext=NULL;
150  }
151}
152
153owl_fmtext *owl_message_get_fmtext(owl_message *m)
154{
155  owl_message_format(m);
156  return(&(m->fmtext->fmtext));
157}
158
159void owl_message_format(owl_message *m)
160{
161  const owl_style *s;
162  const owl_view *v;
163
164  if (!m->fmtext) {
165    m->fmtext = owl_message_next_fmtext();
166    m->fmtext->message = m;
167    /* for now we assume there's just the one view and use that style */
168    v=owl_global_get_current_view(&g);
169    s=owl_view_get_style(v);
170
171    owl_style_get_formattext(s, &(m->fmtext->fmtext), m);
172  }
173}
174
175void owl_message_set_class(owl_message *m, const char *class)
176{
177  owl_message_set_attribute(m, "class", class);
178}
179
180const char *owl_message_get_class(const owl_message *m)
181{
182  const char *class;
183
184  class=owl_message_get_attribute_value(m, "class");
185  if (!class) return("");
186  return(class);
187}
188
189void owl_message_set_instance(owl_message *m, const char *inst)
190{
191  owl_message_set_attribute(m, "instance", inst);
192}
193
194const char *owl_message_get_instance(const owl_message *m)
195{
196  const char *instance;
197
198  instance=owl_message_get_attribute_value(m, "instance");
199  if (!instance) return("");
200  return(instance);
201}
202
203void owl_message_set_sender(owl_message *m, const char *sender)
204{
205  owl_message_set_attribute(m, "sender", sender);
206}
207
208const char *owl_message_get_sender(const owl_message *m)
209{
210  const char *sender;
211
212  sender=owl_message_get_attribute_value(m, "sender");
213  if (!sender) return("");
214  return(sender);
215}
216
217void owl_message_set_zsig(owl_message *m, const char *zsig)
218{
219  owl_message_set_attribute(m, "zsig", zsig);
220}
221
222const char *owl_message_get_zsig(const owl_message *m)
223{
224  const char *zsig;
225
226  zsig=owl_message_get_attribute_value(m, "zsig");
227  if (!zsig) return("");
228  return(zsig);
229}
230
231void owl_message_set_recipient(owl_message *m, const char *recip)
232{
233  owl_message_set_attribute(m, "recipient", recip);
234}
235
236const char *owl_message_get_recipient(const owl_message *m)
237{
238  /* this is stupid for outgoing messages, we need to fix it. */
239
240  const char *recip;
241
242  recip=owl_message_get_attribute_value(m, "recipient");
243  if (!recip) return("");
244  return(recip);
245}
246
247void owl_message_set_realm(owl_message *m, const char *realm)
248{
249  owl_message_set_attribute(m, "realm", realm);
250}
251
252const char *owl_message_get_realm(const owl_message *m)
253{
254  const char *realm;
255 
256  realm=owl_message_get_attribute_value(m, "realm");
257  if (!realm) return("");
258  return(realm);
259}
260
261void owl_message_set_body(owl_message *m, const char *body)
262{
263  owl_message_set_attribute(m, "body", body);
264}
265
266const char *owl_message_get_body(const owl_message *m)
267{
268  const char *body;
269
270  body=owl_message_get_attribute_value(m, "body");
271  if (!body) return("");
272  return(body);
273}
274
275
276void owl_message_set_opcode(owl_message *m, const char *opcode)
277{
278  owl_message_set_attribute(m, "opcode", opcode);
279}
280
281const char *owl_message_get_opcode(const owl_message *m)
282{
283  const char *opcode;
284
285  opcode=owl_message_get_attribute_value(m, "opcode");
286  if (!opcode) return("");
287  return(opcode);
288}
289
290
291void owl_message_set_islogin(owl_message *m)
292{
293  owl_message_set_attribute(m, "loginout", "login");
294}
295
296
297void owl_message_set_islogout(owl_message *m)
298{
299  owl_message_set_attribute(m, "loginout", "logout");
300}
301
302int owl_message_is_loginout(const owl_message *m)
303{
304  const char *res;
305
306  res=owl_message_get_attribute_value(m, "loginout");
307  if (!res) return(0);
308  return(1);
309}
310
311int owl_message_is_login(const owl_message *m)
312{
313  const char *res;
314
315  res=owl_message_get_attribute_value(m, "loginout");
316  if (!res) return(0);
317  if (!strcmp(res, "login")) return(1);
318  return(0);
319}
320
321
322int owl_message_is_logout(const owl_message *m)
323{
324  const char *res;
325
326  res=owl_message_get_attribute_value(m, "loginout");
327  if (!res) return(0);
328  if (!strcmp(res, "logout")) return(1);
329  return(0);
330}
331
332void owl_message_set_isprivate(owl_message *m)
333{
334  owl_message_set_attribute(m, "isprivate", "true");
335}
336
337int owl_message_is_private(const owl_message *m)
338{
339  const char *res;
340
341  res=owl_message_get_attribute_value(m, "isprivate");
342  if (!res) return(0);
343  return !strcmp(res, "true");
344}
345
346const char *owl_message_get_timestr(const owl_message *m)
347{
348  if (m->timestr) return(m->timestr);
349  return("");
350}
351
352void owl_message_set_type_admin(owl_message *m)
353{
354  owl_message_set_attribute(m, "type", "admin");
355}
356
357void owl_message_set_type_loopback(owl_message *m)
358{
359  owl_message_set_attribute(m, "type", "loopback");
360}
361
362void owl_message_set_type_zephyr(owl_message *m)
363{
364  owl_message_set_attribute(m, "type", "zephyr");
365}
366
367void owl_message_set_type_aim(owl_message *m)
368{
369  owl_message_set_attribute(m, "type", "AIM");
370}
371
372void owl_message_set_type(owl_message *m, const char* type)
373{
374  owl_message_set_attribute(m, "type", type);
375}
376
377int owl_message_is_type(const owl_message *m, const char *type) {
378  const char * t = owl_message_get_attribute_value(m, "type");
379  if(!t) return 0;
380  return !strcasecmp(t, type);
381}
382                                               
383int owl_message_is_type_admin(const owl_message *m)
384{
385  return owl_message_is_type(m, "admin");
386}
387
388int owl_message_is_type_zephyr(const owl_message *m)
389{
390  return owl_message_is_type(m, "zephyr");
391}
392
393int owl_message_is_type_aim(const owl_message *m)
394{
395  return owl_message_is_type(m, "aim");
396}
397
398/* XXX TODO: deprecate this */
399int owl_message_is_type_jabber(const owl_message *m)
400{
401  return owl_message_is_type(m, "jabber");
402}
403
404int owl_message_is_type_loopback(const owl_message *m)
405{
406  return owl_message_is_type(m, "loopback");
407}
408
409int owl_message_is_pseudo(const owl_message *m)
410{
411  if (owl_message_get_attribute_value(m, "pseudo")) return(1);
412  return(0);
413}
414
415const char *owl_message_get_text(owl_message *m)
416{
417  owl_message_format(m);
418  return(owl_fmtext_get_text(&(m->fmtext->fmtext)));
419}
420
421void owl_message_set_direction_in(owl_message *m)
422{
423  m->direction=OWL_MESSAGE_DIRECTION_IN;
424}
425
426void owl_message_set_direction_out(owl_message *m)
427{
428  m->direction=OWL_MESSAGE_DIRECTION_OUT;
429}
430
431void owl_message_set_direction_none(owl_message *m)
432{
433  m->direction=OWL_MESSAGE_DIRECTION_NONE;
434}
435
436void owl_message_set_direction(owl_message *m, int direction)
437{
438  m->direction=direction;
439}
440
441int owl_message_is_direction_in(const owl_message *m)
442{
443  if (m->direction==OWL_MESSAGE_DIRECTION_IN) return(1);
444  return(0);
445}
446
447int owl_message_is_direction_out(const owl_message *m)
448{
449  if (m->direction==OWL_MESSAGE_DIRECTION_OUT) return(1);
450  return(0);
451}
452
453int owl_message_is_direction_none(const owl_message *m)
454{
455  if (m->direction==OWL_MESSAGE_DIRECTION_NONE) return(1);
456  return(0);
457}
458
459int owl_message_get_numlines(owl_message *m)
460{
461  if (m == NULL) return(0);
462  owl_message_format(m);
463  return(owl_fmtext_num_lines(&(m->fmtext->fmtext)));
464}
465
466void owl_message_mark_delete(owl_message *m)
467{
468  if (m == NULL) return;
469  m->delete=1;
470}
471
472void owl_message_unmark_delete(owl_message *m)
473{
474  if (m == NULL) return;
475  m->delete=0;
476}
477
478const char *owl_message_get_zwriteline(const owl_message *m)
479{
480  const char *z = owl_message_get_attribute_value(m, "zwriteline");
481  if (!z) return "";
482  return z;
483}
484
485void owl_message_set_zwriteline(owl_message *m, const char *line)
486{
487  owl_message_set_attribute(m, "zwriteline", line);
488}
489
490int owl_message_is_delete(const owl_message *m)
491{
492  if (m == NULL) return(0);
493  if (m->delete==1) return(1);
494  return(0);
495}
496
497#ifdef HAVE_LIBZEPHYR
498const ZNotice_t *owl_message_get_notice(const owl_message *m)
499{
500  return(&(m->notice));
501}
502#else
503void *owl_message_get_notice(const owl_message *m)
504{
505  return(NULL);
506}
507#endif
508
509void owl_message_set_hostname(owl_message *m, const char *hostname)
510{
511  m->hostname = g_intern_string(hostname);
512}
513
514const char *owl_message_get_hostname(const owl_message *m)
515{
516  return(m->hostname);
517}
518
519void owl_message_curs_waddstr(owl_message *m, WINDOW *win, int aline, int bline, int acol, int bcol, int fgcolor, int bgcolor)
520{
521  owl_fmtext a, b;
522
523  /* this will ensure that our cached copy is up to date */
524  owl_message_format(m);
525
526  owl_fmtext_init_null(&a);
527  owl_fmtext_init_null(&b);
528 
529  owl_fmtext_truncate_lines(&(m->fmtext->fmtext), aline, bline-aline, &a);
530  owl_fmtext_truncate_cols(&a, acol, bcol, &b);
531
532  owl_fmtext_curs_waddstr(&b, win, OWL_FMTEXT_ATTR_NONE, fgcolor, bgcolor);
533
534  owl_fmtext_cleanup(&a);
535  owl_fmtext_cleanup(&b);
536}
537
538int owl_message_is_personal(const owl_message *m)
539{
540  const owl_filter * f = owl_global_get_filter(&g, "personal");
541  if(!f) {
542      owl_function_error("personal filter is not defined");
543      return (0);
544  }
545  return owl_filter_message_match(f, m);
546}
547
548int owl_message_is_question(const owl_message *m)
549{
550  if(!owl_message_is_type_admin(m)) return 0;
551  if(owl_message_get_attribute_value(m, "question") != NULL) return 1;
552  return 0;
553}
554
555int owl_message_is_answered(const owl_message *m) {
556  const char *q;
557  if(!owl_message_is_question(m)) return 0;
558  q = owl_message_get_attribute_value(m, "question");
559  if(!q) return 0;
560  return !strcmp(q, "answered");
561}
562
563void owl_message_set_isanswered(owl_message *m) {
564  owl_message_set_attribute(m, "question", "answered");
565}
566
567int owl_message_is_mail(const owl_message *m)
568{
569  if (owl_message_is_type_zephyr(m)) {
570    if (!strcasecmp(owl_message_get_class(m), "mail") && owl_message_is_private(m)) {
571      return(1);
572    } else {
573      return(0);
574    }
575  }
576  return(0);
577}
578
579/* caller must free return value. */
580CALLER_OWN char *owl_message_get_cc(const owl_message *m)
581{
582  const char *cur;
583  char *out, *end;
584
585  cur = owl_message_get_body(m);
586  while (*cur && *cur==' ') cur++;
587  if (strncasecmp(cur, "cc:", 3)) return(NULL);
588  cur+=3;
589  while (*cur && *cur==' ') cur++;
590  out = g_strdup(cur);
591  end = strchr(out, '\n');
592  if (end) end[0] = '\0';
593  return(out);
594}
595
596/* caller must free return value */
597CALLER_OWN GList *owl_message_get_cc_without_recipient(const owl_message *m)
598{
599  char *cc, *shortuser, *recip;
600  const char *user;
601  GList *out = NULL;
602
603  cc = owl_message_get_cc(m);
604  if (cc == NULL)
605    return NULL;
606
607  recip = short_zuser(owl_message_get_recipient(m));
608
609  user = strtok(cc, " ");
610  while (user != NULL) {
611    shortuser = short_zuser(user);
612    if (strcasecmp(shortuser, recip) != 0) {
613      out = g_list_prepend(out, g_strdup(user));
614    }
615    g_free(shortuser);
616    user = strtok(NULL, " ");
617  }
618
619  g_free(recip);
620  g_free(cc);
621
622  return(out);
623}
624
625int owl_message_get_id(const owl_message *m)
626{
627  return(m->id);
628}
629
630const char *owl_message_get_type(const owl_message *m) {
631  const char * type = owl_message_get_attribute_value(m, "type");
632  if(!type) {
633    return "generic";
634  }
635  return type;
636}
637
638const char *owl_message_get_direction(const owl_message *m) {
639  switch (m->direction) {
640  case OWL_MESSAGE_DIRECTION_IN:
641    return("in");
642  case OWL_MESSAGE_DIRECTION_OUT:
643    return("out");
644  case OWL_MESSAGE_DIRECTION_NONE:
645    return("none");
646  default:
647    return("unknown");
648  }
649}
650
651int owl_message_parse_direction(const char *d) {
652  if(!strcmp(d, "in")) {
653    return OWL_MESSAGE_DIRECTION_IN;
654  } else if(!strcmp(d, "out")) {
655    return OWL_MESSAGE_DIRECTION_OUT;
656  } else {
657    return OWL_MESSAGE_DIRECTION_NONE;
658  }
659}
660
661
662const char *owl_message_get_login(const owl_message *m) {
663  if (owl_message_is_login(m)) {
664    return "login";
665  } else if (owl_message_is_logout(m)) {
666    return "logout";
667  } else {
668    return "none";
669  }
670}
671
672
673const char *owl_message_get_header(const owl_message *m) {
674  return owl_message_get_attribute_value(m, "adminheader");
675}
676
677/* return 1 if the message contains "string", 0 otherwise.  This is
678 * case insensitive because the functions it uses are
679 */
680int owl_message_search(owl_message *m, const owl_regex *re)
681{
682
683  owl_message_format(m); /* is this necessary? */
684 
685  return owl_fmtext_search(&(m->fmtext->fmtext), re, 0) >= 0;
686}
687
688
689/* if loginout == -1 it's a logout message
690 *                 0 it's not a login/logout message
691 *                 1 it's a login message
692 */
693void owl_message_create_aim(owl_message *m, const char *sender, const char *recipient, const char *text, int direction, int loginout)
694{
695  owl_message_init(m);
696  owl_message_set_body(m, text);
697  owl_message_set_sender(m, sender);
698  owl_message_set_recipient(m, recipient);
699  owl_message_set_type_aim(m);
700
701  if (direction==OWL_MESSAGE_DIRECTION_IN) {
702    owl_message_set_direction_in(m);
703  } else if (direction==OWL_MESSAGE_DIRECTION_OUT) {
704    owl_message_set_direction_out(m);
705  }
706
707  /* for now all messages that aren't loginout are private */
708  if (!loginout) {
709    owl_message_set_isprivate(m);
710  }
711
712  if (loginout==-1) {
713    owl_message_set_islogout(m);
714  } else if (loginout==1) {
715    owl_message_set_islogin(m);
716  }
717}
718
719void owl_message_create_admin(owl_message *m, const char *header, const char *text)
720{
721  owl_message_init(m);
722  owl_message_set_type_admin(m);
723  owl_message_set_body(m, text);
724  owl_message_set_attribute(m, "adminheader", header); /* just a hack for now */
725}
726
727/* caller should set the direction */
728void owl_message_create_loopback(owl_message *m, const char *text)
729{
730  owl_message_init(m);
731  owl_message_set_type_loopback(m);
732  owl_message_set_body(m, text);
733  owl_message_set_sender(m, "loopsender");
734  owl_message_set_recipient(m, "looprecip");
735  owl_message_set_isprivate(m);
736}
737
738void owl_message_save_ccs(owl_message *m) {
739  GList *cc;
740  char *tmp;
741
742  cc = owl_message_get_cc_without_recipient(m);
743
744  if (cc != NULL) {
745    GString *recips = g_string_new("");
746    cc = g_list_prepend(cc, short_zuser(owl_message_get_sender(m)));
747    cc = g_list_prepend(cc, short_zuser(owl_message_get_recipient(m)));
748    cc = g_list_sort(cc, (GCompareFunc)strcasecmp);
749
750    while(cc != NULL) {
751      /* Collapse any identical entries */
752      while (cc->next && strcasecmp(cc->data, cc->next->data) == 0) {
753        g_free(cc->data);
754        cc = g_list_delete_link(cc, cc);
755      }
756
757      tmp = short_zuser(cc->data);
758      g_string_append(recips, tmp);
759
760      g_free(tmp);
761      g_free(cc->data);
762      cc = g_list_delete_link(cc, cc);
763
764      if (cc)
765        g_string_append_c(recips, ' ');
766    }
767
768    owl_message_set_attribute(m, "zephyr_ccs", recips->str);
769    g_string_free(recips, true);
770  }
771}
772
773#ifdef HAVE_LIBZEPHYR
774void owl_message_create_from_znotice(owl_message *m, const ZNotice_t *n)
775{
776#ifdef ZNOTICE_SOCKADDR
777  char hbuf[NI_MAXHOST];
778#else /* !ZNOTICE_SOCKADDR */
779  struct hostent *hent;
780#endif /* ZNOTICE_SOCKADDR */
781  char *tmp, *tmp2;
782  int len;
783
784  owl_message_init(m);
785 
786  owl_message_set_type_zephyr(m);
787  owl_message_set_direction_in(m);
788 
789  /* first save the full notice */
790  m->notice = *n;
791
792  /* a little gross, we'll replace \r's with ' ' for now */
793  owl_zephyr_hackaway_cr(&(m->notice));
794 
795  /* save the time, we need to nuke the string saved by message_init */
796  if (m->timestr) g_free(m->timestr);
797  m->time = n->z_time.tv_sec;
798  m->timestr = owl_util_time_to_timestr(localtime(&m->time));
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.