source: message.c @ cd5adec

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