source: message.c @ 6e05655

barnowl_perlaimdebianowlrelease-1.10release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since 6e05655 was d559df9, checked in by James M. Kretchmar <kretch@mit.edu>, 21 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
RevLine 
[7d4fbcd]1#include <zephyr/zephyr.h>
2#include <stdlib.h>
[b45293f]3#include <unistd.h>
[7d4fbcd]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
[1aee7d9]14static const char fileIdent[] = "$Id$";
15
[0ff8fb57]16void owl_message_init(owl_message *m)
17{
[7d4fbcd]18  time_t t;
19
20  m->id=owl_global_get_nextmsgid(&g);
[4b464a4]21  m->type=OWL_MESSAGE_TYPE_GENERIC;
22  owl_message_set_direction_none(m);
[7d4fbcd]23  m->delete=0;
24  strcpy(m->hostname, "");
25  m->zwriteline=strdup("");
26
[d0d65df]27  owl_list_create(&(m->attributes));
28 
[7d4fbcd]29  /* save the time */
30  t=time(NULL);
31  m->time=owl_strdup(ctime(&t));
32  m->time[strlen(m->time)-1]='\0';
[4b464a4]33}
34
[0ff8fb57]35void owl_message_set_attribute(owl_message *m, char *attrname, char *attrvalue)
36{
[d0d65df]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
[0ff8fb57]62char *owl_message_get_attribute_value(owl_message *m, char *attrname)
63{
[d0d65df]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
[5789230]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}
[4b464a4]99
[0ff8fb57]100owl_fmtext *owl_message_get_fmtext(owl_message *m)
101{
[4b464a4]102  return(&(m->fmtext));
103}
104
[0ff8fb57]105void owl_message_set_class(owl_message *m, char *class)
106{
[d0d65df]107  owl_message_set_attribute(m, "class", class);
[4b464a4]108}
109
[0ff8fb57]110char *owl_message_get_class(owl_message *m)
111{
[d0d65df]112  char *class;
113
114  class=owl_message_get_attribute_value(m, "class");
115  if (!class) return("");
116  return(class);
[4b464a4]117}
118
[0ff8fb57]119void owl_message_set_instance(owl_message *m, char *inst)
120{
[d0d65df]121  owl_message_set_attribute(m, "instance", inst);
[4b464a4]122}
123
[0ff8fb57]124char *owl_message_get_instance(owl_message *m)
125{
[d0d65df]126  char *instance;
127
128  instance=owl_message_get_attribute_value(m, "instance");
129  if (!instance) return("");
130  return(instance);
[4b464a4]131}
132
[0ff8fb57]133void owl_message_set_sender(owl_message *m, char *sender)
134{
[d0d65df]135  owl_message_set_attribute(m, "sender", sender);
[4b464a4]136}
137
[0ff8fb57]138char *owl_message_get_sender(owl_message *m)
139{
[d0d65df]140  char *sender;
141
142  sender=owl_message_get_attribute_value(m, "sender");
143  if (!sender) return("");
144  return(sender);
[4b464a4]145}
146
[0ff8fb57]147void owl_message_set_zsig(owl_message *m, char *zsig)
148{
[d0d65df]149  owl_message_set_attribute(m, "zsig", zsig);
[b45293f]150}
151
[0ff8fb57]152char *owl_message_get_zsig(owl_message *m)
153{
[d0d65df]154  char *zsig;
155
156  zsig=owl_message_get_attribute_value(m, "zsig");
157  if (!zsig) return("");
158  return(zsig);
[b45293f]159}
160
[0ff8fb57]161void owl_message_set_recipient(owl_message *m, char *recip)
162{
[d0d65df]163  owl_message_set_attribute(m, "recipient", recip);
[4b464a4]164}
165
[0ff8fb57]166char *owl_message_get_recipient(owl_message *m)
167{
[4b464a4]168  /* this is stupid for outgoing messages, we need to fix it. */
[d0d65df]169
170  char *recip;
[0ff8fb57]171
172  recip=owl_message_get_attribute_value(m, "recipient");
[d0d65df]173  if (!recip) return("");
174  return(recip);
[4b464a4]175}
176
[0ff8fb57]177void owl_message_set_realm(owl_message *m, char *realm)
178{
[d0d65df]179  owl_message_set_attribute(m, "realm", realm);
[4b464a4]180}
181
[0ff8fb57]182char *owl_message_get_realm(owl_message *m)
183{
[d0d65df]184  char *realm;
185 
186  realm=owl_message_get_attribute_value(m, "realm");
187  if (!realm) return("");
188  return(realm);
189}
190
[0ff8fb57]191void owl_message_set_body(owl_message *m, char *body)
192{
[d0d65df]193  owl_message_set_attribute(m, "body", body);
194}
195
[0ff8fb57]196char *owl_message_get_body(owl_message *m)
197{
[d0d65df]198  char *body;
199
200  body=owl_message_get_attribute_value(m, "body");
201  if (!body) return("");
202  return(body);
[4b464a4]203}
204
[d0d65df]205
[0ff8fb57]206void owl_message_set_opcode(owl_message *m, char *opcode)
207{
[d0d65df]208  owl_message_set_attribute(m, "opcode", opcode);
[4b464a4]209}
210
[0ff8fb57]211char *owl_message_get_opcode(owl_message *m)
212{
[d0d65df]213  char *opcode;
214
215  opcode=owl_message_get_attribute_value(m, "opcode");
216  if (!opcode) return("");
217  return(opcode);
[4b464a4]218}
219
[5789230]220
[d559df9]221void owl_message_set_islogin(owl_message *m)
[5789230]222{
[d559df9]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");
[5789230]230}
231
232int owl_message_is_loginout(owl_message *m)
233{
234  char *res;
235
[d559df9]236  res=owl_message_get_attribute_value(m, "loginout");
[5789230]237  if (!res) return(0);
238  return(1);
239}
240
[d559df9]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
[5789230]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
[0ff8fb57]276char *owl_message_get_timestr(owl_message *m)
277{
[4b464a4]278  return(m->time);
279}
280
[0ff8fb57]281void owl_message_set_type_admin(owl_message *m)
282{
[4b464a4]283  m->type=OWL_MESSAGE_TYPE_ADMIN;
284}
285
[0ff8fb57]286void owl_message_set_type_zephyr(owl_message *m)
287{
[4b464a4]288  m->type=OWL_MESSAGE_TYPE_ZEPHYR;
289}
[d09e5a1]290
[0ff8fb57]291void owl_message_set_type_aim(owl_message *m)
292{
[d09e5a1]293  m->type=OWL_MESSAGE_TYPE_AIM;
294}
[4b464a4]295                                               
[0ff8fb57]296int owl_message_is_type_admin(owl_message *m)
297{
[4b464a4]298  if (m->type==OWL_MESSAGE_TYPE_ADMIN) return(1);
299  return(0);
300}
301
[0ff8fb57]302int owl_message_is_type_zephyr(owl_message *m)
303{
[4b464a4]304  if (m->type==OWL_MESSAGE_TYPE_ZEPHYR) return(1);
305  return(0);
306}
307
[0ff8fb57]308int owl_message_is_type_aim(owl_message *m)
309{
[d09e5a1]310  if (m->type==OWL_MESSAGE_TYPE_AIM) return(1);
311  return(0);
312}
313
[0ff8fb57]314int owl_message_is_type_generic(owl_message *m)
315{
[4b464a4]316  if (m->type==OWL_MESSAGE_TYPE_GENERIC) return(1);
317  return(0);
318}
319
[df0d93a]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
[0ff8fb57]332char *owl_message_get_text(owl_message *m)
333{
[4b464a4]334  return(owl_fmtext_get_text(&(m->fmtext)));
335}
336
[0ff8fb57]337void owl_message_set_direction_in(owl_message *m)
338{
[4b464a4]339  m->direction=OWL_MESSAGE_DIRECTION_IN;
340}
341
[0ff8fb57]342void owl_message_set_direction_out(owl_message *m)
343{
[4b464a4]344  m->direction=OWL_MESSAGE_DIRECTION_OUT;
345}
346
[0ff8fb57]347void owl_message_set_direction_none(owl_message *m)
348{
[4b464a4]349  m->direction=OWL_MESSAGE_DIRECTION_NONE;
350}
351
[0ff8fb57]352int owl_message_is_direction_in(owl_message *m)
353{
[4b464a4]354  if (m->direction==OWL_MESSAGE_DIRECTION_IN) return(1);
355  return(0);
356}
357
[0ff8fb57]358int owl_message_is_direction_out(owl_message *m)
359{
[4b464a4]360  if (m->direction==OWL_MESSAGE_DIRECTION_OUT) return(1);
361  return(0);
362}
363
[0ff8fb57]364int owl_message_is_direction_none(owl_message *m)
365{
[4b464a4]366  if (m->direction==OWL_MESSAGE_DIRECTION_NONE) return(1);
367  return(0);
368}
369
[0ff8fb57]370int owl_message_get_numlines(owl_message *m)
371{
[4b464a4]372  if (m == NULL) return(0);
373  return(owl_fmtext_num_lines(&(m->fmtext)));
374}
375
[0ff8fb57]376void owl_message_mark_delete(owl_message *m)
377{
[4b464a4]378  if (m == NULL) return;
379  m->delete=1;
380}
381
[0ff8fb57]382void owl_message_unmark_delete(owl_message *m)
383{
[4b464a4]384  if (m == NULL) return;
385  m->delete=0;
386}
387
[0ff8fb57]388char *owl_message_get_zwriteline(owl_message *m)
389{
[4b464a4]390  return(m->zwriteline);
391}
392
[0ff8fb57]393void owl_message_set_zwriteline(owl_message *m, char *line)
394{
[4b464a4]395  m->zwriteline=strdup(line);
396}
397
[0ff8fb57]398int owl_message_is_delete(owl_message *m)
399{
[4b464a4]400  if (m == NULL) return(0);
401  if (m->delete==1) return(1);
402  return(0);
403}
404
[0ff8fb57]405ZNotice_t *owl_message_get_notice(owl_message *m)
406{
[4b464a4]407  return(&(m->notice));
408}
409
[0ff8fb57]410char *owl_message_get_hostname(owl_message *m)
411{
[4b464a4]412  return(m->hostname);
413}
414
415
[0ff8fb57]416void owl_message_curs_waddstr(owl_message *m, WINDOW *win, int aline, int bline, int acol, int bcol, int color)
417{
[4b464a4]418  owl_fmtext a, b;
419
[af2ca19]420  owl_fmtext_init_null(&a);
421  owl_fmtext_init_null(&b);
422 
[4b464a4]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
[0ff8fb57]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  }
[4b464a4]469  return(0);
470}
471
[0ff8fb57]472int owl_message_is_mail(owl_message *m)
473{
474  if (owl_message_is_type_zephyr(m)) {
[5789230]475    if (!strcasecmp(owl_message_get_class(m), "mail") && owl_message_is_private(m)) {
[0ff8fb57]476      return(1);
477    } else {
478      return(0);
479    }
[4b464a4]480  }
481  return(0);
482}
483
[0ff8fb57]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  }
[4b464a4]493  return(0);
494}
495
[0ff8fb57]496int owl_message_is_burningears(owl_message *m)
497{
[4b464a4]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 */
[5789230]502  if (owl_message_is_from_me(m) || owl_message_is_private(m)) return(0);
[0ff8fb57]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  }
[4b464a4]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. */
[0ff8fb57]521char *owl_message_get_cc(owl_message *m)
522{
[4b464a4]523  char *cur, *out, *end;
524
525  cur = owl_message_get_body(m);
526  while (*cur && *cur==' ') cur++;
[985f85b]527  if (strncasecmp(cur, "cc:", 3)) return(NULL);
[4b464a4]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
[0ff8fb57]536int owl_message_get_id(owl_message *m)
537{
[4b464a4]538  return(m->id);
539}
540                                       
[0ff8fb57]541int owl_message_search(owl_message *m, char *string)
542{
[4b464a4]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
[0ff8fb57]549void owl_message_create(owl_message *m, char *header, char *text)
550{
[4b464a4]551  char *indent;
552
[d0d65df]553  owl_message_init(m);
554  owl_message_set_body(m, text);
[4b464a4]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
[d559df9]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)
[0ff8fb57]575{
[d09e5a1]576  owl_message_init(m);
577  owl_message_set_body(m, text);
578  owl_message_set_sender(m, sender);
[440ce01]579  owl_message_set_recipient(m, recipient);
[d09e5a1]580  owl_message_set_type_aim(m);
[3abf28b]581
[d559df9]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);
[3abf28b]586  }
587
[d559df9]588  /* for now all messages that aren't loginout are private */
589  if (!loginout) {
590    owl_message_set_isprivate(m);
591  }
[3abf28b]592
[d559df9]593  if (loginout==-1) {
594    owl_message_set_islogout(m);
595  } else if (loginout==1) {
596    owl_message_set_islogin(m);
597  }
[d09e5a1]598
[d559df9]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);
[d09e5a1]604  }
605}
606
[d559df9]607void _owl_message_make_text_from_aim(owl_message *m)
[aa5f725]608{
[d559df9]609  char *indent;
[5789230]610
[d559df9]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);
[aa5f725]648  }
649}
650
[0ff8fb57]651void owl_message_create_admin(owl_message *m, char *header, char *text)
652{
[4b464a4]653  char *indent;
654
[d0d65df]655  owl_message_init(m);
[4b464a4]656  owl_message_set_type_admin(m);
[7d4fbcd]657
[d0d65df]658  owl_message_set_body(m, text);
[7d4fbcd]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
[0ff8fb57]677void owl_message_create_from_znotice(owl_message *m, ZNotice_t *n)
678{
[7d4fbcd]679  struct hostent *hent;
[c269e22]680  int k;
[d0d65df]681  char *ptr, *tmp, *tmp2;
[7d4fbcd]682
[d0d65df]683  owl_message_init(m);
684 
[4b464a4]685  owl_message_set_type_zephyr(m);
686  owl_message_set_direction_in(m);
[7d4fbcd]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 */
[d0d65df]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);
[7d4fbcd]701  if (n->z_opcode) {
[d0d65df]702    owl_message_set_opcode(m, n->z_opcode);
[7d4fbcd]703  } else {
[d0d65df]704    owl_message_set_opcode(m, "");
[7d4fbcd]705  }
[d0d65df]706  owl_message_set_zsig(m, n->z_message);
[7d4fbcd]707
708  if ((ptr=strchr(n->z_recipient, '@'))!=NULL) {
[d0d65df]709    owl_message_set_realm(m, ptr+1);
[7d4fbcd]710  } else {
[d0d65df]711    owl_message_set_realm(m, ZGetRealm());
[7d4fbcd]712  }
713
[5789230]714  /* Set the "isloginout" attribute if it's a login message */
715  if (!strcasecmp(n->z_class, "login")) {
[d559df9]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    }
[5789230]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
[7d4fbcd]728  m->zwriteline=strdup("");
729
[b45293f]730  /* set the body */
[7d4fbcd]731  ptr=owl_zephyr_get_message(n, &k);
[7e3e00a]732  tmp=owl_malloc(k+10);
733  memcpy(tmp, ptr, k);
734  tmp[k]='\0';
735  if (owl_global_is_newlinestrip(&g)) {
[d0d65df]736    tmp2=owl_util_stripnewlines(tmp);
737    owl_message_set_body(m, tmp2);
738    owl_free(tmp2);
[7e3e00a]739  } else {
[d0d65df]740    owl_message_set_body(m, tmp);
[7e3e00a]741  }
[ecd5dc5]742  owl_free(tmp);
[7d4fbcd]743
[c86a35c]744#ifdef OWL_ENABLE_ZCRYPT
[d309eb3]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;
[c269e22]748    int ret;
[d309eb3]749
[d0d65df]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));
[a15a84f]752    if (ret==0) {
[d0d65df]753      owl_message_set_body(m, out);
[a15a84f]754    } else {
755      owl_free(out);
756    }
[d309eb3]757  }
[c269e22]758#endif 
[d309eb3]759
[7d4fbcd]760  /* save the hostname */
[3a2daac]761  owl_function_debugmsg("About to do gethostbyaddr");
[7d4fbcd]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
[0ff8fb57]784void owl_message_create_from_zwriteline(owl_message *m, char *line, char *body, char *zsig)
785{
[b45293f]786  owl_zwrite z;
787  int ret;
788 
[d0d65df]789  owl_message_init(m);
[b45293f]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);
[8fec514]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));
[d0d65df]800  owl_message_set_recipient(m,
801                            long_zuser(owl_zwrite_get_recip_n(&z, 0))); /* only gets the first user, must fix */
[8fec514]802  owl_message_set_opcode(m, owl_zwrite_get_opcode(&z));
[d0d65df]803  owl_message_set_realm(m, owl_zwrite_get_realm(&z)); /* also a hack, but not here */
[b45293f]804  m->zwriteline=owl_strdup(line);
[d0d65df]805  owl_message_set_body(m, body);
[8fec514]806  owl_message_set_zsig(m, zsig);
[b45293f]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}
[7d4fbcd]825
[0ff8fb57]826void _owl_message_make_text_from_config(owl_message *m)
827{
[7d4fbcd]828  char *body, *indent;
[d0d65df]829
[7d4fbcd]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
[0ff8fb57]846void _owl_message_make_text_from_zwriteline_standard(owl_message *m)
847{
[be97670]848  char *indent, *text, *zsigbuff, *foo;
[b45293f]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 ");
[be97670]857  foo=short_zuser(owl_message_get_recipient(m));
858  owl_fmtext_append_normal(&(m->fmtext), foo);
859  owl_free(foo);
[b45293f]860  owl_fmtext_append_normal(&(m->fmtext), "  (Zsig: ");
861
[8fec514]862  zsigbuff=owl_malloc(strlen(owl_message_get_zsig(m))+30);
[b45293f]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
[0ff8fb57]877void _owl_message_make_text_from_zwriteline_simple(owl_message *m)
878{
[b12c219]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
[a3ba748]893  zsigbuff=owl_malloc(strlen(owl_message_get_zsig(m))+30);
[b12c219]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);
[b45293f]906}
907
[0ff8fb57]908void _owl_message_make_text_from_notice_standard(owl_message *m)
909{
[b45293f]910  char *body, *indent, *ptr, *zsigbuff, frombuff[LINE];
[7d4fbcd]911  ZNotice_t *n;
912
913  n=&(m->notice);
[d309eb3]914 
915  /* get the body */
[d0d65df]916  body=owl_malloc(strlen(owl_message_get_body(m))+30);
917  strcpy(body, owl_message_get_body(m));
[7d4fbcd]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 */
[d0d65df]929  strcpy(frombuff, owl_message_get_sender(m));
[7d4fbcd]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
[5789230]939  if (!strcasecmp(owl_message_get_opcode(m), "ping") && owl_message_is_private(m)) {
[7d4fbcd]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 ");
[4b464a4]961    ptr=short_zuser(n->z_class_inst);
[7d4fbcd]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 {
[d0d65df]970    owl_fmtext_append_normal(&(m->fmtext), owl_message_get_class(m));
[7d4fbcd]971    owl_fmtext_append_normal(&(m->fmtext), " / ");
[d0d65df]972    owl_fmtext_append_normal(&(m->fmtext), owl_message_get_instance(m));
[7d4fbcd]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), " [");
[d0d65df]982      owl_fmtext_append_normal(&(m->fmtext), owl_message_get_opcode(m));
[7d4fbcd]983      owl_fmtext_append_normal(&(m->fmtext), "] ");
984    }
985
986    /* stick on the zsig */
[a3ba748]987    zsigbuff=owl_malloc(strlen(owl_message_get_zsig(m))+30);
[b45293f]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), ")");
[7d4fbcd]992    owl_fmtext_append_normal(&(m->fmtext), "\n");
[b45293f]993    owl_free(zsigbuff);
994
995    /* then the indented message */
[7d4fbcd]996    owl_fmtext_append_ztext(&(m->fmtext), indent);
997
[3abf28b]998    /* make private messages bold for smaat users */
[7d4fbcd]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
[0ff8fb57]1010void _owl_message_make_text_from_notice_simple(owl_message *m)
1011{
[b45293f]1012  char *body, *indent, *ptr, *zsigbuff, frombuff[LINE];
[7d4fbcd]1013  ZNotice_t *n;
1014
1015  n=&(m->notice);
[d309eb3]1016
1017  /* get the body */
[ecd5dc5]1018  body=owl_strdup(owl_message_get_body(m));
1019  body=realloc(body, strlen(body)+30);
[7d4fbcd]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 */
[d0d65df]1031  strcpy(frombuff, owl_message_get_sender(m));
[7d4fbcd]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
[d0d65df]1041  if (!strcasecmp(owl_message_get_opcode(m), "ping")) {
[7d4fbcd]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");
[d0d65df]1046  } else if (!strcasecmp(owl_message_get_class(m), "login")) {
[7d4fbcd]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   
[d0d65df]1057    if (!strcasecmp(owl_message_get_opcode(m), "user_login")) {
[7d4fbcd]1058      owl_fmtext_append_bold(&(m->fmtext), "LOGIN");
[d0d65df]1059    } else if (!strcasecmp(owl_message_get_opcode(m), "user_logout")) {
[7d4fbcd]1060      owl_fmtext_append_bold(&(m->fmtext), "LOGOUT");
1061    }
1062    owl_fmtext_append_normal(&(m->fmtext), " for ");
[d0d65df]1063    ptr=short_zuser(owl_message_get_instance(m));
[7d4fbcd]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: ");
[d0d65df]1073    if (strcasecmp(owl_message_get_class(m), "message")) {
[7d4fbcd]1074      owl_fmtext_append_normal(&(m->fmtext), "Class ");
[d0d65df]1075      owl_fmtext_append_normal(&(m->fmtext), owl_message_get_class(m));
[7d4fbcd]1076      owl_fmtext_append_normal(&(m->fmtext), " / Instance ");
[d0d65df]1077      owl_fmtext_append_normal(&(m->fmtext), owl_message_get_instance(m));
[7d4fbcd]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 */
[a3ba748]1088    zsigbuff=owl_malloc(strlen(owl_message_get_zsig(m))+30);
[b45293f]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), ")");
[7d4fbcd]1093    owl_fmtext_append_normal(&(m->fmtext), "\n");
[b45293f]1094    owl_free(zsigbuff);
1095
1096    /* then the indented message */
[7d4fbcd]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
[0ff8fb57]1111void owl_message_pretty_zsig(owl_message *m, char *buff)
1112{
[b45293f]1113  /* stick a one line version of the zsig in buff */
[7d4fbcd]1114  char *ptr;
1115
[d0d65df]1116  strcpy(buff, owl_message_get_zsig(m));
[b45293f]1117  ptr=strchr(buff, '\n');
1118  if (ptr) ptr[0]='\0';
[7d4fbcd]1119}
1120
[0ff8fb57]1121void owl_message_free(owl_message *m)
1122{
[d0d65df]1123  int i, j;
1124  owl_pair *p;
1125   
[4b464a4]1126  if (owl_message_is_type_zephyr(m) && owl_message_is_direction_in(m)) {
[7d4fbcd]1127    ZFreeNotice(&(m->notice));
1128  }
1129  if (m->time) owl_free(m->time);
1130  if (m->zwriteline) owl_free(m->zwriteline);
[d0d65df]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));
[7d4fbcd]1142 
1143  owl_fmtext_free(&(m->fmtext));
1144}
Note: See TracBrowser for help on using the repository browser.