source: message.c @ 776c4bb

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