source: message.c @ 21f0a9d

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