source: message.c @ 883502d

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