Changeset d0d65df for message.c


Ignore:
Timestamp:
Feb 17, 2003, 3:47:41 PM (18 years ago)
Author:
James M. Kretchmar <kretch@mit.edu>
Branches:
master, barnowl_perlaim, debian, owl, release-1.4, release-1.5, release-1.6, release-1.7, release-1.8, release-1.9
Children:
985f85b
Parents:
a2641cd9
Message:
The message object now uses a list of attributes internally, in
  prep. for supporting new messaging protocols
owl_function_info now uses fmtext instead of one staticly sized
  buffer
File:
1 edited

Legend:

Unmodified
Added
Removed
  • message.c

    ra2641cd9 rd0d65df  
    1414static const char fileIdent[] = "$Id$";
    1515
    16 void owl_message_init_raw(owl_message *m) {
     16void owl_message_init(owl_message *m) {
    1717  time_t t;
    1818
     
    2121  owl_message_set_direction_none(m);
    2222  m->delete=0;
    23   m->sender=owl_strdup("");
    24   m->class=owl_strdup("");
    25   m->inst=owl_strdup("");
    26   m->recip=owl_strdup("");
    27   m->opcode=owl_strdup("");
    28   m->realm=owl_strdup("");
    29   m->zsig=owl_strdup("");
    3023  strcpy(m->hostname, "");
    3124  m->zwriteline=strdup("");
    3225
     26  owl_list_create(&(m->attributes));
     27 
    3328  /* save the time */
    3429  t=time(NULL);
     
    3732}
    3833
     34void owl_message_set_attribute(owl_message *m, char *attrname, char *attrvalue) {
     35  /* add the named attribute to the message.  If an attribute with the
     36     name already exists, replace the old value with the new value */
     37
     38  int i, j;
     39  owl_pair *p;
     40
     41  /* look for an existing pair with this key, and nuke the entry if
     42     found */
     43  j=owl_list_get_size(&(m->attributes));
     44  for (i=0; i<j; i++) {
     45    p=owl_list_get_element(&(m->attributes), i);
     46    if (!strcmp(owl_pair_get_key(p), attrname)) {
     47      owl_free(owl_pair_get_key(p));
     48      owl_free(owl_pair_get_value(p));
     49      owl_free(p);
     50      owl_list_remove_element(&(m->attributes), i);
     51      break;
     52    }
     53  }
     54
     55  p=owl_malloc(sizeof(owl_pair));
     56  owl_pair_create(p, owl_strdup(attrname), owl_strdup(attrvalue));
     57  owl_list_append_element(&(m->attributes), p);
     58}
     59
     60char *owl_message_get_attribute_value(owl_message *m, char *attrname) {
     61  /* return the value associated with the named attribute, or NULL if
     62     the attribute does not exist */
     63  int i, j;
     64  owl_pair *p;
     65
     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      return(owl_pair_get_value(p));
     71    }
     72  }
     73  owl_function_debugmsg("No attribute %s found", attrname);
     74  return(NULL);
     75}
     76
    3977
    4078owl_fmtext *owl_message_get_fmtext(owl_message *m) {
     
    4381
    4482void owl_message_set_class(owl_message *m, char *class) {
    45   if (m->class) owl_free(m->class);
    46   m->class=owl_strdup(class);
     83  owl_message_set_attribute(m, "class", class);
    4784}
    4885
    4986char *owl_message_get_class(owl_message *m) {
    50   return(m->class);
     87  char *class;
     88
     89  class=owl_message_get_attribute_value(m, "class");
     90  if (!class) return("");
     91  return(class);
    5192}
    5293
    5394void owl_message_set_instance(owl_message *m, char *inst) {
    54   if (m->inst) owl_free(m->inst);
    55   m->inst=owl_strdup(inst);
     95  owl_message_set_attribute(m, "instance", inst);
    5696}
    5797
    5898char *owl_message_get_instance(owl_message *m) {
    59   return(m->inst);
     99  char *instance;
     100
     101  instance=owl_message_get_attribute_value(m, "instance");
     102  if (!instance) return("");
     103  return(instance);
    60104}
    61105
    62106void owl_message_set_sender(owl_message *m, char *sender) {
    63   if (m->sender) owl_free(m->sender);
    64   m->sender=owl_strdup(sender);
     107  owl_message_set_attribute(m, "sender", sender);
    65108}
    66109
    67110char *owl_message_get_sender(owl_message *m) {
    68   return(m->sender);
     111  char *sender;
     112
     113  sender=owl_message_get_attribute_value(m, "sender");
     114  if (!sender) return("");
     115  return(sender);
    69116}
    70117
    71118void owl_message_set_zsig(owl_message *m, char *zsig) {
    72   if (m->zsig) owl_free(m->zsig);
    73   m->zsig=owl_strdup(zsig);
     119  owl_message_set_attribute(m, "zsig", zsig);
    74120}
    75121
    76122char *owl_message_get_zsig(owl_message *m) {
    77   return(m->zsig);
     123  char *zsig;
     124
     125  zsig=owl_message_get_attribute_value(m, "zsig");
     126  if (!zsig) return("");
     127  return(zsig);
    78128}
    79129
    80130void owl_message_set_recipient(owl_message *m, char *recip) {
    81   if (m->recip) owl_free(m->recip);
    82   m->recip=owl_strdup(recip);
     131  owl_message_set_attribute(m, "recipient", recip);
    83132}
    84133
    85134char *owl_message_get_recipient(owl_message *m) {
    86135  /* this is stupid for outgoing messages, we need to fix it. */
     136
     137  char *recip;
    87138     
    88139  if (m->type==OWL_MESSAGE_TYPE_ZEPHYR) {
    89     return(m->recip);
     140    recip=owl_message_get_attribute_value(m, "recipient");
    90141  } else if (owl_message_is_direction_out(m)) {
    91     return(m->zwriteline);
     142    recip=m->zwriteline;
    92143  } else {
    93     return(m->recip);
    94   }
     144    recip=owl_message_get_attribute_value(m, "recipient");
     145  }
     146  if (!recip) return("");
     147  return(recip);
    95148}
    96149
    97150void owl_message_set_realm(owl_message *m, char *realm) {
    98   if (m->realm) owl_free(m->realm);
    99   m->realm=owl_strdup(realm);
     151  owl_message_set_attribute(m, "realm", realm);
    100152}
    101153
    102154char *owl_message_get_realm(owl_message *m) {
    103   return(m->realm);
    104 }
     155  char *realm;
     156 
     157  realm=owl_message_get_attribute_value(m, "realm");
     158  if (!realm) return("");
     159  return(realm);
     160}
     161
     162void owl_message_set_body(owl_message *m, char *body) {
     163  owl_message_set_attribute(m, "body", body);
     164}
     165
     166char *owl_message_get_body(owl_message *m) {
     167  char *body;
     168
     169  body=owl_message_get_attribute_value(m, "body");
     170  if (!body) return("");
     171  return(body);
     172}
     173
    105174
    106175void owl_message_set_opcode(owl_message *m, char *opcode) {
    107   if (m->opcode) free(m->opcode);
    108   m->opcode=owl_strdup(opcode);
     176  owl_message_set_attribute(m, "opcode", opcode);
    109177}
    110178
    111179char *owl_message_get_opcode(owl_message *m) {
    112   return(m->opcode);
     180  char *opcode;
     181
     182  opcode=owl_message_get_attribute_value(m, "opcode");
     183  if (!opcode) return("");
     184  return(opcode);
    113185}
    114186
     
    142214char *owl_message_get_text(owl_message *m) {
    143215  return(owl_fmtext_get_text(&(m->fmtext)));
    144 }
    145 
    146 char *owl_message_get_body(owl_message *m) {
    147   return(m->body);
    148216}
    149217
     
    315383  char *indent;
    316384
    317   owl_message_init_raw(m);
    318 
    319   m->body=owl_strdup(text);
     385  owl_message_init(m);
     386
     387  owl_message_set_body(m, text);
    320388
    321389  indent=owl_malloc(strlen(text)+owl_text_num_lines(text)*OWL_MSGTAB+10);
     
    336404  char *indent;
    337405
    338   owl_message_init_raw(m);
     406  owl_message_init(m);
    339407  owl_message_set_type_admin(m);
    340408
    341   m->body=owl_strdup(text);
     409  owl_message_set_body(m, text);
    342410
    343411  /* do something to make it clear the notice shouldn't be used for now */
     
    361429  struct hostent *hent;
    362430  int k, ret;
    363   char *ptr, *tmp;
    364 
    365   m->id=owl_global_get_nextmsgid(&g);
     431  char *ptr, *tmp, *tmp2;
     432
     433  owl_message_init(m);
     434 
    366435  owl_message_set_type_zephyr(m);
    367436  owl_message_set_direction_in(m);
     
    376445
    377446  /* set other info */
    378   m->sender=owl_strdup(n->z_sender);
    379   m->class=owl_strdup(n->z_class);
    380   m->inst=owl_strdup(n->z_class_inst);
    381   m->recip=owl_strdup(n->z_recipient);
     447  owl_message_set_sender(m, n->z_sender);
     448  owl_message_set_class(m, n->z_class);
     449  owl_message_set_instance(m, n->z_class_inst);
     450  owl_message_set_recipient(m, n->z_recipient);
    382451  if (n->z_opcode) {
    383     m->opcode=owl_strdup(n->z_opcode);
     452    owl_message_set_opcode(m, n->z_opcode);
    384453  } else {
    385     n->z_opcode=owl_strdup("");
    386   }
    387   m->zsig=owl_strdup(n->z_message);
     454    owl_message_set_opcode(m, "");
     455  }
     456  owl_message_set_zsig(m, n->z_message);
    388457
    389458  if ((ptr=strchr(n->z_recipient, '@'))!=NULL) {
    390     m->realm=owl_strdup(ptr+1);
     459    owl_message_set_realm(m, ptr+1);
    391460  } else {
    392     m->realm=owl_strdup(ZGetRealm());
     461    owl_message_set_realm(m, ZGetRealm());
    393462  }
    394463
     
    401470  tmp[k]='\0';
    402471  if (owl_global_is_newlinestrip(&g)) {
    403     m->body=owl_util_stripnewlines(tmp);
     472    tmp2=owl_util_stripnewlines(tmp);
     473    owl_message_set_body(m, tmp2);
    404474    owl_free(tmp);
     475    owl_free(tmp2);
    405476  } else {
    406     m->body=tmp;
     477    owl_message_set_body(m, tmp);
    407478  }
    408479
     
    411482    char *out;
    412483
    413     out=owl_malloc(strlen(m->body)*16+20);
    414     ret=zcrypt_decrypt(out, m->body, m->class, m->inst);
     484    out=owl_malloc(strlen(owl_message_get_body(m))*16+20);
     485    ret=zcrypt_decrypt(out, owl_message_get_body(m), owl_message_get_class(m), owl_message_get_instance(m));
    415486    if (ret==0) {
    416       owl_free(m->body);
    417       m->body=out;
     487      owl_message_set_body(m, out);
    418488    } else {
    419489      owl_free(out);
     
    449519  int ret;
    450520 
    451   owl_message_init_raw(m);
     521  owl_message_init(m);
    452522
    453523  /* create a zwrite for the purpose of filling in other message fields */
     
    460530  owl_message_set_class(m, owl_zwrite_get_class(&z));
    461531  owl_message_set_instance(m, owl_zwrite_get_instance(&z));
    462   m->recip=long_zuser(owl_zwrite_get_recip_n(&z, 0)); /* only gets the first user, must fix */
     532  owl_message_set_recipient(m,
     533                            long_zuser(owl_zwrite_get_recip_n(&z, 0))); /* only gets the first user, must fix */
    463534  owl_message_set_opcode(m, owl_zwrite_get_opcode(&z));
    464   m->realm=owl_strdup(owl_zwrite_get_realm(&z)); /* also a hack, but not here */
     535  owl_message_set_realm(m, owl_zwrite_get_realm(&z)); /* also a hack, but not here */
    465536  m->zwriteline=owl_strdup(line);
    466   m->body=owl_strdup(body);
     537  owl_message_set_body(m, body);
    467538  owl_message_set_zsig(m, zsig);
    468539 
     
    487558void _owl_message_make_text_from_config(owl_message *m) {
    488559  char *body, *indent;
    489  
     560
    490561  owl_fmtext_init_null(&(m->fmtext));
    491562
     
    571642 
    572643  /* get the body */
    573   body=owl_malloc(strlen(m->body)+30);
    574   strcpy(body, m->body);
     644  body=owl_malloc(strlen(owl_message_get_body(m))+30);
     645  strcpy(body, owl_message_get_body(m));
    575646
    576647  /* add a newline if we need to */
     
    584655
    585656  /* edit the from addr for printing */
    586   strcpy(frombuff, m->sender);
     657  strcpy(frombuff, owl_message_get_sender(m));
    587658  ptr=strchr(frombuff, '@');
    588659  if (ptr && !strncmp(ptr+1, ZGetRealm(), strlen(ZGetRealm()))) {
     
    594665  owl_fmtext_append_normal(&(m->fmtext), OWL_TABSTR);
    595666
    596   if (!strcasecmp(n->z_opcode, "ping") && owl_message_is_private(m)) {
     667  if (!strcasecmp(owl_message_get_opcode(m), "ping") && owl_message_is_private(m)) {
    597668    owl_fmtext_append_bold(&(m->fmtext), "PING");
    598669    owl_fmtext_append_normal(&(m->fmtext), " from ");
     
    625696    owl_fmtext_append_normal(&(m->fmtext), "\n");
    626697  } else {
    627     owl_fmtext_append_normal(&(m->fmtext), m->class);
     698    owl_fmtext_append_normal(&(m->fmtext), owl_message_get_class(m));
    628699    owl_fmtext_append_normal(&(m->fmtext), " / ");
    629     owl_fmtext_append_normal(&(m->fmtext), m->inst);
     700    owl_fmtext_append_normal(&(m->fmtext), owl_message_get_instance(m));
    630701    owl_fmtext_append_normal(&(m->fmtext), " / ");
    631702    owl_fmtext_append_bold(&(m->fmtext), frombuff);
     
    637708    if (n->z_opcode[0]!='\0') {
    638709      owl_fmtext_append_normal(&(m->fmtext), " [");
    639       owl_fmtext_append_normal(&(m->fmtext), n->z_opcode);
     710      owl_fmtext_append_normal(&(m->fmtext), owl_message_get_opcode(m));
    640711      owl_fmtext_append_normal(&(m->fmtext), "] ");
    641712    }
     
    672743
    673744  /* get the body */
    674   body=owl_malloc(strlen(m->body)+30);
    675   strcpy(body, m->body);
     745  body=owl_malloc(strlen(owl_message_get_body(m)+30));
     746  strcpy(body, owl_message_get_body(m));
    676747
    677748  /* add a newline if we need to */
     
    685756
    686757  /* edit the from addr for printing */
    687   strcpy(frombuff, m->sender);
     758  strcpy(frombuff, owl_message_get_sender(m));
    688759  ptr=strchr(frombuff, '@');
    689760  if (ptr && !strncmp(ptr+1, ZGetRealm(), strlen(ZGetRealm()))) {
     
    695766  owl_fmtext_append_normal(&(m->fmtext), OWL_TABSTR);
    696767
    697   if (!strcasecmp(n->z_opcode, "ping")) {
     768  if (!strcasecmp(owl_message_get_opcode(m), "ping")) {
    698769    owl_fmtext_append_bold(&(m->fmtext), "PING");
    699770    owl_fmtext_append_normal(&(m->fmtext), " from ");
    700771    owl_fmtext_append_bold(&(m->fmtext), frombuff);
    701772    owl_fmtext_append_normal(&(m->fmtext), "\n");
    702   } else if (!strcasecmp(n->z_class, "login")) {
     773  } else if (!strcasecmp(owl_message_get_class(m), "login")) {
    703774    char *ptr, host[LINE], tty[LINE];
    704775    int len;
     
    711782    tty[len]='\0';
    712783   
    713     if (!strcasecmp(n->z_opcode, "user_login")) {
     784    if (!strcasecmp(owl_message_get_opcode(m), "user_login")) {
    714785      owl_fmtext_append_bold(&(m->fmtext), "LOGIN");
    715     } else if (!strcasecmp(n->z_opcode, "user_logout")) {
     786    } else if (!strcasecmp(owl_message_get_opcode(m), "user_logout")) {
    716787      owl_fmtext_append_bold(&(m->fmtext), "LOGOUT");
    717788    }
    718789    owl_fmtext_append_normal(&(m->fmtext), " for ");
    719     ptr=short_zuser(n->z_class_inst);
     790    ptr=short_zuser(owl_message_get_instance(m));
    720791    owl_fmtext_append_bold(&(m->fmtext), ptr);
    721792    owl_free(ptr);
     
    727798  } else {
    728799    owl_fmtext_append_normal(&(m->fmtext), "From: ");
    729     if (strcasecmp(m->class, "message")) {
     800    if (strcasecmp(owl_message_get_class(m), "message")) {
    730801      owl_fmtext_append_normal(&(m->fmtext), "Class ");
    731       owl_fmtext_append_normal(&(m->fmtext), m->class);
     802      owl_fmtext_append_normal(&(m->fmtext), owl_message_get_class(m));
    732803      owl_fmtext_append_normal(&(m->fmtext), " / Instance ");
    733       owl_fmtext_append_normal(&(m->fmtext), m->inst);
     804      owl_fmtext_append_normal(&(m->fmtext), owl_message_get_instance(m));
    734805      owl_fmtext_append_normal(&(m->fmtext), " / ");
    735806    }
     
    769840  char *ptr;
    770841
    771   strcpy(buff, m->zsig);
     842  strcpy(buff, owl_message_get_zsig(m));
    772843  ptr=strchr(buff, '\n');
    773844  if (ptr) ptr[0]='\0';
     
    775846
    776847void owl_message_free(owl_message *m) {
     848  int i, j;
     849  owl_pair *p;
     850   
    777851  if (owl_message_is_type_zephyr(m) && owl_message_is_direction_in(m)) {
    778852    ZFreeNotice(&(m->notice));
    779853  }
    780   if (m->sender) owl_free(m->sender);
    781   if (m->recip) owl_free(m->recip);
    782   if (m->class) owl_free(m->class);
    783   if (m->inst) owl_free(m->inst);
    784   if (m->opcode) owl_free(m->opcode);
    785854  if (m->time) owl_free(m->time);
    786   if (m->realm) owl_free(m->realm);
    787   if (m->body) owl_free(m->body);
    788855  if (m->zwriteline) owl_free(m->zwriteline);
     856
     857  /* free all the attributes */
     858  j=owl_list_get_size(&(m->attributes));
     859  for (i=0; i<j; i++) {
     860    p=owl_list_get_element(&(m->attributes), i);
     861    owl_free(owl_pair_get_key(p));
     862    owl_free(owl_pair_get_value(p));
     863    owl_free(p);
     864  }
     865
     866  owl_list_free_simple(&(m->attributes));
    789867 
    790868  owl_fmtext_free(&(m->fmtext));
Note: See TracChangeset for help on using the changeset viewer.