source: message.c @ 147d880

debianrelease-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since 147d880 was 147d880, checked in by Nelson Elhage <nelhage@mit.edu>, 12 years ago
Move 'zwriteline' to a message attribute, not a field in struct owl_message.
  • Property mode set to 100644
File size: 23.6 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
340/* caller must free the return */
341char *owl_message_get_shorttimestr(owl_message *m)
342{
343  struct tm *tmstruct;
344  char *out;
345
346  tmstruct=localtime(&(m->time));
347  out=owl_sprintf("%2.2i:%2.2i", tmstruct->tm_hour, tmstruct->tm_min);
348  if (out) return(out);
349  return("??:??");
350}
351
352void owl_message_set_type_admin(owl_message *m)
353{
354  owl_message_set_attribute(m, "type", "admin");
355}
356
357void owl_message_set_type_loopback(owl_message *m)
358{
359  owl_message_set_attribute(m, "type", "loopback");
360}
361
362void owl_message_set_type_zephyr(owl_message *m)
363{
364  owl_message_set_attribute(m, "type", "zephyr");
365}
366
367void owl_message_set_type_aim(owl_message *m)
368{
369  owl_message_set_attribute(m, "type", "AIM");
370}
371
372void owl_message_set_type(owl_message *m, char* type)
373{
374  owl_message_set_attribute(m, "type", type);
375}
376
377int owl_message_is_type(owl_message *m, char *type) {
378  char * t = owl_message_get_attribute_value(m, "type");
379  if(!t) return 0;
380  return !strcasecmp(t, type);
381}
382                                               
383int owl_message_is_type_admin(owl_message *m)
384{
385  return owl_message_is_type(m, "admin");
386}
387
388int owl_message_is_type_generic(owl_message *m)
389{
390  char * t = owl_message_get_attribute_value(m, "type");
391  return (t == NULL);
392}
393
394int owl_message_is_type_zephyr(owl_message *m)
395{
396  return owl_message_is_type(m, "zephyr");
397}
398
399int owl_message_is_type_aim(owl_message *m)
400{
401  return owl_message_is_type(m, "aim");
402}
403
404/* XXX TODO: deprecate this */
405int owl_message_is_type_jabber(owl_message *m)
406{
407  return owl_message_is_type(m, "jabber");
408}
409
410int owl_message_is_type_loopback(owl_message *m)
411{
412  return owl_message_is_type(m, "loopback");
413}
414
415int owl_message_is_pseudo(owl_message *m)
416{
417  if (owl_message_get_attribute_value(m, "pseudo")) return(1);
418  return(0);
419}
420
421char *owl_message_get_text(owl_message *m)
422{
423  owl_message_format(m);
424  return(owl_fmtext_get_text(&(m->fmtext->fmtext)));
425}
426
427void owl_message_set_direction_in(owl_message *m)
428{
429  m->direction=OWL_MESSAGE_DIRECTION_IN;
430}
431
432void owl_message_set_direction_out(owl_message *m)
433{
434  m->direction=OWL_MESSAGE_DIRECTION_OUT;
435}
436
437void owl_message_set_direction_none(owl_message *m)
438{
439  m->direction=OWL_MESSAGE_DIRECTION_NONE;
440}
441
442void owl_message_set_direction(owl_message *m, int direction)
443{
444  m->direction=direction;
445}
446
447int owl_message_is_direction_in(owl_message *m)
448{
449  if (m->direction==OWL_MESSAGE_DIRECTION_IN) return(1);
450  return(0);
451}
452
453int owl_message_is_direction_out(owl_message *m)
454{
455  if (m->direction==OWL_MESSAGE_DIRECTION_OUT) return(1);
456  return(0);
457}
458
459int owl_message_is_direction_none(owl_message *m)
460{
461  if (m->direction==OWL_MESSAGE_DIRECTION_NONE) return(1);
462  return(0);
463}
464
465int owl_message_get_numlines(owl_message *m)
466{
467  if (m == NULL) return(0);
468  owl_message_format(m);
469  return(owl_fmtext_num_lines(&(m->fmtext->fmtext)));
470}
471
472void owl_message_mark_delete(owl_message *m)
473{
474  if (m == NULL) return;
475  m->delete=1;
476}
477
478void owl_message_unmark_delete(owl_message *m)
479{
480  if (m == NULL) return;
481  m->delete=0;
482}
483
484char *owl_message_get_zwriteline(owl_message *m)
485{
486  char *z = owl_message_get_attribute_value(m, "zwriteline");
487  if (!z) return "";
488  return z;
489}
490
491void owl_message_set_zwriteline(owl_message *m, char *line)
492{
493  owl_message_set_attribute(m, "zwriteline", line);
494}
495
496int owl_message_is_delete(owl_message *m)
497{
498  if (m == NULL) return(0);
499  if (m->delete==1) return(1);
500  return(0);
501}
502
503#ifdef HAVE_LIBZEPHYR
504ZNotice_t *owl_message_get_notice(owl_message *m)
505{
506  return(&(m->notice));
507}
508#else
509void *owl_message_get_notice(owl_message *m)
510{
511  return(NULL);
512}
513#endif
514
515void owl_message_set_hostname(owl_message *m, char *hostname)
516{
517  m->hostname=owl_global_intern(&g, hostname);
518}
519
520char *owl_message_get_hostname(owl_message *m)
521{
522  return(m->hostname);
523}
524
525void owl_message_curs_waddstr(owl_message *m, WINDOW *win, int aline, int bline, int acol, int bcol, int fgcolor, int bgcolor)
526{
527  owl_fmtext a, b;
528
529  /* this will ensure that our cached copy is up to date */
530  owl_message_format(m);
531
532  owl_fmtext_init_null(&a);
533  owl_fmtext_init_null(&b);
534 
535  owl_fmtext_truncate_lines(&(m->fmtext->fmtext), aline, bline-aline, &a);
536  owl_fmtext_truncate_cols(&a, acol, bcol, &b);
537  owl_fmtext_colorize(&b, fgcolor);
538  owl_fmtext_colorizebg(&b, bgcolor);
539
540  owl_fmtext_curs_waddstr(&b, win);
541
542  owl_fmtext_free(&a);
543  owl_fmtext_free(&b);
544}
545
546int owl_message_is_personal(owl_message *m)
547{
548  owl_filter * f = owl_global_get_filter(&g, "personal");
549  if(!f) {
550      owl_function_error("personal filter is not defined");
551      return (0);
552  }
553  return owl_filter_message_match(f, m);
554}
555
556int owl_message_is_question(owl_message *m)
557{
558  if(!owl_message_is_type_admin(m)) return 0;
559  if(owl_message_get_attribute_value(m, "question") != NULL) return 1;
560  return 0;
561}
562
563int owl_message_is_answered(owl_message *m) {
564  char *q;
565  if(!owl_message_is_question(m)) return 0;
566  q = owl_message_get_attribute_value(m, "question");
567  if(!q) return 0;
568  return !strcmp(q, "answered");
569}
570
571void owl_message_set_isanswered(owl_message *m) {
572  owl_message_set_attribute(m, "question", "answered");
573}
574
575int owl_message_is_from_me(owl_message *m)
576{
577  if (owl_message_is_type_zephyr(m)) {
578    if (!strcasecmp(owl_message_get_sender(m), owl_zephyr_get_sender())) {
579      return(1);
580    } else {
581      return(0);
582    }
583  } else if (owl_message_is_type_aim(m)) {
584    if (!strcasecmp(owl_message_get_sender(m), owl_global_get_aim_screenname(&g))) {
585      return(1);
586    } else {
587      return(0);
588    }
589  } else if (owl_message_is_type_admin(m)) {
590    return(0);
591  }
592  return(0);
593}
594
595int owl_message_is_mail(owl_message *m)
596{
597  if (owl_message_is_type_zephyr(m)) {
598    if (!strcasecmp(owl_message_get_class(m), "mail") && owl_message_is_private(m)) {
599      return(1);
600    } else {
601      return(0);
602    }
603  }
604  return(0);
605}
606
607int owl_message_is_ping(owl_message *m)
608{
609  if (owl_message_is_type_zephyr(m)) {
610    if (!strcasecmp(owl_message_get_opcode(m), "ping")) {
611      return(1);
612    } else {
613      return(0);
614    }
615  }
616  return(0);
617}
618
619/* caller must free return value. */
620char *owl_message_get_cc(owl_message *m)
621{
622  char *cur, *out, *end;
623
624  cur = owl_message_get_body(m);
625  while (*cur && *cur==' ') cur++;
626  if (strncasecmp(cur, "cc:", 3)) return(NULL);
627  cur+=3;
628  while (*cur && *cur==' ') cur++;
629  out = owl_strdup(cur);
630  end = strchr(out, '\n');
631  if (end) end[0] = '\0';
632  return(out);
633}
634
635/* caller must free return value */
636char *owl_message_get_cc_without_recipient(owl_message *m)
637{
638  char *cc, *out, *end, *user, *shortuser, *recip;
639
640  cc = owl_message_get_cc(m);
641  if (cc == NULL)
642    return NULL;
643
644  recip = short_zuser(owl_message_get_recipient(m));
645  out = owl_malloc(strlen(cc));
646  end = out;
647
648  user = strtok(cc, " ");
649  while (user != NULL) {
650    shortuser = short_zuser(user);
651    if (strcasecmp(shortuser, recip) != 0) {
652      strcpy(end, user);
653      end[strlen(user)] = ' ';
654      end += strlen(user) + 1;
655    }
656    free(shortuser);
657    user = strtok(NULL, " ");
658  }
659  end[0] = '\0';
660
661  owl_free(recip);
662  owl_free(cc);
663
664  if (strlen(out) == 0) {
665    owl_free(out);
666    out = NULL;
667  }
668
669  return(out);
670}
671
672int owl_message_get_id(owl_message *m)
673{
674  return(m->id);
675}
676
677char *owl_message_get_type(owl_message *m) {
678  char * type = owl_message_get_attribute_value(m, "type");
679  if(!type) {
680    return "generic";
681  }
682  return type;
683}
684
685char *owl_message_get_direction(owl_message *m) {
686  switch (m->direction) {
687  case OWL_MESSAGE_DIRECTION_IN:
688    return("in");
689  case OWL_MESSAGE_DIRECTION_OUT:
690    return("out");
691  case OWL_MESSAGE_DIRECTION_NONE:
692    return("none");
693  default:
694    return("unknown");
695  }
696}
697
698int owl_message_parse_direction(char *d) {
699  if(!strcmp(d, "in")) {
700    return OWL_MESSAGE_DIRECTION_IN;
701  } else if(!strcmp(d, "out")) {
702    return OWL_MESSAGE_DIRECTION_OUT;
703  } else {
704    return OWL_MESSAGE_DIRECTION_NONE;
705  }
706}
707
708
709char *owl_message_get_login(owl_message *m) {
710  if (owl_message_is_login(m)) {
711    return "login";
712  } else if (owl_message_is_logout(m)) {
713    return "logout";
714  } else {
715    return "none";
716  }
717}
718
719
720char *owl_message_get_header(owl_message *m) {
721  return owl_message_get_attribute_value(m, "adminheader");
722}
723
724/* return 1 if the message contains "string", 0 otherwise.  This is
725 * case insensitive because the functions it uses are
726 */
727int owl_message_search(owl_message *m, char *string)
728{
729
730  owl_message_format(m); /* is this necessary? */
731 
732  return (owl_fmtext_search(&(m->fmtext->fmtext), string));
733}
734
735
736/* if loginout == -1 it's a logout message
737 *                 0 it's not a login/logout message
738 *                 1 it's a login message
739 */
740void owl_message_create_aim(owl_message *m, char *sender, char *recipient, char *text, int direction, int loginout)
741{
742  owl_message_init(m);
743  owl_message_set_body(m, text);
744  owl_message_set_sender(m, sender);
745  owl_message_set_recipient(m, recipient);
746  owl_message_set_type_aim(m);
747
748  if (direction==OWL_MESSAGE_DIRECTION_IN) {
749    owl_message_set_direction_in(m);
750  } else if (direction==OWL_MESSAGE_DIRECTION_OUT) {
751    owl_message_set_direction_out(m);
752  }
753
754  /* for now all messages that aren't loginout are private */
755  if (!loginout) {
756    owl_message_set_isprivate(m);
757  }
758
759  if (loginout==-1) {
760    owl_message_set_islogout(m);
761  } else if (loginout==1) {
762    owl_message_set_islogin(m);
763  }
764}
765
766void owl_message_create_admin(owl_message *m, char *header, char *text)
767{
768  owl_message_init(m);
769  owl_message_set_type_admin(m);
770  owl_message_set_body(m, text);
771  owl_message_set_attribute(m, "adminheader", header); /* just a hack for now */
772}
773
774/* caller should set the direction */
775void owl_message_create_loopback(owl_message *m, char *text)
776{
777  owl_message_init(m);
778  owl_message_set_type_loopback(m);
779  owl_message_set_body(m, text);
780  owl_message_set_sender(m, "loopsender");
781  owl_message_set_recipient(m, "looprecip");
782  owl_message_set_isprivate(m);
783}
784
785#ifdef HAVE_LIBZEPHYR
786void owl_message_create_from_znotice(owl_message *m, ZNotice_t *n)
787{
788  struct hostent *hent;
789  char *ptr, *tmp, *tmp2;
790  int len;
791
792  owl_message_init(m);
793 
794  owl_message_set_type_zephyr(m);
795  owl_message_set_direction_in(m);
796 
797  /* first save the full notice */
798  memcpy(&(m->notice), n, sizeof(ZNotice_t));
799
800  /* a little gross, we'll replace \r's with ' ' for now */
801  owl_zephyr_hackaway_cr(&(m->notice));
802 
803  /* save the time, we need to nuke the string saved by message_init */
804  if (m->timestr) owl_free(m->timestr);
805  m->time=n->z_time.tv_sec;
806  m->timestr=owl_strdup(ctime(&(m->time)));
807  m->timestr[strlen(m->timestr)-1]='\0';
808
809  /* set other info */
810  owl_message_set_sender(m, n->z_sender);
811  owl_message_set_class(m, n->z_class);
812  owl_message_set_instance(m, n->z_class_inst);
813  owl_message_set_recipient(m, n->z_recipient);
814  if (n->z_opcode) {
815    owl_message_set_opcode(m, n->z_opcode);
816  } else {
817    owl_message_set_opcode(m, "");
818  }
819  owl_message_set_zsig(m, owl_zephyr_get_zsig(n, &len));
820
821  if ((ptr=strchr(n->z_recipient, '@'))!=NULL) {
822    owl_message_set_realm(m, ptr+1);
823  } else {
824    owl_message_set_realm(m, owl_zephyr_get_realm());
825  }
826
827  /* Set the "isloginout" attribute if it's a login message */
828  if (!strcasecmp(n->z_class, "login") || !strcasecmp(n->z_class, OWL_WEBZEPHYR_CLASS)) {
829    if (!strcasecmp(n->z_opcode, "user_login") || !strcasecmp(n->z_opcode, "user_logout")) {
830      tmp=owl_zephyr_get_field(n, 1);
831      owl_message_set_attribute(m, "loginhost", tmp);
832      owl_free(tmp);
833
834      tmp=owl_zephyr_get_field(n, 3);
835      owl_message_set_attribute(m, "logintty", tmp);
836      owl_free(tmp);
837    }
838
839    if (!strcasecmp(n->z_opcode, "user_login")) {
840      owl_message_set_islogin(m);
841    } else if (!strcasecmp(n->z_opcode, "user_logout")) {
842      owl_message_set_islogout(m);
843    }
844  }
845
846 
847  /* set the "isprivate" attribute if it's a private zephyr.
848   ``private'' means recipient is non-empty and doesn't start wit
849   `@' */
850  if (*n->z_recipient && *n->z_recipient != '@') {
851    owl_message_set_isprivate(m);
852  }
853
854  /* set the "isauto" attribute if it's an autoreply */
855  if (!strcasecmp(n->z_message, "Automated reply:") ||
856      !strcasecmp(n->z_opcode, "auto")) {
857    owl_message_set_attribute(m, "isauto", "");
858  }
859
860  /* save the hostname */
861  owl_function_debugmsg("About to do gethostbyaddr");
862  hent=gethostbyaddr((char *) &(n->z_uid.zuid_addr), sizeof(n->z_uid.zuid_addr), AF_INET);
863  if (hent && hent->h_name) {
864    owl_message_set_hostname(m, hent->h_name);
865  } else {
866    owl_message_set_hostname(m, inet_ntoa(n->z_sender_addr));
867  }
868
869  /* set the body */
870  tmp=owl_zephyr_get_message(n, m);
871  if (owl_global_is_newlinestrip(&g)) {
872    tmp2=owl_util_stripnewlines(tmp);
873    owl_message_set_body(m, tmp2);
874    owl_free(tmp2);
875  } else {
876    owl_message_set_body(m, tmp);
877  }
878  owl_free(tmp);
879
880#ifdef OWL_ENABLE_ZCRYPT
881  /* if zcrypt is enabled try to decrypt the message */
882  if (owl_global_is_zcrypt(&g) && !strcasecmp(n->z_opcode, "crypt")) {
883    char *out;
884    int ret;
885
886    out=owl_malloc(strlen(owl_message_get_body(m))*16+20);
887    ret=owl_zcrypt_decrypt(out, owl_message_get_body(m), owl_message_get_class(m), owl_message_get_instance(m));
888    if (ret==0) {
889      owl_message_set_body(m, out);
890    } else {
891      owl_free(out);
892    }
893  }
894#endif 
895}
896#else
897void owl_message_create_from_znotice(owl_message *m, void *n)
898{
899}
900#endif
901
902/* If 'direction' is '0' it is a login message, '1' is a logout message. */
903void owl_message_create_pseudo_zlogin(owl_message *m, int direction, char *user, char *host, char *time, char *tty)
904{
905  char *longuser, *ptr;
906
907#ifdef HAVE_LIBZEPHYR
908  memset(&(m->notice), 0, sizeof(ZNotice_t));
909#endif
910 
911  longuser=long_zuser(user);
912 
913  owl_message_init(m);
914 
915  owl_message_set_type_zephyr(m);
916  owl_message_set_direction_in(m);
917
918  owl_message_set_attribute(m, "pseudo", "");
919  owl_message_set_attribute(m, "loginhost", host ? host : "");
920  owl_message_set_attribute(m, "logintty", tty ? tty : "");
921
922  owl_message_set_sender(m, longuser);
923  owl_message_set_class(m, "LOGIN");
924  owl_message_set_instance(m, longuser);
925  owl_message_set_recipient(m, "");
926  if (direction==0) {
927    owl_message_set_opcode(m, "USER_LOGIN");
928    owl_message_set_islogin(m);
929  } else if (direction==1) {
930    owl_message_set_opcode(m, "USER_LOGOUT");
931    owl_message_set_islogout(m);
932  }
933
934  if ((ptr=strchr(longuser, '@'))!=NULL) {
935    owl_message_set_realm(m, ptr+1);
936  } else {
937    owl_message_set_realm(m, owl_zephyr_get_realm());
938  }
939
940  owl_message_set_body(m, "<uninitialized>");
941
942  /* save the hostname */
943  owl_function_debugmsg("create_pseudo_login: host is %s", host ? host : "");
944  owl_message_set_hostname(m, host ? host : "");
945  owl_free(longuser);
946}
947
948void owl_message_create_from_zwriteline(owl_message *m, char *line, char *body, char *zsig)
949{
950  owl_zwrite z;
951  int ret;
952  char hostbuff[5000];
953 
954  owl_message_init(m);
955
956  /* create a zwrite for the purpose of filling in other message fields */
957  owl_zwrite_create_from_line(&z, line);
958
959  /* set things */
960  owl_message_set_direction_out(m);
961  owl_message_set_type_zephyr(m);
962  owl_message_set_sender(m, owl_zephyr_get_sender());
963  owl_message_set_class(m, owl_zwrite_get_class(&z));
964  owl_message_set_instance(m, owl_zwrite_get_instance(&z));
965  if (owl_zwrite_get_numrecips(&z)>0) {
966    owl_message_set_recipient(m,
967                              long_zuser(owl_zwrite_get_recip_n(&z, 0))); /* only gets the first user, must fix */
968  }
969  owl_message_set_opcode(m, owl_zwrite_get_opcode(&z));
970  owl_message_set_realm(m, owl_zwrite_get_realm(&z)); /* also a hack, but not here */
971  owl_message_set_zwriteline(m, line);
972  owl_message_set_body(m, body);
973  owl_message_set_zsig(m, zsig);
974 
975  /* save the hostname */
976  ret=gethostname(hostbuff, MAXHOSTNAMELEN);
977  hostbuff[MAXHOSTNAMELEN]='\0';
978  if (ret) {
979    owl_message_set_hostname(m, "localhost");
980  } else {
981    owl_message_set_hostname(m, hostbuff);
982  }
983
984  /* set the "isprivate" attribute if it's a private zephyr. */
985  if (owl_zwrite_is_personal(&z)) {
986    owl_message_set_isprivate(m);
987  }
988
989  owl_zwrite_free(&z);
990}
991
992void owl_message_pretty_zsig(owl_message *m, char *buff)
993{
994  /* stick a one line version of the zsig in buff */
995  char *ptr;
996
997  strcpy(buff, owl_message_get_zsig(m));
998  ptr=strchr(buff, '\n');
999  if (ptr) ptr[0]='\0';
1000}
1001
1002void owl_message_free(owl_message *m)
1003{
1004  int i, j;
1005  owl_pair *p;
1006#ifdef HAVE_LIBZEPHYR   
1007  if (owl_message_is_type_zephyr(m) && owl_message_is_direction_in(m)) {
1008    ZFreeNotice(&(m->notice));
1009  }
1010#endif
1011  if (m->timestr) owl_free(m->timestr);
1012
1013  /* free all the attributes */
1014  j=owl_list_get_size(&(m->attributes));
1015  for (i=0; i<j; i++) {
1016    p=owl_list_get_element(&(m->attributes), i);
1017    owl_free(owl_pair_get_value(p));
1018    owl_free(p);
1019  }
1020
1021  owl_list_free_simple(&(m->attributes));
1022 
1023  owl_message_invalidate_format(m);
1024}
Note: See TracBrowser for help on using the repository browser.