source: message.c @ f98c74e

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