source: message.c @ 4cc0ee0b

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