source: message.c @ aecf3e6

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