source: message.c @ 38cf544c

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