source: message.c @ d559df9

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