source: message.c @ cf02dd6

barnowl_perlaimdebianowlrelease-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since cf02dd6 was 25dd31a, checked in by James M. Kretchmar <kretch@mit.edu>, 18 years ago
Added a timestamp to the default style.
  • Property mode set to 100644
File size: 19.8 KB
RevLine 
[7d4fbcd]1#include <stdlib.h>
[b45293f]2#include <unistd.h>
[7d4fbcd]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
[1aee7d9]13static const char fileIdent[] = "$Id$";
14
[0ff8fb57]15void owl_message_init(owl_message *m)
16{
[7d4fbcd]17  m->id=owl_global_get_nextmsgid(&g);
[4b464a4]18  m->type=OWL_MESSAGE_TYPE_GENERIC;
19  owl_message_set_direction_none(m);
[7d4fbcd]20  m->delete=0;
21  strcpy(m->hostname, "");
22  m->zwriteline=strdup("");
[bd3f232]23  m->invalid_format=1;
[7d4fbcd]24
[d0d65df]25  owl_list_create(&(m->attributes));
26 
[7d4fbcd]27  /* save the time */
[25dd31a]28  m->time=time(NULL);
29  m->timestr=owl_strdup(ctime(&(m->time)));
30  m->timestr[strlen(m->timestr)-1]='\0';
31
32  /* initialize the fmtext */
[bd3f232]33  owl_fmtext_init_null(&(m->fmtext));
[4b464a4]34}
35
[0ff8fb57]36void owl_message_set_attribute(owl_message *m, char *attrname, char *attrvalue)
37{
[d0d65df]38  /* add the named attribute to the message.  If an attribute with the
39     name already exists, replace the old value with the new value */
40
41  int i, j;
42  owl_pair *p;
43
44  /* look for an existing pair with this key, and nuke the entry if
45     found */
46  j=owl_list_get_size(&(m->attributes));
47  for (i=0; i<j; i++) {
48    p=owl_list_get_element(&(m->attributes), i);
49    if (!strcmp(owl_pair_get_key(p), attrname)) {
50      owl_free(owl_pair_get_key(p));
51      owl_free(owl_pair_get_value(p));
52      owl_free(p);
53      owl_list_remove_element(&(m->attributes), i);
54      break;
55    }
56  }
57
58  p=owl_malloc(sizeof(owl_pair));
59  owl_pair_create(p, owl_strdup(attrname), owl_strdup(attrvalue));
60  owl_list_append_element(&(m->attributes), p);
61}
62
[0ff8fb57]63char *owl_message_get_attribute_value(owl_message *m, char *attrname)
64{
[d0d65df]65  /* return the value associated with the named attribute, or NULL if
66     the attribute does not exist */
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  }
[f4d0975]77
78  /*
79  owl_function_debugmsg("No attribute %s found for message %i",
80                        attrname,
81                        owl_message_get_id(m));
82  */
[d0d65df]83  return(NULL);
84}
85
[5789230]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}
[4b464a4]105
[bd3f232]106void owl_message_invalidate_format(owl_message *m)
107{
108  m->invalid_format=1;
109}
110
[0ff8fb57]111owl_fmtext *owl_message_get_fmtext(owl_message *m)
112{
[f14a7ee]113  owl_message_format(m);
114  return(&(m->fmtext));
115}
116
117void owl_message_format(owl_message *m)
118{
[bd3f232]119  owl_style *s;
[ef56a67]120  owl_view *v;
[bd3f232]121
122  if (m->invalid_format) {
[ef56a67]123    /* for now we assume there's jsut the one view and use that style */
124    v=owl_global_get_current_view(&g);
125    s=owl_view_get_style(v);
126
[bd3f232]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  }
[4b464a4]132}
133
[0ff8fb57]134void owl_message_set_class(owl_message *m, char *class)
135{
[d0d65df]136  owl_message_set_attribute(m, "class", class);
[4b464a4]137}
138
[0ff8fb57]139char *owl_message_get_class(owl_message *m)
140{
[d0d65df]141  char *class;
142
143  class=owl_message_get_attribute_value(m, "class");
144  if (!class) return("");
145  return(class);
[4b464a4]146}
147
[0ff8fb57]148void owl_message_set_instance(owl_message *m, char *inst)
149{
[d0d65df]150  owl_message_set_attribute(m, "instance", inst);
[4b464a4]151}
152
[0ff8fb57]153char *owl_message_get_instance(owl_message *m)
154{
[d0d65df]155  char *instance;
156
157  instance=owl_message_get_attribute_value(m, "instance");
158  if (!instance) return("");
159  return(instance);
[4b464a4]160}
161
[0ff8fb57]162void owl_message_set_sender(owl_message *m, char *sender)
163{
[d0d65df]164  owl_message_set_attribute(m, "sender", sender);
[4b464a4]165}
166
[0ff8fb57]167char *owl_message_get_sender(owl_message *m)
168{
[d0d65df]169  char *sender;
170
171  sender=owl_message_get_attribute_value(m, "sender");
172  if (!sender) return("");
173  return(sender);
[4b464a4]174}
175
[0ff8fb57]176void owl_message_set_zsig(owl_message *m, char *zsig)
177{
[d0d65df]178  owl_message_set_attribute(m, "zsig", zsig);
[b45293f]179}
180
[0ff8fb57]181char *owl_message_get_zsig(owl_message *m)
182{
[d0d65df]183  char *zsig;
184
185  zsig=owl_message_get_attribute_value(m, "zsig");
186  if (!zsig) return("");
187  return(zsig);
[b45293f]188}
189
[0ff8fb57]190void owl_message_set_recipient(owl_message *m, char *recip)
191{
[d0d65df]192  owl_message_set_attribute(m, "recipient", recip);
[4b464a4]193}
194
[0ff8fb57]195char *owl_message_get_recipient(owl_message *m)
196{
[4b464a4]197  /* this is stupid for outgoing messages, we need to fix it. */
[d0d65df]198
199  char *recip;
[0ff8fb57]200
201  recip=owl_message_get_attribute_value(m, "recipient");
[d0d65df]202  if (!recip) return("");
203  return(recip);
[4b464a4]204}
205
[0ff8fb57]206void owl_message_set_realm(owl_message *m, char *realm)
207{
[d0d65df]208  owl_message_set_attribute(m, "realm", realm);
[4b464a4]209}
210
[0ff8fb57]211char *owl_message_get_realm(owl_message *m)
212{
[d0d65df]213  char *realm;
214 
215  realm=owl_message_get_attribute_value(m, "realm");
216  if (!realm) return("");
217  return(realm);
218}
219
[0ff8fb57]220void owl_message_set_body(owl_message *m, char *body)
221{
[d0d65df]222  owl_message_set_attribute(m, "body", body);
223}
224
[0ff8fb57]225char *owl_message_get_body(owl_message *m)
226{
[d0d65df]227  char *body;
228
229  body=owl_message_get_attribute_value(m, "body");
230  if (!body) return("");
231  return(body);
[4b464a4]232}
233
[d0d65df]234
[0ff8fb57]235void owl_message_set_opcode(owl_message *m, char *opcode)
236{
[d0d65df]237  owl_message_set_attribute(m, "opcode", opcode);
[4b464a4]238}
239
[0ff8fb57]240char *owl_message_get_opcode(owl_message *m)
241{
[d0d65df]242  char *opcode;
243
244  opcode=owl_message_get_attribute_value(m, "opcode");
245  if (!opcode) return("");
246  return(opcode);
[4b464a4]247}
248
[5789230]249
[d559df9]250void owl_message_set_islogin(owl_message *m)
[5789230]251{
[d559df9]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");
[5789230]259}
260
261int owl_message_is_loginout(owl_message *m)
262{
263  char *res;
264
[d559df9]265  res=owl_message_get_attribute_value(m, "loginout");
[5789230]266  if (!res) return(0);
267  return(1);
268}
269
[d559df9]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
[5789230]291void owl_message_set_isprivate(owl_message *m)
292{
293  owl_message_set_attribute(m, "isprivate", "");
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
[0ff8fb57]305char *owl_message_get_timestr(owl_message *m)
306{
[25dd31a]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("??:??");
[4b464a4]321}
322
[0ff8fb57]323void owl_message_set_type_admin(owl_message *m)
324{
[4b464a4]325  m->type=OWL_MESSAGE_TYPE_ADMIN;
326}
327
[37eab7f]328void owl_message_set_type_loopback(owl_message *m)
329{
330  m->type=OWL_MESSAGE_TYPE_LOOPBACK;
331}
332
[0ff8fb57]333void owl_message_set_type_zephyr(owl_message *m)
334{
[4b464a4]335  m->type=OWL_MESSAGE_TYPE_ZEPHYR;
336}
[d09e5a1]337
[0ff8fb57]338void owl_message_set_type_aim(owl_message *m)
339{
[d09e5a1]340  m->type=OWL_MESSAGE_TYPE_AIM;
341}
[4b464a4]342                                               
[0ff8fb57]343int owl_message_is_type_admin(owl_message *m)
344{
[4b464a4]345  if (m->type==OWL_MESSAGE_TYPE_ADMIN) return(1);
346  return(0);
347}
348
[37eab7f]349int owl_message_is_type_loopback(owl_message *m)
350{
351  if (m->type==OWL_MESSAGE_TYPE_LOOPBACK) return(1);
352  return(0);
353}
354
[0ff8fb57]355int owl_message_is_type_zephyr(owl_message *m)
356{
[4b464a4]357  if (m->type==OWL_MESSAGE_TYPE_ZEPHYR) return(1);
358  return(0);
359}
360
[0ff8fb57]361int owl_message_is_type_aim(owl_message *m)
362{
[d09e5a1]363  if (m->type==OWL_MESSAGE_TYPE_AIM) return(1);
364  return(0);
365}
366
[0ff8fb57]367int owl_message_is_type_generic(owl_message *m)
368{
[4b464a4]369  if (m->type==OWL_MESSAGE_TYPE_GENERIC) return(1);
370  return(0);
371}
372
[0ff8fb57]373char *owl_message_get_text(owl_message *m)
374{
[4b464a4]375  return(owl_fmtext_get_text(&(m->fmtext)));
376}
377
[0ff8fb57]378void owl_message_set_direction_in(owl_message *m)
379{
[4b464a4]380  m->direction=OWL_MESSAGE_DIRECTION_IN;
381}
382
[0ff8fb57]383void owl_message_set_direction_out(owl_message *m)
384{
[4b464a4]385  m->direction=OWL_MESSAGE_DIRECTION_OUT;
386}
387
[0ff8fb57]388void owl_message_set_direction_none(owl_message *m)
389{
[4b464a4]390  m->direction=OWL_MESSAGE_DIRECTION_NONE;
391}
392
[0ff8fb57]393int owl_message_is_direction_in(owl_message *m)
394{
[4b464a4]395  if (m->direction==OWL_MESSAGE_DIRECTION_IN) return(1);
396  return(0);
397}
398
[0ff8fb57]399int owl_message_is_direction_out(owl_message *m)
400{
[4b464a4]401  if (m->direction==OWL_MESSAGE_DIRECTION_OUT) return(1);
402  return(0);
403}
404
[0ff8fb57]405int owl_message_is_direction_none(owl_message *m)
406{
[4b464a4]407  if (m->direction==OWL_MESSAGE_DIRECTION_NONE) return(1);
408  return(0);
409}
410
[0ff8fb57]411int owl_message_get_numlines(owl_message *m)
412{
[4b464a4]413  if (m == NULL) return(0);
[f14a7ee]414  owl_message_format(m);
[4b464a4]415  return(owl_fmtext_num_lines(&(m->fmtext)));
416}
417
[0ff8fb57]418void owl_message_mark_delete(owl_message *m)
419{
[4b464a4]420  if (m == NULL) return;
421  m->delete=1;
422}
423
[0ff8fb57]424void owl_message_unmark_delete(owl_message *m)
425{
[4b464a4]426  if (m == NULL) return;
427  m->delete=0;
428}
429
[0ff8fb57]430char *owl_message_get_zwriteline(owl_message *m)
431{
[4b464a4]432  return(m->zwriteline);
433}
434
[0ff8fb57]435void owl_message_set_zwriteline(owl_message *m, char *line)
436{
[4b464a4]437  m->zwriteline=strdup(line);
438}
439
[0ff8fb57]440int owl_message_is_delete(owl_message *m)
441{
[4b464a4]442  if (m == NULL) return(0);
443  if (m->delete==1) return(1);
444  return(0);
445}
446
[be0a79f]447#ifdef HAVE_LIBZEPHYR
[0ff8fb57]448ZNotice_t *owl_message_get_notice(owl_message *m)
449{
[4b464a4]450  return(&(m->notice));
451}
[09489b89]452#else
453void *owl_message_get_notice(owl_message *m)
454{
455  return(NULL);
456}
[be0a79f]457#endif
[4b464a4]458
[0ff8fb57]459char *owl_message_get_hostname(owl_message *m)
460{
[4b464a4]461  return(m->hostname);
462}
463
[0ff8fb57]464void owl_message_curs_waddstr(owl_message *m, WINDOW *win, int aline, int bline, int acol, int bcol, int color)
465{
[4b464a4]466  owl_fmtext a, b;
467
[bd3f232]468  /* this will ensure that our cached copy is up to date */
[f14a7ee]469  owl_message_format(m);
[bd3f232]470
[af2ca19]471  owl_fmtext_init_null(&a);
472  owl_fmtext_init_null(&b);
473 
[4b464a4]474  owl_fmtext_truncate_lines(&(m->fmtext), aline, bline-aline+1, &a);
475  owl_fmtext_truncate_cols(&a, acol, bcol, &b);
476  if (color!=OWL_COLOR_DEFAULT) {
477    owl_fmtext_colorize(&b, color);
478  }
479
480  if (owl_global_is_search_active(&g)) {
481    owl_fmtext_search_and_highlight(&b, owl_global_get_search_string(&g));
482  }
483     
484  owl_fmtext_curs_waddstr(&b, win);
485
486  owl_fmtext_free(&a);
487  owl_fmtext_free(&b);
488}
489
[0ff8fb57]490int owl_message_is_personal(owl_message *m)
491{
492  if (owl_message_is_type_zephyr(m)) {
493    if (strcasecmp(owl_message_get_class(m), "message")) return(0);
494    if (strcasecmp(owl_message_get_instance(m), "personal")) return(0);
[09489b89]495    if (!strcasecmp(owl_message_get_recipient(m), owl_zephyr_get_sender()) ||
496        !strcasecmp(owl_message_get_sender(m), owl_zephyr_get_sender())) {
[0ff8fb57]497      return(1);
498    }
499  }
500  return(0);
501}
502
503int owl_message_is_from_me(owl_message *m)
504{
505  if (owl_message_is_type_zephyr(m)) {
[09489b89]506    if (!strcasecmp(owl_message_get_sender(m), owl_zephyr_get_sender())) {
[0ff8fb57]507      return(1);
508    } else {
509      return(0);
510    }
511  } else if (owl_message_is_type_aim(m)) {
512    if (!strcasecmp(owl_message_get_sender(m), owl_global_get_aim_screenname(&g))) {
513      return(1);
514    } else {
515      return(0);
516    }
517  } else if (owl_message_is_type_admin(m)) {
518    return(0);
519  }
[4b464a4]520  return(0);
521}
522
[0ff8fb57]523int owl_message_is_mail(owl_message *m)
524{
525  if (owl_message_is_type_zephyr(m)) {
[5789230]526    if (!strcasecmp(owl_message_get_class(m), "mail") && owl_message_is_private(m)) {
[0ff8fb57]527      return(1);
528    } else {
529      return(0);
530    }
[4b464a4]531  }
532  return(0);
533}
534
[0ff8fb57]535int owl_message_is_ping(owl_message *m)
536{
537  if (owl_message_is_type_zephyr(m)) {
538    if (!strcasecmp(owl_message_get_opcode(m), "ping")) {
539      return(1);
540    } else {
541      return(0);
542    }
543  }
[4b464a4]544  return(0);
545}
546
[0ff8fb57]547int owl_message_is_burningears(owl_message *m)
548{
[4b464a4]549  /* we should add a global to cache the short zsender */
550  char sender[LINE], *ptr;
551
552  /* if the message is from us or to us, it doesn't count */
[5789230]553  if (owl_message_is_from_me(m) || owl_message_is_private(m)) return(0);
[0ff8fb57]554
555  if (owl_message_is_type_zephyr(m)) {
[09489b89]556    strcpy(sender, owl_zephyr_get_sender());
[0ff8fb57]557    ptr=strchr(sender, '@');
558    if (ptr) *ptr='\0';
559  } else if (owl_message_is_type_aim(m)) {
560    strcpy(sender, owl_global_get_aim_screenname(&g));
561  } else {
562    return(0);
563  }
[4b464a4]564
565  if (stristr(owl_message_get_body(m), sender)) {
566    return(1);
567  }
568  return(0);
569}
570
571/* caller must free return value. */
[0ff8fb57]572char *owl_message_get_cc(owl_message *m)
573{
[4b464a4]574  char *cur, *out, *end;
575
576  cur = owl_message_get_body(m);
577  while (*cur && *cur==' ') cur++;
[985f85b]578  if (strncasecmp(cur, "cc:", 3)) return(NULL);
[4b464a4]579  cur+=3;
580  while (*cur && *cur==' ') cur++;
581  out = owl_strdup(cur);
582  end = strchr(out, '\n');
583  if (end) end[0] = '\0';
584  return(out);
585}
586
[0ff8fb57]587int owl_message_get_id(owl_message *m)
588{
[4b464a4]589  return(m->id);
590}
[bd3f232]591
[f1e629d]592char *owl_message_get_type(owl_message *m) {
593  switch (m->type) {
594  case OWL_MESSAGE_TYPE_ADMIN:
595    return("admin");
596  case OWL_MESSAGE_TYPE_ZEPHYR:
597    return("zephyr");
598  case OWL_MESSAGE_TYPE_GENERIC:
599    return("generic");
600  case OWL_MESSAGE_TYPE_AIM:
601    return("aim");
602  case OWL_MESSAGE_TYPE_JABBER:
603    return("jabber");
604  case OWL_MESSAGE_TYPE_ICQ:
605    return("icq");
606  case OWL_MESSAGE_TYPE_YAHOO:
607    return("yahoo");
608  case OWL_MESSAGE_TYPE_MSN:
609    return("msn");
[37eab7f]610  case OWL_MESSAGE_TYPE_LOOPBACK:
611    return("loopback");
[f1e629d]612  default:
613    return("unknown");
614  }
615}
616
617char *owl_message_get_direction(owl_message *m) {
618  switch (m->direction) {
619  case OWL_MESSAGE_DIRECTION_IN:
620    return("in");
621  case OWL_MESSAGE_DIRECTION_OUT:
622    return("out");
623  case OWL_MESSAGE_DIRECTION_NONE:
624    return("none");
625  default:
626    return("unknown");
627  }
628}
629
630char *owl_message_get_login(owl_message *m) {
631  if (owl_message_is_login(m)) {
632    return "login";
633  } else if (owl_message_is_logout(m)) {
634    return "logout";
635  } else {
636    return "none";
637  }
638}
639
640char *owl_message_get_header(owl_message *m) {
641  return owl_message_get_attribute_value(m, "adminheader");
642}
643
[bd3f232]644/* return 1 if the message contains "string", 0 otherwise.  This is
645 * case insensitive because the functions it uses are
646 */
[0ff8fb57]647int owl_message_search(owl_message *m, char *string)
648{
[4b464a4]649
[f14a7ee]650  owl_message_format(m); /* is this necessary? */
[bd3f232]651 
[4b464a4]652  return (owl_fmtext_search(&(m->fmtext), string));
653}
654
655
[d559df9]656/* if loginout == -1 it's a logout message
657 *                 0 it's not a login/logout message
658 *                 1 it's a login message
659 */
660void owl_message_create_aim(owl_message *m, char *sender, char *recipient, char *text, int direction, int loginout)
[0ff8fb57]661{
[d09e5a1]662  owl_message_init(m);
663  owl_message_set_body(m, text);
664  owl_message_set_sender(m, sender);
[440ce01]665  owl_message_set_recipient(m, recipient);
[d09e5a1]666  owl_message_set_type_aim(m);
[3abf28b]667
[d559df9]668  if (direction==OWL_MESSAGE_DIRECTION_IN) {
669    owl_message_set_direction_in(m);
670  } else if (direction==OWL_MESSAGE_DIRECTION_OUT) {
671    owl_message_set_direction_out(m);
[3abf28b]672  }
673
[d559df9]674  /* for now all messages that aren't loginout are private */
675  if (!loginout) {
676    owl_message_set_isprivate(m);
677  }
[3abf28b]678
[d559df9]679  if (loginout==-1) {
680    owl_message_set_islogout(m);
681  } else if (loginout==1) {
682    owl_message_set_islogin(m);
683  }
[aa5f725]684}
685
[0ff8fb57]686void owl_message_create_admin(owl_message *m, char *header, char *text)
687{
[d0d65df]688  owl_message_init(m);
[4b464a4]689  owl_message_set_type_admin(m);
[d0d65df]690  owl_message_set_body(m, text);
[bd3f232]691  owl_message_set_attribute(m, "adminheader", header); /* just a hack for now */
[7d4fbcd]692}
693
[37eab7f]694/* caller should set the direction */
695void owl_message_create_loopback(owl_message *m, char *text)
696{
697  owl_message_init(m);
698  owl_message_set_type_loopback(m);
699  owl_message_set_body(m, text);
[eec69e1]700  owl_message_set_sender(m, "loopsender");
701  owl_message_set_recipient(m, "looprecip");
[37eab7f]702  owl_message_set_isprivate(m);
703}
704
[09489b89]705#ifdef HAVE_LIBZEPHYR
[0ff8fb57]706void owl_message_create_from_znotice(owl_message *m, ZNotice_t *n)
707{
[7d4fbcd]708  struct hostent *hent;
[c269e22]709  int k;
[d0d65df]710  char *ptr, *tmp, *tmp2;
[7d4fbcd]711
[d0d65df]712  owl_message_init(m);
713 
[4b464a4]714  owl_message_set_type_zephyr(m);
715  owl_message_set_direction_in(m);
[7d4fbcd]716 
717  /* first save the full notice */
718  memcpy(&(m->notice), n, sizeof(ZNotice_t));
719
[25dd31a]720  /* a little gross, we'll replace \r's with ' ' for now */
[7d4fbcd]721  owl_zephyr_hackaway_cr(&(m->notice));
722 
723  m->delete=0;
724
[25dd31a]725  /* save the time, we need to nuke the string saved by message_init */
726  if (m->timestr) {
727    owl_free(m->timestr);
728  }
729  m->time=n->z_time.tv_sec;
730  m->timestr=owl_strdup(ctime(&(m->time)));
731  m->timestr[strlen(m->timestr)-1]='\0';
732
[7d4fbcd]733  /* set other info */
[d0d65df]734  owl_message_set_sender(m, n->z_sender);
735  owl_message_set_class(m, n->z_class);
736  owl_message_set_instance(m, n->z_class_inst);
737  owl_message_set_recipient(m, n->z_recipient);
[7d4fbcd]738  if (n->z_opcode) {
[d0d65df]739    owl_message_set_opcode(m, n->z_opcode);
[7d4fbcd]740  } else {
[d0d65df]741    owl_message_set_opcode(m, "");
[7d4fbcd]742  }
[d0d65df]743  owl_message_set_zsig(m, n->z_message);
[7d4fbcd]744
745  if ((ptr=strchr(n->z_recipient, '@'))!=NULL) {
[d0d65df]746    owl_message_set_realm(m, ptr+1);
[7d4fbcd]747  } else {
[09489b89]748    owl_message_set_realm(m, owl_zephyr_get_realm());
[7d4fbcd]749  }
750
[5789230]751  /* Set the "isloginout" attribute if it's a login message */
[1d3e925]752  if (!strcasecmp(n->z_class, "login") || !strcasecmp(n->z_class, OWL_WEBZEPHYR_CLASS)) {
[d559df9]753    if (!strcasecmp(n->z_opcode, "user_login")) {
754      owl_message_set_islogin(m);
755    } else if (!strcasecmp(n->z_opcode, "user_logout")) {
756      owl_message_set_islogout(m);
757    }
[5789230]758  }
759
760  /* is the "isprivate" attribute if it's a private zephyr */
[09489b89]761  if (!strcasecmp(n->z_recipient, owl_zephyr_get_sender())) {
[5789230]762    owl_message_set_isprivate(m);
763  }
764
[7d4fbcd]765  m->zwriteline=strdup("");
766
[b45293f]767  /* set the body */
[7d4fbcd]768  ptr=owl_zephyr_get_message(n, &k);
[7e3e00a]769  tmp=owl_malloc(k+10);
770  memcpy(tmp, ptr, k);
771  tmp[k]='\0';
772  if (owl_global_is_newlinestrip(&g)) {
[d0d65df]773    tmp2=owl_util_stripnewlines(tmp);
774    owl_message_set_body(m, tmp2);
775    owl_free(tmp2);
[7e3e00a]776  } else {
[d0d65df]777    owl_message_set_body(m, tmp);
[7e3e00a]778  }
[ecd5dc5]779  owl_free(tmp);
[7d4fbcd]780
[c86a35c]781#ifdef OWL_ENABLE_ZCRYPT
[d309eb3]782  /* if zcrypt is enabled try to decrypt the message */
783  if (owl_global_is_zcrypt(&g) && !strcasecmp(n->z_opcode, "crypt")) {
784    char *out;
[c269e22]785    int ret;
[d309eb3]786
[d0d65df]787    out=owl_malloc(strlen(owl_message_get_body(m))*16+20);
[9ceee9d]788    ret=owl_zcrypt_decrypt(out, owl_message_get_body(m), owl_message_get_class(m), owl_message_get_instance(m));
[a15a84f]789    if (ret==0) {
[d0d65df]790      owl_message_set_body(m, out);
[a15a84f]791    } else {
792      owl_free(out);
793    }
[d309eb3]794  }
[c269e22]795#endif 
[d309eb3]796
[7d4fbcd]797  /* save the hostname */
[3a2daac]798  owl_function_debugmsg("About to do gethostbyaddr");
[7d4fbcd]799  hent=gethostbyaddr((char *) &(n->z_uid.zuid_addr), sizeof(n->z_uid.zuid_addr), AF_INET);
800  if (hent && hent->h_name) {
801    strcpy(m->hostname, hent->h_name);
802  } else {
803    strcpy(m->hostname, inet_ntoa(n->z_sender_addr));
804  }
805}
[09489b89]806#else
807void owl_message_create_from_znotice(owl_message *m, void *n)
808{
809}
810#endif
[7d4fbcd]811
[0ff8fb57]812void owl_message_create_from_zwriteline(owl_message *m, char *line, char *body, char *zsig)
813{
[b45293f]814  owl_zwrite z;
815  int ret;
816 
[d0d65df]817  owl_message_init(m);
[b45293f]818
819  /* create a zwrite for the purpose of filling in other message fields */
820  owl_zwrite_create_from_line(&z, line);
821
822  /* set things */
823  owl_message_set_direction_out(m);
824  owl_message_set_type_zephyr(m);
[09489b89]825  owl_message_set_sender(m, owl_zephyr_get_sender());
[8fec514]826  owl_message_set_class(m, owl_zwrite_get_class(&z));
827  owl_message_set_instance(m, owl_zwrite_get_instance(&z));
[9ceee9d]828  if (owl_zwrite_get_numrecips(&z)>0) {
829    owl_message_set_recipient(m,
830                              long_zuser(owl_zwrite_get_recip_n(&z, 0))); /* only gets the first user, must fix */
831  }
[8fec514]832  owl_message_set_opcode(m, owl_zwrite_get_opcode(&z));
[d0d65df]833  owl_message_set_realm(m, owl_zwrite_get_realm(&z)); /* also a hack, but not here */
[b45293f]834  m->zwriteline=owl_strdup(line);
[d0d65df]835  owl_message_set_body(m, body);
[8fec514]836  owl_message_set_zsig(m, zsig);
[b45293f]837 
838  /* save the hostname */
839  ret=gethostname(m->hostname, MAXHOSTNAMELEN);
840  if (ret) {
841    strcpy(m->hostname, "localhost");
842  }
843
844  owl_zwrite_free(&z);
845}
[7d4fbcd]846
[0ff8fb57]847void owl_message_pretty_zsig(owl_message *m, char *buff)
848{
[b45293f]849  /* stick a one line version of the zsig in buff */
[7d4fbcd]850  char *ptr;
851
[d0d65df]852  strcpy(buff, owl_message_get_zsig(m));
[b45293f]853  ptr=strchr(buff, '\n');
854  if (ptr) ptr[0]='\0';
[7d4fbcd]855}
856
[0ff8fb57]857void owl_message_free(owl_message *m)
858{
[d0d65df]859  int i, j;
860  owl_pair *p;
[09489b89]861#ifdef HAVE_LIBZEPHYR   
[4b464a4]862  if (owl_message_is_type_zephyr(m) && owl_message_is_direction_in(m)) {
[7d4fbcd]863    ZFreeNotice(&(m->notice));
864  }
[09489b89]865#endif
[25dd31a]866  if (m->timestr) owl_free(m->timestr);
[7d4fbcd]867  if (m->zwriteline) owl_free(m->zwriteline);
[d0d65df]868
869  /* free all the attributes */
870  j=owl_list_get_size(&(m->attributes));
871  for (i=0; i<j; i++) {
872    p=owl_list_get_element(&(m->attributes), i);
873    owl_free(owl_pair_get_key(p));
874    owl_free(owl_pair_get_value(p));
875    owl_free(p);
876  }
877
878  owl_list_free_simple(&(m->attributes));
[7d4fbcd]879 
880  owl_fmtext_free(&(m->fmtext));
881}
Note: See TracBrowser for help on using the repository browser.