modules/ak/ack.cc

/* [<][>]
[^][v][top][bottom][index][help] */

FUNCTIONS

This source file includes following functions.
  1. AK_add_to_ack
  2. AK_add_to_ack_string
  3. AK_ack_file_name_generate
  4. AK_send_ack
  5. AK_delete_ack
  6. AK_log_ack

   1 /***************************************
   2   $Revision: 1.8 $
   3 
   4   AK (Acknowledgement) module
   5 
   6   Status: NOT REVIEWED, NOT TESTED
   7 
   8   Author(s):       Engin Gunduz
   9 
  10   ******************/ /******************
  11   Modification History:
  12         engin (10/06/2000) Created.
  13   ******************/ /******************
  14   Copyright (c) 2000                              RIPE NCC
  15  
  16   All Rights Reserved
  17   
  18   Permission to use, copy, modify, and distribute this software and its
  19   documentation for any purpose and without fee is hereby granted,
  20   provided that the above copyright notice appear in all copies and that
  21   both that copyright notice and this permission notice appear in
  22   supporting documentation, and that the name of the author not be
  23   used in advertising or publicity pertaining to distribution of the
  24   software without specific, written prior permission.
  25   
  26   THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  27   ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS; IN NO EVENT SHALL
  28   AUTHOR BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
  29   DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
  30   AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  31   OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  32  ***************************************/
  33 
  34 
  35 
  36 #include "ack.h"
  37 extern int supress_ack_notif;
  38 extern char * humailbox;
  39 extern char * defmail;
  40 
  41 
  42 /*
  43 
  44   AK_add_to_ack: writes a message to the acknowledgement file.
  45 
  46 */
  47 
  48 void AK_add_to_ack(char * filename, char * fmt, ...){
     /* [<][>][^][v][top][bottom][index][help] */
  49 
  50   va_list ap;  /* points to each unnamed arg in turn */
  51   char *p, *sval;
  52   int ival;
  53   double dval;
  54   FILE * ack_file;
  55   
  56   if(( ack_file = fopen(filename, "a")) == NULL){
  57     fprintf(stderr, "Can't open ack file, %s", filename);
  58   }
  59     
  60   va_start(ap, fmt);
  61   
  62   for(p = fmt; *p; p++){
  63     if (*p != '%') {
  64       fprintf(ack_file, "%c", *p);
  65       continue;
  66     }
  67     switch(*++p) {
  68     case 'd':
  69       ival = va_arg(ap, int);
  70       fprintf(ack_file, "%d", ival);
  71       break;
  72     case 'f':
  73       dval = va_arg(ap, double);  
  74       fprintf(ack_file, "%f", dval);
  75       break;
  76     case 'X':
  77       ival = va_arg(ap, int);
  78       fprintf(ack_file, "%X", ival);
  79       break;
  80     case 'x':
  81       ival = va_arg(ap, int);
  82       fprintf(ack_file, "%x", ival);
  83       break;
  84     case 's':
  85       //for(sval = va_arg(ap, char *); *sval; sval++)
  86       //  putchar(*sval);
  87       sval = va_arg(ap, char *);
  88       fprintf(ack_file, "%s", sval);
  89       break;
  90     default:
  91       putchar(*p);
  92       break;
  93     }
  94   }
  95 
  96   va_end(ap); /* clean up */
  97   fclose(ack_file);
  98 }
  99 
 100 /*  */
 101 void AK_add_to_ack_string(const char * file_name, const string msg){
     /* [<][>][^][v][top][bottom][index][help] */
 102 
 103    ofstream ack_file(file_name, ios::app);
 104 
 105    if(!ack_file){
 106      cerr << "Couldn't open ack file" << endl;
 107      return;
 108    }
 109    ack_file << msg;
 110    ack_file.close();
 111 }
 112 
 113 
 114 
 115 
 116 /*
 117 
 118   AK_ack_file_name_generate: Generates a unique name for temporary acknowledgement
 119      files, and also creates it. 
 120 
 121       tmpdir: temporary directory (without a trailing '/')
 122       prefix: prefix for the temp file
 123       
 124       returns: the generated name. 
 125      
 126 
 127 */
 128 
 129 char * AK_ack_file_name_generate( const char * tmpdir, const char * prefix){
     /* [<][>][^][v][top][bottom][index][help] */
 130 
 131    FILE * ack_file;
 132    char * name;
 133       
 134    /* allocate space for name. 32 should be enough for PID */
 135    name = (char*)malloc(strlen(tmpdir) + strlen(prefix) + 32); 
 136    
 137    sprintf(name, "%s/%s.%i", tmpdir, prefix, getpid());
 138 
 139    /* create the file */
 140    if(( ack_file = fopen(name, "w")) == NULL){
 141      fprintf(stderr, "Can't open ack file, %s", name);
 142    }
 143 
 144    /* close it */
 145    fclose(ack_file);
 146     
 147    return name;
 148 
 149 }
 150 
 151 
 152 /* 
 153 
 154 AK_send_ack: sends the ack message contained in the temp file.
 155  
 156    
 157 */
 158 
 159 void AK_send_ack( const char * filename, const char * to_address, const char * mailercommand){
     /* [<][>][^][v][top][bottom][index][help] */
 160 
 161     char * mail_command_line = NULL;
 162     char * supress_file = NULL;
 163     FILE * ack_file, * supr_file_hdl;
 164     char buf[1024];
 165 
 166     /* if we are not supressing acks and notifs, send the ack */
 167     if(!supress_ack_notif){
 168       if(to_address != NULL){
 169         mail_command_line = (char *)malloc(strlen(mailercommand) + strlen(to_address) 
 170             + strlen(filename) + 128);
 171         sprintf(mail_command_line, "%s %s < %s", mailercommand, to_address, filename);
 172         system(mail_command_line);
 173       }
 174     /* if we are supressing acks and notifs, send ack to DEFMAIL  */
 175     }else{
 176       supress_file = (char *)malloc(strlen(filename) + strlen(".supress") + 2); 
 177       sprintf(supress_file, "%s.supress", filename);
 178       if(( supr_file_hdl = fopen(supress_file, "w")) == NULL){
 179         fprintf(stderr, "Can't open supress ack file, %s", supress_file);
 180       }else{
 181         fprintf(supr_file_hdl, "From: %s\nTo: %s\nSubject: Supressed ack mail\n\n",
 182             humailbox, defmail);
 183         if(( ack_file = fopen(filename, "r")) == NULL){
 184           fprintf(stderr, "Can't open ack file for reading, %s", filename);
 185         }else{
 186           while(fgets(buf, 1024, ack_file) != NULL){
 187             fprintf(supr_file_hdl, buf);
 188           }
 189           fclose(ack_file);
 190         }
 191       }
 192       fclose(supr_file_hdl);
 193       mail_command_line = (char *)malloc(strlen(mailercommand) + strlen(defmail) 
 194             + strlen(supress_file) + 128);
 195       sprintf(mail_command_line, "%s %s < %s", mailercommand, defmail, supress_file);
 196       system(mail_command_line);
 197       unlink(supress_file);
 198       free(supress_file);
 199     }
 200 
 201 
 202 }
 203 
 204 
 205 /*
 206 
 207   AK_delete_ack: deletes the temporary acknowledgement file.
 208 
 209 */
 210 
 211 void AK_delete_ack( const char * filename ){
     /* [<][>][^][v][top][bottom][index][help] */
 212 
 213    unlink(filename);   
 214 
 215 }
 216 
 217 /*
 218 
 219 AK_log_ack: logs the acknowledgements in the log_file.
 220 
 221 */
 222 
 223 void AK_log_ack(const char * filename, const char * logfilename){
     /* [<][>][^][v][top][bottom][index][help] */
 224 
 225   FILE * ack_file, * log_file;
 226   char * buf;
 227   time_t cur_time;
 228   char * time_str;
 229 
 230   buf = (char *)malloc(1024);
 231   if(( ack_file = fopen(filename, "r")) == NULL){
 232     fprintf(stderr, "Can't open ack file, %s\n", filename);
 233     return;
 234   }
 235 
 236   if(( log_file = fopen(logfilename, "a")) == NULL){
 237     fprintf(stderr, "Can't open log file, %s\n", logfilename);
 238     return;
 239   }
 240 
 241   /* get time */
 242   cur_time = time(NULL);
 243   time_str = strdup(ctime(&cur_time));
 244   /* cut the '\n' at the end */
 245   time_str[strlen(time_str) - 1] = '\0';
 246 
 247   fprintf(log_file, ">>> time: %s ACK <<<\n\n", time_str);
 248 
 249 
 250   while((buf=fgets(buf, 1023, ack_file)) > 0){
 251     fprintf(log_file, "%s", buf);
 252   }
 253 
 254   free(time_str);
 255   fclose(ack_file);
 256   fclose(log_file);
 257 
 258 }
 259 
 260 
 261 
 262 
 263 
 264 
 265 
 266 

/* [<][>][^][v][top][bottom][index][help] */