source: message.c @ f907cf0

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