source: message.c @ c269e22

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