source: message.c @ ff528e6

Last change on this file since ff528e6 was ff528e6, checked in by Jason Gross <jasongross9@gmail.com>, 7 years ago
Scrapped the last bits of C code dealing with jabber. We still have an OWL_PROTOCOL_JABBER in owl.h, but it does no harm being there, and we don't use any of the other ones except for AIM. I figure there's no harm in waiting and ripping all of them out at the same time in perl/AIM
  • Property mode set to 100644
File size: 24.6 KB
RevLine 
[7d4fbcd]1#include "owl.h"
[d564c3d]2#include "filterproc.h"
[f271129]3#include <sys/socket.h>
4#include <arpa/inet.h>
[7d4fbcd]5
[a387d12e]6static owl_fmtext_cache fmtext_cache[OWL_FMTEXT_CACHE_SIZE];
7static owl_fmtext_cache * fmtext_cache_next = fmtext_cache;
8
[c79a047]9void owl_message_init_fmtext_cache(void)
[a387d12e]10{
11    int i;
12    for(i = 0; i < OWL_FMTEXT_CACHE_SIZE; i++) {
13        owl_fmtext_init_null(&(fmtext_cache[i].fmtext));
14        fmtext_cache[i].message = NULL;
15    }
16}
17
[e9c6fc8]18static owl_fmtext_cache *owl_message_next_fmtext(void)
[a387d12e]19{
[ad15610]20    owl_fmtext_cache * f = fmtext_cache_next;
[a387d12e]21    if(fmtext_cache_next->message != NULL) {
22        owl_message_invalidate_format(fmtext_cache_next->message);
23    }
24    fmtext_cache_next++;
25    if(fmtext_cache_next - fmtext_cache == OWL_FMTEXT_CACHE_SIZE)
26        fmtext_cache_next = fmtext_cache;
27    return f;
28}
29
[0ff8fb57]30void owl_message_init(owl_message *m)
31{
[7d4fbcd]32  m->id=owl_global_get_nextmsgid(&g);
[4b464a4]33  owl_message_set_direction_none(m);
[7d4fbcd]34  m->delete=0;
35
[b9517cf]36#ifdef HAVE_LIBZEPHYR
37  m->has_notice = false;
38#endif
39
[2d1feac]40  owl_message_set_hostname(m, "");
[f9df2f0]41  m->attributes = g_ptr_array_new();
[d0d65df]42 
[7d4fbcd]43  /* save the time */
[4ebbfbc]44  m->time = time(NULL);
[6646fdb]45  m->timestr = g_strdup(ctime(&m->time));
46  m->timestr[strlen(m->timestr)-1] = '\0';
[25dd31a]47
[a387d12e]48  m->fmtext = NULL;
[4b464a4]49}
50
[5a95b69]51/* add the named attribute to the message.  If an attribute with the
52 * name already exists, replace the old value with the new value
53 */
[e19eb97]54void owl_message_set_attribute(owl_message *m, const char *attrname, const char *attrvalue)
[0ff8fb57]55{
[f9df2f0]56  int i;
[e849734]57  owl_pair *p = NULL, *pair = NULL;
[d0d65df]58
[c314f39]59  attrname = g_intern_string(attrname);
60
[e849734]61  /* look for an existing pair with this key, */
[f9df2f0]62  for (i = 0; i < m->attributes->len; i++) {
63    p = m->attributes->pdata[i];
[c314f39]64    if (owl_pair_get_key(p) == attrname) {
[ddbbcffa]65      g_free(owl_pair_get_value(p));
[e849734]66      pair = p;
[d0d65df]67      break;
68    }
69  }
70
[e849734]71  if(pair ==  NULL) {
[7dcef03]72    pair = g_slice_new(owl_pair);
[c314f39]73    owl_pair_create(pair, attrname, NULL);
[f9df2f0]74    g_ptr_array_add(m->attributes, pair);
[e849734]75  }
[6201646]76  owl_pair_set_value(pair, owl_validate_or_convert(attrvalue));
[d0d65df]77}
78
[5a95b69]79/* return the value associated with the named attribute, or NULL if
80 * the attribute does not exist
81 */
[c08c70a]82const char *owl_message_get_attribute_value(const owl_message *m, const char *attrname)
[0ff8fb57]83{
[f9df2f0]84  int i;
[d0d65df]85  owl_pair *p;
[c314f39]86  GQuark quark;
87
88  quark = g_quark_try_string(attrname);
89  if (quark == 0)
90    /* don't bother inserting into string table */
91    return NULL;
92  attrname = g_quark_to_string(quark);
[d0d65df]93
[f9df2f0]94  for (i = 0; i < m->attributes->len; i++) {
95    p = m->attributes->pdata[i];
[c314f39]96    if (owl_pair_get_key(p) == attrname) {
[d0d65df]97      return(owl_pair_get_value(p));
98    }
99  }
[f4d0975]100
101  /*
102  owl_function_debugmsg("No attribute %s found for message %i",
103                        attrname,
104                        owl_message_get_id(m));
105  */
[d0d65df]106  return(NULL);
107}
108
[5789230]109/* We cheat and indent it for now, since we really want this for
110 * the 'info' function.  Later there should just be a generic
111 * function to indent fmtext.
112 */
[c08c70a]113void owl_message_attributes_tofmtext(const owl_message *m, owl_fmtext *fm) {
[f9df2f0]114  int i;
[5789230]115  owl_pair *p;
[259e60a8]116  char *buff, *tmpbuff;
[5789230]117
118  owl_fmtext_init_null(fm);
119
[f9df2f0]120  for (i = 0; i < m->attributes->len; i++) {
121    p = m->attributes->pdata[i];
[259e60a8]122
[6500907]123    buff = g_strdup(owl_pair_get_value(p));
124    if (buff) {
125      tmpbuff = owl_text_indent(buff, 19, false);
126      g_free(buff);
127      buff = g_strdup_printf("  %-15.15s: %s\n", owl_pair_get_key(p), tmpbuff);
128      g_free(tmpbuff);
129    }
[259e60a8]130
[6711361]131    if(buff == NULL) {
[259e60a8]132      buff = g_strdup_printf("  %-15.15s: %s\n", owl_pair_get_key(p), "<error>");
[6711361]133      if(buff == NULL)
[d4927a7]134        buff=g_strdup("   <error>\n");
[6711361]135    }
[5789230]136    owl_fmtext_append_normal(fm, buff);
[ddbbcffa]137    g_free(buff);
[5789230]138  }
139}
[4b464a4]140
[bd3f232]141void owl_message_invalidate_format(owl_message *m)
142{
[a387d12e]143  if(m->fmtext) {
144    m->fmtext->message = NULL;
145    owl_fmtext_clear(&(m->fmtext->fmtext));
146    m->fmtext=NULL;
147  }
[bd3f232]148}
149
[0ff8fb57]150owl_fmtext *owl_message_get_fmtext(owl_message *m)
151{
[f14a7ee]152  owl_message_format(m);
[a387d12e]153  return(&(m->fmtext->fmtext));
[f14a7ee]154}
155
156void owl_message_format(owl_message *m)
157{
[1fdab04]158  const owl_style *s;
[9e5c9f3]159  const owl_view *v;
[bd3f232]160
[a387d12e]161  if (!m->fmtext) {
162    m->fmtext = owl_message_next_fmtext();
163    m->fmtext->message = m;
[421c8ef7]164    /* for now we assume there's just the one view and use that style */
[ef56a67]165    v=owl_global_get_current_view(&g);
166    s=owl_view_get_style(v);
167
[a387d12e]168    owl_style_get_formattext(s, &(m->fmtext->fmtext), m);
[bd3f232]169  }
[4b464a4]170}
171
[e19eb97]172void owl_message_set_class(owl_message *m, const char *class)
[0ff8fb57]173{
[d0d65df]174  owl_message_set_attribute(m, "class", class);
[4b464a4]175}
176
[c08c70a]177const char *owl_message_get_class(const owl_message *m)
[0ff8fb57]178{
[e19eb97]179  const char *class;
[d0d65df]180
181  class=owl_message_get_attribute_value(m, "class");
182  if (!class) return("");
183  return(class);
[4b464a4]184}
185
[e19eb97]186void owl_message_set_instance(owl_message *m, const char *inst)
[0ff8fb57]187{
[d0d65df]188  owl_message_set_attribute(m, "instance", inst);
[4b464a4]189}
190
[c08c70a]191const char *owl_message_get_instance(const owl_message *m)
[0ff8fb57]192{
[e19eb97]193  const char *instance;
[d0d65df]194
195  instance=owl_message_get_attribute_value(m, "instance");
196  if (!instance) return("");
197  return(instance);
[4b464a4]198}
199
[e19eb97]200void owl_message_set_sender(owl_message *m, const char *sender)
[0ff8fb57]201{
[d0d65df]202  owl_message_set_attribute(m, "sender", sender);
[4b464a4]203}
204
[c08c70a]205const char *owl_message_get_sender(const owl_message *m)
[0ff8fb57]206{
[e19eb97]207  const char *sender;
[d0d65df]208
209  sender=owl_message_get_attribute_value(m, "sender");
210  if (!sender) return("");
211  return(sender);
[4b464a4]212}
213
[e19eb97]214void owl_message_set_zsig(owl_message *m, const char *zsig)
[0ff8fb57]215{
[d0d65df]216  owl_message_set_attribute(m, "zsig", zsig);
[b45293f]217}
218
[c08c70a]219const char *owl_message_get_zsig(const owl_message *m)
[0ff8fb57]220{
[e19eb97]221  const char *zsig;
[d0d65df]222
223  zsig=owl_message_get_attribute_value(m, "zsig");
224  if (!zsig) return("");
225  return(zsig);
[b45293f]226}
227
[e19eb97]228void owl_message_set_recipient(owl_message *m, const char *recip)
[0ff8fb57]229{
[d0d65df]230  owl_message_set_attribute(m, "recipient", recip);
[4b464a4]231}
232
[c08c70a]233const char *owl_message_get_recipient(const owl_message *m)
[0ff8fb57]234{
[4b464a4]235  /* this is stupid for outgoing messages, we need to fix it. */
[d0d65df]236
[e19eb97]237  const char *recip;
[0ff8fb57]238
239  recip=owl_message_get_attribute_value(m, "recipient");
[d0d65df]240  if (!recip) return("");
241  return(recip);
[4b464a4]242}
243
[7f6a8a2]244void owl_message_set_realm(owl_message *m, const char *realm)
[0ff8fb57]245{
[d0d65df]246  owl_message_set_attribute(m, "realm", realm);
[4b464a4]247}
248
[c08c70a]249const char *owl_message_get_realm(const owl_message *m)
[0ff8fb57]250{
[e19eb97]251  const char *realm;
[d0d65df]252 
253  realm=owl_message_get_attribute_value(m, "realm");
254  if (!realm) return("");
255  return(realm);
256}
257
[e19eb97]258void owl_message_set_body(owl_message *m, const char *body)
[0ff8fb57]259{
[d0d65df]260  owl_message_set_attribute(m, "body", body);
261}
262
[c08c70a]263const char *owl_message_get_body(const owl_message *m)
[0ff8fb57]264{
[e19eb97]265  const char *body;
[d0d65df]266
267  body=owl_message_get_attribute_value(m, "body");
268  if (!body) return("");
269  return(body);
[4b464a4]270}
271
[d0d65df]272
[e19eb97]273void owl_message_set_opcode(owl_message *m, const char *opcode)
[0ff8fb57]274{
[d0d65df]275  owl_message_set_attribute(m, "opcode", opcode);
[4b464a4]276}
277
[c08c70a]278const char *owl_message_get_opcode(const owl_message *m)
[0ff8fb57]279{
[e19eb97]280  const char *opcode;
[d0d65df]281
282  opcode=owl_message_get_attribute_value(m, "opcode");
283  if (!opcode) return("");
284  return(opcode);
[4b464a4]285}
286
[5789230]287
[d559df9]288void owl_message_set_islogin(owl_message *m)
[5789230]289{
[d559df9]290  owl_message_set_attribute(m, "loginout", "login");
291}
292
293
294void owl_message_set_islogout(owl_message *m)
295{
296  owl_message_set_attribute(m, "loginout", "logout");
[5789230]297}
298
[c08c70a]299int owl_message_is_loginout(const owl_message *m)
[5789230]300{
[e19eb97]301  const char *res;
[5789230]302
[d559df9]303  res=owl_message_get_attribute_value(m, "loginout");
[5789230]304  if (!res) return(0);
305  return(1);
306}
307
[c08c70a]308int owl_message_is_login(const owl_message *m)
[d559df9]309{
[e19eb97]310  const char *res;
[d559df9]311
312  res=owl_message_get_attribute_value(m, "loginout");
313  if (!res) return(0);
314  if (!strcmp(res, "login")) return(1);
315  return(0);
316}
317
318
[c08c70a]319int owl_message_is_logout(const owl_message *m)
[d559df9]320{
[e19eb97]321  const char *res;
[d559df9]322
323  res=owl_message_get_attribute_value(m, "loginout");
324  if (!res) return(0);
325  if (!strcmp(res, "logout")) return(1);
326  return(0);
327}
328
[5789230]329void owl_message_set_isprivate(owl_message *m)
330{
[312675c]331  owl_message_set_attribute(m, "isprivate", "true");
[5789230]332}
333
[c08c70a]334int owl_message_is_private(const owl_message *m)
[5789230]335{
[e19eb97]336  const char *res;
[5789230]337
338  res=owl_message_get_attribute_value(m, "isprivate");
339  if (!res) return(0);
[635881c]340  return !strcmp(res, "true");
[5789230]341}
342
[c08c70a]343const char *owl_message_get_timestr(const owl_message *m)
[0ff8fb57]344{
[25dd31a]345  if (m->timestr) return(m->timestr);
346  return("");
347}
348
[6646fdb]349CALLER_OWN char *owl_message_format_time(const owl_message *m)
350{
351  return owl_util_format_time(localtime(&m->time));
352}
353
[0ff8fb57]354void owl_message_set_type_admin(owl_message *m)
355{
[30678ae]356  owl_message_set_attribute(m, "type", "admin");
[4b464a4]357}
358
[37eab7f]359void owl_message_set_type_loopback(owl_message *m)
360{
[30678ae]361  owl_message_set_attribute(m, "type", "loopback");
[37eab7f]362}
363
[0ff8fb57]364void owl_message_set_type_zephyr(owl_message *m)
365{
[30678ae]366  owl_message_set_attribute(m, "type", "zephyr");
[4b464a4]367}
[d09e5a1]368
[0ff8fb57]369void owl_message_set_type_aim(owl_message *m)
370{
[e363375]371  owl_message_set_attribute(m, "type", "AIM");
[d09e5a1]372}
[dd16bdd]373
[e19eb97]374void owl_message_set_type(owl_message *m, const char* type)
[dd16bdd]375{
[30678ae]376  owl_message_set_attribute(m, "type", type);
377}
378
[c08c70a]379int owl_message_is_type(const owl_message *m, const char *type) {
[e19eb97]380  const char * t = owl_message_get_attribute_value(m, "type");
[30678ae]381  if(!t) return 0;
[e363375]382  return !strcasecmp(t, type);
[dd16bdd]383}
[4b464a4]384                                               
[c08c70a]385int owl_message_is_type_admin(const owl_message *m)
[0ff8fb57]386{
[30678ae]387  return owl_message_is_type(m, "admin");
[4b464a4]388}
389
[c08c70a]390int owl_message_is_type_zephyr(const owl_message *m)
[0ff8fb57]391{
[30678ae]392  return owl_message_is_type(m, "zephyr");
[4b464a4]393}
394
[c08c70a]395int owl_message_is_type_aim(const owl_message *m)
[0ff8fb57]396{
[30678ae]397  return owl_message_is_type(m, "aim");
[d09e5a1]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{
[b9517cf]496  return m->has_notice ? &m->notice : NULL;
[4b464a4]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);
527
[237d02c]528  owl_fmtext_curs_waddstr(&b, win, OWL_FMTEXT_ATTR_NONE, fgcolor, bgcolor);
[4b464a4]529
[7ab0020]530  owl_fmtext_cleanup(&a);
531  owl_fmtext_cleanup(&b);
[4b464a4]532}
533
[c08c70a]534int owl_message_is_personal(const owl_message *m)
[0ff8fb57]535{
[4542047]536  const owl_filter * f = owl_global_get_filter(&g, "personal");
[ce74deb]537  if(!f) {
538      owl_function_error("personal filter is not defined");
539      return (0);
[0ff8fb57]540  }
[ce74deb]541  return owl_filter_message_match(f, m);
[0ff8fb57]542}
543
[c08c70a]544int owl_message_is_question(const owl_message *m)
[f4d32cd]545{
546  if(!owl_message_is_type_admin(m)) return 0;
547  if(owl_message_get_attribute_value(m, "question") != NULL) return 1;
548  return 0;
549}
550
[c08c70a]551int owl_message_is_answered(const owl_message *m) {
[e19eb97]552  const char *q;
[f4d32cd]553  if(!owl_message_is_question(m)) return 0;
[ad15610]554  q = owl_message_get_attribute_value(m, "question");
[f4d32cd]555  if(!q) return 0;
556  return !strcmp(q, "answered");
557}
558
559void owl_message_set_isanswered(owl_message *m) {
560  owl_message_set_attribute(m, "question", "answered");
561}
562
[c08c70a]563int owl_message_is_mail(const owl_message *m)
[0ff8fb57]564{
565  if (owl_message_is_type_zephyr(m)) {
[5789230]566    if (!strcasecmp(owl_message_get_class(m), "mail") && owl_message_is_private(m)) {
[0ff8fb57]567      return(1);
568    } else {
569      return(0);
570    }
[4b464a4]571  }
572  return(0);
573}
574
575/* caller must free return value. */
[6829afc]576CALLER_OWN char *owl_message_get_cc(const owl_message *m)
[0ff8fb57]577{
[e19eb97]578  const char *cur;
[65b2173]579  char *out, *end;
[4b464a4]580
581  cur = owl_message_get_body(m);
582  while (*cur && *cur==' ') cur++;
[985f85b]583  if (strncasecmp(cur, "cc:", 3)) return(NULL);
[4b464a4]584  cur+=3;
585  while (*cur && *cur==' ') cur++;
[d4927a7]586  out = g_strdup(cur);
[4b464a4]587  end = strchr(out, '\n');
588  if (end) end[0] = '\0';
589  return(out);
590}
591
[48609ce]592/* caller must free return value */
[6829afc]593CALLER_OWN GList *owl_message_get_cc_without_recipient(const owl_message *m)
[48609ce]594{
[839697d]595  char *cc, *shortuser, *recip;
[e19eb97]596  const char *user;
[839697d]597  GList *out = NULL;
[48609ce]598
599  cc = owl_message_get_cc(m);
[9c590d4]600  if (cc == NULL)
601    return NULL;
602
603  recip = short_zuser(owl_message_get_recipient(m));
[48609ce]604
605  user = strtok(cc, " ");
606  while (user != NULL) {
[18108b1e]607    shortuser = short_zuser(user);
608    if (strcasecmp(shortuser, recip) != 0) {
[d4927a7]609      out = g_list_prepend(out, g_strdup(user));
[48609ce]610    }
[ddbbcffa]611    g_free(shortuser);
[48609ce]612    user = strtok(NULL, " ");
613  }
614
[ddbbcffa]615  g_free(recip);
616  g_free(cc);
[9c590d4]617
[48609ce]618  return(out);
619}
620
[c08c70a]621int owl_message_get_id(const owl_message *m)
[0ff8fb57]622{
[4b464a4]623  return(m->id);
624}
[bd3f232]625
[c08c70a]626const char *owl_message_get_type(const owl_message *m) {
[e19eb97]627  const char * type = owl_message_get_attribute_value(m, "type");
[30678ae]628  if(!type) {
629    return "generic";
[dd16bdd]630  }
[30678ae]631  return type;
[dd16bdd]632}
633
[c08c70a]634const char *owl_message_get_direction(const owl_message *m) {
[f1e629d]635  switch (m->direction) {
636  case OWL_MESSAGE_DIRECTION_IN:
637    return("in");
638  case OWL_MESSAGE_DIRECTION_OUT:
639    return("out");
640  case OWL_MESSAGE_DIRECTION_NONE:
641    return("none");
642  default:
643    return("unknown");
644  }
645}
646
[e19eb97]647int owl_message_parse_direction(const char *d) {
[dd16bdd]648  if(!strcmp(d, "in")) {
649    return OWL_MESSAGE_DIRECTION_IN;
650  } else if(!strcmp(d, "out")) {
651    return OWL_MESSAGE_DIRECTION_OUT;
652  } else {
653    return OWL_MESSAGE_DIRECTION_NONE;
654  }
655}
656
657
[c08c70a]658const char *owl_message_get_login(const owl_message *m) {
[f1e629d]659  if (owl_message_is_login(m)) {
660    return "login";
661  } else if (owl_message_is_logout(m)) {
662    return "logout";
663  } else {
664    return "none";
665  }
666}
667
[dd16bdd]668
[c08c70a]669const char *owl_message_get_header(const owl_message *m) {
[f1e629d]670  return owl_message_get_attribute_value(m, "adminheader");
671}
672
[bd3f232]673/* return 1 if the message contains "string", 0 otherwise.  This is
674 * case insensitive because the functions it uses are
675 */
[89b2daf]676int owl_message_search(owl_message *m, const owl_regex *re)
[0ff8fb57]677{
[4b464a4]678
[f14a7ee]679  owl_message_format(m); /* is this necessary? */
[bd3f232]680 
[72f613a]681  return owl_fmtext_search(&(m->fmtext->fmtext), re, 0) >= 0;
[4b464a4]682}
683
684
[d559df9]685/* if loginout == -1 it's a logout message
686 *                 0 it's not a login/logout message
687 *                 1 it's a login message
688 */
[e19eb97]689void owl_message_create_aim(owl_message *m, const char *sender, const char *recipient, const char *text, int direction, int loginout)
[0ff8fb57]690{
[d09e5a1]691  owl_message_init(m);
692  owl_message_set_body(m, text);
693  owl_message_set_sender(m, sender);
[440ce01]694  owl_message_set_recipient(m, recipient);
[d09e5a1]695  owl_message_set_type_aim(m);
[3abf28b]696
[d559df9]697  if (direction==OWL_MESSAGE_DIRECTION_IN) {
698    owl_message_set_direction_in(m);
699  } else if (direction==OWL_MESSAGE_DIRECTION_OUT) {
700    owl_message_set_direction_out(m);
[3abf28b]701  }
702
[d559df9]703  /* for now all messages that aren't loginout are private */
704  if (!loginout) {
705    owl_message_set_isprivate(m);
706  }
[3abf28b]707
[d559df9]708  if (loginout==-1) {
709    owl_message_set_islogout(m);
710  } else if (loginout==1) {
711    owl_message_set_islogin(m);
712  }
[aa5f725]713}
714
[e19eb97]715void owl_message_create_admin(owl_message *m, const char *header, const char *text)
[0ff8fb57]716{
[d0d65df]717  owl_message_init(m);
[4b464a4]718  owl_message_set_type_admin(m);
[d0d65df]719  owl_message_set_body(m, text);
[bd3f232]720  owl_message_set_attribute(m, "adminheader", header); /* just a hack for now */
[7d4fbcd]721}
722
[37eab7f]723/* caller should set the direction */
[e19eb97]724void owl_message_create_loopback(owl_message *m, const char *text)
[37eab7f]725{
726  owl_message_init(m);
727  owl_message_set_type_loopback(m);
728  owl_message_set_body(m, text);
[eec69e1]729  owl_message_set_sender(m, "loopsender");
730  owl_message_set_recipient(m, "looprecip");
[37eab7f]731  owl_message_set_isprivate(m);
732}
733
[4727d31]734void owl_message_save_ccs(owl_message *m) {
735  GList *cc;
736  char *tmp;
737
738  cc = owl_message_get_cc_without_recipient(m);
739
740  if (cc != NULL) {
741    GString *recips = g_string_new("");
742    cc = g_list_prepend(cc, short_zuser(owl_message_get_sender(m)));
743    cc = g_list_prepend(cc, short_zuser(owl_message_get_recipient(m)));
744    cc = g_list_sort(cc, (GCompareFunc)strcasecmp);
745
746    while(cc != NULL) {
747      /* Collapse any identical entries */
748      while (cc->next && strcasecmp(cc->data, cc->next->data) == 0) {
[ddbbcffa]749        g_free(cc->data);
[4727d31]750        cc = g_list_delete_link(cc, cc);
751      }
752
753      tmp = short_zuser(cc->data);
754      g_string_append(recips, tmp);
755
[ddbbcffa]756      g_free(tmp);
757      g_free(cc->data);
[4727d31]758      cc = g_list_delete_link(cc, cc);
759
760      if (cc)
761        g_string_append_c(recips, ' ');
762    }
763
764    owl_message_set_attribute(m, "zephyr_ccs", recips->str);
765    g_string_free(recips, true);
766  }
767}
768
[09489b89]769#ifdef HAVE_LIBZEPHYR
[1077891a]770void owl_message_create_from_znotice(owl_message *m, const ZNotice_t *n)
[0ff8fb57]771{
[ba88ae7]772#ifdef ZNOTICE_SOCKADDR
773  char hbuf[NI_MAXHOST];
774#else /* !ZNOTICE_SOCKADDR */
[7d4fbcd]775  struct hostent *hent;
[ba88ae7]776#endif /* ZNOTICE_SOCKADDR */
[65b2173]777  char *tmp, *tmp2;
[804ab8a]778  int len;
[7d4fbcd]779
[d0d65df]780  owl_message_init(m);
781 
[4b464a4]782  owl_message_set_type_zephyr(m);
783  owl_message_set_direction_in(m);
[7d4fbcd]784 
785  /* first save the full notice */
[66a8cd6]786  m->notice = *n;
[b9517cf]787  m->has_notice = true;
[7d4fbcd]788
[25dd31a]789  /* a little gross, we'll replace \r's with ' ' for now */
[7d4fbcd]790  owl_zephyr_hackaway_cr(&(m->notice));
791 
[25dd31a]792  /* save the time, we need to nuke the string saved by message_init */
[ddbbcffa]793  if (m->timestr) g_free(m->timestr);
[4ebbfbc]794  m->time = n->z_time.tv_sec;
[6646fdb]795  m->timestr = g_strdup(ctime(&m->time));
796  m->timestr[strlen(m->timestr)-1] = '\0';
[25dd31a]797
[7d4fbcd]798  /* set other info */
[d0d65df]799  owl_message_set_sender(m, n->z_sender);
800  owl_message_set_class(m, n->z_class);
801  owl_message_set_instance(m, n->z_class_inst);
802  owl_message_set_recipient(m, n->z_recipient);
[7d4fbcd]803  if (n->z_opcode) {
[d0d65df]804    owl_message_set_opcode(m, n->z_opcode);
[7d4fbcd]805  } else {
[d0d65df]806    owl_message_set_opcode(m, "");
[7d4fbcd]807  }
[804ab8a]808  owl_message_set_zsig(m, owl_zephyr_get_zsig(n, &len));
[7d4fbcd]809
[4e29ecb]810  owl_message_set_realm(m, zuser_realm(n->z_recipient));
[7d4fbcd]811
[5789230]812  /* Set the "isloginout" attribute if it's a login message */
[1d3e925]813  if (!strcasecmp(n->z_class, "login") || !strcasecmp(n->z_class, OWL_WEBZEPHYR_CLASS)) {
[5a95b69]814    if (!strcasecmp(n->z_opcode, "user_login") || !strcasecmp(n->z_opcode, "user_logout")) {
[b0430a6]815      tmp=owl_zephyr_get_field(n, 1);
[5a95b69]816      owl_message_set_attribute(m, "loginhost", tmp);
[ddbbcffa]817      g_free(tmp);
[5a95b69]818
[b0430a6]819      tmp=owl_zephyr_get_field(n, 3);
[5a95b69]820      owl_message_set_attribute(m, "logintty", tmp);
[ddbbcffa]821      g_free(tmp);
[5a95b69]822    }
823
[d559df9]824    if (!strcasecmp(n->z_opcode, "user_login")) {
825      owl_message_set_islogin(m);
826    } else if (!strcasecmp(n->z_opcode, "user_logout")) {
827      owl_message_set_islogout(m);
828    }
[5789230]829  }
830
[5a95b69]831 
[bff1f22]832  /* set the "isprivate" attribute if it's a private zephyr. */
833  if (owl_zwrite_recip_is_personal(n->z_recipient)) {
[5789230]834    owl_message_set_isprivate(m);
835  }
836
[9854278]837  /* set the "isauto" attribute if it's an autoreply */
838  if (!strcasecmp(n->z_message, "Automated reply:") ||
839      !strcasecmp(n->z_opcode, "auto")) {
840    owl_message_set_attribute(m, "isauto", "");
841  }
842
[85d1795]843  /* save the hostname */
[ba88ae7]844#ifdef ZNOTICE_SOCKADDR
845  owl_function_debugmsg("About to do getnameinfo");
846  if (getnameinfo(&n->z_sender_sockaddr.sa, sizeof(n->z_sender_sockaddr), hbuf, sizeof(hbuf), NULL, 0, 0) == 0)
847    owl_message_set_hostname(m, hbuf);
848#else /* !ZNOTICE_SOCKADDR */
[85d1795]849  owl_function_debugmsg("About to do gethostbyaddr");
[ba88ae7]850  hent = gethostbyaddr(&n->z_uid.zuid_addr, sizeof(n->z_uid.zuid_addr), AF_INET);
851  if (hent && hent->h_name)
[85d1795]852    owl_message_set_hostname(m, hent->h_name);
[ba88ae7]853  else
[85d1795]854    owl_message_set_hostname(m, inet_ntoa(n->z_sender_addr));
[ba88ae7]855#endif /* ZNOTICE_SOCKADDR */
[85d1795]856
[b45293f]857  /* set the body */
[85d1795]858  tmp=owl_zephyr_get_message(n, m);
[7e3e00a]859  if (owl_global_is_newlinestrip(&g)) {
[d0d65df]860    tmp2=owl_util_stripnewlines(tmp);
861    owl_message_set_body(m, tmp2);
[ddbbcffa]862    g_free(tmp2);
[7e3e00a]863  } else {
[d0d65df]864    owl_message_set_body(m, tmp);
[7e3e00a]865  }
[ddbbcffa]866  g_free(tmp);
[7d4fbcd]867
[d309eb3]868  /* if zcrypt is enabled try to decrypt the message */
869  if (owl_global_is_zcrypt(&g) && !strcasecmp(n->z_opcode, "crypt")) {
[d564c3d]870    const char *argv[] = {
[97cdbaf5]871      NULL,
[d564c3d]872      "-D",
873      "-c", owl_message_get_class(m),
874      "-i", owl_message_get_instance(m),
875      NULL
876    };
[2354e9a]877    char *out = NULL;
[d564c3d]878    int rv;
879    int status;
[9a7b4f2]880    char *zcrypt;
[d564c3d]881
[dde1b4d]882    zcrypt = g_build_filename(owl_get_bindir(), "zcrypt", NULL);
[97cdbaf5]883    argv[0] = zcrypt;
[9a7b4f2]884
[97cdbaf5]885    rv = call_filter(argv, owl_message_get_body(m), &out, &status);
[ddbbcffa]886    g_free(zcrypt);
[d564c3d]887
888    if(!rv && !status) {
889      int len = strlen(out);
890      if(len >= 8 && !strcmp(out + len - 8, "**END**\n")) {
891        out[len - 8] = 0;
892      }
[d0d65df]893      owl_message_set_body(m, out);
[2354e9a]894    } else {
895      /* Replace the opcode. Otherwise the UI and other bits of code think the
896       * message was encrypted. */
897      owl_message_set_opcode(m, "failed-decrypt");
[dacb555]898    }
[2354e9a]899    g_free(out);
[d309eb3]900  }
[4727d31]901
902  owl_message_save_ccs(m);
[7d4fbcd]903}
[09489b89]904#else
[1077891a]905void owl_message_create_from_znotice(owl_message *m, const void *n)
[09489b89]906{
907}
908#endif
[7d4fbcd]909
[5a95b69]910/* If 'direction' is '0' it is a login message, '1' is a logout message. */
[e19eb97]911void owl_message_create_pseudo_zlogin(owl_message *m, int direction, const char *user, const char *host, const char *time, const char *tty)
[5a95b69]912{
[65b2173]913  char *longuser;
[5a95b69]914
[ba9f236]915#ifdef HAVE_LIBZEPHYR
[5a95b69]916  memset(&(m->notice), 0, sizeof(ZNotice_t));
[ba9f236]917#endif
918 
[5a95b69]919  longuser=long_zuser(user);
920 
921  owl_message_init(m);
922 
923  owl_message_set_type_zephyr(m);
924  owl_message_set_direction_in(m);
925
926  owl_message_set_attribute(m, "pseudo", "");
927  owl_message_set_attribute(m, "loginhost", host ? host : "");
928  owl_message_set_attribute(m, "logintty", tty ? tty : "");
929
930  owl_message_set_sender(m, longuser);
931  owl_message_set_class(m, "LOGIN");
932  owl_message_set_instance(m, longuser);
933  owl_message_set_recipient(m, "");
934  if (direction==0) {
935    owl_message_set_opcode(m, "USER_LOGIN");
936    owl_message_set_islogin(m);
937  } else if (direction==1) {
938    owl_message_set_opcode(m, "USER_LOGOUT");
939    owl_message_set_islogout(m);
940  }
941
[4e29ecb]942  owl_message_set_realm(m, zuser_realm(longuser));
[5a95b69]943
944  owl_message_set_body(m, "<uninitialized>");
945
946  /* save the hostname */
[2de4f20]947  owl_function_debugmsg("create_pseudo_login: host is %s", host ? host : "");
[8298425]948  owl_message_set_hostname(m, host ? host : "");
[ddbbcffa]949  g_free(longuser);
[5a95b69]950}
951
[e5da3fe]952void owl_message_create_from_zwrite(owl_message *m, const owl_zwrite *z, const char *body, int recip_index)
[0ff8fb57]953{
[719119de]954  char *replyline;
[b45293f]955 
[d0d65df]956  owl_message_init(m);
[b45293f]957
958  /* set things */
959  owl_message_set_direction_out(m);
960  owl_message_set_type_zephyr(m);
[09489b89]961  owl_message_set_sender(m, owl_zephyr_get_sender());
[24ccc01]962  owl_message_set_class(m, owl_zwrite_get_class(z));
963  owl_message_set_instance(m, owl_zwrite_get_instance(z));
[e5da3fe]964  if (recip_index < owl_zwrite_get_numrecips(z)) {
[3a3863e]965    char *zuser = owl_zwrite_get_recip_n_with_realm(z, recip_index);
966    char *longzuser = long_zuser(zuser);
[e5da3fe]967    owl_message_set_recipient(m, longzuser);
[3a3863e]968    owl_message_set_realm(m, zuser_realm(longzuser));
[ddbbcffa]969    g_free(longzuser);
[3a3863e]970    g_free(zuser);
971  } else {
972    /* TODO: We should maybe munge this into the case above, as that comparison
973     * is a little overly clever. It's also not clear this codepath ever runs
974     * anyway. */
975    const char *realm = owl_zwrite_get_realm(z);
976    owl_message_set_realm(m, realm[0] ? realm : owl_zephyr_get_realm());
[9ceee9d]977  }
[24ccc01]978  owl_message_set_opcode(m, owl_zwrite_get_opcode(z));
[719119de]979
980  /* Although not strictly the zwriteline, anyone using the unsantized version
981   * of it probably has a bug. */
[a5b5d00]982  replyline = owl_zwrite_get_replyline(z, recip_index);
[719119de]983  owl_message_set_zwriteline(m, replyline);
[ddbbcffa]984  g_free(replyline);
[719119de]985
[d0d65df]986  owl_message_set_body(m, body);
[24ccc01]987  owl_message_set_zsig(m, owl_zwrite_get_zsig(z));
[b45293f]988 
989  /* save the hostname */
[f54b07d]990  owl_message_set_hostname(m, g_get_host_name());
[312675c]991
[ce74deb]992  /* set the "isprivate" attribute if it's a private zephyr. */
[24ccc01]993  if (owl_zwrite_is_personal(z)) {
[312675c]994    owl_message_set_isprivate(m);
[ce74deb]995  }
[4727d31]996
997  owl_message_save_ccs(m);
[b45293f]998}
[7d4fbcd]999
[a44cd91]1000void owl_message_cleanup(owl_message *m)
[0ff8fb57]1001{
[f9df2f0]1002  int i;
[d0d65df]1003  owl_pair *p;
[09489b89]1004#ifdef HAVE_LIBZEPHYR   
[b9517cf]1005  if (m->has_notice) {
[7d4fbcd]1006    ZFreeNotice(&(m->notice));
1007  }
[09489b89]1008#endif
[ddbbcffa]1009  if (m->timestr) g_free(m->timestr);
[d0d65df]1010
1011  /* free all the attributes */
[f9df2f0]1012  for (i = 0; i < m->attributes->len; i++) {
1013    p = m->attributes->pdata[i];
[ddbbcffa]1014    g_free(owl_pair_get_value(p));
[7dcef03]1015    g_slice_free(owl_pair, p);
[d0d65df]1016  }
1017
[f9df2f0]1018  g_ptr_array_free(m->attributes, true);
[7d4fbcd]1019 
[a387d12e]1020  owl_message_invalidate_format(m);
[7d4fbcd]1021}
[91634ec]1022
1023void owl_message_delete(owl_message *m)
1024{
[a44cd91]1025  owl_message_cleanup(m);
[7dcef03]1026  g_slice_free(owl_message, m);
[91634ec]1027}
Note: See TracBrowser for help on using the repository browser.