source: message.c @ 7488f27

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