source: message.c @ 5789230

barnowl_perlaimdebianowlrelease-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since 5789230 was 5789230, checked in by James M. Kretchmar <kretch@mit.edu>, 18 years ago
'isloginout' and 'isprivate' are now message attributes improved 'info' function lists seperate info for zephyr, aim and also prints all message attributes
  • Property mode set to 100644
File size: 29.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{
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/* We cheat and indent it for now, since we really want this for
81 * the 'info' function.  Later there should just be a generic
82 * function to indent fmtext.
83 */
84void owl_message_attributes_tofmtext(owl_message *m, owl_fmtext *fm) {
85  int i, j;
86  owl_pair *p;
87  char *buff;
88
89  owl_fmtext_init_null(fm);
90
91  j=owl_list_get_size(&(m->attributes));
92  for (i=0; i<j; i++) {
93    p=owl_list_get_element(&(m->attributes), i);
94    buff=owl_sprintf("  %-15.15s: %-35.35s\n", owl_pair_get_key(p), owl_pair_get_value(p));
95    owl_fmtext_append_normal(fm, buff);
96    owl_free(buff);
97  }
98}
99
100owl_fmtext *owl_message_get_fmtext(owl_message *m)
101{
102  return(&(m->fmtext));
103}
104
105void owl_message_set_class(owl_message *m, char *class)
106{
107  owl_message_set_attribute(m, "class", class);
108}
109
110char *owl_message_get_class(owl_message *m)
111{
112  char *class;
113
114  class=owl_message_get_attribute_value(m, "class");
115  if (!class) return("");
116  return(class);
117}
118
119void owl_message_set_instance(owl_message *m, char *inst)
120{
121  owl_message_set_attribute(m, "instance", inst);
122}
123
124char *owl_message_get_instance(owl_message *m)
125{
126  char *instance;
127
128  instance=owl_message_get_attribute_value(m, "instance");
129  if (!instance) return("");
130  return(instance);
131}
132
133void owl_message_set_sender(owl_message *m, char *sender)
134{
135  owl_message_set_attribute(m, "sender", sender);
136}
137
138char *owl_message_get_sender(owl_message *m)
139{
140  char *sender;
141
142  sender=owl_message_get_attribute_value(m, "sender");
143  if (!sender) return("");
144  return(sender);
145}
146
147void owl_message_set_zsig(owl_message *m, char *zsig)
148{
149  owl_message_set_attribute(m, "zsig", zsig);
150}
151
152char *owl_message_get_zsig(owl_message *m)
153{
154  char *zsig;
155
156  zsig=owl_message_get_attribute_value(m, "zsig");
157  if (!zsig) return("");
158  return(zsig);
159}
160
161void owl_message_set_recipient(owl_message *m, char *recip)
162{
163  owl_message_set_attribute(m, "recipient", recip);
164}
165
166char *owl_message_get_recipient(owl_message *m)
167{
168  /* this is stupid for outgoing messages, we need to fix it. */
169
170  char *recip;
171
172  recip=owl_message_get_attribute_value(m, "recipient");
173  if (!recip) return("");
174  return(recip);
175}
176
177void owl_message_set_realm(owl_message *m, char *realm)
178{
179  owl_message_set_attribute(m, "realm", realm);
180}
181
182char *owl_message_get_realm(owl_message *m)
183{
184  char *realm;
185 
186  realm=owl_message_get_attribute_value(m, "realm");
187  if (!realm) return("");
188  return(realm);
189}
190
191void owl_message_set_body(owl_message *m, char *body)
192{
193  owl_message_set_attribute(m, "body", body);
194}
195
196char *owl_message_get_body(owl_message *m)
197{
198  char *body;
199
200  body=owl_message_get_attribute_value(m, "body");
201  if (!body) return("");
202  return(body);
203}
204
205
206void owl_message_set_opcode(owl_message *m, char *opcode)
207{
208  owl_message_set_attribute(m, "opcode", opcode);
209}
210
211char *owl_message_get_opcode(owl_message *m)
212{
213  char *opcode;
214
215  opcode=owl_message_get_attribute_value(m, "opcode");
216  if (!opcode) return("");
217  return(opcode);
218}
219
220
221void owl_message_set_isloginout(owl_message *m)
222{
223  owl_message_set_attribute(m, "isloginout", "");
224}
225
226int owl_message_is_loginout(owl_message *m)
227{
228  char *res;
229
230  res=owl_message_get_attribute_value(m, "isloginout");
231  if (!res) return(0);
232  return(1);
233}
234
235void owl_message_set_isprivate(owl_message *m)
236{
237  owl_message_set_attribute(m, "isprivate", "");
238}
239
240int owl_message_is_private(owl_message *m)
241{
242  char *res;
243
244  res=owl_message_get_attribute_value(m, "isprivate");
245  if (!res) return(0);
246  return(1);
247}
248
249char *owl_message_get_timestr(owl_message *m)
250{
251  return(m->time);
252}
253
254void owl_message_set_type_admin(owl_message *m)
255{
256  m->type=OWL_MESSAGE_TYPE_ADMIN;
257}
258
259void owl_message_set_type_zephyr(owl_message *m)
260{
261  m->type=OWL_MESSAGE_TYPE_ZEPHYR;
262}
263
264void owl_message_set_type_aim(owl_message *m)
265{
266  m->type=OWL_MESSAGE_TYPE_AIM;
267}
268                                               
269int owl_message_is_type_admin(owl_message *m)
270{
271  if (m->type==OWL_MESSAGE_TYPE_ADMIN) return(1);
272  return(0);
273}
274
275int owl_message_is_type_zephyr(owl_message *m)
276{
277  if (m->type==OWL_MESSAGE_TYPE_ZEPHYR) return(1);
278  return(0);
279}
280
281int owl_message_is_type_aim(owl_message *m)
282{
283  if (m->type==OWL_MESSAGE_TYPE_AIM) return(1);
284  return(0);
285}
286
287int owl_message_is_type_generic(owl_message *m)
288{
289  if (m->type==OWL_MESSAGE_TYPE_GENERIC) return(1);
290  return(0);
291}
292
293char *owl_message_type_to_string(owl_message *m)
294{
295  if (m->type==OWL_MESSAGE_TYPE_ADMIN) return("admin");
296  if (m->type==OWL_MESSAGE_TYPE_GENERIC) return("generic");
297  if (m->type==OWL_MESSAGE_TYPE_ZEPHYR) return("zephyr");
298  if (m->type==OWL_MESSAGE_TYPE_AIM) return("aim");
299  if (m->type==OWL_MESSAGE_TYPE_JABBER) return("jabber");
300  if (m->type==OWL_MESSAGE_TYPE_ICQ) return("icq");
301  if (m->type==OWL_MESSAGE_TYPE_MSN) return("msn");
302  return("unknown");
303}
304
305char *owl_message_get_text(owl_message *m)
306{
307  return(owl_fmtext_get_text(&(m->fmtext)));
308}
309
310void owl_message_set_direction_in(owl_message *m)
311{
312  m->direction=OWL_MESSAGE_DIRECTION_IN;
313}
314
315void owl_message_set_direction_out(owl_message *m)
316{
317  m->direction=OWL_MESSAGE_DIRECTION_OUT;
318}
319
320void owl_message_set_direction_none(owl_message *m)
321{
322  m->direction=OWL_MESSAGE_DIRECTION_NONE;
323}
324
325int owl_message_is_direction_in(owl_message *m)
326{
327  if (m->direction==OWL_MESSAGE_DIRECTION_IN) return(1);
328  return(0);
329}
330
331int owl_message_is_direction_out(owl_message *m)
332{
333  if (m->direction==OWL_MESSAGE_DIRECTION_OUT) return(1);
334  return(0);
335}
336
337int owl_message_is_direction_none(owl_message *m)
338{
339  if (m->direction==OWL_MESSAGE_DIRECTION_NONE) return(1);
340  return(0);
341}
342
343int owl_message_get_numlines(owl_message *m)
344{
345  if (m == NULL) return(0);
346  return(owl_fmtext_num_lines(&(m->fmtext)));
347}
348
349void owl_message_mark_delete(owl_message *m)
350{
351  if (m == NULL) return;
352  m->delete=1;
353}
354
355void owl_message_unmark_delete(owl_message *m)
356{
357  if (m == NULL) return;
358  m->delete=0;
359}
360
361char *owl_message_get_zwriteline(owl_message *m)
362{
363  return(m->zwriteline);
364}
365
366void owl_message_set_zwriteline(owl_message *m, char *line)
367{
368  m->zwriteline=strdup(line);
369}
370
371int owl_message_is_delete(owl_message *m)
372{
373  if (m == NULL) return(0);
374  if (m->delete==1) return(1);
375  return(0);
376}
377
378ZNotice_t *owl_message_get_notice(owl_message *m)
379{
380  return(&(m->notice));
381}
382
383char *owl_message_get_hostname(owl_message *m)
384{
385  return(m->hostname);
386}
387
388
389void owl_message_curs_waddstr(owl_message *m, WINDOW *win, int aline, int bline, int acol, int bcol, int color)
390{
391  owl_fmtext a, b;
392
393  owl_fmtext_init_null(&a);
394  owl_fmtext_init_null(&b);
395 
396  owl_fmtext_truncate_lines(&(m->fmtext), aline, bline-aline+1, &a);
397  owl_fmtext_truncate_cols(&a, acol, bcol, &b);
398  if (color!=OWL_COLOR_DEFAULT) {
399    owl_fmtext_colorize(&b, color);
400  }
401
402  if (owl_global_is_search_active(&g)) {
403    owl_fmtext_search_and_highlight(&b, owl_global_get_search_string(&g));
404  }
405     
406  owl_fmtext_curs_waddstr(&b, win);
407
408  owl_fmtext_free(&a);
409  owl_fmtext_free(&b);
410}
411
412int owl_message_is_personal(owl_message *m)
413{
414  if (owl_message_is_type_zephyr(m)) {
415    if (strcasecmp(owl_message_get_class(m), "message")) return(0);
416    if (strcasecmp(owl_message_get_instance(m), "personal")) return(0);
417    if (!strcasecmp(owl_message_get_recipient(m), ZGetSender()) ||
418        !strcasecmp(owl_message_get_sender(m), ZGetSender())) {
419      return(1);
420    }
421  }
422  return(0);
423}
424
425int owl_message_is_from_me(owl_message *m)
426{
427  if (owl_message_is_type_zephyr(m)) {
428    if (!strcasecmp(owl_message_get_sender(m), ZGetSender())) {
429      return(1);
430    } else {
431      return(0);
432    }
433  } else if (owl_message_is_type_aim(m)) {
434    if (!strcasecmp(owl_message_get_sender(m), owl_global_get_aim_screenname(&g))) {
435      return(1);
436    } else {
437      return(0);
438    }
439  } else if (owl_message_is_type_admin(m)) {
440    return(0);
441  }
442  return(0);
443}
444
445int owl_message_is_mail(owl_message *m)
446{
447  if (owl_message_is_type_zephyr(m)) {
448    if (!strcasecmp(owl_message_get_class(m), "mail") && owl_message_is_private(m)) {
449      return(1);
450    } else {
451      return(0);
452    }
453  }
454  return(0);
455}
456
457int owl_message_is_ping(owl_message *m)
458{
459  if (owl_message_is_type_zephyr(m)) {
460    if (!strcasecmp(owl_message_get_opcode(m), "ping")) {
461      return(1);
462    } else {
463      return(0);
464    }
465  }
466  return(0);
467}
468
469int owl_message_is_burningears(owl_message *m)
470{
471  /* we should add a global to cache the short zsender */
472  char sender[LINE], *ptr;
473
474  /* if the message is from us or to us, it doesn't count */
475  if (owl_message_is_from_me(m) || owl_message_is_private(m)) return(0);
476
477  if (owl_message_is_type_zephyr(m)) {
478    strcpy(sender, ZGetSender());
479    ptr=strchr(sender, '@');
480    if (ptr) *ptr='\0';
481  } else if (owl_message_is_type_aim(m)) {
482    strcpy(sender, owl_global_get_aim_screenname(&g));
483  } else {
484    return(0);
485  }
486
487  if (stristr(owl_message_get_body(m), sender)) {
488    return(1);
489  }
490  return(0);
491}
492
493/* caller must free return value. */
494char *owl_message_get_cc(owl_message *m)
495{
496  char *cur, *out, *end;
497
498  cur = owl_message_get_body(m);
499  while (*cur && *cur==' ') cur++;
500  if (strncasecmp(cur, "cc:", 3)) return(NULL);
501  cur+=3;
502  while (*cur && *cur==' ') cur++;
503  out = owl_strdup(cur);
504  end = strchr(out, '\n');
505  if (end) end[0] = '\0';
506  return(out);
507}
508
509int owl_message_get_id(owl_message *m)
510{
511  return(m->id);
512}
513                                       
514int owl_message_search(owl_message *m, char *string)
515{
516  /* return 1 if the message contains "string", 0 otherwise.  This is
517   * case insensitive because the functions it uses are */
518
519  return (owl_fmtext_search(&(m->fmtext), string));
520}
521
522void owl_message_create(owl_message *m, char *header, char *text)
523{
524  char *indent;
525
526  owl_message_init(m);
527  owl_message_set_body(m, text);
528
529  indent=owl_malloc(strlen(text)+owl_text_num_lines(text)*OWL_MSGTAB+10);
530  owl_text_indent(indent, text, OWL_MSGTAB);
531  owl_fmtext_init_null(&(m->fmtext));
532  owl_fmtext_append_normal(&(m->fmtext), OWL_TABSTR);
533  owl_fmtext_append_ztext(&(m->fmtext), header);
534  owl_fmtext_append_normal(&(m->fmtext), "\n");
535  owl_fmtext_append_ztext(&(m->fmtext), indent);
536  if (text[strlen(text)-1]!='\n') {
537    owl_fmtext_append_normal(&(m->fmtext), "\n");
538  }
539
540  owl_free(indent);
541}
542
543void owl_message_create_incoming_aim(owl_message *m, char *sender, char *recipient, char *text)
544{
545  char *indent;
546
547  owl_message_init(m);
548  owl_message_set_body(m, text);
549  owl_message_set_sender(m, sender);
550  owl_message_set_recipient(m, recipient);
551  owl_message_set_type_aim(m);
552  owl_message_set_direction_in(m);
553
554  /* for now, all AIM messages are private messages since we don't do chat rooms */
555  owl_message_set_isprivate(m);
556
557  indent=owl_malloc(strlen(text)+owl_text_num_lines(text)*OWL_MSGTAB+10);
558  owl_text_indent(indent, text, OWL_MSGTAB);
559  owl_fmtext_init_null(&(m->fmtext));
560  owl_fmtext_append_bold(&(m->fmtext), OWL_TABSTR);
561  owl_fmtext_append_bold(&(m->fmtext), "AIM from ");
562  owl_fmtext_append_bold(&(m->fmtext), sender);
563  owl_fmtext_append_bold(&(m->fmtext), "\n");
564  owl_fmtext_append_bold(&(m->fmtext), indent);
565  if (text[strlen(text)-1]!='\n') {
566    owl_fmtext_append_bold(&(m->fmtext), "\n");
567  }
568 
569  owl_free(indent);
570}
571
572void owl_message_create_outgoing_aim(owl_message *m, char *sender, char *recipient, char *text)
573{
574  char *indent;
575
576  owl_message_init(m);
577  owl_message_set_body(m, text);
578  owl_message_set_sender(m, sender);
579  owl_message_set_recipient(m, recipient);
580  owl_message_set_type_aim(m);
581  owl_message_set_direction_out(m);
582
583  indent=owl_malloc(strlen(text)+owl_text_num_lines(text)*OWL_MSGTAB+10);
584  owl_text_indent(indent, text, OWL_MSGTAB);
585  owl_fmtext_init_null(&(m->fmtext));
586  owl_fmtext_append_normal(&(m->fmtext), OWL_TABSTR);
587  owl_fmtext_append_normal(&(m->fmtext), "AIM sent to ");
588  owl_fmtext_append_normal(&(m->fmtext), recipient);
589  owl_fmtext_append_normal(&(m->fmtext), "\n");
590  owl_fmtext_append_ztext(&(m->fmtext), indent);
591  if (text[strlen(text)-1]!='\n') {
592    owl_fmtext_append_normal(&(m->fmtext), "\n");
593  }
594 
595  owl_free(indent);
596}
597
598/* For login direction == 0
599 * For logout direction == 1
600 */
601void owl_message_create_aim_login(owl_message *m, int direction, char *screenname)
602{
603  owl_message_init(m);
604  owl_message_set_body(m, "");
605  owl_message_set_sender(m, screenname);
606  owl_message_set_recipient(m, owl_global_get_aim_screenname(&g));
607  owl_message_set_type_aim(m);
608  owl_message_set_direction_in(m);
609
610  owl_message_set_isloginout(m);
611
612  owl_fmtext_init_null(&(m->fmtext));
613  owl_fmtext_append_normal(&(m->fmtext), OWL_TABSTR);
614  if (direction==0) {
615    owl_fmtext_append_bold(&(m->fmtext), "AIM LOGIN");
616  } else if (direction==1) {
617    owl_fmtext_append_bold(&(m->fmtext), "AIM LOGOUT");
618  }
619  owl_fmtext_append_normal(&(m->fmtext), " for ");
620  owl_fmtext_append_normal(&(m->fmtext), screenname);
621  owl_fmtext_append_normal(&(m->fmtext), "\n");
622}
623
624void owl_message_create_admin(owl_message *m, char *header, char *text)
625{
626  char *indent;
627
628  owl_message_init(m);
629  owl_message_set_type_admin(m);
630
631  owl_message_set_body(m, text);
632
633  /* do something to make it clear the notice shouldn't be used for now */
634
635  indent=owl_malloc(strlen(text)+owl_text_num_lines(text)*OWL_MSGTAB+10);
636  owl_text_indent(indent, text, OWL_MSGTAB);
637  owl_fmtext_init_null(&(m->fmtext));
638  owl_fmtext_append_normal(&(m->fmtext), OWL_TABSTR);
639  owl_fmtext_append_bold(&(m->fmtext), "OWL ADMIN ");
640  owl_fmtext_append_ztext(&(m->fmtext), header);
641  owl_fmtext_append_normal(&(m->fmtext), "\n");
642  owl_fmtext_append_ztext(&(m->fmtext), indent);
643  if (text[strlen(text)-1]!='\n') {
644    owl_fmtext_append_normal(&(m->fmtext), "\n");
645  }
646
647  owl_free(indent);
648}
649
650void owl_message_create_from_znotice(owl_message *m, ZNotice_t *n)
651{
652  struct hostent *hent;
653  int k;
654  char *ptr, *tmp, *tmp2;
655
656  owl_message_init(m);
657 
658  owl_message_set_type_zephyr(m);
659  owl_message_set_direction_in(m);
660 
661  /* first save the full notice */
662  memcpy(&(m->notice), n, sizeof(ZNotice_t));
663
664  /* a little gross, we'll reaplace \r's with ' ' for now */
665  owl_zephyr_hackaway_cr(&(m->notice));
666 
667  m->delete=0;
668
669  /* set other info */
670  owl_message_set_sender(m, n->z_sender);
671  owl_message_set_class(m, n->z_class);
672  owl_message_set_instance(m, n->z_class_inst);
673  owl_message_set_recipient(m, n->z_recipient);
674  if (n->z_opcode) {
675    owl_message_set_opcode(m, n->z_opcode);
676  } else {
677    owl_message_set_opcode(m, "");
678  }
679  owl_message_set_zsig(m, n->z_message);
680
681  if ((ptr=strchr(n->z_recipient, '@'))!=NULL) {
682    owl_message_set_realm(m, ptr+1);
683  } else {
684    owl_message_set_realm(m, ZGetRealm());
685  }
686
687  /* Set the "isloginout" attribute if it's a login message */
688  if (!strcasecmp(n->z_class, "login")) {
689    owl_message_set_isloginout(m);
690  }
691
692  /* is the "isprivate" attribute if it's a private zephyr */
693  if (!strcasecmp(n->z_recipient, ZGetSender())) {
694    owl_message_set_isprivate(m);
695  }
696
697  m->zwriteline=strdup("");
698
699  /* set the body */
700  ptr=owl_zephyr_get_message(n, &k);
701  tmp=owl_malloc(k+10);
702  memcpy(tmp, ptr, k);
703  tmp[k]='\0';
704  if (owl_global_is_newlinestrip(&g)) {
705    tmp2=owl_util_stripnewlines(tmp);
706    owl_message_set_body(m, tmp2);
707    owl_free(tmp2);
708  } else {
709    owl_message_set_body(m, tmp);
710  }
711  owl_free(tmp);
712
713#ifdef OWL_ENABLE_ZCRYPT
714  /* if zcrypt is enabled try to decrypt the message */
715  if (owl_global_is_zcrypt(&g) && !strcasecmp(n->z_opcode, "crypt")) {
716    char *out;
717    int ret;
718
719    out=owl_malloc(strlen(owl_message_get_body(m))*16+20);
720    ret=zcrypt_decrypt(out, owl_message_get_body(m), owl_message_get_class(m), owl_message_get_instance(m));
721    if (ret==0) {
722      owl_message_set_body(m, out);
723    } else {
724      owl_free(out);
725    }
726  }
727#endif 
728
729  /* save the hostname */
730  owl_function_debugmsg("About to do gethostbyaddr");
731  hent=gethostbyaddr((char *) &(n->z_uid.zuid_addr), sizeof(n->z_uid.zuid_addr), AF_INET);
732  if (hent && hent->h_name) {
733    strcpy(m->hostname, hent->h_name);
734  } else {
735    strcpy(m->hostname, inet_ntoa(n->z_sender_addr));
736  }
737
738  /* save the time */
739  m->time=owl_strdup(ctime((time_t *) &n->z_time.tv_sec));
740  m->time[strlen(m->time)-1]='\0';
741
742  /* create the formatted message */
743  if (owl_global_is_config_format(&g)) {
744    _owl_message_make_text_from_config(m);
745  } else if (owl_global_is_userclue(&g, OWL_USERCLUE_CLASSES)) {
746    _owl_message_make_text_from_notice_standard(m);
747  } else {
748    _owl_message_make_text_from_notice_simple(m);
749  }
750
751}
752
753void owl_message_create_from_zwriteline(owl_message *m, char *line, char *body, char *zsig)
754{
755  owl_zwrite z;
756  int ret;
757 
758  owl_message_init(m);
759
760  /* create a zwrite for the purpose of filling in other message fields */
761  owl_zwrite_create_from_line(&z, line);
762
763  /* set things */
764  owl_message_set_direction_out(m);
765  owl_message_set_type_zephyr(m);
766  owl_message_set_sender(m, ZGetSender());
767  owl_message_set_class(m, owl_zwrite_get_class(&z));
768  owl_message_set_instance(m, owl_zwrite_get_instance(&z));
769  owl_message_set_recipient(m,
770                            long_zuser(owl_zwrite_get_recip_n(&z, 0))); /* only gets the first user, must fix */
771  owl_message_set_opcode(m, owl_zwrite_get_opcode(&z));
772  owl_message_set_realm(m, owl_zwrite_get_realm(&z)); /* also a hack, but not here */
773  m->zwriteline=owl_strdup(line);
774  owl_message_set_body(m, body);
775  owl_message_set_zsig(m, zsig);
776 
777  /* save the hostname */
778  ret=gethostname(m->hostname, MAXHOSTNAMELEN);
779  if (ret) {
780    strcpy(m->hostname, "localhost");
781  }
782
783  /* create the formatted message */
784  if (owl_global_is_config_format(&g)) {
785    _owl_message_make_text_from_config(m);
786  } else if (owl_global_is_userclue(&g, OWL_USERCLUE_CLASSES)) {
787    _owl_message_make_text_from_zwriteline_standard(m);
788  } else {
789    _owl_message_make_text_from_zwriteline_simple(m);
790  }
791
792  owl_zwrite_free(&z);
793}
794
795void _owl_message_make_text_from_config(owl_message *m)
796{
797  char *body, *indent;
798
799  owl_fmtext_init_null(&(m->fmtext));
800
801  /* get body from the config */
802  body=owl_config_getmsg(m, 1);
803 
804  /* indent */
805  indent=owl_malloc(strlen(body)+owl_text_num_lines(body)*OWL_TAB+10);
806  owl_text_indent(indent, body, OWL_TAB);
807
808  /* fmtext_append.  This needs to change */
809  owl_fmtext_append_ztext(&(m->fmtext), indent);
810
811  owl_free(indent);
812  owl_free(body);
813}
814
815void _owl_message_make_text_from_zwriteline_standard(owl_message *m)
816{
817  char *indent, *text, *zsigbuff, *foo;
818
819  text=owl_message_get_body(m);
820
821  indent=owl_malloc(strlen(text)+owl_text_num_lines(text)*OWL_MSGTAB+10);
822  owl_text_indent(indent, text, OWL_MSGTAB);
823  owl_fmtext_init_null(&(m->fmtext));
824  owl_fmtext_append_normal(&(m->fmtext), OWL_TABSTR);
825  owl_fmtext_append_normal(&(m->fmtext), "Zephyr sent to ");
826  foo=short_zuser(owl_message_get_recipient(m));
827  owl_fmtext_append_normal(&(m->fmtext), foo);
828  owl_free(foo);
829  owl_fmtext_append_normal(&(m->fmtext), "  (Zsig: ");
830
831  zsigbuff=owl_malloc(strlen(owl_message_get_zsig(m))+30);
832  owl_message_pretty_zsig(m, zsigbuff);
833  owl_fmtext_append_ztext(&(m->fmtext), zsigbuff);
834  owl_free(zsigbuff);
835 
836  owl_fmtext_append_normal(&(m->fmtext), ")");
837  owl_fmtext_append_normal(&(m->fmtext), "\n");
838  owl_fmtext_append_ztext(&(m->fmtext), indent);
839  if (text[strlen(text)-1]!='\n') {
840    owl_fmtext_append_normal(&(m->fmtext), "\n");
841  }
842
843  owl_free(indent);
844}
845
846void _owl_message_make_text_from_zwriteline_simple(owl_message *m)
847{
848  char *indent, *text, *zsigbuff, *foo;
849
850  text=owl_message_get_body(m);
851
852  indent=owl_malloc(strlen(text)+owl_text_num_lines(text)*OWL_MSGTAB+10);
853  owl_text_indent(indent, text, OWL_MSGTAB);
854  owl_fmtext_init_null(&(m->fmtext));
855  owl_fmtext_append_normal(&(m->fmtext), OWL_TABSTR);
856  owl_fmtext_append_normal(&(m->fmtext), "To: ");
857  foo=short_zuser(owl_message_get_recipient(m));
858  owl_fmtext_append_normal(&(m->fmtext), foo);
859  owl_free(foo);
860  owl_fmtext_append_normal(&(m->fmtext), "  (Zsig: ");
861
862  zsigbuff=owl_malloc(strlen(owl_message_get_zsig(m))+30);
863  owl_message_pretty_zsig(m, zsigbuff);
864  owl_fmtext_append_ztext(&(m->fmtext), zsigbuff);
865  owl_free(zsigbuff);
866 
867  owl_fmtext_append_normal(&(m->fmtext), ")");
868  owl_fmtext_append_normal(&(m->fmtext), "\n");
869  owl_fmtext_append_ztext(&(m->fmtext), indent);
870  if (text[strlen(text)-1]!='\n') {
871    owl_fmtext_append_normal(&(m->fmtext), "\n");
872  }
873
874  owl_free(indent);
875}
876
877void _owl_message_make_text_from_notice_standard(owl_message *m)
878{
879  char *body, *indent, *ptr, *zsigbuff, frombuff[LINE];
880  ZNotice_t *n;
881
882  n=&(m->notice);
883 
884  /* get the body */
885  body=owl_malloc(strlen(owl_message_get_body(m))+30);
886  strcpy(body, owl_message_get_body(m));
887
888  /* add a newline if we need to */
889  if (body[0]!='\0' && body[strlen(body)-1]!='\n') {
890    strcat(body, "\n");
891  }
892
893  /* do the indenting into indent */
894  indent=owl_malloc(strlen(body)+owl_text_num_lines(body)*OWL_MSGTAB+10);
895  owl_text_indent(indent, body, OWL_MSGTAB);
896
897  /* edit the from addr for printing */
898  strcpy(frombuff, owl_message_get_sender(m));
899  ptr=strchr(frombuff, '@');
900  if (ptr && !strncmp(ptr+1, ZGetRealm(), strlen(ZGetRealm()))) {
901    *ptr='\0';
902  }
903
904  /* set the message for printing */
905  owl_fmtext_init_null(&(m->fmtext));
906  owl_fmtext_append_normal(&(m->fmtext), OWL_TABSTR);
907
908  if (!strcasecmp(owl_message_get_opcode(m), "ping") && owl_message_is_private(m)) {
909    owl_fmtext_append_bold(&(m->fmtext), "PING");
910    owl_fmtext_append_normal(&(m->fmtext), " from ");
911    owl_fmtext_append_bold(&(m->fmtext), frombuff);
912    owl_fmtext_append_normal(&(m->fmtext), "\n");
913  } else if (!strcasecmp(n->z_class, "login")) {
914    char *ptr, host[LINE], tty[LINE];
915    int len;
916
917    ptr=owl_zephyr_get_field(n, 1, &len);
918    strncpy(host, ptr, len);
919    host[len]='\0';
920    ptr=owl_zephyr_get_field(n, 3, &len);
921    strncpy(tty, ptr, len);
922    tty[len]='\0';
923   
924    if (!strcasecmp(n->z_opcode, "user_login")) {
925      owl_fmtext_append_bold(&(m->fmtext), "LOGIN");
926    } else if (!strcasecmp(n->z_opcode, "user_logout")) {
927      owl_fmtext_append_bold(&(m->fmtext), "LOGOUT");
928    }
929    owl_fmtext_append_normal(&(m->fmtext), " for ");
930    ptr=short_zuser(n->z_class_inst);
931    owl_fmtext_append_bold(&(m->fmtext), ptr);
932    owl_free(ptr);
933    owl_fmtext_append_normal(&(m->fmtext), " at ");
934    owl_fmtext_append_normal(&(m->fmtext), host);
935    owl_fmtext_append_normal(&(m->fmtext), " ");
936    owl_fmtext_append_normal(&(m->fmtext), tty);
937    owl_fmtext_append_normal(&(m->fmtext), "\n");
938  } else {
939    owl_fmtext_append_normal(&(m->fmtext), owl_message_get_class(m));
940    owl_fmtext_append_normal(&(m->fmtext), " / ");
941    owl_fmtext_append_normal(&(m->fmtext), owl_message_get_instance(m));
942    owl_fmtext_append_normal(&(m->fmtext), " / ");
943    owl_fmtext_append_bold(&(m->fmtext), frombuff);
944    if (strcasecmp(owl_message_get_realm(m), ZGetRealm())) {
945      owl_fmtext_append_normal(&(m->fmtext), " {");
946      owl_fmtext_append_normal(&(m->fmtext), owl_message_get_realm(m));
947      owl_fmtext_append_normal(&(m->fmtext), "} ");
948    }
949    if (n->z_opcode[0]!='\0') {
950      owl_fmtext_append_normal(&(m->fmtext), " [");
951      owl_fmtext_append_normal(&(m->fmtext), owl_message_get_opcode(m));
952      owl_fmtext_append_normal(&(m->fmtext), "] ");
953    }
954
955    /* stick on the zsig */
956    zsigbuff=owl_malloc(strlen(owl_message_get_zsig(m))+30);
957    owl_message_pretty_zsig(m, zsigbuff);
958    owl_fmtext_append_normal(&(m->fmtext), "    (");
959    owl_fmtext_append_ztext(&(m->fmtext), zsigbuff);
960    owl_fmtext_append_normal(&(m->fmtext), ")");
961    owl_fmtext_append_normal(&(m->fmtext), "\n");
962    owl_free(zsigbuff);
963
964    /* then the indented message */
965    owl_fmtext_append_ztext(&(m->fmtext), indent);
966
967    /* make private messages bold for smaat users */
968    if (owl_global_is_userclue(&g, OWL_USERCLUE_CLASSES)) {
969      if (owl_message_is_personal(m)) {
970        owl_fmtext_addattr((&m->fmtext), OWL_FMTEXT_ATTR_BOLD);
971      }
972    }
973  }
974
975  owl_free(body);
976  owl_free(indent);
977}
978
979void _owl_message_make_text_from_notice_simple(owl_message *m)
980{
981  char *body, *indent, *ptr, *zsigbuff, frombuff[LINE];
982  ZNotice_t *n;
983
984  n=&(m->notice);
985
986  /* get the body */
987  body=owl_strdup(owl_message_get_body(m));
988  body=realloc(body, strlen(body)+30);
989
990  /* add a newline if we need to */
991  if (body[0]!='\0' && body[strlen(body)-1]!='\n') {
992    strcat(body, "\n");
993  }
994
995  /* do the indenting into indent */
996  indent=owl_malloc(strlen(body)+owl_text_num_lines(body)*OWL_MSGTAB+10);
997  owl_text_indent(indent, body, OWL_MSGTAB);
998
999  /* edit the from addr for printing */
1000  strcpy(frombuff, owl_message_get_sender(m));
1001  ptr=strchr(frombuff, '@');
1002  if (ptr && !strncmp(ptr+1, ZGetRealm(), strlen(ZGetRealm()))) {
1003    *ptr='\0';
1004  }
1005
1006  /* set the message for printing */
1007  owl_fmtext_init_null(&(m->fmtext));
1008  owl_fmtext_append_normal(&(m->fmtext), OWL_TABSTR);
1009
1010  if (!strcasecmp(owl_message_get_opcode(m), "ping")) {
1011    owl_fmtext_append_bold(&(m->fmtext), "PING");
1012    owl_fmtext_append_normal(&(m->fmtext), " from ");
1013    owl_fmtext_append_bold(&(m->fmtext), frombuff);
1014    owl_fmtext_append_normal(&(m->fmtext), "\n");
1015  } else if (!strcasecmp(owl_message_get_class(m), "login")) {
1016    char *ptr, host[LINE], tty[LINE];
1017    int len;
1018
1019    ptr=owl_zephyr_get_field(n, 1, &len);
1020    strncpy(host, ptr, len);
1021    host[len]='\0';
1022    ptr=owl_zephyr_get_field(n, 3, &len);
1023    strncpy(tty, ptr, len);
1024    tty[len]='\0';
1025   
1026    if (!strcasecmp(owl_message_get_opcode(m), "user_login")) {
1027      owl_fmtext_append_bold(&(m->fmtext), "LOGIN");
1028    } else if (!strcasecmp(owl_message_get_opcode(m), "user_logout")) {
1029      owl_fmtext_append_bold(&(m->fmtext), "LOGOUT");
1030    }
1031    owl_fmtext_append_normal(&(m->fmtext), " for ");
1032    ptr=short_zuser(owl_message_get_instance(m));
1033    owl_fmtext_append_bold(&(m->fmtext), ptr);
1034    owl_free(ptr);
1035    owl_fmtext_append_normal(&(m->fmtext), " at ");
1036    owl_fmtext_append_normal(&(m->fmtext), host);
1037    owl_fmtext_append_normal(&(m->fmtext), " ");
1038    owl_fmtext_append_normal(&(m->fmtext), tty);
1039    owl_fmtext_append_normal(&(m->fmtext), "\n");
1040  } else {
1041    owl_fmtext_append_normal(&(m->fmtext), "From: ");
1042    if (strcasecmp(owl_message_get_class(m), "message")) {
1043      owl_fmtext_append_normal(&(m->fmtext), "Class ");
1044      owl_fmtext_append_normal(&(m->fmtext), owl_message_get_class(m));
1045      owl_fmtext_append_normal(&(m->fmtext), " / Instance ");
1046      owl_fmtext_append_normal(&(m->fmtext), owl_message_get_instance(m));
1047      owl_fmtext_append_normal(&(m->fmtext), " / ");
1048    }
1049    owl_fmtext_append_normal(&(m->fmtext), frombuff);
1050    if (strcasecmp(owl_message_get_realm(m), ZGetRealm())) {
1051      owl_fmtext_append_normal(&(m->fmtext), " {");
1052      owl_fmtext_append_normal(&(m->fmtext), owl_message_get_realm(m));
1053      owl_fmtext_append_normal(&(m->fmtext), "} ");
1054    }
1055
1056    /* stick on the zsig */
1057    zsigbuff=owl_malloc(strlen(owl_message_get_zsig(m))+30);
1058    owl_message_pretty_zsig(m, zsigbuff);
1059    owl_fmtext_append_normal(&(m->fmtext), "    (");
1060    owl_fmtext_append_ztext(&(m->fmtext), zsigbuff);
1061    owl_fmtext_append_normal(&(m->fmtext), ")");
1062    owl_fmtext_append_normal(&(m->fmtext), "\n");
1063    owl_free(zsigbuff);
1064
1065    /* then the indented message */
1066    owl_fmtext_append_ztext(&(m->fmtext), indent);
1067
1068    /* make personal messages bold for smaat users */
1069    if (owl_global_is_userclue(&g, OWL_USERCLUE_CLASSES)) {
1070      if (owl_message_is_personal(m)) {
1071        owl_fmtext_addattr((&m->fmtext), OWL_FMTEXT_ATTR_BOLD);
1072      }
1073    }
1074  }
1075
1076  owl_free(body);
1077  owl_free(indent);
1078}
1079
1080void owl_message_pretty_zsig(owl_message *m, char *buff)
1081{
1082  /* stick a one line version of the zsig in buff */
1083  char *ptr;
1084
1085  strcpy(buff, owl_message_get_zsig(m));
1086  ptr=strchr(buff, '\n');
1087  if (ptr) ptr[0]='\0';
1088}
1089
1090void owl_message_free(owl_message *m)
1091{
1092  int i, j;
1093  owl_pair *p;
1094   
1095  if (owl_message_is_type_zephyr(m) && owl_message_is_direction_in(m)) {
1096    ZFreeNotice(&(m->notice));
1097  }
1098  if (m->time) owl_free(m->time);
1099  if (m->zwriteline) owl_free(m->zwriteline);
1100
1101  /* free all the attributes */
1102  j=owl_list_get_size(&(m->attributes));
1103  for (i=0; i<j; i++) {
1104    p=owl_list_get_element(&(m->attributes), i);
1105    owl_free(owl_pair_get_key(p));
1106    owl_free(owl_pair_get_value(p));
1107    owl_free(p);
1108  }
1109
1110  owl_list_free_simple(&(m->attributes));
1111 
1112  owl_fmtext_free(&(m->fmtext));
1113}
Note: See TracBrowser for help on using the repository browser.