source: message.c @ df0d93a

barnowl_perlaimdebianowlrelease-1.10release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since df0d93a was df0d93a, checked in by James M. Kretchmar <kretch@mit.edu>, 21 years ago
cleaned up the info funciton, put back additional data for outgoing messages
  • Property mode set to 100644
File size: 27.0 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_type_to_string(owl_message *m)
246{
247  if (m->type==OWL_MESSAGE_TYPE_ADMIN) return("admin");
248  if (m->type==OWL_MESSAGE_TYPE_GENERIC) return("generic");
249  if (m->type==OWL_MESSAGE_TYPE_ZEPHYR) return("zephyr");
250  if (m->type==OWL_MESSAGE_TYPE_AIM) return("aim");
251  if (m->type==OWL_MESSAGE_TYPE_JABBER) return("jabber");
252  if (m->type==OWL_MESSAGE_TYPE_ICQ) return("icq");
253  if (m->type==OWL_MESSAGE_TYPE_MSN) return("msn");
254  return("unknown");
255}
256
257
258char *owl_message_get_text(owl_message *m)
259{
260  return(owl_fmtext_get_text(&(m->fmtext)));
261}
262
263void owl_message_set_direction_in(owl_message *m)
264{
265  m->direction=OWL_MESSAGE_DIRECTION_IN;
266}
267
268void owl_message_set_direction_out(owl_message *m)
269{
270  m->direction=OWL_MESSAGE_DIRECTION_OUT;
271}
272
273void owl_message_set_direction_none(owl_message *m)
274{
275  m->direction=OWL_MESSAGE_DIRECTION_NONE;
276}
277
278int owl_message_is_direction_in(owl_message *m)
279{
280  if (m->direction==OWL_MESSAGE_DIRECTION_IN) return(1);
281  return(0);
282}
283
284int owl_message_is_direction_out(owl_message *m)
285{
286  if (m->direction==OWL_MESSAGE_DIRECTION_OUT) return(1);
287  return(0);
288}
289
290int owl_message_is_direction_none(owl_message *m)
291{
292  if (m->direction==OWL_MESSAGE_DIRECTION_NONE) return(1);
293  return(0);
294}
295
296int owl_message_get_numlines(owl_message *m)
297{
298  if (m == NULL) return(0);
299  return(owl_fmtext_num_lines(&(m->fmtext)));
300}
301
302void owl_message_mark_delete(owl_message *m)
303{
304  if (m == NULL) return;
305  m->delete=1;
306}
307
308void owl_message_unmark_delete(owl_message *m)
309{
310  if (m == NULL) return;
311  m->delete=0;
312}
313
314char *owl_message_get_zwriteline(owl_message *m)
315{
316  return(m->zwriteline);
317}
318
319void owl_message_set_zwriteline(owl_message *m, char *line)
320{
321  m->zwriteline=strdup(line);
322}
323
324int owl_message_is_delete(owl_message *m)
325{
326  if (m == NULL) return(0);
327  if (m->delete==1) return(1);
328  return(0);
329}
330
331ZNotice_t *owl_message_get_notice(owl_message *m)
332{
333  return(&(m->notice));
334}
335
336char *owl_message_get_hostname(owl_message *m)
337{
338  return(m->hostname);
339}
340
341
342void owl_message_curs_waddstr(owl_message *m, WINDOW *win, int aline, int bline, int acol, int bcol, int color)
343{
344  owl_fmtext a, b;
345
346  owl_fmtext_init_null(&a);
347  owl_fmtext_init_null(&b);
348 
349  owl_fmtext_truncate_lines(&(m->fmtext), aline, bline-aline+1, &a);
350  owl_fmtext_truncate_cols(&a, acol, bcol, &b);
351  if (color!=OWL_COLOR_DEFAULT) {
352    owl_fmtext_colorize(&b, color);
353  }
354
355  if (owl_global_is_search_active(&g)) {
356    owl_fmtext_search_and_highlight(&b, owl_global_get_search_string(&g));
357  }
358     
359  owl_fmtext_curs_waddstr(&b, win);
360
361  owl_fmtext_free(&a);
362  owl_fmtext_free(&b);
363}
364
365int owl_message_is_personal(owl_message *m)
366{
367  if (owl_message_is_type_zephyr(m)) {
368    if (strcasecmp(owl_message_get_class(m), "message")) return(0);
369    if (strcasecmp(owl_message_get_instance(m), "personal")) return(0);
370    if (!strcasecmp(owl_message_get_recipient(m), ZGetSender()) ||
371        !strcasecmp(owl_message_get_sender(m), ZGetSender())) {
372      return(1);
373    }
374  }
375  return(0);
376}
377
378/* true if the message is only intended for one recipient (me) */
379int owl_message_is_to_me(owl_message *m)
380{
381  if (owl_message_is_type_zephyr(m)) {
382    if (!strcasecmp(owl_message_get_recipient(m), ZGetSender())) {
383      return(1);
384    } else {
385      return(0);
386    }
387  } else if (owl_message_is_type_aim(m)) {
388    /* right now we don't support chat rooms */
389    return(1);
390  } else if (owl_message_is_type_admin(m)) {
391    return(1);
392  }
393  return(0);
394}
395
396
397int owl_message_is_from_me(owl_message *m)
398{
399  if (owl_message_is_type_zephyr(m)) {
400    if (!strcasecmp(owl_message_get_sender(m), ZGetSender())) {
401      return(1);
402    } else {
403      return(0);
404    }
405  } else if (owl_message_is_type_aim(m)) {
406    if (!strcasecmp(owl_message_get_sender(m), owl_global_get_aim_screenname(&g))) {
407      return(1);
408    } else {
409      return(0);
410    }
411  } else if (owl_message_is_type_admin(m)) {
412    return(0);
413  }
414  return(0);
415}
416
417int owl_message_is_mail(owl_message *m)
418{
419  if (owl_message_is_type_zephyr(m)) {
420    if (!strcasecmp(owl_message_get_class(m), "mail") && owl_message_is_to_me(m)) {
421      return(1);
422    } else {
423      return(0);
424    }
425  }
426  return(0);
427}
428
429int owl_message_is_ping(owl_message *m)
430{
431  if (owl_message_is_type_zephyr(m)) {
432    if (!strcasecmp(owl_message_get_opcode(m), "ping")) {
433      return(1);
434    } else {
435      return(0);
436    }
437  }
438  return(0);
439}
440
441int owl_message_is_login(owl_message *m)
442{
443  if (owl_message_is_type_zephyr(m)) {
444    if (!strcasecmp(owl_message_get_class(m), "login")) {
445      return(1);
446    } else {
447      return(0);
448    }
449  } else if (owl_message_is_type_aim(m)) {
450    /* deal with this once we can use buddy lists */
451    return(0);
452  }
453       
454  return(0);
455}
456
457int owl_message_is_burningears(owl_message *m)
458{
459  /* we should add a global to cache the short zsender */
460  char sender[LINE], *ptr;
461
462  /* if the message is from us or to us, it doesn't count */
463  if (owl_message_is_from_me(m) || owl_message_is_to_me(m)) return(0);
464
465  if (owl_message_is_type_zephyr(m)) {
466    strcpy(sender, ZGetSender());
467    ptr=strchr(sender, '@');
468    if (ptr) *ptr='\0';
469  } else if (owl_message_is_type_aim(m)) {
470    strcpy(sender, owl_global_get_aim_screenname(&g));
471  } else {
472    return(0);
473  }
474
475  if (stristr(owl_message_get_body(m), sender)) {
476    return(1);
477  }
478  return(0);
479}
480
481/* caller must free return value. */
482char *owl_message_get_cc(owl_message *m)
483{
484  char *cur, *out, *end;
485
486  cur = owl_message_get_body(m);
487  while (*cur && *cur==' ') cur++;
488  if (strncasecmp(cur, "cc:", 3)) return(NULL);
489  cur+=3;
490  while (*cur && *cur==' ') cur++;
491  out = owl_strdup(cur);
492  end = strchr(out, '\n');
493  if (end) end[0] = '\0';
494  return(out);
495}
496
497int owl_message_get_id(owl_message *m)
498{
499  return(m->id);
500}
501                                       
502int owl_message_search(owl_message *m, char *string)
503{
504  /* return 1 if the message contains "string", 0 otherwise.  This is
505   * case insensitive because the functions it uses are */
506
507  return (owl_fmtext_search(&(m->fmtext), string));
508}
509
510void owl_message_create(owl_message *m, char *header, char *text)
511{
512  char *indent;
513
514  owl_message_init(m);
515  owl_message_set_body(m, text);
516
517  indent=owl_malloc(strlen(text)+owl_text_num_lines(text)*OWL_MSGTAB+10);
518  owl_text_indent(indent, text, OWL_MSGTAB);
519  owl_fmtext_init_null(&(m->fmtext));
520  owl_fmtext_append_normal(&(m->fmtext), OWL_TABSTR);
521  owl_fmtext_append_ztext(&(m->fmtext), header);
522  owl_fmtext_append_normal(&(m->fmtext), "\n");
523  owl_fmtext_append_ztext(&(m->fmtext), indent);
524  if (text[strlen(text)-1]!='\n') {
525    owl_fmtext_append_normal(&(m->fmtext), "\n");
526  }
527
528  owl_free(indent);
529}
530
531void owl_message_create_aim(owl_message *m, char *sender, char *recipient, char *text)
532{
533  char *indent;
534
535  owl_message_init(m);
536  owl_message_set_body(m, text);
537  owl_message_set_sender(m, sender);
538  owl_message_set_recipient(m, recipient);
539  owl_message_set_type_aim(m);
540
541  indent=owl_malloc(strlen(text)+owl_text_num_lines(text)*OWL_MSGTAB+10);
542  owl_text_indent(indent, text, OWL_MSGTAB);
543  owl_fmtext_init_null(&(m->fmtext));
544  owl_fmtext_append_normal(&(m->fmtext), OWL_TABSTR);
545  owl_fmtext_append_normal(&(m->fmtext), "AIM: ");
546  owl_fmtext_append_normal(&(m->fmtext), sender);
547  owl_fmtext_append_normal(&(m->fmtext), " -> ");
548  owl_fmtext_append_normal(&(m->fmtext), recipient);
549  owl_fmtext_append_normal(&(m->fmtext), "\n");
550  owl_fmtext_append_ztext(&(m->fmtext), indent);
551  if (text[strlen(text)-1]!='\n') {
552    owl_fmtext_append_normal(&(m->fmtext), "\n");
553  }
554 
555  owl_free(indent);
556}
557
558void owl_message_create_admin(owl_message *m, char *header, char *text)
559{
560  char *indent;
561
562  owl_message_init(m);
563  owl_message_set_type_admin(m);
564
565  owl_message_set_body(m, text);
566
567  /* do something to make it clear the notice shouldn't be used for now */
568
569  indent=owl_malloc(strlen(text)+owl_text_num_lines(text)*OWL_MSGTAB+10);
570  owl_text_indent(indent, text, OWL_MSGTAB);
571  owl_fmtext_init_null(&(m->fmtext));
572  owl_fmtext_append_normal(&(m->fmtext), OWL_TABSTR);
573  owl_fmtext_append_bold(&(m->fmtext), "OWL ADMIN ");
574  owl_fmtext_append_ztext(&(m->fmtext), header);
575  owl_fmtext_append_normal(&(m->fmtext), "\n");
576  owl_fmtext_append_ztext(&(m->fmtext), indent);
577  if (text[strlen(text)-1]!='\n') {
578    owl_fmtext_append_normal(&(m->fmtext), "\n");
579  }
580
581  owl_free(indent);
582}
583
584void owl_message_create_from_znotice(owl_message *m, ZNotice_t *n)
585{
586  struct hostent *hent;
587  int k;
588  char *ptr, *tmp, *tmp2;
589
590  owl_message_init(m);
591 
592  owl_message_set_type_zephyr(m);
593  owl_message_set_direction_in(m);
594 
595  /* first save the full notice */
596  memcpy(&(m->notice), n, sizeof(ZNotice_t));
597
598  /* a little gross, we'll reaplace \r's with ' ' for now */
599  owl_zephyr_hackaway_cr(&(m->notice));
600 
601  m->delete=0;
602
603  /* set other info */
604  owl_message_set_sender(m, n->z_sender);
605  owl_message_set_class(m, n->z_class);
606  owl_message_set_instance(m, n->z_class_inst);
607  owl_message_set_recipient(m, n->z_recipient);
608  if (n->z_opcode) {
609    owl_message_set_opcode(m, n->z_opcode);
610  } else {
611    owl_message_set_opcode(m, "");
612  }
613  owl_message_set_zsig(m, n->z_message);
614
615  if ((ptr=strchr(n->z_recipient, '@'))!=NULL) {
616    owl_message_set_realm(m, ptr+1);
617  } else {
618    owl_message_set_realm(m, ZGetRealm());
619  }
620
621  m->zwriteline=strdup("");
622
623  /* set the body */
624  ptr=owl_zephyr_get_message(n, &k);
625  tmp=owl_malloc(k+10);
626  memcpy(tmp, ptr, k);
627  tmp[k]='\0';
628  if (owl_global_is_newlinestrip(&g)) {
629    tmp2=owl_util_stripnewlines(tmp);
630    owl_message_set_body(m, tmp2);
631    owl_free(tmp2);
632  } else {
633    owl_message_set_body(m, tmp);
634  }
635  owl_free(tmp);
636
637#ifdef OWL_ENABLE_ZCRYPT
638  /* if zcrypt is enabled try to decrypt the message */
639  if (owl_global_is_zcrypt(&g) && !strcasecmp(n->z_opcode, "crypt")) {
640    char *out;
641    int ret;
642
643    out=owl_malloc(strlen(owl_message_get_body(m))*16+20);
644    ret=zcrypt_decrypt(out, owl_message_get_body(m), owl_message_get_class(m), owl_message_get_instance(m));
645    if (ret==0) {
646      owl_message_set_body(m, out);
647    } else {
648      owl_free(out);
649    }
650  }
651#endif 
652
653  /* save the hostname */
654  owl_function_debugmsg("About to do gethostbyaddr");
655  hent=gethostbyaddr((char *) &(n->z_uid.zuid_addr), sizeof(n->z_uid.zuid_addr), AF_INET);
656  if (hent && hent->h_name) {
657    strcpy(m->hostname, hent->h_name);
658  } else {
659    strcpy(m->hostname, inet_ntoa(n->z_sender_addr));
660  }
661
662  /* save the time */
663  m->time=owl_strdup(ctime((time_t *) &n->z_time.tv_sec));
664  m->time[strlen(m->time)-1]='\0';
665
666  /* create the formatted message */
667  if (owl_global_is_config_format(&g)) {
668    _owl_message_make_text_from_config(m);
669  } else if (owl_global_is_userclue(&g, OWL_USERCLUE_CLASSES)) {
670    _owl_message_make_text_from_notice_standard(m);
671  } else {
672    _owl_message_make_text_from_notice_simple(m);
673  }
674
675}
676
677void owl_message_create_from_zwriteline(owl_message *m, char *line, char *body, char *zsig)
678{
679  owl_zwrite z;
680  int ret;
681 
682  owl_message_init(m);
683
684  /* create a zwrite for the purpose of filling in other message fields */
685  owl_zwrite_create_from_line(&z, line);
686
687  /* set things */
688  owl_message_set_direction_out(m);
689  owl_message_set_type_zephyr(m);
690  owl_message_set_sender(m, ZGetSender());
691  owl_message_set_class(m, owl_zwrite_get_class(&z));
692  owl_message_set_instance(m, owl_zwrite_get_instance(&z));
693  owl_message_set_recipient(m,
694                            long_zuser(owl_zwrite_get_recip_n(&z, 0))); /* only gets the first user, must fix */
695  owl_message_set_opcode(m, owl_zwrite_get_opcode(&z));
696  owl_message_set_realm(m, owl_zwrite_get_realm(&z)); /* also a hack, but not here */
697  m->zwriteline=owl_strdup(line);
698  owl_message_set_body(m, body);
699  owl_message_set_zsig(m, zsig);
700 
701  /* save the hostname */
702  ret=gethostname(m->hostname, MAXHOSTNAMELEN);
703  if (ret) {
704    strcpy(m->hostname, "localhost");
705  }
706
707  /* create the formatted message */
708  if (owl_global_is_config_format(&g)) {
709    _owl_message_make_text_from_config(m);
710  } else if (owl_global_is_userclue(&g, OWL_USERCLUE_CLASSES)) {
711    _owl_message_make_text_from_zwriteline_standard(m);
712  } else {
713    _owl_message_make_text_from_zwriteline_simple(m);
714  }
715
716  owl_zwrite_free(&z);
717}
718
719void _owl_message_make_text_from_config(owl_message *m)
720{
721  char *body, *indent;
722
723  owl_fmtext_init_null(&(m->fmtext));
724
725  /* get body from the config */
726  body=owl_config_getmsg(m, 1);
727 
728  /* indent */
729  indent=owl_malloc(strlen(body)+owl_text_num_lines(body)*OWL_TAB+10);
730  owl_text_indent(indent, body, OWL_TAB);
731
732  /* fmtext_append.  This needs to change */
733  owl_fmtext_append_ztext(&(m->fmtext), indent);
734
735  owl_free(indent);
736  owl_free(body);
737}
738
739void _owl_message_make_text_from_zwriteline_standard(owl_message *m)
740{
741  char *indent, *text, *zsigbuff, *foo;
742
743  text=owl_message_get_body(m);
744
745  indent=owl_malloc(strlen(text)+owl_text_num_lines(text)*OWL_MSGTAB+10);
746  owl_text_indent(indent, text, OWL_MSGTAB);
747  owl_fmtext_init_null(&(m->fmtext));
748  owl_fmtext_append_normal(&(m->fmtext), OWL_TABSTR);
749  owl_fmtext_append_normal(&(m->fmtext), "Zephyr sent to ");
750  foo=short_zuser(owl_message_get_recipient(m));
751  owl_fmtext_append_normal(&(m->fmtext), foo);
752  owl_free(foo);
753  owl_fmtext_append_normal(&(m->fmtext), "  (Zsig: ");
754
755  zsigbuff=owl_malloc(strlen(owl_message_get_zsig(m))+30);
756  owl_message_pretty_zsig(m, zsigbuff);
757  owl_fmtext_append_ztext(&(m->fmtext), zsigbuff);
758  owl_free(zsigbuff);
759 
760  owl_fmtext_append_normal(&(m->fmtext), ")");
761  owl_fmtext_append_normal(&(m->fmtext), "\n");
762  owl_fmtext_append_ztext(&(m->fmtext), indent);
763  if (text[strlen(text)-1]!='\n') {
764    owl_fmtext_append_normal(&(m->fmtext), "\n");
765  }
766
767  owl_free(indent);
768}
769
770void _owl_message_make_text_from_zwriteline_simple(owl_message *m)
771{
772  char *indent, *text, *zsigbuff, *foo;
773
774  text=owl_message_get_body(m);
775
776  indent=owl_malloc(strlen(text)+owl_text_num_lines(text)*OWL_MSGTAB+10);
777  owl_text_indent(indent, text, OWL_MSGTAB);
778  owl_fmtext_init_null(&(m->fmtext));
779  owl_fmtext_append_normal(&(m->fmtext), OWL_TABSTR);
780  owl_fmtext_append_normal(&(m->fmtext), "To: ");
781  foo=short_zuser(owl_message_get_recipient(m));
782  owl_fmtext_append_normal(&(m->fmtext), foo);
783  owl_free(foo);
784  owl_fmtext_append_normal(&(m->fmtext), "  (Zsig: ");
785
786  zsigbuff=owl_malloc(strlen(owl_message_get_zsig(m))+30);
787  owl_message_pretty_zsig(m, zsigbuff);
788  owl_fmtext_append_ztext(&(m->fmtext), zsigbuff);
789  owl_free(zsigbuff);
790 
791  owl_fmtext_append_normal(&(m->fmtext), ")");
792  owl_fmtext_append_normal(&(m->fmtext), "\n");
793  owl_fmtext_append_ztext(&(m->fmtext), indent);
794  if (text[strlen(text)-1]!='\n') {
795    owl_fmtext_append_normal(&(m->fmtext), "\n");
796  }
797
798  owl_free(indent);
799}
800
801void _owl_message_make_text_from_notice_standard(owl_message *m)
802{
803  char *body, *indent, *ptr, *zsigbuff, frombuff[LINE];
804  ZNotice_t *n;
805
806  n=&(m->notice);
807 
808  /* get the body */
809  body=owl_malloc(strlen(owl_message_get_body(m))+30);
810  strcpy(body, owl_message_get_body(m));
811
812  /* add a newline if we need to */
813  if (body[0]!='\0' && body[strlen(body)-1]!='\n') {
814    strcat(body, "\n");
815  }
816
817  /* do the indenting into indent */
818  indent=owl_malloc(strlen(body)+owl_text_num_lines(body)*OWL_MSGTAB+10);
819  owl_text_indent(indent, body, OWL_MSGTAB);
820
821  /* edit the from addr for printing */
822  strcpy(frombuff, owl_message_get_sender(m));
823  ptr=strchr(frombuff, '@');
824  if (ptr && !strncmp(ptr+1, ZGetRealm(), strlen(ZGetRealm()))) {
825    *ptr='\0';
826  }
827
828  /* set the message for printing */
829  owl_fmtext_init_null(&(m->fmtext));
830  owl_fmtext_append_normal(&(m->fmtext), OWL_TABSTR);
831
832  if (!strcasecmp(owl_message_get_opcode(m), "ping") && owl_message_is_to_me(m)) {
833    owl_fmtext_append_bold(&(m->fmtext), "PING");
834    owl_fmtext_append_normal(&(m->fmtext), " from ");
835    owl_fmtext_append_bold(&(m->fmtext), frombuff);
836    owl_fmtext_append_normal(&(m->fmtext), "\n");
837  } else if (!strcasecmp(n->z_class, "login")) {
838    char *ptr, host[LINE], tty[LINE];
839    int len;
840
841    ptr=owl_zephyr_get_field(n, 1, &len);
842    strncpy(host, ptr, len);
843    host[len]='\0';
844    ptr=owl_zephyr_get_field(n, 3, &len);
845    strncpy(tty, ptr, len);
846    tty[len]='\0';
847   
848    if (!strcasecmp(n->z_opcode, "user_login")) {
849      owl_fmtext_append_bold(&(m->fmtext), "LOGIN");
850    } else if (!strcasecmp(n->z_opcode, "user_logout")) {
851      owl_fmtext_append_bold(&(m->fmtext), "LOGOUT");
852    }
853    owl_fmtext_append_normal(&(m->fmtext), " for ");
854    ptr=short_zuser(n->z_class_inst);
855    owl_fmtext_append_bold(&(m->fmtext), ptr);
856    owl_free(ptr);
857    owl_fmtext_append_normal(&(m->fmtext), " at ");
858    owl_fmtext_append_normal(&(m->fmtext), host);
859    owl_fmtext_append_normal(&(m->fmtext), " ");
860    owl_fmtext_append_normal(&(m->fmtext), tty);
861    owl_fmtext_append_normal(&(m->fmtext), "\n");
862  } else {
863    owl_fmtext_append_normal(&(m->fmtext), owl_message_get_class(m));
864    owl_fmtext_append_normal(&(m->fmtext), " / ");
865    owl_fmtext_append_normal(&(m->fmtext), owl_message_get_instance(m));
866    owl_fmtext_append_normal(&(m->fmtext), " / ");
867    owl_fmtext_append_bold(&(m->fmtext), frombuff);
868    if (strcasecmp(owl_message_get_realm(m), ZGetRealm())) {
869      owl_fmtext_append_normal(&(m->fmtext), " {");
870      owl_fmtext_append_normal(&(m->fmtext), owl_message_get_realm(m));
871      owl_fmtext_append_normal(&(m->fmtext), "} ");
872    }
873    if (n->z_opcode[0]!='\0') {
874      owl_fmtext_append_normal(&(m->fmtext), " [");
875      owl_fmtext_append_normal(&(m->fmtext), owl_message_get_opcode(m));
876      owl_fmtext_append_normal(&(m->fmtext), "] ");
877    }
878
879    /* stick on the zsig */
880    zsigbuff=owl_malloc(strlen(owl_message_get_zsig(m))+30);
881    owl_message_pretty_zsig(m, zsigbuff);
882    owl_fmtext_append_normal(&(m->fmtext), "    (");
883    owl_fmtext_append_ztext(&(m->fmtext), zsigbuff);
884    owl_fmtext_append_normal(&(m->fmtext), ")");
885    owl_fmtext_append_normal(&(m->fmtext), "\n");
886    owl_free(zsigbuff);
887
888    /* then the indented message */
889    owl_fmtext_append_ztext(&(m->fmtext), indent);
890
891    /* make personal messages bold for smaat users */
892    if (owl_global_is_userclue(&g, OWL_USERCLUE_CLASSES)) {
893      if (owl_message_is_personal(m)) {
894        owl_fmtext_addattr((&m->fmtext), OWL_FMTEXT_ATTR_BOLD);
895      }
896    }
897  }
898
899  owl_free(body);
900  owl_free(indent);
901}
902
903void _owl_message_make_text_from_notice_simple(owl_message *m)
904{
905  char *body, *indent, *ptr, *zsigbuff, frombuff[LINE];
906  ZNotice_t *n;
907
908  n=&(m->notice);
909
910  /* get the body */
911  body=owl_strdup(owl_message_get_body(m));
912  body=realloc(body, strlen(body)+30);
913
914  /* add a newline if we need to */
915  if (body[0]!='\0' && body[strlen(body)-1]!='\n') {
916    strcat(body, "\n");
917  }
918
919  /* do the indenting into indent */
920  indent=owl_malloc(strlen(body)+owl_text_num_lines(body)*OWL_MSGTAB+10);
921  owl_text_indent(indent, body, OWL_MSGTAB);
922
923  /* edit the from addr for printing */
924  strcpy(frombuff, owl_message_get_sender(m));
925  ptr=strchr(frombuff, '@');
926  if (ptr && !strncmp(ptr+1, ZGetRealm(), strlen(ZGetRealm()))) {
927    *ptr='\0';
928  }
929
930  /* set the message for printing */
931  owl_fmtext_init_null(&(m->fmtext));
932  owl_fmtext_append_normal(&(m->fmtext), OWL_TABSTR);
933
934  if (!strcasecmp(owl_message_get_opcode(m), "ping")) {
935    owl_fmtext_append_bold(&(m->fmtext), "PING");
936    owl_fmtext_append_normal(&(m->fmtext), " from ");
937    owl_fmtext_append_bold(&(m->fmtext), frombuff);
938    owl_fmtext_append_normal(&(m->fmtext), "\n");
939  } else if (!strcasecmp(owl_message_get_class(m), "login")) {
940    char *ptr, host[LINE], tty[LINE];
941    int len;
942
943    ptr=owl_zephyr_get_field(n, 1, &len);
944    strncpy(host, ptr, len);
945    host[len]='\0';
946    ptr=owl_zephyr_get_field(n, 3, &len);
947    strncpy(tty, ptr, len);
948    tty[len]='\0';
949   
950    if (!strcasecmp(owl_message_get_opcode(m), "user_login")) {
951      owl_fmtext_append_bold(&(m->fmtext), "LOGIN");
952    } else if (!strcasecmp(owl_message_get_opcode(m), "user_logout")) {
953      owl_fmtext_append_bold(&(m->fmtext), "LOGOUT");
954    }
955    owl_fmtext_append_normal(&(m->fmtext), " for ");
956    ptr=short_zuser(owl_message_get_instance(m));
957    owl_fmtext_append_bold(&(m->fmtext), ptr);
958    owl_free(ptr);
959    owl_fmtext_append_normal(&(m->fmtext), " at ");
960    owl_fmtext_append_normal(&(m->fmtext), host);
961    owl_fmtext_append_normal(&(m->fmtext), " ");
962    owl_fmtext_append_normal(&(m->fmtext), tty);
963    owl_fmtext_append_normal(&(m->fmtext), "\n");
964  } else {
965    owl_fmtext_append_normal(&(m->fmtext), "From: ");
966    if (strcasecmp(owl_message_get_class(m), "message")) {
967      owl_fmtext_append_normal(&(m->fmtext), "Class ");
968      owl_fmtext_append_normal(&(m->fmtext), owl_message_get_class(m));
969      owl_fmtext_append_normal(&(m->fmtext), " / Instance ");
970      owl_fmtext_append_normal(&(m->fmtext), owl_message_get_instance(m));
971      owl_fmtext_append_normal(&(m->fmtext), " / ");
972    }
973    owl_fmtext_append_normal(&(m->fmtext), frombuff);
974    if (strcasecmp(owl_message_get_realm(m), ZGetRealm())) {
975      owl_fmtext_append_normal(&(m->fmtext), " {");
976      owl_fmtext_append_normal(&(m->fmtext), owl_message_get_realm(m));
977      owl_fmtext_append_normal(&(m->fmtext), "} ");
978    }
979
980    /* stick on the zsig */
981    zsigbuff=owl_malloc(strlen(owl_message_get_zsig(m))+30);
982    owl_message_pretty_zsig(m, zsigbuff);
983    owl_fmtext_append_normal(&(m->fmtext), "    (");
984    owl_fmtext_append_ztext(&(m->fmtext), zsigbuff);
985    owl_fmtext_append_normal(&(m->fmtext), ")");
986    owl_fmtext_append_normal(&(m->fmtext), "\n");
987    owl_free(zsigbuff);
988
989    /* then the indented message */
990    owl_fmtext_append_ztext(&(m->fmtext), indent);
991
992    /* make personal messages bold for smaat users */
993    if (owl_global_is_userclue(&g, OWL_USERCLUE_CLASSES)) {
994      if (owl_message_is_personal(m)) {
995        owl_fmtext_addattr((&m->fmtext), OWL_FMTEXT_ATTR_BOLD);
996      }
997    }
998  }
999
1000  owl_free(body);
1001  owl_free(indent);
1002}
1003
1004void owl_message_pretty_zsig(owl_message *m, char *buff)
1005{
1006  /* stick a one line version of the zsig in buff */
1007  char *ptr;
1008
1009  strcpy(buff, owl_message_get_zsig(m));
1010  ptr=strchr(buff, '\n');
1011  if (ptr) ptr[0]='\0';
1012}
1013
1014void owl_message_free(owl_message *m)
1015{
1016  int i, j;
1017  owl_pair *p;
1018   
1019  if (owl_message_is_type_zephyr(m) && owl_message_is_direction_in(m)) {
1020    ZFreeNotice(&(m->notice));
1021  }
1022  if (m->time) owl_free(m->time);
1023  if (m->zwriteline) owl_free(m->zwriteline);
1024
1025  /* free all the attributes */
1026  j=owl_list_get_size(&(m->attributes));
1027  for (i=0; i<j; i++) {
1028    p=owl_list_get_element(&(m->attributes), i);
1029    owl_free(owl_pair_get_key(p));
1030    owl_free(owl_pair_get_value(p));
1031    owl_free(p);
1032  }
1033
1034  owl_list_free_simple(&(m->attributes));
1035 
1036  owl_fmtext_free(&(m->fmtext));
1037}
Note: See TracBrowser for help on using the repository browser.