Changeset b45293f for message.c


Ignore:
Timestamp:
Oct 11, 2002, 11:56:23 PM (22 years ago)
Author:
James M. Kretchmar <kretch@mit.edu>
Branches:
master, barnowl_perlaim, debian, owl, release-1.10, release-1.4, release-1.5, release-1.6, release-1.7, release-1.8, release-1.9
Children:
68b41b0
Parents:
2adaf1d
Message:
Outgoing messages now go through the config for formatting
File:
1 edited

Legend:

Unmodified
Added
Removed
  • message.c

    r3a2daac rb45293f  
    11#include <zephyr/zephyr.h>
    22#include <stdlib.h>
     3#include <unistd.h>
    34#include <string.h>
    45#include <sys/socket.h>
     
    2627  m->opcode=owl_strdup("");
    2728  m->realm=owl_strdup("");
     29  m->zsig=owl_strdup("");
    2830  strcpy(m->hostname, "");
    2931  m->zwriteline=strdup("");
     
    6567char *owl_message_get_sender(owl_message *m) {
    6668  return(m->sender);
     69}
     70
     71void owl_message_set_zsig(owl_message *m, char *zsig) {
     72  if (m->zsig) owl_free(m->zsig);
     73  m->zsig=owl_strdup(zsig);
     74}
     75
     76char *owl_message_get_zsig(owl_message *m) {
     77  return(m->zsig);
    6778}
    6879
     
    347358}
    348359
    349 void owl_message_create_from_zephyr(owl_message *m, ZNotice_t *n) {
     360void owl_message_create_from_znotice(owl_message *m, ZNotice_t *n) {
    350361  struct hostent *hent;
    351362  int k;
     
    374385    n->z_opcode=owl_strdup("");
    375386  }
     387  m->zsig=owl_strdup(n->z_message);
    376388
    377389  if ((ptr=strchr(n->z_recipient, '@'))!=NULL) {
     
    383395  m->zwriteline=strdup("");
    384396
     397  /* set the body */
    385398  ptr=owl_zephyr_get_message(n, &k);
    386399  m->body=owl_malloc(k+10);
     
    412425}
    413426
     427void owl_message_create_from_zwriteline(owl_message *m, char *line, char *body, char *zsig) {
     428  owl_zwrite z;
     429  int ret;
     430 
     431  owl_message_init_raw(m);
     432
     433  /* create a zwrite for the purpose of filling in other message fields */
     434  owl_zwrite_create_from_line(&z, line);
     435
     436  /* set things */
     437  owl_message_set_direction_out(m);
     438  owl_message_set_type_zephyr(m);
     439  m->sender=owl_strdup(ZGetSender());
     440  m->class=owl_strdup(owl_zwrite_get_class(&z));
     441  m->inst=owl_strdup(owl_zwrite_get_instance(&z));
     442  m->recip=long_zuser(owl_zwrite_get_recip_n(&z, 0)); /* only gets the first user, must fix */
     443  owl_zwrite_get_recipstr(&z, m->recip);
     444  m->opcode=owl_strdup(owl_zwrite_get_opcode(&z));
     445  m->realm=owl_strdup(owl_zwrite_get_realm(&z)); /* also a hack, but not here */
     446  m->zwriteline=owl_strdup(line);
     447  m->body=owl_strdup(body);
     448  m->zsig=owl_strdup(zsig);
     449 
     450  /* save the hostname */
     451  ret=gethostname(m->hostname, MAXHOSTNAMELEN);
     452  if (ret) {
     453    strcpy(m->hostname, "localhost");
     454  }
     455
     456  /* create the formatted message */
     457  if (owl_global_is_config_format(&g)) {
     458    _owl_message_make_text_from_config(m);
     459  } else if (owl_global_is_userclue(&g, OWL_USERCLUE_CLASSES)) {
     460    _owl_message_make_text_from_zwriteline_standard(m);
     461  } else {
     462    _owl_message_make_text_from_zwriteline_simple(m);
     463  }
     464
     465  owl_zwrite_free(&z);
     466}
    414467
    415468void _owl_message_make_text_from_config(owl_message *m) {
     
    432485}
    433486
     487void _owl_message_make_text_from_zwriteline_standard(owl_message *m) {
     488  char *indent, *text, *zsigbuff;
     489
     490  text=owl_message_get_body(m);
     491
     492  indent=owl_malloc(strlen(text)+owl_text_num_lines(text)*OWL_MSGTAB+10);
     493  owl_text_indent(indent, text, OWL_MSGTAB);
     494  owl_fmtext_init_null(&(m->fmtext));
     495  owl_fmtext_append_normal(&(m->fmtext), OWL_TABSTR);
     496  owl_fmtext_append_normal(&(m->fmtext), "Zephyr sent to ");
     497  owl_fmtext_append_normal(&(m->fmtext), owl_message_get_recipient(m));
     498  owl_fmtext_append_normal(&(m->fmtext), "  (Zsig: ");
     499
     500  zsigbuff=owl_malloc(strlen(owl_message_get_zsig(m)));
     501  owl_message_pretty_zsig(m, zsigbuff);
     502  owl_fmtext_append_ztext(&(m->fmtext), zsigbuff);
     503  owl_free(zsigbuff);
     504 
     505  owl_fmtext_append_normal(&(m->fmtext), ")");
     506  owl_fmtext_append_normal(&(m->fmtext), "\n");
     507  owl_fmtext_append_ztext(&(m->fmtext), indent);
     508  if (text[strlen(text)-1]!='\n') {
     509    owl_fmtext_append_normal(&(m->fmtext), "\n");
     510  }
     511
     512  owl_free(indent);
     513}
     514
     515void _owl_message_make_text_from_zwriteline_simple(owl_message *m) {
     516  _owl_message_make_text_from_zwriteline_standard(m);
     517}
     518
    434519void _owl_message_make_text_from_notice_standard(owl_message *m) {
    435   char *body, *indent, *ptr;
    436   char frombuff[1024];
    437   char zsigbuff[LINE];
     520  char *body, *indent, *ptr, *zsigbuff, frombuff[LINE];
    438521  ZNotice_t *n;
    439522  int len;
     
    514597
    515598    /* stick on the zsig */
    516     _owl_message_get_zsig(m, zsigbuff, LINE);
    517     if (zsigbuff[0]!='\0') {
    518       owl_fmtext_append_normal(&(m->fmtext), "    (");
    519       owl_fmtext_append_ztext(&(m->fmtext), zsigbuff);
    520       owl_fmtext_append_normal(&(m->fmtext), ")");
    521     }
    522     owl_fmtext_append_normal(&(m->fmtext), "\n");
     599    zsigbuff=owl_malloc(strlen(owl_message_get_zsig(m)));
     600    owl_message_pretty_zsig(m, zsigbuff);
     601    owl_fmtext_append_normal(&(m->fmtext), "    (");
     602    owl_fmtext_append_ztext(&(m->fmtext), zsigbuff);
     603    owl_fmtext_append_normal(&(m->fmtext), ")");
     604    owl_fmtext_append_normal(&(m->fmtext), "\n");
     605    owl_free(zsigbuff);
     606
     607    /* then the indented message */
    523608    owl_fmtext_append_ztext(&(m->fmtext), indent);
    524609
     
    536621
    537622void _owl_message_make_text_from_notice_simple(owl_message *m) {
    538   char *body, *indent, *ptr;
    539   char frombuff[1024];
    540   char zsigbuff[LINE];
     623  char *body, *indent, *ptr, *zsigbuff, frombuff[LINE];
    541624  ZNotice_t *n;
    542625  int len;
     
    616699
    617700    /* stick on the zsig */
    618     _owl_message_get_zsig(m, zsigbuff, LINE);
    619     if (zsigbuff[0]!='\0') {
    620       owl_fmtext_append_normal(&(m->fmtext), "    (");
    621       owl_fmtext_append_ztext(&(m->fmtext), zsigbuff);
    622       owl_fmtext_append_normal(&(m->fmtext), ")");
    623     }
    624     owl_fmtext_append_normal(&(m->fmtext), "\n");
     701    zsigbuff=owl_malloc(strlen(owl_message_get_zsig(m)));
     702    owl_message_pretty_zsig(m, zsigbuff);
     703    owl_fmtext_append_normal(&(m->fmtext), "    (");
     704    owl_fmtext_append_ztext(&(m->fmtext), zsigbuff);
     705    owl_fmtext_append_normal(&(m->fmtext), ")");
     706    owl_fmtext_append_normal(&(m->fmtext), "\n");
     707    owl_free(zsigbuff);
     708
     709    /* then the indented message */
    625710    owl_fmtext_append_ztext(&(m->fmtext), indent);
    626711
     
    637722}
    638723
    639 void _owl_message_get_zsig(owl_message *m, char *buff, int size) {
     724void owl_message_pretty_zsig(owl_message *m, char *buff) {
     725  /* stick a one line version of the zsig in buff */
    640726  char *ptr;
    641   ZNotice_t n;
    642   int len;
    643   /* just a hackish thing for now.  We'll only present the first line
    644      or the first 'size'. characters.  If the message is not
    645      appropriate for having a zsig we'll return an empty string */
    646   n=m->notice;
    647 
    648 
    649   /* bail if it shouldn't have a zsig */
    650   buff[0]='\0';
    651   if (!strcasecmp(n.z_opcode, "ping")) {
    652     return;
    653   }
    654 
    655   /* find the right length to copy */
    656   len=strlen(n.z_message);
    657   if (size < len) {
    658     len=size;
    659   }
    660   if ((ptr=strchr(n.z_message, '\n'))!=NULL) {
    661     if ((ptr-n.z_message) < len) {
    662       len=ptr-n.z_message;
    663     }
    664   }
    665 
    666   /* copy */
    667   strncpy(buff, n.z_message, len);
    668   buff[len]='\0';
    669 }
    670 
     727
     728  strcpy(buff, m->zsig);
     729  ptr=strchr(buff, '\n');
     730  if (ptr) ptr[0]='\0';
     731}
    671732
    672733void owl_message_free(owl_message *m) {
Note: See TracChangeset for help on using the changeset viewer.