source: message.c @ 7ba9ead9

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