source: message.c @ 85d1795

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