source: message.c @ 5639bf2

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