source: message.c @ cba04e0

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