source: message.c @ db90f03

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