source: message.c @ dab82f29

owl
Last change on this file since dab82f29 was dab82f29, checked in by James M. Kretchmar <kretch@mit.edu>, 16 years ago
Don't crash on mobile device AIM messages [BZ 20] Don't crash when sending to someone not on AIM buddy list [BZ 94] Fix overflow vulerability in zwrite.c and zcrypt.c Add stack protector and -D_FORTIFY_SOURCE=2 where possible Fix hang [barnowl 4c46dfdebc294ca24fef59924f01688b8ee07dee] Fix segfault on missing subs [barnowl 99dabeed7de6d19acb50f1e73aa0bfe5f9469c02] Fix zcrypt bufffer size bug [barnowl 559076cd86edc3a8317819814dd5877b8bc7c3cb] Fix double free [barnowl e97c4a306ae2c9d2147d83da29fee59918198e70] Memory leak fix [barnowl c0a90c2c7ab97b9465c9873757faf312faa0021e] Memory leak fix [barnowl 95caa16b2e9ba10770d87d0955198c14c2d0e16a] Memory leak fix [barnowl 1716fed8c2650e46892cf6571555eac937266c6e] Add getstyle command [barnowl 216c73421653e3ef0e957aa9a9804e208a77c80e] Binary search for msgid [barnowl 0c8ab5eadbb6ecc97a120c91b9a824b33538c764] File-handle leak [barnowl e78397d6ac5da0de31a4e269c0ba7f3d691857a3] Fix delay in jump from top to bottom [barnowl 801b7ac63b962640debbcfd422cb9a60da5fea31] Load subs in chunks [barnowl 93e883d60051b80bf6d35391f9d76fd7dfd198e3] Load subs in chunks [barnowl f6050ee9b0a171a5031f84409eb181062afacd18] Better zsig logging [barnowl d857b667a5a9b108b1a2a26b4a5513bef2b53f80] free() -> owlfree() [barnowl d524c838ac7c115628424b8ac171c3489622ea3a] Escape AIM users in smartfilters [barnowl af9b92e3e7ccb7de276a94b5c7e5861b24e71eff] Better regex escape chars [barnowl 80e54a7631f091be8c9762adb9746bad38104738] Deal with quotes in smart filters [barnowl 4099cf83702763fa8d1efc4f1582a605431bdb77] Deal with 0 length zephyr fields [barnowl 128171aaf7cefa91a4bb1eada93a19d8fd0c355c] Deal with 0 length zephyr fields [barnowl 50e29e35c64d64e223d378d350a7bc4f038d78f5] Deal with 0 length zephyr fields [barnowl 804ab8af8b6d00bcd7e2402df892db8fbd61a3ec] Leave curmsg on screen after resize [barnowl c0f9e3009bc03e80a44de64cd5f2b4033290236e] Rip out brower stuff [barnowl 8e5935d11c699a7ce5a3e6e9a47799564c696d6a] Rip out browser stuff [barnowl 4f15e8e9ceada0d4b2cc969ebf43b0a1fb3709ea] No passwords in command history [barnowl 6e400cc71aa59e041dce677aadf50dc1f25228e2] Format NOC mssages [barnowl a1bb1980e4bca23b8329cc0e7c0bd5027055ea0a] Expand ~ in loadsubs [barnowl 27d8d835dc6d58c08fae10e75aae306c49215143] Expand ~ in source [barnowl 10d67d57cb29221f63a43a30643c697fc7b38911] Only use resizeterm() if it's available Debian backports Change license to GPLv3
  • 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 the branched BarnOwl project, Copyright (c)
22 * 2006-2008 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  /* we should add a global to cache the short zsender */
593  char sender[LINE], *ptr;
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    strcpy(sender, owl_zephyr_get_sender());
600    ptr=strchr(sender, '@');
601    if (ptr) *ptr='\0';
602  } else if (owl_message_is_type_aim(m)) {
603    strcpy(sender, owl_global_get_aim_screenname(&g));
604  } else {
605    return(0);
606  }
607
608  if (stristr(owl_message_get_body(m), sender)) {
609    return(1);
610  }
611  return(0);
612}
613
614/* caller must free return value. */
615char *owl_message_get_cc(owl_message *m)
616{
617  char *cur, *out, *end;
618
619  cur = owl_message_get_body(m);
620  while (*cur && *cur==' ') cur++;
621  if (strncasecmp(cur, "cc:", 3)) return(NULL);
622  cur+=3;
623  while (*cur && *cur==' ') cur++;
624  out = owl_strdup(cur);
625  end = strchr(out, '\n');
626  if (end) end[0] = '\0';
627  return(out);
628}
629
630int owl_message_get_id(owl_message *m)
631{
632  return(m->id);
633}
634
635char *owl_message_get_type(owl_message *m) {
636  switch (m->type) {
637  case OWL_MESSAGE_TYPE_ADMIN:
638    return("admin");
639  case OWL_MESSAGE_TYPE_ZEPHYR:
640    return("zephyr");
641  case OWL_MESSAGE_TYPE_GENERIC:
642    return("generic");
643  case OWL_MESSAGE_TYPE_AIM:
644    return("aim");
645  case OWL_MESSAGE_TYPE_JABBER:
646    return("jabber");
647  case OWL_MESSAGE_TYPE_ICQ:
648    return("icq");
649  case OWL_MESSAGE_TYPE_YAHOO:
650    return("yahoo");
651  case OWL_MESSAGE_TYPE_MSN:
652    return("msn");
653  case OWL_MESSAGE_TYPE_LOOPBACK:
654    return("loopback");
655  default:
656    return("unknown");
657  }
658}
659
660char *owl_message_get_direction(owl_message *m) {
661  switch (m->direction) {
662  case OWL_MESSAGE_DIRECTION_IN:
663    return("in");
664  case OWL_MESSAGE_DIRECTION_OUT:
665    return("out");
666  case OWL_MESSAGE_DIRECTION_NONE:
667    return("none");
668  default:
669    return("unknown");
670  }
671}
672
673char *owl_message_get_login(owl_message *m) {
674  if (owl_message_is_login(m)) {
675    return "login";
676  } else if (owl_message_is_logout(m)) {
677    return "logout";
678  } else {
679    return "none";
680  }
681}
682
683char *owl_message_get_header(owl_message *m) {
684  return owl_message_get_attribute_value(m, "adminheader");
685}
686
687/* return 1 if the message contains "string", 0 otherwise.  This is
688 * case insensitive because the functions it uses are
689 */
690int owl_message_search(owl_message *m, char *string)
691{
692
693  owl_message_format(m); /* is this necessary? */
694 
695  return (owl_fmtext_search(&(m->fmtext), string));
696}
697
698
699/* if loginout == -1 it's a logout message
700 *                 0 it's not a login/logout message
701 *                 1 it's a login message
702 */
703void owl_message_create_aim(owl_message *m, char *sender, char *recipient, char *text, int direction, int loginout)
704{
705  owl_message_init(m);
706  owl_message_set_body(m, text);
707  owl_message_set_sender(m, sender);
708  owl_message_set_recipient(m, recipient);
709  owl_message_set_type_aim(m);
710
711  if (direction==OWL_MESSAGE_DIRECTION_IN) {
712    owl_message_set_direction_in(m);
713  } else if (direction==OWL_MESSAGE_DIRECTION_OUT) {
714    owl_message_set_direction_out(m);
715  }
716
717  /* for now all messages that aren't loginout are private */
718  if (!loginout) {
719    owl_message_set_isprivate(m);
720  }
721
722  if (loginout==-1) {
723    owl_message_set_islogout(m);
724  } else if (loginout==1) {
725    owl_message_set_islogin(m);
726  }
727}
728
729void owl_message_create_admin(owl_message *m, char *header, char *text)
730{
731  owl_message_init(m);
732  owl_message_set_type_admin(m);
733  owl_message_set_body(m, text);
734  owl_message_set_attribute(m, "adminheader", header); /* just a hack for now */
735}
736
737/* caller should set the direction */
738void owl_message_create_loopback(owl_message *m, char *text)
739{
740  owl_message_init(m);
741  owl_message_set_type_loopback(m);
742  owl_message_set_body(m, text);
743  owl_message_set_sender(m, "loopsender");
744  owl_message_set_recipient(m, "looprecip");
745  owl_message_set_isprivate(m);
746}
747
748#ifdef HAVE_LIBZEPHYR
749void owl_message_create_from_znotice(owl_message *m, ZNotice_t *n)
750{
751  struct hostent *hent;
752  char *ptr, *tmp, *tmp2;
753  int len;
754
755  owl_message_init(m);
756 
757  owl_message_set_type_zephyr(m);
758  owl_message_set_direction_in(m);
759 
760  /* first save the full notice */
761  memcpy(&(m->notice), n, sizeof(ZNotice_t));
762
763  /* a little gross, we'll replace \r's with ' ' for now */
764  owl_zephyr_hackaway_cr(&(m->notice));
765 
766  /* save the time, we need to nuke the string saved by message_init */
767  if (m->timestr) owl_free(m->timestr);
768  m->time=n->z_time.tv_sec;
769  m->timestr=owl_strdup(ctime(&(m->time)));
770  m->timestr[strlen(m->timestr)-1]='\0';
771
772  /* set other info */
773  owl_message_set_sender(m, n->z_sender);
774  owl_message_set_class(m, n->z_class);
775  owl_message_set_instance(m, n->z_class_inst);
776  owl_message_set_recipient(m, n->z_recipient);
777  if (n->z_opcode) {
778    owl_message_set_opcode(m, n->z_opcode);
779  } else {
780    owl_message_set_opcode(m, "");
781  }
782  owl_message_set_zsig(m, owl_zephyr_get_zsig(n, &len));
783
784  if ((ptr=strchr(n->z_recipient, '@'))!=NULL) {
785    owl_message_set_realm(m, ptr+1);
786  } else {
787    owl_message_set_realm(m, owl_zephyr_get_realm());
788  }
789
790  /* Set the "isloginout" attribute if it's a login message */
791  if (!strcasecmp(n->z_class, "login") || !strcasecmp(n->z_class, OWL_WEBZEPHYR_CLASS)) {
792    if (!strcasecmp(n->z_opcode, "user_login") || !strcasecmp(n->z_opcode, "user_logout")) {
793      tmp=owl_zephyr_get_field(n, 1);
794      owl_message_set_attribute(m, "loginhost", tmp);
795      owl_free(tmp);
796
797      tmp=owl_zephyr_get_field(n, 3);
798      owl_message_set_attribute(m, "logintty", tmp);
799      owl_free(tmp);
800    }
801
802    if (!strcasecmp(n->z_opcode, "user_login")) {
803      owl_message_set_islogin(m);
804    } else if (!strcasecmp(n->z_opcode, "user_logout")) {
805      owl_message_set_islogout(m);
806    }
807  }
808
809 
810  /* set the "isprivate" attribute if it's a private zephyr */
811  if (!strcasecmp(n->z_recipient, owl_zephyr_get_sender())) {
812    owl_message_set_isprivate(m);
813  }
814
815  /* set the "isauto" attribute if it's an autoreply */
816  if (!strcasecmp(n->z_message, "Automated reply:") ||
817      !strcasecmp(n->z_opcode, "auto")) {
818    owl_message_set_attribute(m, "isauto", "");
819  }
820
821  m->zwriteline=strdup("");
822
823  /* set the body */
824  tmp=owl_zephyr_get_message(n);
825  if (owl_global_is_newlinestrip(&g)) {
826    tmp2=owl_util_stripnewlines(tmp);
827    owl_message_set_body(m, tmp2);
828    owl_free(tmp2);
829  } else {
830    owl_message_set_body(m, tmp);
831  }
832  owl_free(tmp);
833
834#ifdef OWL_ENABLE_ZCRYPT
835  /* if zcrypt is enabled try to decrypt the message */
836  if (owl_global_is_zcrypt(&g) && !strcasecmp(n->z_opcode, "crypt")) {
837    char *out;
838    int ret;
839
840    out=owl_malloc(strlen(owl_message_get_body(m))*16+20);
841    ret=owl_zcrypt_decrypt(out, owl_message_get_body(m), owl_message_get_class(m), owl_message_get_instance(m));
842    if (ret==0) {
843      owl_message_set_body(m, out);
844    } else {
845      owl_free(out);
846    }
847  }
848#endif 
849
850  /* save the hostname */
851  owl_function_debugmsg("About to do gethostbyaddr");
852  hent=gethostbyaddr((char *) &(n->z_uid.zuid_addr), sizeof(n->z_uid.zuid_addr), AF_INET);
853  if (hent && hent->h_name) {
854    owl_message_set_hostname(m, hent->h_name);
855  } else {
856    owl_message_set_hostname(m, inet_ntoa(n->z_sender_addr));
857  }
858}
859#else
860void owl_message_create_from_znotice(owl_message *m, void *n)
861{
862}
863#endif
864
865/* If 'direction' is '0' it is a login message, '1' is a logout message. */
866void owl_message_create_pseudo_zlogin(owl_message *m, int direction, char *user, char *host, char *time, char *tty)
867{
868  char *longuser, *ptr;
869
870#ifdef HAVE_LIBZEPHYR
871  memset(&(m->notice), 0, sizeof(ZNotice_t));
872#endif
873 
874  longuser=long_zuser(user);
875 
876  owl_message_init(m);
877 
878  owl_message_set_type_zephyr(m);
879  owl_message_set_direction_in(m);
880
881  owl_message_set_attribute(m, "pseudo", "");
882  owl_message_set_attribute(m, "loginhost", host ? host : "");
883  owl_message_set_attribute(m, "logintty", tty ? tty : "");
884
885  owl_message_set_sender(m, longuser);
886  owl_message_set_class(m, "LOGIN");
887  owl_message_set_instance(m, longuser);
888  owl_message_set_recipient(m, "");
889  if (direction==0) {
890    owl_message_set_opcode(m, "USER_LOGIN");
891    owl_message_set_islogin(m);
892  } else if (direction==1) {
893    owl_message_set_opcode(m, "USER_LOGOUT");
894    owl_message_set_islogout(m);
895  }
896
897  if ((ptr=strchr(longuser, '@'))!=NULL) {
898    owl_message_set_realm(m, ptr+1);
899  } else {
900    owl_message_set_realm(m, owl_zephyr_get_realm());
901  }
902
903  m->zwriteline=strdup("");
904
905  owl_message_set_body(m, "<uninitialized>");
906
907  /* save the hostname */
908  owl_function_debugmsg("create_pseudo_login: host is %s", host ? host : "");
909  owl_message_set_hostname(m, host ? host : "");
910  owl_free(longuser);
911}
912
913void owl_message_create_from_zwriteline(owl_message *m, char *line, char *body, char *zsig)
914{
915  owl_zwrite z;
916  int ret;
917  char hostbuff[5000];
918 
919  owl_message_init(m);
920
921  /* create a zwrite for the purpose of filling in other message fields */
922  owl_zwrite_create_from_line(&z, line);
923
924  /* set things */
925  owl_message_set_direction_out(m);
926  owl_message_set_type_zephyr(m);
927  owl_message_set_sender(m, owl_zephyr_get_sender());
928  owl_message_set_class(m, owl_zwrite_get_class(&z));
929  owl_message_set_instance(m, owl_zwrite_get_instance(&z));
930  if (owl_zwrite_get_numrecips(&z)>0) {
931    owl_message_set_recipient(m,
932                              long_zuser(owl_zwrite_get_recip_n(&z, 0))); /* only gets the first user, must fix */
933  }
934  owl_message_set_opcode(m, owl_zwrite_get_opcode(&z));
935  owl_message_set_realm(m, owl_zwrite_get_realm(&z)); /* also a hack, but not here */
936  m->zwriteline=owl_strdup(line);
937  owl_message_set_body(m, body);
938  owl_message_set_zsig(m, zsig);
939 
940  /* save the hostname */
941  ret=gethostname(hostbuff, MAXHOSTNAMELEN);
942  hostbuff[MAXHOSTNAMELEN]='\0';
943  if (ret) {
944    owl_message_set_hostname(m, "localhost");
945  } else {
946    owl_message_set_hostname(m, hostbuff);
947  }
948  owl_zwrite_free(&z);
949}
950
951void owl_message_pretty_zsig(owl_message *m, char *buff)
952{
953  /* stick a one line version of the zsig in buff */
954  char *ptr;
955
956  strcpy(buff, owl_message_get_zsig(m));
957  ptr=strchr(buff, '\n');
958  if (ptr) ptr[0]='\0';
959}
960
961void owl_message_free(owl_message *m)
962{
963  int i, j;
964  owl_pair *p;
965#ifdef HAVE_LIBZEPHYR   
966  if (owl_message_is_type_zephyr(m) && owl_message_is_direction_in(m)) {
967    ZFreeNotice(&(m->notice));
968  }
969#endif
970  if (m->timestr) owl_free(m->timestr);
971  if (m->zwriteline) owl_free(m->zwriteline);
972
973  /* free all the attributes */
974  j=owl_list_get_size(&(m->attributes));
975  for (i=0; i<j; i++) {
976    p=owl_list_get_element(&(m->attributes), i);
977    owl_free(owl_pair_get_key(p));
978    owl_free(owl_pair_get_value(p));
979    owl_free(p);
980  }
981
982  owl_list_free_simple(&(m->attributes));
983 
984  owl_fmtext_free(&(m->fmtext));
985}
Note: See TracBrowser for help on using the repository browser.