
#include<stdio.h>
#include<unistd.h>
#include<netinet/in.h>
#include<string.h>
#include<stdlib.h>
#include<pthread.h>


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


extern global_data_t gd;



/* ------------------ CLIENT SETG ----------------- */
int client_setg( private_data_t * pd )
{
static int stat;
int inA = 1;
int count, cho;
char * tmp;

char * chA = (char *)calloc(IP_LEN, sizeof(char));
char * chB = (char *)calloc(IP_LEN, sizeof(char));
char * chC = (char *)calloc(IP_LEN, sizeof(char));

// 最後のカラムに：が無いことを確認 臨時w

count = strlen(pd->rec.data);
if(count > 0){
   if(pd->rec.data[count -1] == ':'){
      free(chA);
      free(chB);
      free(chC);
      return -1;
      }
   }
cho = mem_count(pd->rec.data,':');
chA = safe_memcpy(chA, pd->rec.data, BUF_MAX);

stat = -1;
if( NULL != strchr(pd->rec.data,':') && 3 <= cho && (count - cho) > 0 && count > 7 
    && 1 != mem_count(pd->rec.data, '#')){ 
   chB = safe_memcpy(chB, strtok_r(chA, ":", &tmp), BUF_MAX);
   chC = safe_memcpy(chC, strtok_r(NULL, ":", &tmp), BUF_MAX);
   if(BUF_LEN > strlen(chC) && IP_LEN > strlen(chB)){
      rm_crlf(chC, (int)strlen(chC));
      inA = path_check(chB);
      stat = 1;
      #ifdef SET_BUG
      printf("SET_BUG: client_setg(): %s\n", chA);
      #endif
      switch(inA){
         case 6 : if(6 < (int)strlen(chC)){
                     gd.md.ch_MailFrom = safe_memcpy(gd.md.ch_MailFrom, chC, IP_LEN);
                     printf("SETG: MailFrom =%s\n", gd.md.ch_MailFrom);
                     }
                  else{
                     printf("SETG: MailFrom is not set\n");
                     stat = -1;
                     } 
                  break;
         case 7 : if(6 < (int)strlen(chC)){
                     gd.md.ch_MailRcptTo = safe_memcpy(gd.md.ch_MailRcptTo, chC, IP_LEN);
                     printf("SETG: MailRcptTo =%s\n", gd.md.ch_MailRcptTo);
                     }
                  else{
                     printf("SETG: MailRcptTo is not set\n");
                     stat = -1;
                     } 
                  break;
         case 8 : if(6 < (int)strlen(chC)){
                     gd.md.ch_MailServ = safe_memcpy(gd.md.ch_MailServ, chC, IP_LEN);
                     printf("SETG: MailServ =%s\n", gd.md.ch_MailServ);
                     }
                  else{
                     printf("SETG: MailServ is not set\n");
                     stat = -1;
                     } 
                  break;
         case 14 : if(0 < (int)strlen(chC)){
                      gd.md.in_mailonoff = strtol(chC, (char **)NULL, 10);
                      printf("SETG: MailOnOff =%d\n", gd.md.in_mailonoff);   break;
                      }
                   else{
                      printf("SETG: MailOnOff is not set\n");
                      stat = -1;
                      } 
                  break;

         default : stat = -1; break;
         }
      }
   }

free(chA);
free(chB);
free(chC);

return(stat);
}





