source: message.c @ f933403

barnowl_perlaimdebianowlrelease-1.10release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since f933403 was f1e629d, checked in by Erik Nygren <nygren@mit.edu>, 21 years ago
New API for perl message formatting functions. Legacy variables are still supported for owl::format_msg and owl::receive_msg, but these functions are now also passed an owl::Message object which contains methods for accessing the contents of the message. See perlwrap.pm (and docs TBD) for the available methods. *** WARNING: The exact API for owl::Message has *** not yet stabilized. Added "style" command for creating new styles. Usage: style <name> perl <function_name> Added support for "show styles". Changed global style table from list to dictionary. Changed AIM password prompt from "Password:" to "AIM Password:". Messages are reformatted after a window resize to allow styles to take into account the width of the window. When perl throws an error, the message is put in the msgwin if possible. Added perl functions for: owl::getcurmsg() -- returns an owl::Message object for the active message in the current view. owl::getnumcols() -- returns the column width of the window owl::zephyr_getrealm() -- returns the zephyr realm owl::zephyr_getsender() -- returns the zephyr sender Made owl::COMMAND("foo"); be syntactic sugar for owl::command("COMMAND foo"); *** Is this a good or bad idea? *** This feature may be taken out before release. Added perlwrap.pm to contain perl code to be compiled into the binary. This is transformed into perlwrap.c by encapsulate.pl. Renamed readconfig.c to perlconfig.c and changed variables accordingly. Minor bugfixes in cmd.c and commands.c
  • Property mode set to 100644
File size: 19.2 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  time_t t;
18
19  m->id=owl_global_get_nextmsgid(&g);
20  m->type=OWL_MESSAGE_TYPE_GENERIC;
21  owl_message_set_direction_none(m);
22  m->delete=0;
23  strcpy(m->hostname, "");
24  m->zwriteline=strdup("");
25  m->invalid_format=1;
26
27  owl_list_create(&(m->attributes));
28 
29  /* save the time */
30  t=time(NULL);
31  m->time=owl_strdup(ctime(&t));
32  m->time[strlen(m->time)-1]='\0';
33  owl_fmtext_init_null(&(m->fmtext));
34}
35
36void owl_message_set_attribute(owl_message *m, char *attrname, char *attrvalue)
37{
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
63char *owl_message_get_attribute_value(owl_message *m, char *attrname)
64{
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
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}
100
101void owl_message_invalidate_format(owl_message *m)
102{
103  m->invalid_format=1;
104}
105
106owl_fmtext *owl_message_get_fmtext(owl_message *m)
107{
108  owl_message_format(m);
109  return(&(m->fmtext));
110}
111
112void owl_message_format(owl_message *m)
113{
114  owl_style *s;
115  owl_view *v;
116
117  if (m->invalid_format) {
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
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  }
127}
128
129void owl_message_set_class(owl_message *m, char *class)
130{
131  owl_message_set_attribute(m, "class", class);
132}
133
134char *owl_message_get_class(owl_message *m)
135{
136  char *class;
137
138  class=owl_message_get_attribute_value(m, "class");
139  if (!class) return("");
140  return(class);
141}
142
143void owl_message_set_instance(owl_message *m, char *inst)
144{
145  owl_message_set_attribute(m, "instance", inst);
146}
147
148char *owl_message_get_instance(owl_message *m)
149{
150  char *instance;
151
152  instance=owl_message_get_attribute_value(m, "instance");
153  if (!instance) return("");
154  return(instance);
155}
156
157void owl_message_set_sender(owl_message *m, char *sender)
158{
159  owl_message_set_attribute(m, "sender", sender);
160}
161
162char *owl_message_get_sender(owl_message *m)
163{
164  char *sender;
165
166  sender=owl_message_get_attribute_value(m, "sender");
167  if (!sender) return("");
168  return(sender);
169}
170
171void owl_message_set_zsig(owl_message *m, char *zsig)
172{
173  owl_message_set_attribute(m, "zsig", zsig);
174}
175
176char *owl_message_get_zsig(owl_message *m)
177{
178  char *zsig;
179
180  zsig=owl_message_get_attribute_value(m, "zsig");
181  if (!zsig) return("");
182  return(zsig);
183}
184
185void owl_message_set_recipient(owl_message *m, char *recip)
186{
187  owl_message_set_attribute(m, "recipient", recip);
188}
189
190char *owl_message_get_recipient(owl_message *m)
191{
192  /* this is stupid for outgoing messages, we need to fix it. */
193
194  char *recip;
195
196  recip=owl_message_get_attribute_value(m, "recipient");
197  if (!recip) return("");
198  return(recip);
199}
200
201void owl_message_set_realm(owl_message *m, char *realm)
202{
203  owl_message_set_attribute(m, "realm", realm);
204}
205
206char *owl_message_get_realm(owl_message *m)
207{
208  char *realm;
209 
210  realm=owl_message_get_attribute_value(m, "realm");
211  if (!realm) return("");
212  return(realm);
213}
214
215void owl_message_set_body(owl_message *m, char *body)
216{
217  owl_message_set_attribute(m, "body", body);
218}
219
220char *owl_message_get_body(owl_message *m)
221{
222  char *body;
223
224  body=owl_message_get_attribute_value(m, "body");
225  if (!body) return("");
226  return(body);
227}
228
229
230void owl_message_set_opcode(owl_message *m, char *opcode)
231{
232  owl_message_set_attribute(m, "opcode", opcode);
233}
234
235char *owl_message_get_opcode(owl_message *m)
236{
237  char *opcode;
238
239  opcode=owl_message_get_attribute_value(m, "opcode");
240  if (!opcode) return("");
241  return(opcode);
242}
243
244
245void owl_message_set_islogin(owl_message *m)
246{
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");
254}
255
256int owl_message_is_loginout(owl_message *m)
257{
258  char *res;
259
260  res=owl_message_get_attribute_value(m, "loginout");
261  if (!res) return(0);
262  return(1);
263}
264
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
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
300char *owl_message_get_timestr(owl_message *m)
301{
302  return(m->time);
303}
304
305void owl_message_set_type_admin(owl_message *m)
306{
307  m->type=OWL_MESSAGE_TYPE_ADMIN;
308}
309
310void owl_message_set_type_zephyr(owl_message *m)
311{
312  m->type=OWL_MESSAGE_TYPE_ZEPHYR;
313}
314
315void owl_message_set_type_aim(owl_message *m)
316{
317  m->type=OWL_MESSAGE_TYPE_AIM;
318}
319                                               
320int owl_message_is_type_admin(owl_message *m)
321{
322  if (m->type==OWL_MESSAGE_TYPE_ADMIN) return(1);
323  return(0);
324}
325
326int owl_message_is_type_zephyr(owl_message *m)
327{
328  if (m->type==OWL_MESSAGE_TYPE_ZEPHYR) return(1);
329  return(0);
330}
331
332int owl_message_is_type_aim(owl_message *m)
333{
334  if (m->type==OWL_MESSAGE_TYPE_AIM) return(1);
335  return(0);
336}
337
338int owl_message_is_type_generic(owl_message *m)
339{
340  if (m->type==OWL_MESSAGE_TYPE_GENERIC) return(1);
341  return(0);
342}
343
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
356char *owl_message_get_text(owl_message *m)
357{
358  return(owl_fmtext_get_text(&(m->fmtext)));
359}
360
361void owl_message_set_direction_in(owl_message *m)
362{
363  m->direction=OWL_MESSAGE_DIRECTION_IN;
364}
365
366void owl_message_set_direction_out(owl_message *m)
367{
368  m->direction=OWL_MESSAGE_DIRECTION_OUT;
369}
370
371void owl_message_set_direction_none(owl_message *m)
372{
373  m->direction=OWL_MESSAGE_DIRECTION_NONE;
374}
375
376int owl_message_is_direction_in(owl_message *m)
377{
378  if (m->direction==OWL_MESSAGE_DIRECTION_IN) return(1);
379  return(0);
380}
381
382int owl_message_is_direction_out(owl_message *m)
383{
384  if (m->direction==OWL_MESSAGE_DIRECTION_OUT) return(1);
385  return(0);
386}
387
388int owl_message_is_direction_none(owl_message *m)
389{
390  if (m->direction==OWL_MESSAGE_DIRECTION_NONE) return(1);
391  return(0);
392}
393
394int owl_message_get_numlines(owl_message *m)
395{
396  if (m == NULL) return(0);
397  owl_message_format(m);
398  return(owl_fmtext_num_lines(&(m->fmtext)));
399}
400
401void owl_message_mark_delete(owl_message *m)
402{
403  if (m == NULL) return;
404  m->delete=1;
405}
406
407void owl_message_unmark_delete(owl_message *m)
408{
409  if (m == NULL) return;
410  m->delete=0;
411}
412
413char *owl_message_get_zwriteline(owl_message *m)
414{
415  return(m->zwriteline);
416}
417
418void owl_message_set_zwriteline(owl_message *m, char *line)
419{
420  m->zwriteline=strdup(line);
421}
422
423int owl_message_is_delete(owl_message *m)
424{
425  if (m == NULL) return(0);
426  if (m->delete==1) return(1);
427  return(0);
428}
429
430#ifdef HAVE_LIBZEPHYR
431ZNotice_t *owl_message_get_notice(owl_message *m)
432{
433  return(&(m->notice));
434}
435#else
436void *owl_message_get_notice(owl_message *m)
437{
438  return(NULL);
439}
440#endif
441
442char *owl_message_get_hostname(owl_message *m)
443{
444  return(m->hostname);
445}
446
447
448void owl_message_curs_waddstr(owl_message *m, WINDOW *win, int aline, int bline, int acol, int bcol, int color)
449{
450  owl_fmtext a, b;
451
452  /* this will ensure that our cached copy is up to date */
453  owl_message_format(m);
454
455  owl_fmtext_init_null(&a);
456  owl_fmtext_init_null(&b);
457 
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
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);
479    if (!strcasecmp(owl_message_get_recipient(m), owl_zephyr_get_sender()) ||
480        !strcasecmp(owl_message_get_sender(m), owl_zephyr_get_sender())) {
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)) {
490    if (!strcasecmp(owl_message_get_sender(m), owl_zephyr_get_sender())) {
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  }
504  return(0);
505}
506
507int owl_message_is_mail(owl_message *m)
508{
509  if (owl_message_is_type_zephyr(m)) {
510    if (!strcasecmp(owl_message_get_class(m), "mail") && owl_message_is_private(m)) {
511      return(1);
512    } else {
513      return(0);
514    }
515  }
516  return(0);
517}
518
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  }
528  return(0);
529}
530
531int owl_message_is_burningears(owl_message *m)
532{
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 */
537  if (owl_message_is_from_me(m) || owl_message_is_private(m)) return(0);
538
539  if (owl_message_is_type_zephyr(m)) {
540    strcpy(sender, owl_zephyr_get_sender());
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  }
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. */
556char *owl_message_get_cc(owl_message *m)
557{
558  char *cur, *out, *end;
559
560  cur = owl_message_get_body(m);
561  while (*cur && *cur==' ') cur++;
562  if (strncasecmp(cur, "cc:", 3)) return(NULL);
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
571int owl_message_get_id(owl_message *m)
572{
573  return(m->id);
574}
575
576char *owl_message_get_type(owl_message *m) {
577  switch (m->type) {
578  case OWL_MESSAGE_TYPE_ADMIN:
579    return("admin");
580  case OWL_MESSAGE_TYPE_ZEPHYR:
581    return("zephyr");
582  case OWL_MESSAGE_TYPE_GENERIC:
583    return("generic");
584  case OWL_MESSAGE_TYPE_AIM:
585    return("aim");
586  case OWL_MESSAGE_TYPE_JABBER:
587    return("jabber");
588  case OWL_MESSAGE_TYPE_ICQ:
589    return("icq");
590  case OWL_MESSAGE_TYPE_YAHOO:
591    return("yahoo");
592  case OWL_MESSAGE_TYPE_MSN:
593    return("msn");
594  default:
595    return("unknown");
596  }
597}
598
599char *owl_message_get_direction(owl_message *m) {
600  switch (m->direction) {
601  case OWL_MESSAGE_DIRECTION_IN:
602    return("in");
603  case OWL_MESSAGE_DIRECTION_OUT:
604    return("out");
605  case OWL_MESSAGE_DIRECTION_NONE:
606    return("none");
607  default:
608    return("unknown");
609  }
610}
611
612char *owl_message_get_login(owl_message *m) {
613  if (owl_message_is_login(m)) {
614    return "login";
615  } else if (owl_message_is_logout(m)) {
616    return "logout";
617  } else {
618    return "none";
619  }
620}
621
622char *owl_message_get_header(owl_message *m) {
623  return owl_message_get_attribute_value(m, "adminheader");
624}
625
626/* return 1 if the message contains "string", 0 otherwise.  This is
627 * case insensitive because the functions it uses are
628 */
629int owl_message_search(owl_message *m, char *string)
630{
631
632  owl_message_format(m); /* is this necessary? */
633 
634  return (owl_fmtext_search(&(m->fmtext), string));
635}
636
637
638/* if loginout == -1 it's a logout message
639 *                 0 it's not a login/logout message
640 *                 1 it's a login message
641 */
642void owl_message_create_aim(owl_message *m, char *sender, char *recipient, char *text, int direction, int loginout)
643{
644  owl_message_init(m);
645  owl_message_set_body(m, text);
646  owl_message_set_sender(m, sender);
647  owl_message_set_recipient(m, recipient);
648  owl_message_set_type_aim(m);
649
650  if (direction==OWL_MESSAGE_DIRECTION_IN) {
651    owl_message_set_direction_in(m);
652  } else if (direction==OWL_MESSAGE_DIRECTION_OUT) {
653    owl_message_set_direction_out(m);
654  }
655
656  /* for now all messages that aren't loginout are private */
657  if (!loginout) {
658    owl_message_set_isprivate(m);
659  }
660
661  if (loginout==-1) {
662    owl_message_set_islogout(m);
663  } else if (loginout==1) {
664    owl_message_set_islogin(m);
665  }
666}
667
668void owl_message_create_admin(owl_message *m, char *header, char *text)
669{
670  owl_message_init(m);
671  owl_message_set_type_admin(m);
672  owl_message_set_body(m, text);
673  owl_message_set_attribute(m, "adminheader", header); /* just a hack for now */
674}
675
676#ifdef HAVE_LIBZEPHYR
677void owl_message_create_from_znotice(owl_message *m, ZNotice_t *n)
678{
679  struct hostent *hent;
680  int k;
681  char *ptr, *tmp, *tmp2;
682
683  owl_message_init(m);
684 
685  owl_message_set_type_zephyr(m);
686  owl_message_set_direction_in(m);
687 
688  /* first save the full notice */
689  memcpy(&(m->notice), n, sizeof(ZNotice_t));
690
691  /* a little gross, we'll reaplace \r's with ' ' for now */
692  owl_zephyr_hackaway_cr(&(m->notice));
693 
694  m->delete=0;
695
696  /* set other info */
697  owl_message_set_sender(m, n->z_sender);
698  owl_message_set_class(m, n->z_class);
699  owl_message_set_instance(m, n->z_class_inst);
700  owl_message_set_recipient(m, n->z_recipient);
701  if (n->z_opcode) {
702    owl_message_set_opcode(m, n->z_opcode);
703  } else {
704    owl_message_set_opcode(m, "");
705  }
706  owl_message_set_zsig(m, n->z_message);
707
708  if ((ptr=strchr(n->z_recipient, '@'))!=NULL) {
709    owl_message_set_realm(m, ptr+1);
710  } else {
711    owl_message_set_realm(m, owl_zephyr_get_realm());
712  }
713
714  /* Set the "isloginout" attribute if it's a login message */
715  if (!strcasecmp(n->z_class, "login") || !strcasecmp(n->z_class, OWL_WEBZEPHYR_CLASS)) {
716    if (!strcasecmp(n->z_opcode, "user_login")) {
717      owl_message_set_islogin(m);
718    } else if (!strcasecmp(n->z_opcode, "user_logout")) {
719      owl_message_set_islogout(m);
720    }
721  }
722
723  /* is the "isprivate" attribute if it's a private zephyr */
724  if (!strcasecmp(n->z_recipient, owl_zephyr_get_sender())) {
725    owl_message_set_isprivate(m);
726  }
727
728  m->zwriteline=strdup("");
729
730  /* set the body */
731  ptr=owl_zephyr_get_message(n, &k);
732  tmp=owl_malloc(k+10);
733  memcpy(tmp, ptr, k);
734  tmp[k]='\0';
735  if (owl_global_is_newlinestrip(&g)) {
736    tmp2=owl_util_stripnewlines(tmp);
737    owl_message_set_body(m, tmp2);
738    owl_free(tmp2);
739  } else {
740    owl_message_set_body(m, tmp);
741  }
742  owl_free(tmp);
743
744#ifdef OWL_ENABLE_ZCRYPT
745  /* if zcrypt is enabled try to decrypt the message */
746  if (owl_global_is_zcrypt(&g) && !strcasecmp(n->z_opcode, "crypt")) {
747    char *out;
748    int ret;
749
750    out=owl_malloc(strlen(owl_message_get_body(m))*16+20);
751    ret=owl_zcrypt_decrypt(out, owl_message_get_body(m), owl_message_get_class(m), owl_message_get_instance(m));
752    if (ret==0) {
753      owl_message_set_body(m, out);
754    } else {
755      owl_free(out);
756    }
757  }
758#endif 
759
760  /* save the hostname */
761  owl_function_debugmsg("About to do gethostbyaddr");
762  hent=gethostbyaddr((char *) &(n->z_uid.zuid_addr), sizeof(n->z_uid.zuid_addr), AF_INET);
763  if (hent && hent->h_name) {
764    strcpy(m->hostname, hent->h_name);
765  } else {
766    strcpy(m->hostname, inet_ntoa(n->z_sender_addr));
767  }
768
769  /* save the time */
770  m->time=owl_strdup(ctime((time_t *) &n->z_time.tv_sec));
771  m->time[strlen(m->time)-1]='\0';
772}
773#else
774void owl_message_create_from_znotice(owl_message *m, void *n)
775{
776}
777#endif
778
779void owl_message_create_from_zwriteline(owl_message *m, char *line, char *body, char *zsig)
780{
781  owl_zwrite z;
782  int ret;
783 
784  owl_message_init(m);
785
786  /* create a zwrite for the purpose of filling in other message fields */
787  owl_zwrite_create_from_line(&z, line);
788
789  /* set things */
790  owl_message_set_direction_out(m);
791  owl_message_set_type_zephyr(m);
792  owl_message_set_sender(m, owl_zephyr_get_sender());
793  owl_message_set_class(m, owl_zwrite_get_class(&z));
794  owl_message_set_instance(m, owl_zwrite_get_instance(&z));
795  if (owl_zwrite_get_numrecips(&z)>0) {
796    owl_message_set_recipient(m,
797                              long_zuser(owl_zwrite_get_recip_n(&z, 0))); /* only gets the first user, must fix */
798  }
799  owl_message_set_opcode(m, owl_zwrite_get_opcode(&z));
800  owl_message_set_realm(m, owl_zwrite_get_realm(&z)); /* also a hack, but not here */
801  m->zwriteline=owl_strdup(line);
802  owl_message_set_body(m, body);
803  owl_message_set_zsig(m, zsig);
804 
805  /* save the hostname */
806  ret=gethostname(m->hostname, MAXHOSTNAMELEN);
807  if (ret) {
808    strcpy(m->hostname, "localhost");
809  }
810
811  owl_zwrite_free(&z);
812}
813
814
815void owl_message_pretty_zsig(owl_message *m, char *buff)
816{
817  /* stick a one line version of the zsig in buff */
818  char *ptr;
819
820  strcpy(buff, owl_message_get_zsig(m));
821  ptr=strchr(buff, '\n');
822  if (ptr) ptr[0]='\0';
823}
824
825void owl_message_free(owl_message *m)
826{
827  int i, j;
828  owl_pair *p;
829#ifdef HAVE_LIBZEPHYR   
830  if (owl_message_is_type_zephyr(m) && owl_message_is_direction_in(m)) {
831    ZFreeNotice(&(m->notice));
832  }
833#endif
834  if (m->time) owl_free(m->time);
835  if (m->zwriteline) owl_free(m->zwriteline);
836
837  /* free all the attributes */
838  j=owl_list_get_size(&(m->attributes));
839  for (i=0; i<j; i++) {
840    p=owl_list_get_element(&(m->attributes), i);
841    owl_free(owl_pair_get_key(p));
842    owl_free(owl_pair_get_value(p));
843    owl_free(p);
844  }
845
846  owl_list_free_simple(&(m->attributes));
847 
848  owl_fmtext_free(&(m->fmtext));
849}
850
851
Note: See TracBrowser for help on using the repository browser.