source: message.c @ 435d6b2

debianrelease-1.10release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since 435d6b2 was f34dd65, checked in by Nelson Elhage <nelhage@mit.edu>, 16 years ago
Kill a whole bunch of unused code. I generated a list of dead functions by building with -ffunction-sections and linking with -Wl,--gc-sections -Wl,--print-gc-sections I kept a number of functions that seemed to be logical parts of an existing API, as well as stuff in varstubs.c, since that file is autogenerated.
  • Property mode set to 100644
File size: 22.4 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
[0ff8fb57]340void owl_message_set_type_admin(owl_message *m)
341{
[30678ae]342  owl_message_set_attribute(m, "type", "admin");
[4b464a4]343}
344
[37eab7f]345void owl_message_set_type_loopback(owl_message *m)
346{
[30678ae]347  owl_message_set_attribute(m, "type", "loopback");
[37eab7f]348}
349
[0ff8fb57]350void owl_message_set_type_zephyr(owl_message *m)
351{
[30678ae]352  owl_message_set_attribute(m, "type", "zephyr");
[4b464a4]353}
[d09e5a1]354
[0ff8fb57]355void owl_message_set_type_aim(owl_message *m)
356{
[e363375]357  owl_message_set_attribute(m, "type", "AIM");
[d09e5a1]358}
[dd16bdd]359
[30678ae]360void owl_message_set_type(owl_message *m, char* type)
[dd16bdd]361{
[30678ae]362  owl_message_set_attribute(m, "type", type);
363}
364
365int owl_message_is_type(owl_message *m, char *type) {
366  char * t = owl_message_get_attribute_value(m, "type");
367  if(!t) return 0;
[e363375]368  return !strcasecmp(t, type);
[dd16bdd]369}
[4b464a4]370                                               
[0ff8fb57]371int owl_message_is_type_admin(owl_message *m)
372{
[30678ae]373  return owl_message_is_type(m, "admin");
[4b464a4]374}
375
[0ff8fb57]376int owl_message_is_type_zephyr(owl_message *m)
377{
[30678ae]378  return owl_message_is_type(m, "zephyr");
[4b464a4]379}
380
[0ff8fb57]381int owl_message_is_type_aim(owl_message *m)
382{
[30678ae]383  return owl_message_is_type(m, "aim");
[d09e5a1]384}
385
[30678ae]386/* XXX TODO: deprecate this */
[421c8ef7]387int owl_message_is_type_jabber(owl_message *m)
[5a95b69]388{
[30678ae]389  return owl_message_is_type(m, "jabber");
[421c8ef7]390}
391
392int owl_message_is_type_loopback(owl_message *m)
393{
[30678ae]394  return owl_message_is_type(m, "loopback");
[421c8ef7]395}
396
397int owl_message_is_pseudo(owl_message *m)
398{
399  if (owl_message_get_attribute_value(m, "pseudo")) return(1);
[4b464a4]400  return(0);
401}
402
[0ff8fb57]403char *owl_message_get_text(owl_message *m)
404{
[21f0a9d]405  owl_message_format(m);
[a387d12e]406  return(owl_fmtext_get_text(&(m->fmtext->fmtext)));
[4b464a4]407}
408
[0ff8fb57]409void owl_message_set_direction_in(owl_message *m)
410{
[4b464a4]411  m->direction=OWL_MESSAGE_DIRECTION_IN;
412}
413
[0ff8fb57]414void owl_message_set_direction_out(owl_message *m)
415{
[4b464a4]416  m->direction=OWL_MESSAGE_DIRECTION_OUT;
417}
418
[0ff8fb57]419void owl_message_set_direction_none(owl_message *m)
420{
[4b464a4]421  m->direction=OWL_MESSAGE_DIRECTION_NONE;
422}
423
[dd16bdd]424void owl_message_set_direction(owl_message *m, int direction)
425{
426  m->direction=direction;
427}
428
[0ff8fb57]429int owl_message_is_direction_in(owl_message *m)
430{
[4b464a4]431  if (m->direction==OWL_MESSAGE_DIRECTION_IN) return(1);
432  return(0);
433}
434
[0ff8fb57]435int owl_message_is_direction_out(owl_message *m)
436{
[4b464a4]437  if (m->direction==OWL_MESSAGE_DIRECTION_OUT) return(1);
438  return(0);
439}
440
[0ff8fb57]441int owl_message_is_direction_none(owl_message *m)
442{
[4b464a4]443  if (m->direction==OWL_MESSAGE_DIRECTION_NONE) return(1);
444  return(0);
445}
446
[0ff8fb57]447int owl_message_get_numlines(owl_message *m)
448{
[4b464a4]449  if (m == NULL) return(0);
[f14a7ee]450  owl_message_format(m);
[a387d12e]451  return(owl_fmtext_num_lines(&(m->fmtext->fmtext)));
[4b464a4]452}
453
[0ff8fb57]454void owl_message_mark_delete(owl_message *m)
455{
[4b464a4]456  if (m == NULL) return;
457  m->delete=1;
458}
459
[0ff8fb57]460void owl_message_unmark_delete(owl_message *m)
461{
[4b464a4]462  if (m == NULL) return;
463  m->delete=0;
464}
465
[0ff8fb57]466char *owl_message_get_zwriteline(owl_message *m)
467{
[147d880]468  char *z = owl_message_get_attribute_value(m, "zwriteline");
469  if (!z) return "";
470  return z;
[4b464a4]471}
472
[0ff8fb57]473void owl_message_set_zwriteline(owl_message *m, char *line)
474{
[147d880]475  owl_message_set_attribute(m, "zwriteline", line);
[4b464a4]476}
477
[0ff8fb57]478int owl_message_is_delete(owl_message *m)
479{
[4b464a4]480  if (m == NULL) return(0);
481  if (m->delete==1) return(1);
482  return(0);
483}
484
[be0a79f]485#ifdef HAVE_LIBZEPHYR
[0ff8fb57]486ZNotice_t *owl_message_get_notice(owl_message *m)
487{
[4b464a4]488  return(&(m->notice));
489}
[09489b89]490#else
491void *owl_message_get_notice(owl_message *m)
492{
493  return(NULL);
494}
[be0a79f]495#endif
[4b464a4]496
[8298425]497void owl_message_set_hostname(owl_message *m, char *hostname)
498{
[e849734]499  m->hostname=owl_global_intern(&g, hostname);
[8298425]500}
501
[0ff8fb57]502char *owl_message_get_hostname(owl_message *m)
503{
[4b464a4]504  return(m->hostname);
505}
506
[8fa9562]507void owl_message_curs_waddstr(owl_message *m, WINDOW *win, int aline, int bline, int acol, int bcol, int fgcolor, int bgcolor)
[0ff8fb57]508{
[4b464a4]509  owl_fmtext a, b;
510
[bd3f232]511  /* this will ensure that our cached copy is up to date */
[f14a7ee]512  owl_message_format(m);
[bd3f232]513
[af2ca19]514  owl_fmtext_init_null(&a);
515  owl_fmtext_init_null(&b);
516 
[3a7cf49]517  owl_fmtext_truncate_lines(&(m->fmtext->fmtext), aline, bline-aline, &a);
[4b464a4]518  owl_fmtext_truncate_cols(&a, acol, bcol, &b);
[9866c3a]519  owl_fmtext_colorize(&b, fgcolor);
520  owl_fmtext_colorizebg(&b, bgcolor);
[4b464a4]521
[47519e1b]522  owl_fmtext_curs_waddstr(&b, win);
[4b464a4]523
524  owl_fmtext_free(&a);
525  owl_fmtext_free(&b);
526}
527
[0ff8fb57]528int owl_message_is_personal(owl_message *m)
529{
[ce74deb]530  owl_filter * f = owl_global_get_filter(&g, "personal");
531  if(!f) {
532      owl_function_error("personal filter is not defined");
533      return (0);
[0ff8fb57]534  }
[ce74deb]535  return owl_filter_message_match(f, m);
[0ff8fb57]536}
537
[f4d32cd]538int owl_message_is_question(owl_message *m)
539{
540  if(!owl_message_is_type_admin(m)) return 0;
541  if(owl_message_get_attribute_value(m, "question") != NULL) return 1;
542  return 0;
543}
544
545int owl_message_is_answered(owl_message *m) {
[ad15610]546  char *q;
[f4d32cd]547  if(!owl_message_is_question(m)) return 0;
[ad15610]548  q = owl_message_get_attribute_value(m, "question");
[f4d32cd]549  if(!q) return 0;
550  return !strcmp(q, "answered");
551}
552
553void owl_message_set_isanswered(owl_message *m) {
554  owl_message_set_attribute(m, "question", "answered");
555}
556
[0ff8fb57]557int owl_message_is_mail(owl_message *m)
558{
559  if (owl_message_is_type_zephyr(m)) {
[5789230]560    if (!strcasecmp(owl_message_get_class(m), "mail") && owl_message_is_private(m)) {
[0ff8fb57]561      return(1);
562    } else {
563      return(0);
564    }
[4b464a4]565  }
566  return(0);
567}
568
569/* caller must free return value. */
[0ff8fb57]570char *owl_message_get_cc(owl_message *m)
571{
[4b464a4]572  char *cur, *out, *end;
573
574  cur = owl_message_get_body(m);
575  while (*cur && *cur==' ') cur++;
[985f85b]576  if (strncasecmp(cur, "cc:", 3)) return(NULL);
[4b464a4]577  cur+=3;
578  while (*cur && *cur==' ') cur++;
579  out = owl_strdup(cur);
580  end = strchr(out, '\n');
581  if (end) end[0] = '\0';
582  return(out);
583}
584
[48609ce]585/* caller must free return value */
586char *owl_message_get_cc_without_recipient(owl_message *m)
587{
[18108b1e]588  char *cc, *out, *end, *user, *shortuser, *recip;
[48609ce]589
590  cc = owl_message_get_cc(m);
[9c590d4]591  if (cc == NULL)
592    return NULL;
593
594  recip = short_zuser(owl_message_get_recipient(m));
[48609ce]595  out = owl_malloc(strlen(cc));
596  end = out;
597
598  user = strtok(cc, " ");
599  while (user != NULL) {
[18108b1e]600    shortuser = short_zuser(user);
601    if (strcasecmp(shortuser, recip) != 0) {
[48609ce]602      strcpy(end, user);
603      end[strlen(user)] = ' ';
604      end += strlen(user) + 1;
605    }
[18108b1e]606    free(shortuser);
[48609ce]607    user = strtok(NULL, " ");
608  }
609  end[0] = '\0';
610
[9c590d4]611  owl_free(recip);
[48609ce]612  owl_free(cc);
[9c590d4]613
614  if (strlen(out) == 0) {
615    owl_free(out);
616    out = NULL;
617  }
618
[48609ce]619  return(out);
620}
621
[0ff8fb57]622int owl_message_get_id(owl_message *m)
623{
[4b464a4]624  return(m->id);
625}
[bd3f232]626
[f1e629d]627char *owl_message_get_type(owl_message *m) {
[30678ae]628  char * type = owl_message_get_attribute_value(m, "type");
629  if(!type) {
630    return "generic";
[dd16bdd]631  }
[30678ae]632  return type;
[dd16bdd]633}
634
[f1e629d]635char *owl_message_get_direction(owl_message *m) {
636  switch (m->direction) {
637  case OWL_MESSAGE_DIRECTION_IN:
638    return("in");
639  case OWL_MESSAGE_DIRECTION_OUT:
640    return("out");
641  case OWL_MESSAGE_DIRECTION_NONE:
642    return("none");
643  default:
644    return("unknown");
645  }
646}
647
[dd16bdd]648int owl_message_parse_direction(char *d) {
649  if(!strcmp(d, "in")) {
650    return OWL_MESSAGE_DIRECTION_IN;
651  } else if(!strcmp(d, "out")) {
652    return OWL_MESSAGE_DIRECTION_OUT;
653  } else {
654    return OWL_MESSAGE_DIRECTION_NONE;
655  }
656}
657
658
[f1e629d]659char *owl_message_get_login(owl_message *m) {
660  if (owl_message_is_login(m)) {
661    return "login";
662  } else if (owl_message_is_logout(m)) {
663    return "logout";
664  } else {
665    return "none";
666  }
667}
668
[dd16bdd]669
[f1e629d]670char *owl_message_get_header(owl_message *m) {
671  return owl_message_get_attribute_value(m, "adminheader");
672}
673
[bd3f232]674/* return 1 if the message contains "string", 0 otherwise.  This is
675 * case insensitive because the functions it uses are
676 */
[0ff8fb57]677int owl_message_search(owl_message *m, char *string)
678{
[4b464a4]679
[f14a7ee]680  owl_message_format(m); /* is this necessary? */
[bd3f232]681 
[a387d12e]682  return (owl_fmtext_search(&(m->fmtext->fmtext), string));
[4b464a4]683}
684
685
[d559df9]686/* if loginout == -1 it's a logout message
687 *                 0 it's not a login/logout message
688 *                 1 it's a login message
689 */
690void owl_message_create_aim(owl_message *m, char *sender, char *recipient, char *text, int direction, int loginout)
[0ff8fb57]691{
[d09e5a1]692  owl_message_init(m);
693  owl_message_set_body(m, text);
694  owl_message_set_sender(m, sender);
[440ce01]695  owl_message_set_recipient(m, recipient);
[d09e5a1]696  owl_message_set_type_aim(m);
[3abf28b]697
[d559df9]698  if (direction==OWL_MESSAGE_DIRECTION_IN) {
699    owl_message_set_direction_in(m);
700  } else if (direction==OWL_MESSAGE_DIRECTION_OUT) {
701    owl_message_set_direction_out(m);
[3abf28b]702  }
703
[d559df9]704  /* for now all messages that aren't loginout are private */
705  if (!loginout) {
706    owl_message_set_isprivate(m);
707  }
[3abf28b]708
[d559df9]709  if (loginout==-1) {
710    owl_message_set_islogout(m);
711  } else if (loginout==1) {
712    owl_message_set_islogin(m);
713  }
[aa5f725]714}
715
[0ff8fb57]716void owl_message_create_admin(owl_message *m, char *header, char *text)
717{
[d0d65df]718  owl_message_init(m);
[4b464a4]719  owl_message_set_type_admin(m);
[d0d65df]720  owl_message_set_body(m, text);
[bd3f232]721  owl_message_set_attribute(m, "adminheader", header); /* just a hack for now */
[7d4fbcd]722}
723
[37eab7f]724/* caller should set the direction */
725void owl_message_create_loopback(owl_message *m, char *text)
726{
727  owl_message_init(m);
728  owl_message_set_type_loopback(m);
729  owl_message_set_body(m, text);
[eec69e1]730  owl_message_set_sender(m, "loopsender");
731  owl_message_set_recipient(m, "looprecip");
[37eab7f]732  owl_message_set_isprivate(m);
733}
734
[09489b89]735#ifdef HAVE_LIBZEPHYR
[0ff8fb57]736void owl_message_create_from_znotice(owl_message *m, ZNotice_t *n)
737{
[7d4fbcd]738  struct hostent *hent;
[d0d65df]739  char *ptr, *tmp, *tmp2;
[804ab8a]740  int len;
[7d4fbcd]741
[d0d65df]742  owl_message_init(m);
743 
[4b464a4]744  owl_message_set_type_zephyr(m);
745  owl_message_set_direction_in(m);
[7d4fbcd]746 
747  /* first save the full notice */
748  memcpy(&(m->notice), n, sizeof(ZNotice_t));
749
[25dd31a]750  /* a little gross, we'll replace \r's with ' ' for now */
[7d4fbcd]751  owl_zephyr_hackaway_cr(&(m->notice));
752 
[25dd31a]753  /* save the time, we need to nuke the string saved by message_init */
[5a95b69]754  if (m->timestr) owl_free(m->timestr);
[25dd31a]755  m->time=n->z_time.tv_sec;
756  m->timestr=owl_strdup(ctime(&(m->time)));
757  m->timestr[strlen(m->timestr)-1]='\0';
758
[7d4fbcd]759  /* set other info */
[d0d65df]760  owl_message_set_sender(m, n->z_sender);
761  owl_message_set_class(m, n->z_class);
762  owl_message_set_instance(m, n->z_class_inst);
763  owl_message_set_recipient(m, n->z_recipient);
[7d4fbcd]764  if (n->z_opcode) {
[d0d65df]765    owl_message_set_opcode(m, n->z_opcode);
[7d4fbcd]766  } else {
[d0d65df]767    owl_message_set_opcode(m, "");
[7d4fbcd]768  }
[804ab8a]769  owl_message_set_zsig(m, owl_zephyr_get_zsig(n, &len));
[7d4fbcd]770
771  if ((ptr=strchr(n->z_recipient, '@'))!=NULL) {
[d0d65df]772    owl_message_set_realm(m, ptr+1);
[7d4fbcd]773  } else {
[09489b89]774    owl_message_set_realm(m, owl_zephyr_get_realm());
[7d4fbcd]775  }
776
[5789230]777  /* Set the "isloginout" attribute if it's a login message */
[1d3e925]778  if (!strcasecmp(n->z_class, "login") || !strcasecmp(n->z_class, OWL_WEBZEPHYR_CLASS)) {
[5a95b69]779    if (!strcasecmp(n->z_opcode, "user_login") || !strcasecmp(n->z_opcode, "user_logout")) {
[b0430a6]780      tmp=owl_zephyr_get_field(n, 1);
[5a95b69]781      owl_message_set_attribute(m, "loginhost", tmp);
782      owl_free(tmp);
783
[b0430a6]784      tmp=owl_zephyr_get_field(n, 3);
[5a95b69]785      owl_message_set_attribute(m, "logintty", tmp);
786      owl_free(tmp);
787    }
788
[d559df9]789    if (!strcasecmp(n->z_opcode, "user_login")) {
790      owl_message_set_islogin(m);
791    } else if (!strcasecmp(n->z_opcode, "user_logout")) {
792      owl_message_set_islogout(m);
793    }
[5789230]794  }
795
[5a95b69]796 
[963542b]797  /* set the "isprivate" attribute if it's a private zephyr.
[ce74deb]798   ``private'' means recipient is non-empty and doesn't start wit
799   `@' */
[963542b]800  if (*n->z_recipient && *n->z_recipient != '@') {
[5789230]801    owl_message_set_isprivate(m);
802  }
803
[9854278]804  /* set the "isauto" attribute if it's an autoreply */
805  if (!strcasecmp(n->z_message, "Automated reply:") ||
806      !strcasecmp(n->z_opcode, "auto")) {
807    owl_message_set_attribute(m, "isauto", "");
808  }
809
[85d1795]810  /* save the hostname */
811  owl_function_debugmsg("About to do gethostbyaddr");
812  hent=gethostbyaddr((char *) &(n->z_uid.zuid_addr), sizeof(n->z_uid.zuid_addr), AF_INET);
813  if (hent && hent->h_name) {
814    owl_message_set_hostname(m, hent->h_name);
815  } else {
816    owl_message_set_hostname(m, inet_ntoa(n->z_sender_addr));
817  }
818
[b45293f]819  /* set the body */
[85d1795]820  tmp=owl_zephyr_get_message(n, m);
[7e3e00a]821  if (owl_global_is_newlinestrip(&g)) {
[d0d65df]822    tmp2=owl_util_stripnewlines(tmp);
823    owl_message_set_body(m, tmp2);
824    owl_free(tmp2);
[7e3e00a]825  } else {
[d0d65df]826    owl_message_set_body(m, tmp);
[7e3e00a]827  }
[ecd5dc5]828  owl_free(tmp);
[7d4fbcd]829
[c86a35c]830#ifdef OWL_ENABLE_ZCRYPT
[d309eb3]831  /* if zcrypt is enabled try to decrypt the message */
832  if (owl_global_is_zcrypt(&g) && !strcasecmp(n->z_opcode, "crypt")) {
833    char *out;
[c269e22]834    int ret;
[d309eb3]835
[d0d65df]836    out=owl_malloc(strlen(owl_message_get_body(m))*16+20);
[9ceee9d]837    ret=owl_zcrypt_decrypt(out, owl_message_get_body(m), owl_message_get_class(m), owl_message_get_instance(m));
[a15a84f]838    if (ret==0) {
[d0d65df]839      owl_message_set_body(m, out);
[a15a84f]840    } else {
841      owl_free(out);
842    }
[d309eb3]843  }
[c269e22]844#endif 
[7d4fbcd]845}
[09489b89]846#else
847void owl_message_create_from_znotice(owl_message *m, void *n)
848{
849}
850#endif
[7d4fbcd]851
[5a95b69]852/* If 'direction' is '0' it is a login message, '1' is a logout message. */
853void owl_message_create_pseudo_zlogin(owl_message *m, int direction, char *user, char *host, char *time, char *tty)
854{
855  char *longuser, *ptr;
856
[ba9f236]857#ifdef HAVE_LIBZEPHYR
[5a95b69]858  memset(&(m->notice), 0, sizeof(ZNotice_t));
[ba9f236]859#endif
860 
[5a95b69]861  longuser=long_zuser(user);
862 
863  owl_message_init(m);
864 
865  owl_message_set_type_zephyr(m);
866  owl_message_set_direction_in(m);
867
868  owl_message_set_attribute(m, "pseudo", "");
869  owl_message_set_attribute(m, "loginhost", host ? host : "");
870  owl_message_set_attribute(m, "logintty", tty ? tty : "");
871
872  owl_message_set_sender(m, longuser);
873  owl_message_set_class(m, "LOGIN");
874  owl_message_set_instance(m, longuser);
875  owl_message_set_recipient(m, "");
876  if (direction==0) {
877    owl_message_set_opcode(m, "USER_LOGIN");
878    owl_message_set_islogin(m);
879  } else if (direction==1) {
880    owl_message_set_opcode(m, "USER_LOGOUT");
881    owl_message_set_islogout(m);
882  }
883
884  if ((ptr=strchr(longuser, '@'))!=NULL) {
885    owl_message_set_realm(m, ptr+1);
886  } else {
887    owl_message_set_realm(m, owl_zephyr_get_realm());
888  }
889
890  owl_message_set_body(m, "<uninitialized>");
891
892  /* save the hostname */
[2de4f20]893  owl_function_debugmsg("create_pseudo_login: host is %s", host ? host : "");
[8298425]894  owl_message_set_hostname(m, host ? host : "");
[5a95b69]895  owl_free(longuser);
896}
897
[0ff8fb57]898void owl_message_create_from_zwriteline(owl_message *m, char *line, char *body, char *zsig)
899{
[b45293f]900  owl_zwrite z;
901  int ret;
[8298425]902  char hostbuff[5000];
[b45293f]903 
[d0d65df]904  owl_message_init(m);
[b45293f]905
906  /* create a zwrite for the purpose of filling in other message fields */
907  owl_zwrite_create_from_line(&z, line);
908
909  /* set things */
910  owl_message_set_direction_out(m);
911  owl_message_set_type_zephyr(m);
[09489b89]912  owl_message_set_sender(m, owl_zephyr_get_sender());
[8fec514]913  owl_message_set_class(m, owl_zwrite_get_class(&z));
914  owl_message_set_instance(m, owl_zwrite_get_instance(&z));
[9ceee9d]915  if (owl_zwrite_get_numrecips(&z)>0) {
[7d471c3]916    char *longzuser = long_zuser(owl_zwrite_get_recip_n(&z, 0));
[9ceee9d]917    owl_message_set_recipient(m,
[7d471c3]918                              longzuser); /* only gets the first user, must fix */
919    owl_free(longzuser);
[9ceee9d]920  }
[8fec514]921  owl_message_set_opcode(m, owl_zwrite_get_opcode(&z));
[d0d65df]922  owl_message_set_realm(m, owl_zwrite_get_realm(&z)); /* also a hack, but not here */
[147d880]923  owl_message_set_zwriteline(m, line);
[d0d65df]924  owl_message_set_body(m, body);
[8fec514]925  owl_message_set_zsig(m, zsig);
[b45293f]926 
927  /* save the hostname */
[8298425]928  ret=gethostname(hostbuff, MAXHOSTNAMELEN);
929  hostbuff[MAXHOSTNAMELEN]='\0';
[b45293f]930  if (ret) {
[8298425]931    owl_message_set_hostname(m, "localhost");
932  } else {
933    owl_message_set_hostname(m, hostbuff);
[b45293f]934  }
[312675c]935
[ce74deb]936  /* set the "isprivate" attribute if it's a private zephyr. */
937  if (owl_zwrite_is_personal(&z)) {
[312675c]938    owl_message_set_isprivate(m);
[ce74deb]939  }
940
941  owl_zwrite_free(&z);
[b45293f]942}
[7d4fbcd]943
[0ff8fb57]944void owl_message_free(owl_message *m)
945{
[d0d65df]946  int i, j;
947  owl_pair *p;
[09489b89]948#ifdef HAVE_LIBZEPHYR   
[4b464a4]949  if (owl_message_is_type_zephyr(m) && owl_message_is_direction_in(m)) {
[7d4fbcd]950    ZFreeNotice(&(m->notice));
951  }
[09489b89]952#endif
[25dd31a]953  if (m->timestr) owl_free(m->timestr);
[d0d65df]954
955  /* free all the attributes */
956  j=owl_list_get_size(&(m->attributes));
957  for (i=0; i<j; i++) {
958    p=owl_list_get_element(&(m->attributes), i);
959    owl_free(owl_pair_get_value(p));
960    owl_free(p);
961  }
962
963  owl_list_free_simple(&(m->attributes));
[7d4fbcd]964 
[a387d12e]965  owl_message_invalidate_format(m);
[7d4fbcd]966}
Note: See TracBrowser for help on using the repository browser.