
#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<pthread.h>

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



extern global_data_t gd;



// --------------- ERR_LOGIC ------------------ 
int err_logi(private_data_t * pd)
{
static int ret;
ret = 0; 

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");

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

   #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 get err data", (unsigned char *)gd.ms.inip_addr[pd->rec.fdno]);
   pthread_mutex_unlock(&con_mutex);
   #endif
   ret = socket_send(gd.ms.sk[pd->rec.fdno], "200 OK get err data\n", (pd->life.life_int + pd->life.life_los + 15));

   if(2 <= strlen(pd->rec.data)){
      // mail to cc default set 
      pd->rec.user_org = safe_memcpy(pd->rec.user_org, pd->rec.user, BUF_LEN);
      pd->mail.rcptto_org = safe_memcpy(pd->mail.rcptto_org, pd->mail.rcptto, BUF_LEN);
      pd->mail.cc_org = safe_memcpy(pd->mail.cc_org, pd->mail.cc, BUF_LEN);
      pd->mail.in_mailonoff = gd.md.in_mailonoff;

      errdata_tok(pd, (unsigned char *)gd.ms.inip_addr[pd->rec.fdno]);

      }

   if(ret < 0){
      fprintf(stderr,"err_logi(1): send 200 OK return stat <%d>\n",  ret);
      }
   }

else{
   #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

   ret = socket_send(gd.ms.sk[pd->rec.fdno], "500 NG\n", (pd->life.life_int + pd->life.life_los + 15));

   if(ret < 0){
      fprintf(stderr,"err_logi(2): send 200 OK return stat <%d>\n",  ret);
      }
   } 

return(ret);
}





