Changeset ce7db4d for zwrite.c


Ignore:
Timestamp:
Jun 24, 2003, 11:12:09 PM (21 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:
658dc2f
Parents:
723c427
Message:
zwrite -m now logs and displays an outgoing message
zwrite -s now works
File:
1 edited

Legend:

Unmodified
Added
Removed
  • zwrite.c

    r8262340 rce7db4d  
    77static const char fileIdent[] = "$Id$";
    88
    9 int owl_zwrite_create_and_send_from_line(char *cmd, char *msg) {
    10   owl_zwrite z;
    11   int rv;
    12   rv = owl_zwrite_create_from_line(&z, cmd);
    13   if (rv) return(rv);
    14   owl_zwrite_send_message(&z, msg);
    15   owl_zwrite_free(&z);
    16   return(0);
    17 }
    18 
    19 int owl_zwrite_create_from_line(owl_zwrite *z, char *line) {
     9int owl_zwrite_create_from_line(owl_zwrite *z, char *line)
     10{
    2011  int argc, badargs, myargc;
    2112  char **argv, **myargv;
     
    2516  badargs=0;
    2617 
    27   /* set the defaults */
    28   strcpy(z->realm, "");
    29   strcpy(z->class, "message");
    30   strcpy(z->inst, "personal");
    31   strcpy(z->opcode, "");
    32   z->zsig=owl_strdup("");
     18  /* start with null entires */
     19  z->realm=NULL;
     20  z->class=NULL;
     21  z->inst=NULL;
     22  z->opcode=NULL;
     23  z->zsig=NULL;
     24  z->message=NULL;
    3325  z->cc=0;
    3426  z->noping=0;
     
    5244        break;
    5345      }
    54       strcpy(z->class, myargv[1]);
     46      z->class=owl_strdup(myargv[1]);
    5547      myargv+=2;
    5648      myargc-=2;
     
    6052        break;
    6153      }
    62       strcpy(z->inst, myargv[1]);
     54      z->inst=owl_strdup(myargv[1]);
    6355      myargv+=2;
    6456      myargc-=2;
     
    6860        break;
    6961      }
    70       strcpy(z->realm, myargv[1]);
     62      z->realm=owl_strdup(myargv[1]);
     63      myargv+=2;
     64      myargc-=2;
     65    } else if (!strcmp(myargv[0], "-s")) {
     66      if (myargc<2) {
     67        badargs=1;
     68        break;
     69      }
     70      z->zsig=owl_strdup(myargv[1]);
    7171      myargv+=2;
    7272      myargc-=2;
     
    7676        break;
    7777      }
    78       strcpy(z->opcode, myargv[1]);
    79       myargv+=2;
    80       myargc-=2;
     78      z->opcode=owl_strdup(myargv[1]);
     79      myargv+=2;
     80      myargc-=2;
     81    } else if (!strcmp(myargv[0], "-m")) {
     82      if (myargc<2) {
     83        badargs=1;
     84        break;
     85      }
     86      /* we must already have users */
     87      if (owl_list_get_size(&(z->recips))<1) {
     88        badargs=1;
     89        break;
     90      }
     91
     92      /* Once we have -m, gobble up everything else on the line */
     93      myargv++;
     94      myargc--;
     95      z->message=owl_strdup("");
     96      while (myargc) {
     97        z->message=realloc(z->message, strlen(z->message)+strlen(myargv[0])+5);
     98        strcat(z->message, " ");
     99        strcat(z->message, myargv[0]);
     100        myargc--;
     101        myargv++;
     102      }
     103      break;
    81104    } else if (!strcmp(myargv[0], "-C")) {
    82105      z->cc=1;
     
    101124  }
    102125
     126  /* now deal with defaults */
     127  if (z->class==NULL) z->class=owl_strdup("message");
     128  if (z->inst==NULL) z->inst=owl_strdup("personal");
     129  if (z->realm==NULL) z->realm=owl_strdup("");
     130  if (z->opcode==NULL) z->opcode=owl_strdup("");
     131  /* z->message is allowed to stay NULL */
     132 
    103133  if (!strcasecmp(z->class, "message") &&
    104134      !strcasecmp(z->inst, "personal") &&
    105135      owl_list_get_size(&(z->recips))==0) {
    106     /* do the makemsg somewhere else */
    107136    owl_function_makemsg("You must specify a recipient");
    108137    return(-1);
    109138  }
    110139
    111   /* set a zsig */
    112   zsigproc = owl_global_get_zsigproc(&g);
    113   zsigowlvar = owl_global_get_zsig(&g);
    114   zsigzvar = ZGetVariable("zwrite-signature");
    115 
    116   if (zsigowlvar && *zsigowlvar) {
    117     owl_free(z->zsig);
    118     z->zsig=strdup(zsigowlvar);
    119   } else if (zsigproc && *zsigproc) {
    120     FILE *file;
    121     char buff[LINE], *openline;
    122 
    123     /* simple hack for now to nuke stderr */
    124     openline=owl_malloc(strlen(zsigproc)+40);
    125     strcpy(openline, zsigproc);
    126     strcat(openline, " 2> /dev/null");
    127     file=popen(openline, "r");
    128     owl_free(openline);
    129     if (!file) {
    130       if (zsigzvar && *zsigzvar) {
    131         owl_free(z->zsig);
    132         z->zsig=owl_strdup(zsigzvar);
    133       }
    134     } else {
    135       owl_free(z->zsig);
    136       z->zsig=owl_malloc(LINE);
    137       strcpy(z->zsig, "");
    138       while (fgets(buff, LINE, file)) { /* wrong sizing */
    139         strcat(z->zsig, buff);
    140       }
    141       pclose(file);
    142       if (z->zsig[strlen(z->zsig)-1]=='\n') {
    143         z->zsig[strlen(z->zsig)-1]='\0';
    144       }
    145     }
    146   } else if (zsigzvar) {
    147     owl_free(z->zsig);
    148     z->zsig=owl_strdup(zsigzvar);
    149   } else if (((pw=getpwuid(getuid()))!=NULL) && (pw->pw_gecos)) {
    150     owl_free(z->zsig);
    151     z->zsig=strdup(pw->pw_gecos);
    152     ptr=strchr(z->zsig, ',');
    153     if (ptr) {
    154       ptr[0]='\0';
    155     }
    156   }
    157 
    158   return(0);
    159 }
    160 
    161 void owl_zwrite_send_ping(owl_zwrite *z) {
     140  /* get a zsig, if not given */
     141  if (z->zsig==NULL) {
     142    zsigproc = owl_global_get_zsigproc(&g);
     143    zsigowlvar = owl_global_get_zsig(&g);
     144    zsigzvar = ZGetVariable("zwrite-signature");
     145
     146    if (zsigowlvar && *zsigowlvar) {
     147      z->zsig=owl_strdup(zsigowlvar);
     148    } else if (zsigproc && *zsigproc) {
     149      FILE *file;
     150      char buff[LINE], *openline;
     151     
     152      /* simple hack for now to nuke stderr */
     153      openline=owl_malloc(strlen(zsigproc)+40);
     154      strcpy(openline, zsigproc);
     155      strcat(openline, " 2> /dev/null");
     156      file=popen(openline, "r");
     157      owl_free(openline);
     158      if (!file) {
     159        if (zsigzvar && *zsigzvar) {
     160          z->zsig=owl_strdup(zsigzvar);
     161        }
     162      } else {
     163        z->zsig=owl_malloc(LINE*5);
     164        strcpy(z->zsig, "");
     165        while (fgets(buff, LINE, file)) { /* wrong sizing */
     166          strcat(z->zsig, buff);
     167        }
     168        pclose(file);
     169        if (z->zsig[strlen(z->zsig)-1]=='\n') {
     170          z->zsig[strlen(z->zsig)-1]='\0';
     171        }
     172      }
     173    } else if (zsigzvar) {
     174      z->zsig=owl_strdup(zsigzvar);
     175    } else if (((pw=getpwuid(getuid()))!=NULL) && (pw->pw_gecos)) {
     176      z->zsig=strdup(pw->pw_gecos);
     177      ptr=strchr(z->zsig, ',');
     178      if (ptr) {
     179        ptr[0]='\0';
     180      }
     181    }
     182  }
     183
     184  return(0);
     185}
     186
     187void owl_zwrite_send_ping(owl_zwrite *z)
     188{
    162189  int i, j;
    163190  char to[LINE];
     
    184211}
    185212
    186 void owl_zwrite_send_message(owl_zwrite *z, char *msg) {
     213void owl_zwrite_set_message(owl_zwrite *z, char *msg)
     214{
     215  if (z->message) owl_free(z->message);
     216  z->message=owl_strdup(msg);
     217}
     218
     219char *owl_zwrite_get_message(owl_zwrite *z)
     220{
     221  if (z->message) return(z->message);
     222  return("");
     223}
     224
     225int owl_zwrite_is_message_set(owl_zwrite *z)
     226{
     227  if (z->message) return(1);
     228  return(0);
     229}
     230
     231int owl_zwrite_send_message(owl_zwrite *z)
     232{
    187233  int i, j;
    188234  char to[LINE];
     235
     236  if (z->message==NULL) return(-1);
    189237
    190238  j=owl_list_get_size(&(z->recips));
     
    202250        }
    203251      }
    204       tmpmsg=owl_malloc(strlen(msg)+strlen(toline)+30);
    205       sprintf(tmpmsg, "%s\n%s", toline, msg);
     252      tmpmsg=owl_malloc(strlen(z->message)+strlen(toline)+30);
     253      sprintf(tmpmsg, "%s\n%s", toline, z->message);
    206254    }
    207255
     
    215263        send_zephyr(z->opcode, z->zsig, z->class, z->inst, to, tmpmsg);
    216264      } else {
    217         send_zephyr(z->opcode, z->zsig, z->class, z->inst, to, msg);
     265        send_zephyr(z->opcode, z->zsig, z->class, z->inst, to, z->message);
    218266      }
    219267    }
     
    223271  } else {
    224272    sprintf(to, "@%s", z->realm);
    225     send_zephyr(z->opcode, z->zsig, z->class, z->inst, to, msg);
    226   }
    227 }
    228 
    229 char *owl_zwrite_get_class(owl_zwrite *z) {
     273    send_zephyr(z->opcode, z->zsig, z->class, z->inst, to, z->message);
     274  }
     275  return(0);
     276}
     277
     278int owl_zwrite_create_and_send_from_line(char *cmd, char *msg)
     279{
     280  owl_zwrite z;
     281  int rv;
     282  rv=owl_zwrite_create_from_line(&z, cmd);
     283  if (rv) return(rv);
     284  if (!owl_zwrite_is_message_set(&z)) {
     285    owl_zwrite_set_message(&z, msg);
     286  }
     287  owl_zwrite_send_message(&z);
     288  owl_zwrite_free(&z);
     289  return(0);
     290}
     291
     292char *owl_zwrite_get_class(owl_zwrite *z)
     293{
    230294  return(z->class);
    231295}
    232296
    233 char *owl_zwrite_get_instance(owl_zwrite *z) {
     297char *owl_zwrite_get_instance(owl_zwrite *z)
     298{
    234299  return(z->inst);
    235300}
    236301
    237 char *owl_zwrite_get_opcode(owl_zwrite *z) {
     302char *owl_zwrite_get_opcode(owl_zwrite *z)
     303{
    238304  return(z->opcode);
    239305}
    240306
    241 char *owl_zwrite_get_realm(owl_zwrite *z) {
     307char *owl_zwrite_get_realm(owl_zwrite *z)
     308{
    242309  return(z->realm);
    243310}
    244311
    245 char *owl_zwrite_get_zsig(owl_zwrite *z) {
    246   return(z->zsig);
    247 }
    248 
    249 void owl_zwrite_get_recipstr(owl_zwrite *z, char *buff) {
     312char *owl_zwrite_get_zsig(owl_zwrite *z)
     313{
     314  if (z->zsig) return(z->zsig);
     315  return("");
     316}
     317
     318void owl_zwrite_get_recipstr(owl_zwrite *z, char *buff)
     319{
    250320  int i, j;
    251321
     
    259329}
    260330
    261 int owl_zwrite_get_numrecips(owl_zwrite *z) {
     331int owl_zwrite_get_numrecips(owl_zwrite *z)
     332{
    262333  return(owl_list_get_size(&(z->recips)));
    263334}
    264335
    265 char *owl_zwrite_get_recip_n(owl_zwrite *z, int n) {
     336char *owl_zwrite_get_recip_n(owl_zwrite *z, int n)
     337{
    266338  return(owl_list_get_element(&(z->recips), n));
    267339}
    268340
    269 int owl_zwrite_is_personal(owl_zwrite *z) {
     341int owl_zwrite_is_personal(owl_zwrite *z)
     342{
    270343  /* return true if at least one of the recipients is personal */
    271344  int i, j;
     
    280353}
    281354 
    282 void owl_zwrite_free(owl_zwrite *z) {
     355void owl_zwrite_free(owl_zwrite *z)
     356{
    283357  owl_list_free_all(&(z->recips), &owl_free);
    284   owl_free(z->zsig);
    285 }
     358  if (z->class) owl_free(z->class);
     359  if (z->inst) owl_free(z->inst);
     360  if (z->opcode) owl_free(z->opcode);
     361  if (z->realm) owl_free(z->realm);
     362  if (z->message) owl_free(z->message);
     363  if (z->zsig) owl_free(z->zsig);
     364}
Note: See TracChangeset for help on using the changeset viewer.