
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<unistd.h>
#include<sys/socket.h>
#include<sys/wait.h>
#include<string.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<errno.h>
#include<pthread.h>

#include"zither.h"
#include<almemsys/almemsys.h>
#include"jpreturn.h"


extern global_data_t gd;


/*--------------- LIFE_LOGIC ------------------- */
int life_logi(private_data_t * pd)
{
int ret, tret;
int checktime, bstat, cstat, fstat, sstat, crfilestat, crliststat;

checktime = 0;
bstat =0;
cstat =0;
fstat =0;
sstat =0;
crfilestat =0;
crliststat =0;

errno = 0;
tret = 0;

char * swap2 = (char *)calloc(IP_LEN, sizeof(char));
char * swap3 = (char *)calloc(IP_LEN, sizeof(char));
char * swapsubject = (char *)calloc(IP_LEN, sizeof(char));
char * swapdata = (char *)calloc(IP_LEN, sizeof(char));

int ustat = self_memcmp(pd->rec.user, pd->rec.def_user); 
int astat = self_memcmp(pd->rec.user, "administrator");
int cgstat = self_memcmp(pd->rec.user, "cgiuser");


ret = 0; 
if(ustat == 0 && astat != 0 && cgstat != 0){ 

   if(pd->life.onoff == 1){
      // life thread 
      if((tret = pthread_create( &pd->life.life_t, NULL, (void *)life_thread, pd )) != 0 ){
         fprintf(stderr,"life_logi.c life_logi(): pthread_create %d\n", tret);
         }
      checktime = life_check(pd);
      #ifdef LIFE_DEBUG
      printf("LIFE_DEBUG: life_logi():  CHECK_TIME> %s >checktime=%d life_los=%d\n",
        pd->rec.def_user, checktime, pd->life.life_los);
      #endif

      // life mail send
      if(checktime >= pd->life.life_los){ 

         if(gd.md.in_mailonoff == 1){
            pd->life.swap1 = safe_sprintf(pd->life.swap1, BUF_MAX,
               "%s %d %s", CLIENT_HEAD_1, checktime, CLIENT_HEAD_2);

            //  subject を作ります
            swapsubject = make_subject(swapsubject, CLIENT_BODY_6, pd->rec.def_user);

            // 本文をつくります
            swapdata = safe_sprintf(swapdata, BUF_MAX, "%s\n\n%s\n\n%s\n", pd->rec.info, pd->life.swap1, CL_VAR);
            swap_mem(swapdata, '\n', (char)0x01);

            mail_grsend(pd->mail.server,
                     pd->mail.from,
                     pd->mail.rcptto,
                     pd->mail.cc,
                     swapdata,
                     swapsubject,
                     "NOT",
                     "2");
            }

         #ifdef LIFE_DEBUG
         printf("LIFE_DEBUG: life_logi():  ERR_MAIL_SEND (1)> %s\n", pd->rec.def_user);
         #endif
         }
      }
   else{
      #ifdef LIFE_DEBUG
      printf("LIFE_DEBUG: life_logi(): LIFE NOT START pd->life.onoff=%d\n", pd->life.onoff);
      #endif
      }

   pd->life.bat_stat = stat_check(pd->life.bat_stat, pd->file.bat_filename, TIME_LEN, &bstat);
   pd->life.scan_stat = stat_check(pd->life.scan_stat, pd->file.scan_filename, TIME_LEN, &cstat);
   pd->life.conf_stat = stat_check(pd->life.conf_stat, pd->file.conf_filename, TIME_LEN, &fstat);
   pd->life.stat_stat = stat_check(pd->life.stat_stat, pd->file.stat_filename, TIME_LEN, &sstat);
   if(gd.cron_onoff == 1){
      pd->life.cronfile_stat = stat_check(pd->life.cronfile_stat, pd->file.cronfile_filename, TIME_LEN, &crfilestat);
      pd->life.cronlist_stat = stat_check(pd->life.cronlist_stat, pd->file.cronlist_filename, TIME_LEN, &crliststat);
      }

      swap2 = safe_memcpy(swap2, "CHECK", BUF_SLEN);
   if(0 > bstat){
      swap2 = safe_strncat(swap2, "_BAT", BUF_SLEN);
      }
   if(0 > cstat){
      swap2 = safe_strncat(swap2, "_SCA", BUF_SLEN);
      }
   if(0 > fstat){
      swap2 = safe_strncat(swap2, "_CONF", BUF_SLEN);
      }
   if(0 > sstat){
      swap2 = safe_strncat(swap2, "_STAT", BUF_SLEN);
      }
   if(gd.cron_onoff == 1){
      if(0 > crfilestat){
         swap2 = safe_strncat(swap2, "_CRONFILE", BUF_SLEN);
         }
      if(0 > crliststat){
         swap2 = safe_strncat(swap2, "_CRONLIST", BUF_SLEN);
         }
      }

   if(0 == memcmp(swap2, "CHECK_", 6) && 5 < (int)strlen(swap2)){
      swap3 = safe_sprintf(swap3, BUF_MAX,"%s 200 OK\n", swap2);
      #ifdef LIFE_DEBUG
      printf("LIFE_DEBUG: life_logi(): LIFE SEND> %s > %s\n", pd->rec.def_user, swap3);
      #endif
      #ifdef LOG_OUT
      safe_pthread_mutex_lock(&con_mutex);
      log_out(gd.fp.con_fd, pd->rec.user, pd->rec.com, pd->rec.data, swap2, (unsigned char *)gd.ms.inip_addr[pd->rec.fdno]);
      pthread_mutex_unlock(&con_mutex);
      #endif
      ret = socket_send(gd.ms.sk[pd->rec.fdno], swap3, (pd->life.life_int + pd->life.life_los + 15));
      }
   else{
      ret = socket_send(gd.ms.sk[pd->rec.fdno], "200 OK\n", (pd->life.life_int + pd->life.life_los + 15));
      #ifdef LIFE_DEBUG
      printf("LIFE_DEBUG: life_logi(): LIFE SEND> %s > 200OK ret = %d\n", pd->rec.def_user, ret);
      #endif
      #ifdef LOG_OUT
      safe_pthread_mutex_lock(&con_mutex);
      log_out(gd.fp.con_fd, pd->rec.user, pd->rec.com, pd->rec.data, "200 OK", (unsigned char *)gd.ms.inip_addr[pd->rec.fdno]);
      pthread_mutex_unlock(&con_mutex);
      #endif
      }

   }
else{
   ret = socket_send(gd.ms.sk[pd->rec.fdno], "500 NG\n", (pd->life.life_int + pd->life.life_los + 15));
   #ifdef LIFE_DEBUG
   printf("LIFE_DEBUG: life_logi(): LIFE SEND> %s > 500OK\n", pd->rec.def_user);
   #endif
   #ifdef LOG_OUT
   safe_pthread_mutex_lock(&con_mutex);
   log_out(gd.fp.con_fd, pd->rec.user, pd->rec.com, pd->rec.data, "500 NG", (unsigned char *)gd.ms.inip_addr[pd->rec.fdno]);
   pthread_mutex_unlock(&con_mutex);
   #endif
   } 


free(swap2);
free(swap3);
free(swapsubject);
free(swapdata);

return(ret);
}








