source: message.c @ 952bb256

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