source: message.c @ 0ff8fb57

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