source: message.c @ 3abf28b

barnowl_perlaimdebianowlrelease-1.10release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since 3abf28b was 3abf28b, checked in by James M. Kretchmar <kretch@mit.edu>, 21 years ago
make smartnarrow work for AIM (though it will crash on names with spaces) don't set an away message by default better default format bold them like personal zephyrs same with terminal bell do some basic HTML stripping (buggy, in progress)
  • Property mode set to 100644
File size: 27.7 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_incoming_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  owl_message_set_direction_in(m);
541
542  indent=owl_malloc(strlen(text)+owl_text_num_lines(text)*OWL_MSGTAB+10);
543  owl_text_indent(indent, text, OWL_MSGTAB);
544  owl_fmtext_init_null(&(m->fmtext));
545  owl_fmtext_append_bold(&(m->fmtext), OWL_TABSTR);
546  owl_fmtext_append_bold(&(m->fmtext), "AIM from ");
547  owl_fmtext_append_bold(&(m->fmtext), sender);
548  owl_fmtext_append_bold(&(m->fmtext), "\n");
549  owl_fmtext_append_bold(&(m->fmtext), indent);
550  if (text[strlen(text)-1]!='\n') {
551    owl_fmtext_append_bold(&(m->fmtext), "\n");
552  }
553 
554  owl_free(indent);
555}
556
557void owl_message_create_outgoing_aim(owl_message *m, char *sender, char *recipient, char *text)
558{
559  char *indent;
560
561  owl_message_init(m);
562  owl_message_set_body(m, text);
563  owl_message_set_sender(m, sender);
564  owl_message_set_recipient(m, recipient);
565  owl_message_set_type_aim(m);
566  owl_message_set_direction_out(m);
567
568  indent=owl_malloc(strlen(text)+owl_text_num_lines(text)*OWL_MSGTAB+10);
569  owl_text_indent(indent, text, OWL_MSGTAB);
570  owl_fmtext_init_null(&(m->fmtext));
571  owl_fmtext_append_normal(&(m->fmtext), OWL_TABSTR);
572  owl_fmtext_append_normal(&(m->fmtext), "AIM sent to ");
573  owl_fmtext_append_normal(&(m->fmtext), recipient);
574  owl_fmtext_append_normal(&(m->fmtext), "\n");
575  owl_fmtext_append_ztext(&(m->fmtext), indent);
576  if (text[strlen(text)-1]!='\n') {
577    owl_fmtext_append_normal(&(m->fmtext), "\n");
578  }
579 
580  owl_free(indent);
581}
582
583void owl_message_create_admin(owl_message *m, char *header, char *text)
584{
585  char *indent;
586
587  owl_message_init(m);
588  owl_message_set_type_admin(m);
589
590  owl_message_set_body(m, text);
591
592  /* do something to make it clear the notice shouldn't be used for now */
593
594  indent=owl_malloc(strlen(text)+owl_text_num_lines(text)*OWL_MSGTAB+10);
595  owl_text_indent(indent, text, OWL_MSGTAB);
596  owl_fmtext_init_null(&(m->fmtext));
597  owl_fmtext_append_normal(&(m->fmtext), OWL_TABSTR);
598  owl_fmtext_append_bold(&(m->fmtext), "OWL ADMIN ");
599  owl_fmtext_append_ztext(&(m->fmtext), header);
600  owl_fmtext_append_normal(&(m->fmtext), "\n");
601  owl_fmtext_append_ztext(&(m->fmtext), indent);
602  if (text[strlen(text)-1]!='\n') {
603    owl_fmtext_append_normal(&(m->fmtext), "\n");
604  }
605
606  owl_free(indent);
607}
608
609void owl_message_create_from_znotice(owl_message *m, ZNotice_t *n)
610{
611  struct hostent *hent;
612  int k;
613  char *ptr, *tmp, *tmp2;
614
615  owl_message_init(m);
616 
617  owl_message_set_type_zephyr(m);
618  owl_message_set_direction_in(m);
619 
620  /* first save the full notice */
621  memcpy(&(m->notice), n, sizeof(ZNotice_t));
622
623  /* a little gross, we'll reaplace \r's with ' ' for now */
624  owl_zephyr_hackaway_cr(&(m->notice));
625 
626  m->delete=0;
627
628  /* set other info */
629  owl_message_set_sender(m, n->z_sender);
630  owl_message_set_class(m, n->z_class);
631  owl_message_set_instance(m, n->z_class_inst);
632  owl_message_set_recipient(m, n->z_recipient);
633  if (n->z_opcode) {
634    owl_message_set_opcode(m, n->z_opcode);
635  } else {
636    owl_message_set_opcode(m, "");
637  }
638  owl_message_set_zsig(m, n->z_message);
639
640  if ((ptr=strchr(n->z_recipient, '@'))!=NULL) {
641    owl_message_set_realm(m, ptr+1);
642  } else {
643    owl_message_set_realm(m, ZGetRealm());
644  }
645
646  m->zwriteline=strdup("");
647
648  /* set the body */
649  ptr=owl_zephyr_get_message(n, &k);
650  tmp=owl_malloc(k+10);
651  memcpy(tmp, ptr, k);
652  tmp[k]='\0';
653  if (owl_global_is_newlinestrip(&g)) {
654    tmp2=owl_util_stripnewlines(tmp);
655    owl_message_set_body(m, tmp2);
656    owl_free(tmp2);
657  } else {
658    owl_message_set_body(m, tmp);
659  }
660  owl_free(tmp);
661
662#ifdef OWL_ENABLE_ZCRYPT
663  /* if zcrypt is enabled try to decrypt the message */
664  if (owl_global_is_zcrypt(&g) && !strcasecmp(n->z_opcode, "crypt")) {
665    char *out;
666    int ret;
667
668    out=owl_malloc(strlen(owl_message_get_body(m))*16+20);
669    ret=zcrypt_decrypt(out, owl_message_get_body(m), owl_message_get_class(m), owl_message_get_instance(m));
670    if (ret==0) {
671      owl_message_set_body(m, out);
672    } else {
673      owl_free(out);
674    }
675  }
676#endif 
677
678  /* save the hostname */
679  owl_function_debugmsg("About to do gethostbyaddr");
680  hent=gethostbyaddr((char *) &(n->z_uid.zuid_addr), sizeof(n->z_uid.zuid_addr), AF_INET);
681  if (hent && hent->h_name) {
682    strcpy(m->hostname, hent->h_name);
683  } else {
684    strcpy(m->hostname, inet_ntoa(n->z_sender_addr));
685  }
686
687  /* save the time */
688  m->time=owl_strdup(ctime((time_t *) &n->z_time.tv_sec));
689  m->time[strlen(m->time)-1]='\0';
690
691  /* create the formatted message */
692  if (owl_global_is_config_format(&g)) {
693    _owl_message_make_text_from_config(m);
694  } else if (owl_global_is_userclue(&g, OWL_USERCLUE_CLASSES)) {
695    _owl_message_make_text_from_notice_standard(m);
696  } else {
697    _owl_message_make_text_from_notice_simple(m);
698  }
699
700}
701
702void owl_message_create_from_zwriteline(owl_message *m, char *line, char *body, char *zsig)
703{
704  owl_zwrite z;
705  int ret;
706 
707  owl_message_init(m);
708
709  /* create a zwrite for the purpose of filling in other message fields */
710  owl_zwrite_create_from_line(&z, line);
711
712  /* set things */
713  owl_message_set_direction_out(m);
714  owl_message_set_type_zephyr(m);
715  owl_message_set_sender(m, ZGetSender());
716  owl_message_set_class(m, owl_zwrite_get_class(&z));
717  owl_message_set_instance(m, owl_zwrite_get_instance(&z));
718  owl_message_set_recipient(m,
719                            long_zuser(owl_zwrite_get_recip_n(&z, 0))); /* only gets the first user, must fix */
720  owl_message_set_opcode(m, owl_zwrite_get_opcode(&z));
721  owl_message_set_realm(m, owl_zwrite_get_realm(&z)); /* also a hack, but not here */
722  m->zwriteline=owl_strdup(line);
723  owl_message_set_body(m, body);
724  owl_message_set_zsig(m, zsig);
725 
726  /* save the hostname */
727  ret=gethostname(m->hostname, MAXHOSTNAMELEN);
728  if (ret) {
729    strcpy(m->hostname, "localhost");
730  }
731
732  /* create the formatted message */
733  if (owl_global_is_config_format(&g)) {
734    _owl_message_make_text_from_config(m);
735  } else if (owl_global_is_userclue(&g, OWL_USERCLUE_CLASSES)) {
736    _owl_message_make_text_from_zwriteline_standard(m);
737  } else {
738    _owl_message_make_text_from_zwriteline_simple(m);
739  }
740
741  owl_zwrite_free(&z);
742}
743
744void _owl_message_make_text_from_config(owl_message *m)
745{
746  char *body, *indent;
747
748  owl_fmtext_init_null(&(m->fmtext));
749
750  /* get body from the config */
751  body=owl_config_getmsg(m, 1);
752 
753  /* indent */
754  indent=owl_malloc(strlen(body)+owl_text_num_lines(body)*OWL_TAB+10);
755  owl_text_indent(indent, body, OWL_TAB);
756
757  /* fmtext_append.  This needs to change */
758  owl_fmtext_append_ztext(&(m->fmtext), indent);
759
760  owl_free(indent);
761  owl_free(body);
762}
763
764void _owl_message_make_text_from_zwriteline_standard(owl_message *m)
765{
766  char *indent, *text, *zsigbuff, *foo;
767
768  text=owl_message_get_body(m);
769
770  indent=owl_malloc(strlen(text)+owl_text_num_lines(text)*OWL_MSGTAB+10);
771  owl_text_indent(indent, text, OWL_MSGTAB);
772  owl_fmtext_init_null(&(m->fmtext));
773  owl_fmtext_append_normal(&(m->fmtext), OWL_TABSTR);
774  owl_fmtext_append_normal(&(m->fmtext), "Zephyr sent to ");
775  foo=short_zuser(owl_message_get_recipient(m));
776  owl_fmtext_append_normal(&(m->fmtext), foo);
777  owl_free(foo);
778  owl_fmtext_append_normal(&(m->fmtext), "  (Zsig: ");
779
780  zsigbuff=owl_malloc(strlen(owl_message_get_zsig(m))+30);
781  owl_message_pretty_zsig(m, zsigbuff);
782  owl_fmtext_append_ztext(&(m->fmtext), zsigbuff);
783  owl_free(zsigbuff);
784 
785  owl_fmtext_append_normal(&(m->fmtext), ")");
786  owl_fmtext_append_normal(&(m->fmtext), "\n");
787  owl_fmtext_append_ztext(&(m->fmtext), indent);
788  if (text[strlen(text)-1]!='\n') {
789    owl_fmtext_append_normal(&(m->fmtext), "\n");
790  }
791
792  owl_free(indent);
793}
794
795void _owl_message_make_text_from_zwriteline_simple(owl_message *m)
796{
797  char *indent, *text, *zsigbuff, *foo;
798
799  text=owl_message_get_body(m);
800
801  indent=owl_malloc(strlen(text)+owl_text_num_lines(text)*OWL_MSGTAB+10);
802  owl_text_indent(indent, text, OWL_MSGTAB);
803  owl_fmtext_init_null(&(m->fmtext));
804  owl_fmtext_append_normal(&(m->fmtext), OWL_TABSTR);
805  owl_fmtext_append_normal(&(m->fmtext), "To: ");
806  foo=short_zuser(owl_message_get_recipient(m));
807  owl_fmtext_append_normal(&(m->fmtext), foo);
808  owl_free(foo);
809  owl_fmtext_append_normal(&(m->fmtext), "  (Zsig: ");
810
811  zsigbuff=owl_malloc(strlen(owl_message_get_zsig(m))+30);
812  owl_message_pretty_zsig(m, zsigbuff);
813  owl_fmtext_append_ztext(&(m->fmtext), zsigbuff);
814  owl_free(zsigbuff);
815 
816  owl_fmtext_append_normal(&(m->fmtext), ")");
817  owl_fmtext_append_normal(&(m->fmtext), "\n");
818  owl_fmtext_append_ztext(&(m->fmtext), indent);
819  if (text[strlen(text)-1]!='\n') {
820    owl_fmtext_append_normal(&(m->fmtext), "\n");
821  }
822
823  owl_free(indent);
824}
825
826void _owl_message_make_text_from_notice_standard(owl_message *m)
827{
828  char *body, *indent, *ptr, *zsigbuff, frombuff[LINE];
829  ZNotice_t *n;
830
831  n=&(m->notice);
832 
833  /* get the body */
834  body=owl_malloc(strlen(owl_message_get_body(m))+30);
835  strcpy(body, owl_message_get_body(m));
836
837  /* add a newline if we need to */
838  if (body[0]!='\0' && body[strlen(body)-1]!='\n') {
839    strcat(body, "\n");
840  }
841
842  /* do the indenting into indent */
843  indent=owl_malloc(strlen(body)+owl_text_num_lines(body)*OWL_MSGTAB+10);
844  owl_text_indent(indent, body, OWL_MSGTAB);
845
846  /* edit the from addr for printing */
847  strcpy(frombuff, owl_message_get_sender(m));
848  ptr=strchr(frombuff, '@');
849  if (ptr && !strncmp(ptr+1, ZGetRealm(), strlen(ZGetRealm()))) {
850    *ptr='\0';
851  }
852
853  /* set the message for printing */
854  owl_fmtext_init_null(&(m->fmtext));
855  owl_fmtext_append_normal(&(m->fmtext), OWL_TABSTR);
856
857  if (!strcasecmp(owl_message_get_opcode(m), "ping") && owl_message_is_to_me(m)) {
858    owl_fmtext_append_bold(&(m->fmtext), "PING");
859    owl_fmtext_append_normal(&(m->fmtext), " from ");
860    owl_fmtext_append_bold(&(m->fmtext), frombuff);
861    owl_fmtext_append_normal(&(m->fmtext), "\n");
862  } else if (!strcasecmp(n->z_class, "login")) {
863    char *ptr, host[LINE], tty[LINE];
864    int len;
865
866    ptr=owl_zephyr_get_field(n, 1, &len);
867    strncpy(host, ptr, len);
868    host[len]='\0';
869    ptr=owl_zephyr_get_field(n, 3, &len);
870    strncpy(tty, ptr, len);
871    tty[len]='\0';
872   
873    if (!strcasecmp(n->z_opcode, "user_login")) {
874      owl_fmtext_append_bold(&(m->fmtext), "LOGIN");
875    } else if (!strcasecmp(n->z_opcode, "user_logout")) {
876      owl_fmtext_append_bold(&(m->fmtext), "LOGOUT");
877    }
878    owl_fmtext_append_normal(&(m->fmtext), " for ");
879    ptr=short_zuser(n->z_class_inst);
880    owl_fmtext_append_bold(&(m->fmtext), ptr);
881    owl_free(ptr);
882    owl_fmtext_append_normal(&(m->fmtext), " at ");
883    owl_fmtext_append_normal(&(m->fmtext), host);
884    owl_fmtext_append_normal(&(m->fmtext), " ");
885    owl_fmtext_append_normal(&(m->fmtext), tty);
886    owl_fmtext_append_normal(&(m->fmtext), "\n");
887  } else {
888    owl_fmtext_append_normal(&(m->fmtext), owl_message_get_class(m));
889    owl_fmtext_append_normal(&(m->fmtext), " / ");
890    owl_fmtext_append_normal(&(m->fmtext), owl_message_get_instance(m));
891    owl_fmtext_append_normal(&(m->fmtext), " / ");
892    owl_fmtext_append_bold(&(m->fmtext), frombuff);
893    if (strcasecmp(owl_message_get_realm(m), ZGetRealm())) {
894      owl_fmtext_append_normal(&(m->fmtext), " {");
895      owl_fmtext_append_normal(&(m->fmtext), owl_message_get_realm(m));
896      owl_fmtext_append_normal(&(m->fmtext), "} ");
897    }
898    if (n->z_opcode[0]!='\0') {
899      owl_fmtext_append_normal(&(m->fmtext), " [");
900      owl_fmtext_append_normal(&(m->fmtext), owl_message_get_opcode(m));
901      owl_fmtext_append_normal(&(m->fmtext), "] ");
902    }
903
904    /* stick on the zsig */
905    zsigbuff=owl_malloc(strlen(owl_message_get_zsig(m))+30);
906    owl_message_pretty_zsig(m, zsigbuff);
907    owl_fmtext_append_normal(&(m->fmtext), "    (");
908    owl_fmtext_append_ztext(&(m->fmtext), zsigbuff);
909    owl_fmtext_append_normal(&(m->fmtext), ")");
910    owl_fmtext_append_normal(&(m->fmtext), "\n");
911    owl_free(zsigbuff);
912
913    /* then the indented message */
914    owl_fmtext_append_ztext(&(m->fmtext), indent);
915
916    /* make private messages bold for smaat users */
917    if (owl_global_is_userclue(&g, OWL_USERCLUE_CLASSES)) {
918      if (owl_message_is_personal(m)) {
919        owl_fmtext_addattr((&m->fmtext), OWL_FMTEXT_ATTR_BOLD);
920      }
921    }
922  }
923
924  owl_free(body);
925  owl_free(indent);
926}
927
928void _owl_message_make_text_from_notice_simple(owl_message *m)
929{
930  char *body, *indent, *ptr, *zsigbuff, frombuff[LINE];
931  ZNotice_t *n;
932
933  n=&(m->notice);
934
935  /* get the body */
936  body=owl_strdup(owl_message_get_body(m));
937  body=realloc(body, strlen(body)+30);
938
939  /* add a newline if we need to */
940  if (body[0]!='\0' && body[strlen(body)-1]!='\n') {
941    strcat(body, "\n");
942  }
943
944  /* do the indenting into indent */
945  indent=owl_malloc(strlen(body)+owl_text_num_lines(body)*OWL_MSGTAB+10);
946  owl_text_indent(indent, body, OWL_MSGTAB);
947
948  /* edit the from addr for printing */
949  strcpy(frombuff, owl_message_get_sender(m));
950  ptr=strchr(frombuff, '@');
951  if (ptr && !strncmp(ptr+1, ZGetRealm(), strlen(ZGetRealm()))) {
952    *ptr='\0';
953  }
954
955  /* set the message for printing */
956  owl_fmtext_init_null(&(m->fmtext));
957  owl_fmtext_append_normal(&(m->fmtext), OWL_TABSTR);
958
959  if (!strcasecmp(owl_message_get_opcode(m), "ping")) {
960    owl_fmtext_append_bold(&(m->fmtext), "PING");
961    owl_fmtext_append_normal(&(m->fmtext), " from ");
962    owl_fmtext_append_bold(&(m->fmtext), frombuff);
963    owl_fmtext_append_normal(&(m->fmtext), "\n");
964  } else if (!strcasecmp(owl_message_get_class(m), "login")) {
965    char *ptr, host[LINE], tty[LINE];
966    int len;
967
968    ptr=owl_zephyr_get_field(n, 1, &len);
969    strncpy(host, ptr, len);
970    host[len]='\0';
971    ptr=owl_zephyr_get_field(n, 3, &len);
972    strncpy(tty, ptr, len);
973    tty[len]='\0';
974   
975    if (!strcasecmp(owl_message_get_opcode(m), "user_login")) {
976      owl_fmtext_append_bold(&(m->fmtext), "LOGIN");
977    } else if (!strcasecmp(owl_message_get_opcode(m), "user_logout")) {
978      owl_fmtext_append_bold(&(m->fmtext), "LOGOUT");
979    }
980    owl_fmtext_append_normal(&(m->fmtext), " for ");
981    ptr=short_zuser(owl_message_get_instance(m));
982    owl_fmtext_append_bold(&(m->fmtext), ptr);
983    owl_free(ptr);
984    owl_fmtext_append_normal(&(m->fmtext), " at ");
985    owl_fmtext_append_normal(&(m->fmtext), host);
986    owl_fmtext_append_normal(&(m->fmtext), " ");
987    owl_fmtext_append_normal(&(m->fmtext), tty);
988    owl_fmtext_append_normal(&(m->fmtext), "\n");
989  } else {
990    owl_fmtext_append_normal(&(m->fmtext), "From: ");
991    if (strcasecmp(owl_message_get_class(m), "message")) {
992      owl_fmtext_append_normal(&(m->fmtext), "Class ");
993      owl_fmtext_append_normal(&(m->fmtext), owl_message_get_class(m));
994      owl_fmtext_append_normal(&(m->fmtext), " / Instance ");
995      owl_fmtext_append_normal(&(m->fmtext), owl_message_get_instance(m));
996      owl_fmtext_append_normal(&(m->fmtext), " / ");
997    }
998    owl_fmtext_append_normal(&(m->fmtext), frombuff);
999    if (strcasecmp(owl_message_get_realm(m), ZGetRealm())) {
1000      owl_fmtext_append_normal(&(m->fmtext), " {");
1001      owl_fmtext_append_normal(&(m->fmtext), owl_message_get_realm(m));
1002      owl_fmtext_append_normal(&(m->fmtext), "} ");
1003    }
1004
1005    /* stick on the zsig */
1006    zsigbuff=owl_malloc(strlen(owl_message_get_zsig(m))+30);
1007    owl_message_pretty_zsig(m, zsigbuff);
1008    owl_fmtext_append_normal(&(m->fmtext), "    (");
1009    owl_fmtext_append_ztext(&(m->fmtext), zsigbuff);
1010    owl_fmtext_append_normal(&(m->fmtext), ")");
1011    owl_fmtext_append_normal(&(m->fmtext), "\n");
1012    owl_free(zsigbuff);
1013
1014    /* then the indented message */
1015    owl_fmtext_append_ztext(&(m->fmtext), indent);
1016
1017    /* make personal messages bold for smaat users */
1018    if (owl_global_is_userclue(&g, OWL_USERCLUE_CLASSES)) {
1019      if (owl_message_is_personal(m)) {
1020        owl_fmtext_addattr((&m->fmtext), OWL_FMTEXT_ATTR_BOLD);
1021      }
1022    }
1023  }
1024
1025  owl_free(body);
1026  owl_free(indent);
1027}
1028
1029void owl_message_pretty_zsig(owl_message *m, char *buff)
1030{
1031  /* stick a one line version of the zsig in buff */
1032  char *ptr;
1033
1034  strcpy(buff, owl_message_get_zsig(m));
1035  ptr=strchr(buff, '\n');
1036  if (ptr) ptr[0]='\0';
1037}
1038
1039void owl_message_free(owl_message *m)
1040{
1041  int i, j;
1042  owl_pair *p;
1043   
1044  if (owl_message_is_type_zephyr(m) && owl_message_is_direction_in(m)) {
1045    ZFreeNotice(&(m->notice));
1046  }
1047  if (m->time) owl_free(m->time);
1048  if (m->zwriteline) owl_free(m->zwriteline);
1049
1050  /* free all the attributes */
1051  j=owl_list_get_size(&(m->attributes));
1052  for (i=0; i<j; i++) {
1053    p=owl_list_get_element(&(m->attributes), i);
1054    owl_free(owl_pair_get_key(p));
1055    owl_free(owl_pair_get_value(p));
1056    owl_free(p);
1057  }
1058
1059  owl_list_free_simple(&(m->attributes));
1060 
1061  owl_fmtext_free(&(m->fmtext));
1062}
Note: See TracBrowser for help on using the repository browser.