source: message.c @ 5d9c664

barnowl_perlaimdebianowlrelease-1.10release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since 5d9c664 was f1e629d, checked in by Erik Nygren <nygren@mit.edu>, 21 years ago
New API for perl message formatting functions. Legacy variables are still supported for owl::format_msg and owl::receive_msg, but these functions are now also passed an owl::Message object which contains methods for accessing the contents of the message. See perlwrap.pm (and docs TBD) for the available methods. *** WARNING: The exact API for owl::Message has *** not yet stabilized. Added "style" command for creating new styles. Usage: style <name> perl <function_name> Added support for "show styles". Changed global style table from list to dictionary. Changed AIM password prompt from "Password:" to "AIM Password:". Messages are reformatted after a window resize to allow styles to take into account the width of the window. When perl throws an error, the message is put in the msgwin if possible. Added perl functions for: owl::getcurmsg() -- returns an owl::Message object for the active message in the current view. owl::getnumcols() -- returns the column width of the window owl::zephyr_getrealm() -- returns the zephyr realm owl::zephyr_getsender() -- returns the zephyr sender Made owl::COMMAND("foo"); be syntactic sugar for owl::command("COMMAND foo"); *** Is this a good or bad idea? *** This feature may be taken out before release. Added perlwrap.pm to contain perl code to be compiled into the binary. This is transformed into perlwrap.c by encapsulate.pl. Renamed readconfig.c to perlconfig.c and changed variables accordingly. Minor bugfixes in cmd.c and commands.c
  • Property mode set to 100644
File size: 19.2 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
[0ff8fb57]15void owl_message_init(owl_message *m)
16{
[7d4fbcd]17  time_t t;
18
19  m->id=owl_global_get_nextmsgid(&g);
[4b464a4]20  m->type=OWL_MESSAGE_TYPE_GENERIC;
21  owl_message_set_direction_none(m);
[7d4fbcd]22  m->delete=0;
23  strcpy(m->hostname, "");
24  m->zwriteline=strdup("");
[bd3f232]25  m->invalid_format=1;
[7d4fbcd]26
[d0d65df]27  owl_list_create(&(m->attributes));
28 
[7d4fbcd]29  /* save the time */
30  t=time(NULL);
31  m->time=owl_strdup(ctime(&t));
32  m->time[strlen(m->time)-1]='\0';
[bd3f232]33  owl_fmtext_init_null(&(m->fmtext));
[4b464a4]34}
35
[0ff8fb57]36void owl_message_set_attribute(owl_message *m, char *attrname, char *attrvalue)
37{
[d0d65df]38  /* add the named attribute to the message.  If an attribute with the
39     name already exists, replace the old value with the new value */
40
41  int i, j;
42  owl_pair *p;
43
44  /* look for an existing pair with this key, and nuke the entry if
45     found */
46  j=owl_list_get_size(&(m->attributes));
47  for (i=0; i<j; i++) {
48    p=owl_list_get_element(&(m->attributes), i);
49    if (!strcmp(owl_pair_get_key(p), attrname)) {
50      owl_free(owl_pair_get_key(p));
51      owl_free(owl_pair_get_value(p));
52      owl_free(p);
53      owl_list_remove_element(&(m->attributes), i);
54      break;
55    }
56  }
57
58  p=owl_malloc(sizeof(owl_pair));
59  owl_pair_create(p, owl_strdup(attrname), owl_strdup(attrvalue));
60  owl_list_append_element(&(m->attributes), p);
61}
62
[0ff8fb57]63char *owl_message_get_attribute_value(owl_message *m, char *attrname)
64{
[d0d65df]65  /* return the value associated with the named attribute, or NULL if
66     the attribute does not exist */
67  int i, j;
68  owl_pair *p;
69
70  j=owl_list_get_size(&(m->attributes));
71  for (i=0; i<j; i++) {
72    p=owl_list_get_element(&(m->attributes), i);
73    if (!strcmp(owl_pair_get_key(p), attrname)) {
74      return(owl_pair_get_value(p));
75    }
76  }
77  owl_function_debugmsg("No attribute %s found", attrname);
78  return(NULL);
79}
80
[5789230]81/* We cheat and indent it for now, since we really want this for
82 * the 'info' function.  Later there should just be a generic
83 * function to indent fmtext.
84 */
85void owl_message_attributes_tofmtext(owl_message *m, owl_fmtext *fm) {
86  int i, j;
87  owl_pair *p;
88  char *buff;
89
90  owl_fmtext_init_null(fm);
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    buff=owl_sprintf("  %-15.15s: %-35.35s\n", owl_pair_get_key(p), owl_pair_get_value(p));
96    owl_fmtext_append_normal(fm, buff);
97    owl_free(buff);
98  }
99}
[4b464a4]100
[bd3f232]101void owl_message_invalidate_format(owl_message *m)
102{
103  m->invalid_format=1;
104}
105
[0ff8fb57]106owl_fmtext *owl_message_get_fmtext(owl_message *m)
107{
[f14a7ee]108  owl_message_format(m);
109  return(&(m->fmtext));
110}
111
112void owl_message_format(owl_message *m)
113{
[bd3f232]114  owl_style *s;
[ef56a67]115  owl_view *v;
[bd3f232]116
117  if (m->invalid_format) {
[ef56a67]118    /* for now we assume there's jsut the one view and use that style */
119    v=owl_global_get_current_view(&g);
120    s=owl_view_get_style(v);
121
[bd3f232]122    owl_fmtext_free(&(m->fmtext));
123    owl_fmtext_init_null(&(m->fmtext));
124    owl_style_get_formattext(s, &(m->fmtext), m);
125    m->invalid_format=0;
126  }
[4b464a4]127}
128
[0ff8fb57]129void owl_message_set_class(owl_message *m, char *class)
130{
[d0d65df]131  owl_message_set_attribute(m, "class", class);
[4b464a4]132}
133
[0ff8fb57]134char *owl_message_get_class(owl_message *m)
135{
[d0d65df]136  char *class;
137
138  class=owl_message_get_attribute_value(m, "class");
139  if (!class) return("");
140  return(class);
[4b464a4]141}
142
[0ff8fb57]143void owl_message_set_instance(owl_message *m, char *inst)
144{
[d0d65df]145  owl_message_set_attribute(m, "instance", inst);
[4b464a4]146}
147
[0ff8fb57]148char *owl_message_get_instance(owl_message *m)
149{
[d0d65df]150  char *instance;
151
152  instance=owl_message_get_attribute_value(m, "instance");
153  if (!instance) return("");
154  return(instance);
[4b464a4]155}
156
[0ff8fb57]157void owl_message_set_sender(owl_message *m, char *sender)
158{
[d0d65df]159  owl_message_set_attribute(m, "sender", sender);
[4b464a4]160}
161
[0ff8fb57]162char *owl_message_get_sender(owl_message *m)
163{
[d0d65df]164  char *sender;
165
166  sender=owl_message_get_attribute_value(m, "sender");
167  if (!sender) return("");
168  return(sender);
[4b464a4]169}
170
[0ff8fb57]171void owl_message_set_zsig(owl_message *m, char *zsig)
172{
[d0d65df]173  owl_message_set_attribute(m, "zsig", zsig);
[b45293f]174}
175
[0ff8fb57]176char *owl_message_get_zsig(owl_message *m)
177{
[d0d65df]178  char *zsig;
179
180  zsig=owl_message_get_attribute_value(m, "zsig");
181  if (!zsig) return("");
182  return(zsig);
[b45293f]183}
184
[0ff8fb57]185void owl_message_set_recipient(owl_message *m, char *recip)
186{
[d0d65df]187  owl_message_set_attribute(m, "recipient", recip);
[4b464a4]188}
189
[0ff8fb57]190char *owl_message_get_recipient(owl_message *m)
191{
[4b464a4]192  /* this is stupid for outgoing messages, we need to fix it. */
[d0d65df]193
194  char *recip;
[0ff8fb57]195
196  recip=owl_message_get_attribute_value(m, "recipient");
[d0d65df]197  if (!recip) return("");
198  return(recip);
[4b464a4]199}
200
[0ff8fb57]201void owl_message_set_realm(owl_message *m, char *realm)
202{
[d0d65df]203  owl_message_set_attribute(m, "realm", realm);
[4b464a4]204}
205
[0ff8fb57]206char *owl_message_get_realm(owl_message *m)
207{
[d0d65df]208  char *realm;
209 
210  realm=owl_message_get_attribute_value(m, "realm");
211  if (!realm) return("");
212  return(realm);
213}
214
[0ff8fb57]215void owl_message_set_body(owl_message *m, char *body)
216{
[d0d65df]217  owl_message_set_attribute(m, "body", body);
218}
219
[0ff8fb57]220char *owl_message_get_body(owl_message *m)
221{
[d0d65df]222  char *body;
223
224  body=owl_message_get_attribute_value(m, "body");
225  if (!body) return("");
226  return(body);
[4b464a4]227}
228
[d0d65df]229
[0ff8fb57]230void owl_message_set_opcode(owl_message *m, char *opcode)
231{
[d0d65df]232  owl_message_set_attribute(m, "opcode", opcode);
[4b464a4]233}
234
[0ff8fb57]235char *owl_message_get_opcode(owl_message *m)
236{
[d0d65df]237  char *opcode;
238
239  opcode=owl_message_get_attribute_value(m, "opcode");
240  if (!opcode) return("");
241  return(opcode);
[4b464a4]242}
243
[5789230]244
[d559df9]245void owl_message_set_islogin(owl_message *m)
[5789230]246{
[d559df9]247  owl_message_set_attribute(m, "loginout", "login");
248}
249
250
251void owl_message_set_islogout(owl_message *m)
252{
253  owl_message_set_attribute(m, "loginout", "logout");
[5789230]254}
255
256int owl_message_is_loginout(owl_message *m)
257{
258  char *res;
259
[d559df9]260  res=owl_message_get_attribute_value(m, "loginout");
[5789230]261  if (!res) return(0);
262  return(1);
263}
264
[d559df9]265int owl_message_is_login(owl_message *m)
266{
267  char *res;
268
269  res=owl_message_get_attribute_value(m, "loginout");
270  if (!res) return(0);
271  if (!strcmp(res, "login")) return(1);
272  return(0);
273}
274
275
276int owl_message_is_logout(owl_message *m)
277{
278  char *res;
279
280  res=owl_message_get_attribute_value(m, "loginout");
281  if (!res) return(0);
282  if (!strcmp(res, "logout")) return(1);
283  return(0);
284}
285
[5789230]286void owl_message_set_isprivate(owl_message *m)
287{
288  owl_message_set_attribute(m, "isprivate", "");
289}
290
291int owl_message_is_private(owl_message *m)
292{
293  char *res;
294
295  res=owl_message_get_attribute_value(m, "isprivate");
296  if (!res) return(0);
297  return(1);
298}
299
[0ff8fb57]300char *owl_message_get_timestr(owl_message *m)
301{
[4b464a4]302  return(m->time);
303}
304
[0ff8fb57]305void owl_message_set_type_admin(owl_message *m)
306{
[4b464a4]307  m->type=OWL_MESSAGE_TYPE_ADMIN;
308}
309
[0ff8fb57]310void owl_message_set_type_zephyr(owl_message *m)
311{
[4b464a4]312  m->type=OWL_MESSAGE_TYPE_ZEPHYR;
313}
[d09e5a1]314
[0ff8fb57]315void owl_message_set_type_aim(owl_message *m)
316{
[d09e5a1]317  m->type=OWL_MESSAGE_TYPE_AIM;
318}
[4b464a4]319                                               
[0ff8fb57]320int owl_message_is_type_admin(owl_message *m)
321{
[4b464a4]322  if (m->type==OWL_MESSAGE_TYPE_ADMIN) return(1);
323  return(0);
324}
325
[0ff8fb57]326int owl_message_is_type_zephyr(owl_message *m)
327{
[4b464a4]328  if (m->type==OWL_MESSAGE_TYPE_ZEPHYR) return(1);
329  return(0);
330}
331
[0ff8fb57]332int owl_message_is_type_aim(owl_message *m)
333{
[d09e5a1]334  if (m->type==OWL_MESSAGE_TYPE_AIM) return(1);
335  return(0);
336}
337
[0ff8fb57]338int owl_message_is_type_generic(owl_message *m)
339{
[4b464a4]340  if (m->type==OWL_MESSAGE_TYPE_GENERIC) return(1);
341  return(0);
342}
343
[df0d93a]344char *owl_message_type_to_string(owl_message *m)
345{
346  if (m->type==OWL_MESSAGE_TYPE_ADMIN) return("admin");
347  if (m->type==OWL_MESSAGE_TYPE_GENERIC) return("generic");
348  if (m->type==OWL_MESSAGE_TYPE_ZEPHYR) return("zephyr");
349  if (m->type==OWL_MESSAGE_TYPE_AIM) return("aim");
350  if (m->type==OWL_MESSAGE_TYPE_JABBER) return("jabber");
351  if (m->type==OWL_MESSAGE_TYPE_ICQ) return("icq");
352  if (m->type==OWL_MESSAGE_TYPE_MSN) return("msn");
353  return("unknown");
354}
355
[0ff8fb57]356char *owl_message_get_text(owl_message *m)
357{
[4b464a4]358  return(owl_fmtext_get_text(&(m->fmtext)));
359}
360
[0ff8fb57]361void owl_message_set_direction_in(owl_message *m)
362{
[4b464a4]363  m->direction=OWL_MESSAGE_DIRECTION_IN;
364}
365
[0ff8fb57]366void owl_message_set_direction_out(owl_message *m)
367{
[4b464a4]368  m->direction=OWL_MESSAGE_DIRECTION_OUT;
369}
370
[0ff8fb57]371void owl_message_set_direction_none(owl_message *m)
372{
[4b464a4]373  m->direction=OWL_MESSAGE_DIRECTION_NONE;
374}
375
[0ff8fb57]376int owl_message_is_direction_in(owl_message *m)
377{
[4b464a4]378  if (m->direction==OWL_MESSAGE_DIRECTION_IN) return(1);
379  return(0);
380}
381
[0ff8fb57]382int owl_message_is_direction_out(owl_message *m)
383{
[4b464a4]384  if (m->direction==OWL_MESSAGE_DIRECTION_OUT) return(1);
385  return(0);
386}
387
[0ff8fb57]388int owl_message_is_direction_none(owl_message *m)
389{
[4b464a4]390  if (m->direction==OWL_MESSAGE_DIRECTION_NONE) return(1);
391  return(0);
392}
393
[0ff8fb57]394int owl_message_get_numlines(owl_message *m)
395{
[4b464a4]396  if (m == NULL) return(0);
[f14a7ee]397  owl_message_format(m);
[4b464a4]398  return(owl_fmtext_num_lines(&(m->fmtext)));
399}
400
[0ff8fb57]401void owl_message_mark_delete(owl_message *m)
402{
[4b464a4]403  if (m == NULL) return;
404  m->delete=1;
405}
406
[0ff8fb57]407void owl_message_unmark_delete(owl_message *m)
408{
[4b464a4]409  if (m == NULL) return;
410  m->delete=0;
411}
412
[0ff8fb57]413char *owl_message_get_zwriteline(owl_message *m)
414{
[4b464a4]415  return(m->zwriteline);
416}
417
[0ff8fb57]418void owl_message_set_zwriteline(owl_message *m, char *line)
419{
[4b464a4]420  m->zwriteline=strdup(line);
421}
422
[0ff8fb57]423int owl_message_is_delete(owl_message *m)
424{
[4b464a4]425  if (m == NULL) return(0);
426  if (m->delete==1) return(1);
427  return(0);
428}
429
[be0a79f]430#ifdef HAVE_LIBZEPHYR
[0ff8fb57]431ZNotice_t *owl_message_get_notice(owl_message *m)
432{
[4b464a4]433  return(&(m->notice));
434}
[09489b89]435#else
436void *owl_message_get_notice(owl_message *m)
437{
438  return(NULL);
439}
[be0a79f]440#endif
[4b464a4]441
[0ff8fb57]442char *owl_message_get_hostname(owl_message *m)
443{
[4b464a4]444  return(m->hostname);
445}
446
447
[0ff8fb57]448void owl_message_curs_waddstr(owl_message *m, WINDOW *win, int aline, int bline, int acol, int bcol, int color)
449{
[4b464a4]450  owl_fmtext a, b;
451
[bd3f232]452  /* this will ensure that our cached copy is up to date */
[f14a7ee]453  owl_message_format(m);
[bd3f232]454
[af2ca19]455  owl_fmtext_init_null(&a);
456  owl_fmtext_init_null(&b);
457 
[4b464a4]458  owl_fmtext_truncate_lines(&(m->fmtext), aline, bline-aline+1, &a);
459  owl_fmtext_truncate_cols(&a, acol, bcol, &b);
460  if (color!=OWL_COLOR_DEFAULT) {
461    owl_fmtext_colorize(&b, color);
462  }
463
464  if (owl_global_is_search_active(&g)) {
465    owl_fmtext_search_and_highlight(&b, owl_global_get_search_string(&g));
466  }
467     
468  owl_fmtext_curs_waddstr(&b, win);
469
470  owl_fmtext_free(&a);
471  owl_fmtext_free(&b);
472}
473
[0ff8fb57]474int owl_message_is_personal(owl_message *m)
475{
476  if (owl_message_is_type_zephyr(m)) {
477    if (strcasecmp(owl_message_get_class(m), "message")) return(0);
478    if (strcasecmp(owl_message_get_instance(m), "personal")) return(0);
[09489b89]479    if (!strcasecmp(owl_message_get_recipient(m), owl_zephyr_get_sender()) ||
480        !strcasecmp(owl_message_get_sender(m), owl_zephyr_get_sender())) {
[0ff8fb57]481      return(1);
482    }
483  }
484  return(0);
485}
486
487int owl_message_is_from_me(owl_message *m)
488{
489  if (owl_message_is_type_zephyr(m)) {
[09489b89]490    if (!strcasecmp(owl_message_get_sender(m), owl_zephyr_get_sender())) {
[0ff8fb57]491      return(1);
492    } else {
493      return(0);
494    }
495  } else if (owl_message_is_type_aim(m)) {
496    if (!strcasecmp(owl_message_get_sender(m), owl_global_get_aim_screenname(&g))) {
497      return(1);
498    } else {
499      return(0);
500    }
501  } else if (owl_message_is_type_admin(m)) {
502    return(0);
503  }
[4b464a4]504  return(0);
505}
506
[0ff8fb57]507int owl_message_is_mail(owl_message *m)
508{
509  if (owl_message_is_type_zephyr(m)) {
[5789230]510    if (!strcasecmp(owl_message_get_class(m), "mail") && owl_message_is_private(m)) {
[0ff8fb57]511      return(1);
512    } else {
513      return(0);
514    }
[4b464a4]515  }
516  return(0);
517}
518
[0ff8fb57]519int owl_message_is_ping(owl_message *m)
520{
521  if (owl_message_is_type_zephyr(m)) {
522    if (!strcasecmp(owl_message_get_opcode(m), "ping")) {
523      return(1);
524    } else {
525      return(0);
526    }
527  }
[4b464a4]528  return(0);
529}
530
[0ff8fb57]531int owl_message_is_burningears(owl_message *m)
532{
[4b464a4]533  /* we should add a global to cache the short zsender */
534  char sender[LINE], *ptr;
535
536  /* if the message is from us or to us, it doesn't count */
[5789230]537  if (owl_message_is_from_me(m) || owl_message_is_private(m)) return(0);
[0ff8fb57]538
539  if (owl_message_is_type_zephyr(m)) {
[09489b89]540    strcpy(sender, owl_zephyr_get_sender());
[0ff8fb57]541    ptr=strchr(sender, '@');
542    if (ptr) *ptr='\0';
543  } else if (owl_message_is_type_aim(m)) {
544    strcpy(sender, owl_global_get_aim_screenname(&g));
545  } else {
546    return(0);
547  }
[4b464a4]548
549  if (stristr(owl_message_get_body(m), sender)) {
550    return(1);
551  }
552  return(0);
553}
554
555/* caller must free return value. */
[0ff8fb57]556char *owl_message_get_cc(owl_message *m)
557{
[4b464a4]558  char *cur, *out, *end;
559
560  cur = owl_message_get_body(m);
561  while (*cur && *cur==' ') cur++;
[985f85b]562  if (strncasecmp(cur, "cc:", 3)) return(NULL);
[4b464a4]563  cur+=3;
564  while (*cur && *cur==' ') cur++;
565  out = owl_strdup(cur);
566  end = strchr(out, '\n');
567  if (end) end[0] = '\0';
568  return(out);
569}
570
[0ff8fb57]571int owl_message_get_id(owl_message *m)
572{
[4b464a4]573  return(m->id);
574}
[bd3f232]575
[f1e629d]576char *owl_message_get_type(owl_message *m) {
577  switch (m->type) {
578  case OWL_MESSAGE_TYPE_ADMIN:
579    return("admin");
580  case OWL_MESSAGE_TYPE_ZEPHYR:
581    return("zephyr");
582  case OWL_MESSAGE_TYPE_GENERIC:
583    return("generic");
584  case OWL_MESSAGE_TYPE_AIM:
585    return("aim");
586  case OWL_MESSAGE_TYPE_JABBER:
587    return("jabber");
588  case OWL_MESSAGE_TYPE_ICQ:
589    return("icq");
590  case OWL_MESSAGE_TYPE_YAHOO:
591    return("yahoo");
592  case OWL_MESSAGE_TYPE_MSN:
593    return("msn");
594  default:
595    return("unknown");
596  }
597}
598
599char *owl_message_get_direction(owl_message *m) {
600  switch (m->direction) {
601  case OWL_MESSAGE_DIRECTION_IN:
602    return("in");
603  case OWL_MESSAGE_DIRECTION_OUT:
604    return("out");
605  case OWL_MESSAGE_DIRECTION_NONE:
606    return("none");
607  default:
608    return("unknown");
609  }
610}
611
612char *owl_message_get_login(owl_message *m) {
613  if (owl_message_is_login(m)) {
614    return "login";
615  } else if (owl_message_is_logout(m)) {
616    return "logout";
617  } else {
618    return "none";
619  }
620}
621
622char *owl_message_get_header(owl_message *m) {
623  return owl_message_get_attribute_value(m, "adminheader");
624}
625
[bd3f232]626/* return 1 if the message contains "string", 0 otherwise.  This is
627 * case insensitive because the functions it uses are
628 */
[0ff8fb57]629int owl_message_search(owl_message *m, char *string)
630{
[4b464a4]631
[f14a7ee]632  owl_message_format(m); /* is this necessary? */
[bd3f232]633 
[4b464a4]634  return (owl_fmtext_search(&(m->fmtext), string));
635}
636
637
[d559df9]638/* if loginout == -1 it's a logout message
639 *                 0 it's not a login/logout message
640 *                 1 it's a login message
641 */
642void owl_message_create_aim(owl_message *m, char *sender, char *recipient, char *text, int direction, int loginout)
[0ff8fb57]643{
[d09e5a1]644  owl_message_init(m);
645  owl_message_set_body(m, text);
646  owl_message_set_sender(m, sender);
[440ce01]647  owl_message_set_recipient(m, recipient);
[d09e5a1]648  owl_message_set_type_aim(m);
[3abf28b]649
[d559df9]650  if (direction==OWL_MESSAGE_DIRECTION_IN) {
651    owl_message_set_direction_in(m);
652  } else if (direction==OWL_MESSAGE_DIRECTION_OUT) {
653    owl_message_set_direction_out(m);
[3abf28b]654  }
655
[d559df9]656  /* for now all messages that aren't loginout are private */
657  if (!loginout) {
658    owl_message_set_isprivate(m);
659  }
[3abf28b]660
[d559df9]661  if (loginout==-1) {
662    owl_message_set_islogout(m);
663  } else if (loginout==1) {
664    owl_message_set_islogin(m);
665  }
[aa5f725]666}
667
[0ff8fb57]668void owl_message_create_admin(owl_message *m, char *header, char *text)
669{
[d0d65df]670  owl_message_init(m);
[4b464a4]671  owl_message_set_type_admin(m);
[d0d65df]672  owl_message_set_body(m, text);
[bd3f232]673  owl_message_set_attribute(m, "adminheader", header); /* just a hack for now */
[7d4fbcd]674}
675
[09489b89]676#ifdef HAVE_LIBZEPHYR
[0ff8fb57]677void owl_message_create_from_znotice(owl_message *m, ZNotice_t *n)
678{
[7d4fbcd]679  struct hostent *hent;
[c269e22]680  int k;
[d0d65df]681  char *ptr, *tmp, *tmp2;
[7d4fbcd]682
[d0d65df]683  owl_message_init(m);
684 
[4b464a4]685  owl_message_set_type_zephyr(m);
686  owl_message_set_direction_in(m);
[7d4fbcd]687 
688  /* first save the full notice */
689  memcpy(&(m->notice), n, sizeof(ZNotice_t));
690
691  /* a little gross, we'll reaplace \r's with ' ' for now */
692  owl_zephyr_hackaway_cr(&(m->notice));
693 
694  m->delete=0;
695
696  /* set other info */
[d0d65df]697  owl_message_set_sender(m, n->z_sender);
698  owl_message_set_class(m, n->z_class);
699  owl_message_set_instance(m, n->z_class_inst);
700  owl_message_set_recipient(m, n->z_recipient);
[7d4fbcd]701  if (n->z_opcode) {
[d0d65df]702    owl_message_set_opcode(m, n->z_opcode);
[7d4fbcd]703  } else {
[d0d65df]704    owl_message_set_opcode(m, "");
[7d4fbcd]705  }
[d0d65df]706  owl_message_set_zsig(m, n->z_message);
[7d4fbcd]707
708  if ((ptr=strchr(n->z_recipient, '@'))!=NULL) {
[d0d65df]709    owl_message_set_realm(m, ptr+1);
[7d4fbcd]710  } else {
[09489b89]711    owl_message_set_realm(m, owl_zephyr_get_realm());
[7d4fbcd]712  }
713
[5789230]714  /* Set the "isloginout" attribute if it's a login message */
[1d3e925]715  if (!strcasecmp(n->z_class, "login") || !strcasecmp(n->z_class, OWL_WEBZEPHYR_CLASS)) {
[d559df9]716    if (!strcasecmp(n->z_opcode, "user_login")) {
717      owl_message_set_islogin(m);
718    } else if (!strcasecmp(n->z_opcode, "user_logout")) {
719      owl_message_set_islogout(m);
720    }
[5789230]721  }
722
723  /* is the "isprivate" attribute if it's a private zephyr */
[09489b89]724  if (!strcasecmp(n->z_recipient, owl_zephyr_get_sender())) {
[5789230]725    owl_message_set_isprivate(m);
726  }
727
[7d4fbcd]728  m->zwriteline=strdup("");
729
[b45293f]730  /* set the body */
[7d4fbcd]731  ptr=owl_zephyr_get_message(n, &k);
[7e3e00a]732  tmp=owl_malloc(k+10);
733  memcpy(tmp, ptr, k);
734  tmp[k]='\0';
735  if (owl_global_is_newlinestrip(&g)) {
[d0d65df]736    tmp2=owl_util_stripnewlines(tmp);
737    owl_message_set_body(m, tmp2);
738    owl_free(tmp2);
[7e3e00a]739  } else {
[d0d65df]740    owl_message_set_body(m, tmp);
[7e3e00a]741  }
[ecd5dc5]742  owl_free(tmp);
[7d4fbcd]743
[c86a35c]744#ifdef OWL_ENABLE_ZCRYPT
[d309eb3]745  /* if zcrypt is enabled try to decrypt the message */
746  if (owl_global_is_zcrypt(&g) && !strcasecmp(n->z_opcode, "crypt")) {
747    char *out;
[c269e22]748    int ret;
[d309eb3]749
[d0d65df]750    out=owl_malloc(strlen(owl_message_get_body(m))*16+20);
[9ceee9d]751    ret=owl_zcrypt_decrypt(out, owl_message_get_body(m), owl_message_get_class(m), owl_message_get_instance(m));
[a15a84f]752    if (ret==0) {
[d0d65df]753      owl_message_set_body(m, out);
[a15a84f]754    } else {
755      owl_free(out);
756    }
[d309eb3]757  }
[c269e22]758#endif 
[d309eb3]759
[7d4fbcd]760  /* save the hostname */
[3a2daac]761  owl_function_debugmsg("About to do gethostbyaddr");
[7d4fbcd]762  hent=gethostbyaddr((char *) &(n->z_uid.zuid_addr), sizeof(n->z_uid.zuid_addr), AF_INET);
763  if (hent && hent->h_name) {
764    strcpy(m->hostname, hent->h_name);
765  } else {
766    strcpy(m->hostname, inet_ntoa(n->z_sender_addr));
767  }
768
769  /* save the time */
770  m->time=owl_strdup(ctime((time_t *) &n->z_time.tv_sec));
771  m->time[strlen(m->time)-1]='\0';
772}
[09489b89]773#else
774void owl_message_create_from_znotice(owl_message *m, void *n)
775{
776}
777#endif
[7d4fbcd]778
[0ff8fb57]779void owl_message_create_from_zwriteline(owl_message *m, char *line, char *body, char *zsig)
780{
[b45293f]781  owl_zwrite z;
782  int ret;
783 
[d0d65df]784  owl_message_init(m);
[b45293f]785
786  /* create a zwrite for the purpose of filling in other message fields */
787  owl_zwrite_create_from_line(&z, line);
788
789  /* set things */
790  owl_message_set_direction_out(m);
791  owl_message_set_type_zephyr(m);
[09489b89]792  owl_message_set_sender(m, owl_zephyr_get_sender());
[8fec514]793  owl_message_set_class(m, owl_zwrite_get_class(&z));
794  owl_message_set_instance(m, owl_zwrite_get_instance(&z));
[9ceee9d]795  if (owl_zwrite_get_numrecips(&z)>0) {
796    owl_message_set_recipient(m,
797                              long_zuser(owl_zwrite_get_recip_n(&z, 0))); /* only gets the first user, must fix */
798  }
[8fec514]799  owl_message_set_opcode(m, owl_zwrite_get_opcode(&z));
[d0d65df]800  owl_message_set_realm(m, owl_zwrite_get_realm(&z)); /* also a hack, but not here */
[b45293f]801  m->zwriteline=owl_strdup(line);
[d0d65df]802  owl_message_set_body(m, body);
[8fec514]803  owl_message_set_zsig(m, zsig);
[b45293f]804 
805  /* save the hostname */
806  ret=gethostname(m->hostname, MAXHOSTNAMELEN);
807  if (ret) {
808    strcpy(m->hostname, "localhost");
809  }
810
811  owl_zwrite_free(&z);
812}
[7d4fbcd]813
814
[0ff8fb57]815void owl_message_pretty_zsig(owl_message *m, char *buff)
816{
[b45293f]817  /* stick a one line version of the zsig in buff */
[7d4fbcd]818  char *ptr;
819
[d0d65df]820  strcpy(buff, owl_message_get_zsig(m));
[b45293f]821  ptr=strchr(buff, '\n');
822  if (ptr) ptr[0]='\0';
[7d4fbcd]823}
824
[0ff8fb57]825void owl_message_free(owl_message *m)
826{
[d0d65df]827  int i, j;
828  owl_pair *p;
[09489b89]829#ifdef HAVE_LIBZEPHYR   
[4b464a4]830  if (owl_message_is_type_zephyr(m) && owl_message_is_direction_in(m)) {
[7d4fbcd]831    ZFreeNotice(&(m->notice));
832  }
[09489b89]833#endif
[7d4fbcd]834  if (m->time) owl_free(m->time);
835  if (m->zwriteline) owl_free(m->zwriteline);
[d0d65df]836
837  /* free all the attributes */
838  j=owl_list_get_size(&(m->attributes));
839  for (i=0; i<j; i++) {
840    p=owl_list_get_element(&(m->attributes), i);
841    owl_free(owl_pair_get_key(p));
842    owl_free(owl_pair_get_value(p));
843    owl_free(p);
844  }
845
846  owl_list_free_simple(&(m->attributes));
[7d4fbcd]847 
848  owl_fmtext_free(&(m->fmtext));
849}
[f1e629d]850
851
Note: See TracBrowser for help on using the repository browser.