
#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;


//--------------- QUIT_LOGIC ------------------- 
int quit_logi(private_data_t * pd)
{
int ret;

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

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

ret = 0; 

if(ustat == 0 || astat == 0 || cgstat == 0){ 

   if(gd.md.in_mailonoff == 1){
      // subject を作ります
      swapsubject = make_subject(swapsubject, CLIENT_BODY_8, pd->rec.def_user);

      // 本文をつくります
      swapdata = safe_sprintf(swapdata, BUF_MAX, "%s\n\n%s\n\n%s\n", pd->rec.info, CLIENT_BODY_7, 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",
               "1");
      }

   #ifdef LOG_OUT
   safe_pthread_mutex_lock(&con_mutex);
   log_out(gd.fp.con_fd, pd->rec.user, pd->rec.com, pd->rec.data,
     "It ends by the QUIT command.", (unsigned char *)gd.ms.inip_addr[pd->rec.fdno]);
   pthread_mutex_unlock(&con_mutex);
   #endif
   socket_send(gd.ms.sk[pd->rec.fdno], "200 OK wait close connection.\n", (pd->life.life_int + pd->life.life_los + 15));
   ret = -1;
   }
else{
   socket_send(gd.ms.sk[pd->rec.fdno], "500 NG\n", (pd->life.life_int + pd->life.life_los + 15));
   ret = -1;
   } 

free(swapsubject);
free(swapdata);

return(ret);
}






