source: message.c @ 0ff8fb57

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