source: message.c @ 8574801

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