source: message.c @ 09489b89

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