source: message.c @ f1589b5

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