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

// V6 対応

int errdata_tok(private_data_t * pd, unsigned char * ip)
{
int maxlen, maxdel, ret, middleno, splen, spmax;
char * swap = (char *)calloc(IP_LEN, sizeof(char));
char * swapdata = (char *)calloc(IP_LEN, sizeof(char));
char * swapsubject = (char *)calloc(IP_LEN, sizeof(char));
char * tmp;

middleno = 0;
spmax = 0;
splen = 0;

swap = safe_memcpy(swap, pd->rec.data, BUF_MAX);
maxlen = strlen(swap);
maxdel = char_count(swap, ';');
ret = maxdel;

if(0 < maxdel && 13 <= maxlen){
   maxdel--;
   swapdata = safe_memcpy(swapdata, strtok_r(swap, ";", &tmp), BUF_MAX);
   if(20 == char_count(swapdata,',') && 20 <= strlen(swapdata)){ 
      swapdata = check_strtol(swapdata);
      #ifdef ERR_OUT
      safe_pthread_mutex_lock(&err_mutex);
      put_log(gd.fp.err_fd, swapdata, pd->rec.user, ip);
      pthread_mutex_unlock(&err_mutex);
      #endif
      swapdata = mail_form(swapdata, pd, ip, &middleno);

      if(middleno == 1){
         pd->rec.data = safe_memcpy(pd->rec.data, MAIL_BODY_1, BUF_MAX);
         #ifdef MAIL_DEBUG
         printf("MAIL_BUG: errdata_tok(): CHECK2 <%s>\n", pd->rec.data);
         #endif
         middleno = 0;
         }
      else if(middleno == 2){
         pd->rec.data = safe_memcpy(pd->rec.data, MAIL_BODY_2, BUF_MAX);
         #ifdef MAIL_DEBUG
         printf("MAIL_BUG: errdata_tok(): CHECK2 <%s>\n", pd->rec.data);
         #endif
         middleno = 0;
         }
      else{
         pd->rec.data = safe_memcpy(pd->rec.data, swapdata, BUF_MAX);
         }
      }
   else{
      pd->rec.data = safe_sprintf(pd->rec.data, BUF_MAX, "Broken data. <1>: %s\n\n", swapdata);
      }


   for(;0 < maxdel; maxdel--){
      swapdata = safe_memcpy(swapdata, strtok_r(NULL, ";", &tmp), BUF_MAX);
      if(20 == char_count(swapdata,',') && 20 <= strlen(swapdata)){ 
         swapdata = check_strtol(swapdata);
         #ifdef ERR_OUT
         safe_pthread_mutex_lock(&err_mutex);
         put_log(gd.fp.err_fd, swapdata, pd->rec.user, ip);
         pthread_mutex_unlock(&err_mutex);
         #endif
         swapdata = mail_form(swapdata, pd, ip, &middleno);

         if((splen = (int)strlen(swapdata)) > 0 ){
            pd->rec.data = safe_strncat(pd->rec.data, swapdata, BUF_MAX);
            spmax = spmax + splen;
            splen = 0;

            }
         }
      else{
         pd->rec.data = safe_sprintf(pd->rec.data, BUF_MAX, "Broken data. <2>: %s\n\n", swapdata);
         }
      }
   }
else{
   pd->rec.data = safe_sprintf(pd->rec.data, BUF_MAX, "Broken data. <3>: %s\n\n", swap);
   }

// 特殊用途です。
#ifdef SERVICES_MODE
pd->rec.minfo = safe_sprintf(pd->rec.minfo, BUF_MAX, "%s\n%s\nhttps://%s/customer/errorlist.php",
   CLIENT_BODY_10, CLIENT_BODY_13, gd.pg.pg_webserver);

#ifdef SIGNATURE_MODE
pd->rec.tinfo = safe_sprintf(pd->rec.tinfo, BUF_MAX, "%s \nE-mail of server watch service by %s\n",
  gd.sig.sig_data[randdata(0, (gd.sig.sig_len - 1))],
  CL_VAR);
#else
pd->rec.tinfo = safe_sprintf(pd->rec.tinfo, BUF_MAX, "E-mail of server watch service by %s\n", CL_VAR);
#endif

#else


// ブロック設定をURLから検索させるかの判断
#ifdef URL_SEARCH
pd->rec.minfo = safe_sprintf(pd->rec.minfo, BUF_MAX,
  "%s %s %s\n%s %s %s\n%s (%s) %s\n%s\n",
  CLIENT_HEAD_16, CL_VAR, CLIENT_HEAD_17,
  CLIENT_HEAD_3, pd->rec.info, CLIENT_HEAD_4,
  CLIENT_HEAD_9, pd->mail.block, CLIENT_HEAD_10,
  CLIENT_HEAD_15
  );
pd->rec.tinfo = safe_sprintf(pd->rec.tinfo, BUF_MAX,
  "%s\n%s\n%s\n\n%s : http://%s/block.php?CNAME=%s\n%s : http://%s/monitor.php?FREE4=%s\n%s           : http://%s/block_newedit.php?BNAME=%s\n",
  CLIENT_HEAD_14,
  CLIENT_HEAD_11,
  CLIENT_HEAD_12,
  CLIENT_HEAD_13, gd.pg.pg_webserver, pd->rec.user,
  CLIENT_HEAD_7, gd.pg.pg_webserver, pd->mail.block,
  CLIENT_HEAD_8, gd.pg.pg_webserver, pd->mail.block
  );
#else
pd->rec.minfo = safe_sprintf(pd->rec.minfo, BUF_MAX,
  "%s %s %s\n%s %s %s\n%s %s %s",
  CLIENT_HEAD_16, CL_VAR, CLIENT_HEAD_17,
  CLIENT_HEAD_3, pd->rec.info, CLIENT_HEAD_4,
  CLIENT_HEAD_5, pd->mail.block, CLIENT_HEAD_6
  );
pd->rec.tinfo = safe_sprintf(pd->rec.tinfo, BUF_MAX, 
  "%s\n",
  CL_VAR);
#endif

#endif


//if(2 <= strlen(pd->rec.data)){
if(2 <= spmax){

   if(pd->mail.in_mailonoff == 1){
      //  subject を作ります
      swapsubject = make_subject(swapsubject, CLIENT_BODY_9, pd->rec.user_org);

      // 本文を作ります
      swapdata = safe_sprintf(swapdata, BUF_MAX, "%s\n\n%s\n\n%s\n", pd->rec.minfo, pd->rec.data, pd->rec.tinfo); 
      swap_mem(swapdata, '\n', (char)0x01);

      mail_grsend(pd->mail.server,
               pd->mail.from,
               pd->mail.rcptto_org,
               pd->mail.cc_org,
               swapdata,
               swapsubject,    
               gd.on_bcc,
               pd->mail.priority);
     }
  }


free(swap);
free(swapdata);
free(swapsubject);

/* return pd->rec.data; */
return ret;
}






