source: message.c @ 74037d9

barnowl_perlaimdebianowlrelease-1.10release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since 74037d9 was 8298425, checked in by James M. Kretchmar <kretch@mit.edu>, 20 years ago
Don't crash on super-long hostnames Improved test for finding libdes425
  • Property mode set to 100644
File size: 22.0 KB
RevLine 
[7d4fbcd]1#include <stdlib.h>
[b45293f]2#include <unistd.h>
[7d4fbcd]3#include <string.h>
4#include <sys/socket.h>
5#include <netdb.h>
6#include <sys/types.h>
7#include <sys/socket.h>
8#include <netinet/in.h>
9#include <arpa/inet.h>
10#include <time.h>
11#include "owl.h"
12
[1aee7d9]13static const char fileIdent[] = "$Id$";
14
[0ff8fb57]15void owl_message_init(owl_message *m)
16{
[7d4fbcd]17  m->id=owl_global_get_nextmsgid(&g);
[4b464a4]18  m->type=OWL_MESSAGE_TYPE_GENERIC;
19  owl_message_set_direction_none(m);
[7d4fbcd]20  m->delete=0;
[8298425]21  m->hostname=owl_strdup("");
[7d4fbcd]22  m->zwriteline=strdup("");
[bd3f232]23  m->invalid_format=1;
[7d4fbcd]24
[d0d65df]25  owl_list_create(&(m->attributes));
26 
[7d4fbcd]27  /* save the time */
[25dd31a]28  m->time=time(NULL);
29  m->timestr=owl_strdup(ctime(&(m->time)));
30  m->timestr[strlen(m->timestr)-1]='\0';
31
32  /* initialize the fmtext */
[bd3f232]33  owl_fmtext_init_null(&(m->fmtext));
[4b464a4]34}
35
[5a95b69]36/* add the named attribute to the message.  If an attribute with the
37 * name already exists, replace the old value with the new value
38 */
[0ff8fb57]39void owl_message_set_attribute(owl_message *m, char *attrname, char *attrvalue)
40{
[d0d65df]41  int i, j;
42  owl_pair *p;
43
44  /* look for an existing pair with this key, and nuke the entry if
45     found */
46  j=owl_list_get_size(&(m->attributes));
47  for (i=0; i<j; i++) {
48    p=owl_list_get_element(&(m->attributes), i);
49    if (!strcmp(owl_pair_get_key(p), attrname)) {
50      owl_free(owl_pair_get_key(p));
51      owl_free(owl_pair_get_value(p));
52      owl_free(p);
53      owl_list_remove_element(&(m->attributes), i);
54      break;
55    }
56  }
57
58  p=owl_malloc(sizeof(owl_pair));
59  owl_pair_create(p, owl_strdup(attrname), owl_strdup(attrvalue));
60  owl_list_append_element(&(m->attributes), p);
61}
62
[5a95b69]63/* return the value associated with the named attribute, or NULL if
64 * the attribute does not exist
65 */
[0ff8fb57]66char *owl_message_get_attribute_value(owl_message *m, char *attrname)
67{
[d0d65df]68  int i, j;
69  owl_pair *p;
70
71  j=owl_list_get_size(&(m->attributes));
72  for (i=0; i<j; i++) {
73    p=owl_list_get_element(&(m->attributes), i);
74    if (!strcmp(owl_pair_get_key(p), attrname)) {
75      return(owl_pair_get_value(p));
76    }
77  }
[f4d0975]78
79  /*
80  owl_function_debugmsg("No attribute %s found for message %i",
81                        attrname,
82                        owl_message_get_id(m));
83  */
[d0d65df]84  return(NULL);
85}
86
[5789230]87/* We cheat and indent it for now, since we really want this for
88 * the 'info' function.  Later there should just be a generic
89 * function to indent fmtext.
90 */
91void owl_message_attributes_tofmtext(owl_message *m, owl_fmtext *fm) {
92  int i, j;
93  owl_pair *p;
94  char *buff;
95
96  owl_fmtext_init_null(fm);
97
98  j=owl_list_get_size(&(m->attributes));
99  for (i=0; i<j; i++) {
100    p=owl_list_get_element(&(m->attributes), i);
101    buff=owl_sprintf("  %-15.15s: %-35.35s\n", owl_pair_get_key(p), owl_pair_get_value(p));
102    owl_fmtext_append_normal(fm, buff);
103    owl_free(buff);
104  }
105}
[4b464a4]106
[bd3f232]107void owl_message_invalidate_format(owl_message *m)
108{
109  m->invalid_format=1;
110}
111
[0ff8fb57]112owl_fmtext *owl_message_get_fmtext(owl_message *m)
113{
[f14a7ee]114  owl_message_format(m);
115  return(&(m->fmtext));
116}
117
118void owl_message_format(owl_message *m)
119{
[bd3f232]120  owl_style *s;
[ef56a67]121  owl_view *v;
[bd3f232]122
123  if (m->invalid_format) {
[ef56a67]124    /* for now we assume there's jsut the one view and use that style */
125    v=owl_global_get_current_view(&g);
126    s=owl_view_get_style(v);
127
[bd3f232]128    owl_fmtext_free(&(m->fmtext));
129    owl_fmtext_init_null(&(m->fmtext));
130    owl_style_get_formattext(s, &(m->fmtext), m);
131    m->invalid_format=0;
132  }
[4b464a4]133}
134
[0ff8fb57]135void owl_message_set_class(owl_message *m, char *class)
136{
[d0d65df]137  owl_message_set_attribute(m, "class", class);
[4b464a4]138}
139
[0ff8fb57]140char *owl_message_get_class(owl_message *m)
141{
[d0d65df]142  char *class;
143
144  class=owl_message_get_attribute_value(m, "class");
145  if (!class) return("");
146  return(class);
[4b464a4]147}
148
[0ff8fb57]149void owl_message_set_instance(owl_message *m, char *inst)
150{
[d0d65df]151  owl_message_set_attribute(m, "instance", inst);
[4b464a4]152}
153
[0ff8fb57]154char *owl_message_get_instance(owl_message *m)
155{
[d0d65df]156  char *instance;
157
158  instance=owl_message_get_attribute_value(m, "instance");
159  if (!instance) return("");
160  return(instance);
[4b464a4]161}
162
[0ff8fb57]163void owl_message_set_sender(owl_message *m, char *sender)
164{
[d0d65df]165  owl_message_set_attribute(m, "sender", sender);
[4b464a4]166}
167
[0ff8fb57]168char *owl_message_get_sender(owl_message *m)
169{
[d0d65df]170  char *sender;
171
172  sender=owl_message_get_attribute_value(m, "sender");
173  if (!sender) return("");
174  return(sender);
[4b464a4]175}
176
[0ff8fb57]177void owl_message_set_zsig(owl_message *m, char *zsig)
178{
[d0d65df]179  owl_message_set_attribute(m, "zsig", zsig);
[b45293f]180}
181
[0ff8fb57]182char *owl_message_get_zsig(owl_message *m)
183{
[d0d65df]184  char *zsig;
185
186  zsig=owl_message_get_attribute_value(m, "zsig");
187  if (!zsig) return("");
188  return(zsig);
[b45293f]189}
190
[0ff8fb57]191void owl_message_set_recipient(owl_message *m, char *recip)
192{
[d0d65df]193  owl_message_set_attribute(m, "recipient", recip);
[4b464a4]194}
195
[0ff8fb57]196char *owl_message_get_recipient(owl_message *m)
197{
[4b464a4]198  /* this is stupid for outgoing messages, we need to fix it. */
[d0d65df]199
200  char *recip;
[0ff8fb57]201
202  recip=owl_message_get_attribute_value(m, "recipient");
[d0d65df]203  if (!recip) return("");
204  return(recip);
[4b464a4]205}
206
[0ff8fb57]207void owl_message_set_realm(owl_message *m, char *realm)
208{
[d0d65df]209  owl_message_set_attribute(m, "realm", realm);
[4b464a4]210}
211
[0ff8fb57]212char *owl_message_get_realm(owl_message *m)
213{
[d0d65df]214  char *realm;
215 
216  realm=owl_message_get_attribute_value(m, "realm");
217  if (!realm) return("");
218  return(realm);
219}
220
[0ff8fb57]221void owl_message_set_body(owl_message *m, char *body)
222{
[d0d65df]223  owl_message_set_attribute(m, "body", body);
224}
225
[0ff8fb57]226char *owl_message_get_body(owl_message *m)
227{
[d0d65df]228  char *body;
229
230  body=owl_message_get_attribute_value(m, "body");
231  if (!body) return("");
232  return(body);
[4b464a4]233}
234
[d0d65df]235
[0ff8fb57]236void owl_message_set_opcode(owl_message *m, char *opcode)
237{
[d0d65df]238  owl_message_set_attribute(m, "opcode", opcode);
[4b464a4]239}
240
[0ff8fb57]241char *owl_message_get_opcode(owl_message *m)
242{
[d0d65df]243  char *opcode;
244
245  opcode=owl_message_get_attribute_value(m, "opcode");
246  if (!opcode) return("");
247  return(opcode);
[4b464a4]248}
249
[5789230]250
[d559df9]251void owl_message_set_islogin(owl_message *m)
[5789230]252{
[d559df9]253  owl_message_set_attribute(m, "loginout", "login");
254}
255
256
257void owl_message_set_islogout(owl_message *m)
258{
259  owl_message_set_attribute(m, "loginout", "logout");
[5789230]260}
261
262int owl_message_is_loginout(owl_message *m)
263{
264  char *res;
265
[d559df9]266  res=owl_message_get_attribute_value(m, "loginout");
[5789230]267  if (!res) return(0);
268  return(1);
269}
270
[d559df9]271int owl_message_is_login(owl_message *m)
272{
273  char *res;
274
275  res=owl_message_get_attribute_value(m, "loginout");
276  if (!res) return(0);
277  if (!strcmp(res, "login")) return(1);
278  return(0);
279}
280
281
282int owl_message_is_logout(owl_message *m)
283{
284  char *res;
285
286  res=owl_message_get_attribute_value(m, "loginout");
287  if (!res) return(0);
288  if (!strcmp(res, "logout")) return(1);
289  return(0);
290}
291
[5789230]292void owl_message_set_isprivate(owl_message *m)
293{
294  owl_message_set_attribute(m, "isprivate", "");
295}
296
297int owl_message_is_private(owl_message *m)
298{
299  char *res;
300
301  res=owl_message_get_attribute_value(m, "isprivate");
302  if (!res) return(0);
303  return(1);
304}
305
[0ff8fb57]306char *owl_message_get_timestr(owl_message *m)
307{
[25dd31a]308  if (m->timestr) return(m->timestr);
309  return("");
310}
311
312/* caller must free the return */
313char *owl_message_get_shorttimestr(owl_message *m)
314{
315  struct tm *tmstruct;
316  char *out;
317
318  tmstruct=localtime(&(m->time));
319  out=owl_sprintf("%2.2i:%2.2i", tmstruct->tm_hour, tmstruct->tm_min);
320  if (out) return(out);
321  return("??:??");
[4b464a4]322}
323
[0ff8fb57]324void owl_message_set_type_admin(owl_message *m)
325{
[4b464a4]326  m->type=OWL_MESSAGE_TYPE_ADMIN;
327}
328
[37eab7f]329void owl_message_set_type_loopback(owl_message *m)
330{
331  m->type=OWL_MESSAGE_TYPE_LOOPBACK;
332}
333
[0ff8fb57]334void owl_message_set_type_zephyr(owl_message *m)
335{
[4b464a4]336  m->type=OWL_MESSAGE_TYPE_ZEPHYR;
337}
[d09e5a1]338
[0ff8fb57]339void owl_message_set_type_aim(owl_message *m)
340{
[d09e5a1]341  m->type=OWL_MESSAGE_TYPE_AIM;
342}
[4b464a4]343                                               
[0ff8fb57]344int owl_message_is_type_admin(owl_message *m)
345{
[4b464a4]346  if (m->type==OWL_MESSAGE_TYPE_ADMIN) return(1);
347  return(0);
348}
349
[37eab7f]350int owl_message_is_type_loopback(owl_message *m)
351{
352  if (m->type==OWL_MESSAGE_TYPE_LOOPBACK) return(1);
353  return(0);
354}
355
[0ff8fb57]356int owl_message_is_type_zephyr(owl_message *m)
357{
[4b464a4]358  if (m->type==OWL_MESSAGE_TYPE_ZEPHYR) return(1);
359  return(0);
360}
361
[0ff8fb57]362int owl_message_is_type_aim(owl_message *m)
363{
[d09e5a1]364  if (m->type==OWL_MESSAGE_TYPE_AIM) return(1);
365  return(0);
366}
367
[5a95b69]368int owl_message_is_pseudo(owl_message *m)
369{
370  if (owl_message_get_attribute_value(m, "pseudo")) return(1);
371  return(0);
372}
373
[0ff8fb57]374int owl_message_is_type_generic(owl_message *m)
375{
[4b464a4]376  if (m->type==OWL_MESSAGE_TYPE_GENERIC) return(1);
377  return(0);
378}
379
[0ff8fb57]380char *owl_message_get_text(owl_message *m)
381{
[4b464a4]382  return(owl_fmtext_get_text(&(m->fmtext)));
383}
384
[0ff8fb57]385void owl_message_set_direction_in(owl_message *m)
386{
[4b464a4]387  m->direction=OWL_MESSAGE_DIRECTION_IN;
388}
389
[0ff8fb57]390void owl_message_set_direction_out(owl_message *m)
391{
[4b464a4]392  m->direction=OWL_MESSAGE_DIRECTION_OUT;
393}
394
[0ff8fb57]395void owl_message_set_direction_none(owl_message *m)
396{
[4b464a4]397  m->direction=OWL_MESSAGE_DIRECTION_NONE;
398}
399
[0ff8fb57]400int owl_message_is_direction_in(owl_message *m)
401{
[4b464a4]402  if (m->direction==OWL_MESSAGE_DIRECTION_IN) return(1);
403  return(0);
404}
405
[0ff8fb57]406int owl_message_is_direction_out(owl_message *m)
407{
[4b464a4]408  if (m->direction==OWL_MESSAGE_DIRECTION_OUT) return(1);
409  return(0);
410}
411
[0ff8fb57]412int owl_message_is_direction_none(owl_message *m)
413{
[4b464a4]414  if (m->direction==OWL_MESSAGE_DIRECTION_NONE) return(1);
415  return(0);
416}
417
[0ff8fb57]418int owl_message_get_numlines(owl_message *m)
419{
[4b464a4]420  if (m == NULL) return(0);
[f14a7ee]421  owl_message_format(m);
[4b464a4]422  return(owl_fmtext_num_lines(&(m->fmtext)));
423}
424
[0ff8fb57]425void owl_message_mark_delete(owl_message *m)
426{
[4b464a4]427  if (m == NULL) return;
428  m->delete=1;
429}
430
[0ff8fb57]431void owl_message_unmark_delete(owl_message *m)
432{
[4b464a4]433  if (m == NULL) return;
434  m->delete=0;
435}
436
[0ff8fb57]437char *owl_message_get_zwriteline(owl_message *m)
438{
[4b464a4]439  return(m->zwriteline);
440}
441
[0ff8fb57]442void owl_message_set_zwriteline(owl_message *m, char *line)
443{
[4b464a4]444  m->zwriteline=strdup(line);
445}
446
[0ff8fb57]447int owl_message_is_delete(owl_message *m)
448{
[4b464a4]449  if (m == NULL) return(0);
450  if (m->delete==1) return(1);
451  return(0);
452}
453
[be0a79f]454#ifdef HAVE_LIBZEPHYR
[0ff8fb57]455ZNotice_t *owl_message_get_notice(owl_message *m)
456{
[4b464a4]457  return(&(m->notice));
458}
[09489b89]459#else
460void *owl_message_get_notice(owl_message *m)
461{
462  return(NULL);
463}
[be0a79f]464#endif
[4b464a4]465
[8298425]466void owl_message_set_hostname(owl_message *m, char *hostname)
467{
468  if (m==NULL) return;
469  if (m->hostname!=NULL) {
470    owl_free(m->hostname);
471  }
472  m->hostname=owl_strdup(hostname);
473}
474
[0ff8fb57]475char *owl_message_get_hostname(owl_message *m)
476{
[4b464a4]477  return(m->hostname);
478}
479
[0ff8fb57]480void owl_message_curs_waddstr(owl_message *m, WINDOW *win, int aline, int bline, int acol, int bcol, int color)
481{
[4b464a4]482  owl_fmtext a, b;
483
[bd3f232]484  /* this will ensure that our cached copy is up to date */
[f14a7ee]485  owl_message_format(m);
[bd3f232]486
[af2ca19]487  owl_fmtext_init_null(&a);
488  owl_fmtext_init_null(&b);
489 
[4b464a4]490  owl_fmtext_truncate_lines(&(m->fmtext), aline, bline-aline+1, &a);
491  owl_fmtext_truncate_cols(&a, acol, bcol, &b);
492  if (color!=OWL_COLOR_DEFAULT) {
493    owl_fmtext_colorize(&b, color);
494  }
495
496  if (owl_global_is_search_active(&g)) {
497    owl_fmtext_search_and_highlight(&b, owl_global_get_search_string(&g));
498  }
499     
500  owl_fmtext_curs_waddstr(&b, win);
501
502  owl_fmtext_free(&a);
503  owl_fmtext_free(&b);
504}
505
[0ff8fb57]506int owl_message_is_personal(owl_message *m)
507{
508  if (owl_message_is_type_zephyr(m)) {
509    if (strcasecmp(owl_message_get_class(m), "message")) return(0);
510    if (strcasecmp(owl_message_get_instance(m), "personal")) return(0);
[09489b89]511    if (!strcasecmp(owl_message_get_recipient(m), owl_zephyr_get_sender()) ||
512        !strcasecmp(owl_message_get_sender(m), owl_zephyr_get_sender())) {
[0ff8fb57]513      return(1);
514    }
515  }
516  return(0);
517}
518
519int owl_message_is_from_me(owl_message *m)
520{
521  if (owl_message_is_type_zephyr(m)) {
[09489b89]522    if (!strcasecmp(owl_message_get_sender(m), owl_zephyr_get_sender())) {
[0ff8fb57]523      return(1);
524    } else {
525      return(0);
526    }
527  } else if (owl_message_is_type_aim(m)) {
528    if (!strcasecmp(owl_message_get_sender(m), owl_global_get_aim_screenname(&g))) {
529      return(1);
530    } else {
531      return(0);
532    }
533  } else if (owl_message_is_type_admin(m)) {
534    return(0);
535  }
[4b464a4]536  return(0);
537}
538
[0ff8fb57]539int owl_message_is_mail(owl_message *m)
540{
541  if (owl_message_is_type_zephyr(m)) {
[5789230]542    if (!strcasecmp(owl_message_get_class(m), "mail") && owl_message_is_private(m)) {
[0ff8fb57]543      return(1);
544    } else {
545      return(0);
546    }
[4b464a4]547  }
548  return(0);
549}
550
[0ff8fb57]551int owl_message_is_ping(owl_message *m)
552{
553  if (owl_message_is_type_zephyr(m)) {
554    if (!strcasecmp(owl_message_get_opcode(m), "ping")) {
555      return(1);
556    } else {
557      return(0);
558    }
559  }
[4b464a4]560  return(0);
561}
562
[0ff8fb57]563int owl_message_is_burningears(owl_message *m)
564{
[4b464a4]565  /* we should add a global to cache the short zsender */
566  char sender[LINE], *ptr;
567
568  /* if the message is from us or to us, it doesn't count */
[5789230]569  if (owl_message_is_from_me(m) || owl_message_is_private(m)) return(0);
[0ff8fb57]570
571  if (owl_message_is_type_zephyr(m)) {
[09489b89]572    strcpy(sender, owl_zephyr_get_sender());
[0ff8fb57]573    ptr=strchr(sender, '@');
574    if (ptr) *ptr='\0';
575  } else if (owl_message_is_type_aim(m)) {
576    strcpy(sender, owl_global_get_aim_screenname(&g));
577  } else {
578    return(0);
579  }
[4b464a4]580
581  if (stristr(owl_message_get_body(m), sender)) {
582    return(1);
583  }
584  return(0);
585}
586
587/* caller must free return value. */
[0ff8fb57]588char *owl_message_get_cc(owl_message *m)
589{
[4b464a4]590  char *cur, *out, *end;
591
592  cur = owl_message_get_body(m);
593  while (*cur && *cur==' ') cur++;
[985f85b]594  if (strncasecmp(cur, "cc:", 3)) return(NULL);
[4b464a4]595  cur+=3;
596  while (*cur && *cur==' ') cur++;
597  out = owl_strdup(cur);
598  end = strchr(out, '\n');
599  if (end) end[0] = '\0';
600  return(out);
601}
602
[0ff8fb57]603int owl_message_get_id(owl_message *m)
604{
[4b464a4]605  return(m->id);
606}
[bd3f232]607
[f1e629d]608char *owl_message_get_type(owl_message *m) {
609  switch (m->type) {
610  case OWL_MESSAGE_TYPE_ADMIN:
611    return("admin");
612  case OWL_MESSAGE_TYPE_ZEPHYR:
613    return("zephyr");
614  case OWL_MESSAGE_TYPE_GENERIC:
615    return("generic");
616  case OWL_MESSAGE_TYPE_AIM:
617    return("aim");
618  case OWL_MESSAGE_TYPE_JABBER:
619    return("jabber");
620  case OWL_MESSAGE_TYPE_ICQ:
621    return("icq");
622  case OWL_MESSAGE_TYPE_YAHOO:
623    return("yahoo");
624  case OWL_MESSAGE_TYPE_MSN:
625    return("msn");
[37eab7f]626  case OWL_MESSAGE_TYPE_LOOPBACK:
627    return("loopback");
[f1e629d]628  default:
629    return("unknown");
630  }
631}
632
633char *owl_message_get_direction(owl_message *m) {
634  switch (m->direction) {
635  case OWL_MESSAGE_DIRECTION_IN:
636    return("in");
637  case OWL_MESSAGE_DIRECTION_OUT:
638    return("out");
639  case OWL_MESSAGE_DIRECTION_NONE:
640    return("none");
641  default:
642    return("unknown");
643  }
644}
645
646char *owl_message_get_login(owl_message *m) {
647  if (owl_message_is_login(m)) {
648    return "login";
649  } else if (owl_message_is_logout(m)) {
650    return "logout";
651  } else {
652    return "none";
653  }
654}
655
656char *owl_message_get_header(owl_message *m) {
657  return owl_message_get_attribute_value(m, "adminheader");
658}
659
[bd3f232]660/* return 1 if the message contains "string", 0 otherwise.  This is
661 * case insensitive because the functions it uses are
662 */
[0ff8fb57]663int owl_message_search(owl_message *m, char *string)
664{
[4b464a4]665
[f14a7ee]666  owl_message_format(m); /* is this necessary? */
[bd3f232]667 
[4b464a4]668  return (owl_fmtext_search(&(m->fmtext), string));
669}
670
671
[d559df9]672/* if loginout == -1 it's a logout message
673 *                 0 it's not a login/logout message
674 *                 1 it's a login message
675 */
676void owl_message_create_aim(owl_message *m, char *sender, char *recipient, char *text, int direction, int loginout)
[0ff8fb57]677{
[d09e5a1]678  owl_message_init(m);
679  owl_message_set_body(m, text);
680  owl_message_set_sender(m, sender);
[440ce01]681  owl_message_set_recipient(m, recipient);
[d09e5a1]682  owl_message_set_type_aim(m);
[3abf28b]683
[d559df9]684  if (direction==OWL_MESSAGE_DIRECTION_IN) {
685    owl_message_set_direction_in(m);
686  } else if (direction==OWL_MESSAGE_DIRECTION_OUT) {
687    owl_message_set_direction_out(m);
[3abf28b]688  }
689
[d559df9]690  /* for now all messages that aren't loginout are private */
691  if (!loginout) {
692    owl_message_set_isprivate(m);
693  }
[3abf28b]694
[d559df9]695  if (loginout==-1) {
696    owl_message_set_islogout(m);
697  } else if (loginout==1) {
698    owl_message_set_islogin(m);
699  }
[aa5f725]700}
701
[0ff8fb57]702void owl_message_create_admin(owl_message *m, char *header, char *text)
703{
[d0d65df]704  owl_message_init(m);
[4b464a4]705  owl_message_set_type_admin(m);
[d0d65df]706  owl_message_set_body(m, text);
[bd3f232]707  owl_message_set_attribute(m, "adminheader", header); /* just a hack for now */
[7d4fbcd]708}
709
[37eab7f]710/* caller should set the direction */
711void owl_message_create_loopback(owl_message *m, char *text)
712{
713  owl_message_init(m);
714  owl_message_set_type_loopback(m);
715  owl_message_set_body(m, text);
[eec69e1]716  owl_message_set_sender(m, "loopsender");
717  owl_message_set_recipient(m, "looprecip");
[37eab7f]718  owl_message_set_isprivate(m);
719}
720
[09489b89]721#ifdef HAVE_LIBZEPHYR
[0ff8fb57]722void owl_message_create_from_znotice(owl_message *m, ZNotice_t *n)
723{
[7d4fbcd]724  struct hostent *hent;
[d0d65df]725  char *ptr, *tmp, *tmp2;
[7d4fbcd]726
[d0d65df]727  owl_message_init(m);
728 
[4b464a4]729  owl_message_set_type_zephyr(m);
730  owl_message_set_direction_in(m);
[7d4fbcd]731 
732  /* first save the full notice */
733  memcpy(&(m->notice), n, sizeof(ZNotice_t));
734
[25dd31a]735  /* a little gross, we'll replace \r's with ' ' for now */
[7d4fbcd]736  owl_zephyr_hackaway_cr(&(m->notice));
737 
[25dd31a]738  /* save the time, we need to nuke the string saved by message_init */
[5a95b69]739  if (m->timestr) owl_free(m->timestr);
[25dd31a]740  m->time=n->z_time.tv_sec;
741  m->timestr=owl_strdup(ctime(&(m->time)));
742  m->timestr[strlen(m->timestr)-1]='\0';
743
[7d4fbcd]744  /* set other info */
[d0d65df]745  owl_message_set_sender(m, n->z_sender);
746  owl_message_set_class(m, n->z_class);
747  owl_message_set_instance(m, n->z_class_inst);
748  owl_message_set_recipient(m, n->z_recipient);
[7d4fbcd]749  if (n->z_opcode) {
[d0d65df]750    owl_message_set_opcode(m, n->z_opcode);
[7d4fbcd]751  } else {
[d0d65df]752    owl_message_set_opcode(m, "");
[7d4fbcd]753  }
[d0d65df]754  owl_message_set_zsig(m, n->z_message);
[7d4fbcd]755
756  if ((ptr=strchr(n->z_recipient, '@'))!=NULL) {
[d0d65df]757    owl_message_set_realm(m, ptr+1);
[7d4fbcd]758  } else {
[09489b89]759    owl_message_set_realm(m, owl_zephyr_get_realm());
[7d4fbcd]760  }
761
[5789230]762  /* Set the "isloginout" attribute if it's a login message */
[1d3e925]763  if (!strcasecmp(n->z_class, "login") || !strcasecmp(n->z_class, OWL_WEBZEPHYR_CLASS)) {
[5a95b69]764    if (!strcasecmp(n->z_opcode, "user_login") || !strcasecmp(n->z_opcode, "user_logout")) {
[b0430a6]765      tmp=owl_zephyr_get_field(n, 1);
[5a95b69]766      owl_message_set_attribute(m, "loginhost", tmp);
767      owl_free(tmp);
768
[b0430a6]769      tmp=owl_zephyr_get_field(n, 3);
[5a95b69]770      owl_message_set_attribute(m, "logintty", tmp);
771      owl_free(tmp);
772    }
773
[d559df9]774    if (!strcasecmp(n->z_opcode, "user_login")) {
775      owl_message_set_islogin(m);
776    } else if (!strcasecmp(n->z_opcode, "user_logout")) {
777      owl_message_set_islogout(m);
778    }
[5789230]779  }
780
[5a95b69]781 
[9854278]782  /* set the "isprivate" attribute if it's a private zephyr */
[09489b89]783  if (!strcasecmp(n->z_recipient, owl_zephyr_get_sender())) {
[5789230]784    owl_message_set_isprivate(m);
785  }
786
[9854278]787  /* set the "isauto" attribute if it's an autoreply */
788  if (!strcasecmp(n->z_message, "Automated reply:") ||
789      !strcasecmp(n->z_opcode, "auto")) {
790    owl_message_set_attribute(m, "isauto", "");
791  }
792
[7d4fbcd]793  m->zwriteline=strdup("");
794
[b45293f]795  /* set the body */
[b0430a6]796  tmp=owl_zephyr_get_message(n);
[7e3e00a]797  if (owl_global_is_newlinestrip(&g)) {
[d0d65df]798    tmp2=owl_util_stripnewlines(tmp);
799    owl_message_set_body(m, tmp2);
800    owl_free(tmp2);
[7e3e00a]801  } else {
[d0d65df]802    owl_message_set_body(m, tmp);
[7e3e00a]803  }
[ecd5dc5]804  owl_free(tmp);
[7d4fbcd]805
[c86a35c]806#ifdef OWL_ENABLE_ZCRYPT
[d309eb3]807  /* if zcrypt is enabled try to decrypt the message */
808  if (owl_global_is_zcrypt(&g) && !strcasecmp(n->z_opcode, "crypt")) {
809    char *out;
[c269e22]810    int ret;
[d309eb3]811
[d0d65df]812    out=owl_malloc(strlen(owl_message_get_body(m))*16+20);
[9ceee9d]813    ret=owl_zcrypt_decrypt(out, owl_message_get_body(m), owl_message_get_class(m), owl_message_get_instance(m));
[a15a84f]814    if (ret==0) {
[d0d65df]815      owl_message_set_body(m, out);
[a15a84f]816    } else {
817      owl_free(out);
818    }
[d309eb3]819  }
[c269e22]820#endif 
[d309eb3]821
[7d4fbcd]822  /* save the hostname */
[3a2daac]823  owl_function_debugmsg("About to do gethostbyaddr");
[7d4fbcd]824  hent=gethostbyaddr((char *) &(n->z_uid.zuid_addr), sizeof(n->z_uid.zuid_addr), AF_INET);
825  if (hent && hent->h_name) {
[8298425]826    owl_message_set_hostname(m, hent->h_name);
[7d4fbcd]827  } else {
[8298425]828    owl_message_set_hostname(m, inet_ntoa(n->z_sender_addr));
[7d4fbcd]829  }
830}
[09489b89]831#else
832void owl_message_create_from_znotice(owl_message *m, void *n)
833{
834}
835#endif
[7d4fbcd]836
[5a95b69]837/* If 'direction' is '0' it is a login message, '1' is a logout message. */
838void owl_message_create_pseudo_zlogin(owl_message *m, int direction, char *user, char *host, char *time, char *tty)
839{
840  char *longuser, *ptr;
841
[ba9f236]842#ifdef HAVE_LIBZEPHYR
[5a95b69]843  memset(&(m->notice), 0, sizeof(ZNotice_t));
[ba9f236]844#endif
845 
[5a95b69]846  longuser=long_zuser(user);
847 
848  owl_message_init(m);
849 
850  owl_message_set_type_zephyr(m);
851  owl_message_set_direction_in(m);
852
853  owl_message_set_attribute(m, "pseudo", "");
854  owl_message_set_attribute(m, "loginhost", host ? host : "");
855  owl_message_set_attribute(m, "logintty", tty ? tty : "");
856
857  owl_message_set_sender(m, longuser);
858  owl_message_set_class(m, "LOGIN");
859  owl_message_set_instance(m, longuser);
860  owl_message_set_recipient(m, "");
861  if (direction==0) {
862    owl_message_set_opcode(m, "USER_LOGIN");
863    owl_message_set_islogin(m);
864  } else if (direction==1) {
865    owl_message_set_opcode(m, "USER_LOGOUT");
866    owl_message_set_islogout(m);
867  }
868
869  if ((ptr=strchr(longuser, '@'))!=NULL) {
870    owl_message_set_realm(m, ptr+1);
871  } else {
872    owl_message_set_realm(m, owl_zephyr_get_realm());
873  }
874
875  m->zwriteline=strdup("");
876
877  owl_message_set_body(m, "<uninitialized>");
878
879  /* save the hostname */
[2de4f20]880  owl_function_debugmsg("create_pseudo_login: host is %s", host ? host : "");
[8298425]881  owl_message_set_hostname(m, host ? host : "");
[5a95b69]882  owl_free(longuser);
883}
884
[0ff8fb57]885void owl_message_create_from_zwriteline(owl_message *m, char *line, char *body, char *zsig)
886{
[b45293f]887  owl_zwrite z;
888  int ret;
[8298425]889  char hostbuff[5000];
[b45293f]890 
[d0d65df]891  owl_message_init(m);
[b45293f]892
893  /* create a zwrite for the purpose of filling in other message fields */
894  owl_zwrite_create_from_line(&z, line);
895
896  /* set things */
897  owl_message_set_direction_out(m);
898  owl_message_set_type_zephyr(m);
[09489b89]899  owl_message_set_sender(m, owl_zephyr_get_sender());
[8fec514]900  owl_message_set_class(m, owl_zwrite_get_class(&z));
901  owl_message_set_instance(m, owl_zwrite_get_instance(&z));
[9ceee9d]902  if (owl_zwrite_get_numrecips(&z)>0) {
903    owl_message_set_recipient(m,
904                              long_zuser(owl_zwrite_get_recip_n(&z, 0))); /* only gets the first user, must fix */
905  }
[8fec514]906  owl_message_set_opcode(m, owl_zwrite_get_opcode(&z));
[d0d65df]907  owl_message_set_realm(m, owl_zwrite_get_realm(&z)); /* also a hack, but not here */
[b45293f]908  m->zwriteline=owl_strdup(line);
[d0d65df]909  owl_message_set_body(m, body);
[8fec514]910  owl_message_set_zsig(m, zsig);
[b45293f]911 
912  /* save the hostname */
[8298425]913  ret=gethostname(hostbuff, MAXHOSTNAMELEN);
914  hostbuff[MAXHOSTNAMELEN]='\0';
[b45293f]915  if (ret) {
[8298425]916    owl_message_set_hostname(m, "localhost");
917  } else {
918    owl_message_set_hostname(m, hostbuff);
[b45293f]919  }
920  owl_zwrite_free(&z);
921}
[7d4fbcd]922
[0ff8fb57]923void owl_message_pretty_zsig(owl_message *m, char *buff)
924{
[b45293f]925  /* stick a one line version of the zsig in buff */
[7d4fbcd]926  char *ptr;
927
[d0d65df]928  strcpy(buff, owl_message_get_zsig(m));
[b45293f]929  ptr=strchr(buff, '\n');
930  if (ptr) ptr[0]='\0';
[7d4fbcd]931}
932
[0ff8fb57]933void owl_message_free(owl_message *m)
934{
[d0d65df]935  int i, j;
936  owl_pair *p;
[09489b89]937#ifdef HAVE_LIBZEPHYR   
[4b464a4]938  if (owl_message_is_type_zephyr(m) && owl_message_is_direction_in(m)) {
[7d4fbcd]939    ZFreeNotice(&(m->notice));
940  }
[09489b89]941#endif
[25dd31a]942  if (m->timestr) owl_free(m->timestr);
[7d4fbcd]943  if (m->zwriteline) owl_free(m->zwriteline);
[d0d65df]944
945  /* free all the attributes */
946  j=owl_list_get_size(&(m->attributes));
947  for (i=0; i<j; i++) {
948    p=owl_list_get_element(&(m->attributes), i);
949    owl_free(owl_pair_get_key(p));
950    owl_free(owl_pair_get_value(p));
951    owl_free(p);
952  }
953
954  owl_list_free_simple(&(m->attributes));
[7d4fbcd]955 
956  owl_fmtext_free(&(m->fmtext));
957}
Note: See TracBrowser for help on using the repository browser.