source: message.c @ d09e5a1

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