source: message.c @ dd16bdd

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