source: message.c @ ebf0128

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