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


// V6 対応

extern global_data_t gd;


char * mail_form(char * swap, private_data_t * pd, unsigned char * chIpadd, int * middleno)
{
int pri, ret;

pri = 0;
ret = 0;

char * tool = (char *)calloc(2, sizeof(char));
char * utime = (char *)calloc(2, sizeof(char));
char * year = (char *)calloc(2, sizeof(char));
char * month = (char *)calloc(2, sizeof(char));
char * day = (char *)calloc(2, sizeof(char));
char * hour = (char *)calloc(2, sizeof(char));
char * minute = (char *)calloc(2, sizeof(char));
char * seconds = (char *)calloc(2, sizeof(char));
char * stat = (char *)calloc(2, sizeof(char));
char * prot = (char *)calloc(2, sizeof(char));
char * com = (char *)calloc(2, sizeof(char));
char * host = (char *)calloc(2, sizeof(char));
char * port = (char *)calloc(2, sizeof(char));
char * timeout = (char *)calloc(2, sizeof(char));
char * data = (char *)calloc(2, sizeof(char));
char * info = (char *)calloc(2, sizeof(char));
char * plai = (char *)calloc(2, sizeof(char));
char * free1 = (char *)calloc(2, sizeof(char));
char * free2 = (char *)calloc(2, sizeof(char));
char * free3 = (char *)calloc(2, sizeof(char));
char * free4 = (char *)calloc(2, sizeof(char));

char * kanshi = (char *)calloc(2, sizeof(char));
char * pri_data = (char *)calloc(2, sizeof(char));

swap_mem(swap, '\'', ' ');

tool = delim_get(swap, tool, ',', &ret, 0);
utime = delim_get(swap, utime, ',', &ret, 1);
year = delim_get(swap, year, ',', &ret, 2);
month = delim_get(swap, month, ',', &ret, 3);
day = delim_get(swap, day, ',', &ret, 4);
hour = delim_get(swap, hour, ',', &ret, 5);
minute = delim_get(swap, minute, ',', &ret, 6);
seconds = delim_get(swap, seconds, ',', &ret, 7);
stat = delim_get(swap, stat, ',', &ret, 8);
prot = delim_get(swap, prot, ',', &ret, 9);
com = delim_get(swap, com, ',', &ret, 10);
host = delim_get(swap, host, ',', &ret, 11);
port = delim_get(swap, port, ',', &ret, 12);
timeout = delim_get(swap, timeout, ',', &ret, 13);
data = delim_get(swap, data, ',', &ret, 14);
info = delim_get(swap, info, ',', &ret, 15);
plai = delim_get(swap, plai, ',', &ret, 16);
free1 = delim_get(swap, free1, ',', &ret, 17);
free2 = delim_get(swap, free2, ',', &ret, 18);
free3 = delim_get(swap, free3, ',', &ret, 19);
free4 = delim_get(swap, free4, ',', &ret, 20);

#ifdef MAIL_DEBUG
printf("MAIL_DEBUG: mail_form(): TOOL=%s STAT=%s\n", tool, stat);
printf("MAIL_DEBUG: mail_form(): free1=%s free2=%s free3=%s free4=%s\n", free1, free2, free3, free4);
#endif


if(0 == self_memcmp(tool, "SET")){
   #ifdef DEBUG
   printf("DEBUG: mail_form(): Geting SET free1=%s free2=%s free3=%s free4=%s\n", free1, free2, free3, free4);
   #endif


   //  SET 個別でプライオリティーを設定する部分 char * にするのはちょっと、、、あとでなおす。
   if(0 == self_memcmp(plai, "1")){
      pd->mail.priority = safe_memcpy(pd->mail.priority, "1", BUF_LEN);
      }
   else if(0 == self_memcmp(plai, "2")){
      pd->mail.priority = safe_memcpy(pd->mail.priority, "2", BUF_LEN);
      }
   else if(0 == self_memcmp(plai, "3")){
      pd->mail.priority = safe_memcpy(pd->mail.priority, "3", BUF_LEN);
      }
   else if(0 == self_memcmp(plai, "4")){
      pd->mail.priority = safe_memcpy(pd->mail.priority, "4", BUF_LEN);
      }
   else if(0 == self_memcmp(plai, "5")){
      pd->mail.priority = safe_memcpy(pd->mail.priority, "5", BUF_LEN);
      }
   else {
      pd->mail.priority = safe_memcpy(pd->mail.priority, "3", BUF_LEN);
      }

   #ifdef MAIL_DEBUG
   printf("MAIL_DEBUG: mail_form(): SET MAIL PRIORITY = %s id = %s ---------\n", plai, pd->mail.priority);
   #endif


   if(0 == self_memcmp(info, "MID2")){
      * middleno = 1;
      }
   if(0 == self_memcmp(info, "MIX2")){
      * middleno = 2;
      }

   if(0 != self_memcmp(free4, "NOT") && 2 <= strlen(free4)){
      pd->mail.block = safe_memcpy(pd->mail.block, free4, BUF_LEN);
      }
   else {
      pd->mail.block = safe_memcpy(pd->mail.block, "NOT", BUF_LEN);
      }

   #ifdef MAIL_DEBUG
   printf("MAIL_DEBUG: mail_form(): blockname pd->mail.block=%s\n", pd->mail.block);
   #endif

   /* set mail on off */
   if(0 != self_memcmp(free1, "NOT") && 2 <= strlen(free1)){
      if(0 == self_memcmp(free1, "ON")){
         /* user side mail on off set */
         pd->mail.in_mailonoff = gd.md.in_mailonoff;
         }
      else if(0 == self_memcmp(free1, "OFF")){
         /* user side mail on off set */
         pd->mail.in_mailonoff = 0;
         }
      else{
         /* user side mail on off set */
         pd->mail.in_mailonoff = gd.md.in_mailonoff;
         }
      }
   else{
      pd->mail.in_mailonoff = gd.md.in_mailonoff;
      }

   #ifdef DEBUG
   printf("DEBUG: mail_form(): user side mail on off = %d\n", pd->mail.in_mailonoff);
   #endif

   /*  set User name */ 
   if(0 != self_memcmp(free4, "NOT") && 4 <= strlen(free4)){
      /* user side user name set */
      pd->rec.user_org = safe_memcpy(pd->rec.user_org, free4, BUF_LEN);
      }
   else{
      pd->rec.user_org = safe_memcpy(pd->rec.user_org, pd->rec.user, BUF_LEN);
      }

   /*  set To: */ 
   if(0 != self_memcmp(free2, "NOT") && 4 <= strlen(free2) && 2 <= char_point(free2, '@', 1)){
      /* user side mail.rcptto set */
      pd->mail.rcptto_org = safe_memcpy(pd->mail.rcptto_org, free2, BUF_LEN);
      pd->mail.cc_org = safe_memcpy(pd->mail.cc_org, " ", BUF_LEN);
      swap_mem( pd->mail.rcptto_org, '|', ',');

      /*  set Cc: */ 
      if(0 != self_memcmp(free3, "NOT") && 4 <= strlen(free3) && 2 <= char_point(free3, '@', 1)){
         /* user side mail.rcptto set */
         pd->mail.cc_org = safe_memcpy(pd->mail.cc_org, free3, BUF_LEN);
         swap_mem( pd->mail.cc_org, '|', ',');
         }

      }
   else{
      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);
      printf("2 default rcptto %s\n", pd->mail.rcptto_org);
      printf("2 default cc %s\n", pd->mail.cc_org);
      }

   clean_mem(swap, (char)0x00, (int)strlen(swap));
   goto mail_form_end;
   }   // SET 


#ifdef POSTGRES_IN
char * pgerrlog_table = "(cip,cname,monitor,uct,ye,mo,da,ha,mi,se,sign,proto,com,cinfo,port,wtime,pinfo,errdata,prino,free1,free2,free3,free4)"; 
swap = safe_sprintf(swap, BUF_MAX, "%s%s%s('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s');",
     "INSERT INTO errlog ",
     pgerrlog_table,
     " VALUES ",
     chIpadd,
     pd->rec.user,
     tool,
     utime,
     year,
     month,
     day,
     hour,
     minute,
     seconds,
     stat,
     prot,
     com,
     host,
     port,
     timeout,
     data,
     info,
     plai,
     free1,
     free2,
     free3,
     free4);

#ifdef POSTGRES_DEBUG
printf("POSTGRES_DEBUG: mail_form(): PG_INDATA > %s\n", swap);
#endif

swap_mem(swap, (char)0x01, '\n');


/* data insert */
safe_pthread_mutex_lock(&pg_mutex);
psqlexec_datain(pd, swap);
pthread_mutex_unlock(&pg_mutex);
#endif


#ifdef MYSQL_IN
char * myerrlog_table = "(cip,cname,monitor,uct,ye,mo,da,ha,mi,se,sign,proto,com,cinfo,port,wtime,pinfo,errdata,prino,free1,free2,free3,free4)"; 
swap = safe_sprintf(swap, BUF_MAX, "%s%s%s('%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s');",
     "INSERT INTO errlog ",
     myerrlog_table,
     " VALUES ",
     chIpadd,
     pd->rec.user,
     tool,
     utime,
     year,
     month,
     day,
     hour,
     minute,
     seconds,
     stat,
     prot,
     com,
     host,
     port,
     timeout,
     data,
     info,
     plai,
     free1,
     free2,
     free3,
     free4);

#ifdef MYSQL_DEBUG
printf("MYSQL_DEBUG: mail_form(): MYSQL_INDATA > %s\n", swap);
#endif

swap_mem(swap, (char)0x01, '\n');


/* data insert */
safe_pthread_mutex_lock(&mysql_mutex);
mysqlexec_datain(swap);
pthread_mutex_unlock(&mysql_mutex);
#endif




// メールのプライオリティー設定
if(0 == ch_int2(plai)){
   pri = strtol(plai, (char **)NULL, 10);

   if(pri == 1){
      pri_data = safe_memcpy(pri_data, MAIL_PR_1, BUF_MAX);
      }
   else if(pri == 2){
      pri_data = safe_memcpy(pri_data, MAIL_PR_2, BUF_MAX);
      }
   else{
      pri_data = safe_memcpy(pri_data, MAIL_PR_3, BUF_MAX);
      }

   }
else{
   pri = 3;
   fprintf(stderr,"mail_form(): プライオリティーが数字でないので3にします %s\n", plai);
   }


if(pri < 3){

   // 特殊用途です
   #ifdef SERVICES_MODE
// kanshi = safe_sprintf(kanshi, BUF_MAX,"%shttps://%s/customer/%s/monitor_edit.php?STATUS=UPDATE&UNO=%s",
   kanshi = safe_memcpy(kanshi, "", BUF_MAX);
   #else

   // 監視設定をURLから検索させるかの判断
   #ifdef URL_SEARCH
// kanshi = safe_sprintf(kanshi, BUF_MAX,"%shttp://%s/monitor_edit.php?STATUS=UPDATE&UNO=%s",
   kanshi = safe_sprintf(kanshi, BUF_MAX,"%shttp://%s/monitor_edit.php?STATUS=UPDATE&UNO=%s\n",
     MONITOR_SER, gd.pg.pg_webserver, free3);
   #else
   kanshi = safe_sprintf(kanshi, BUF_MAX,"%s%s", MONITOR_ID, free3);
   #endif

   #endif

   /* --- mail format --- */
   if(0 == self_memcmp(tool, "INFO")){
      swap = safe_sprintf(swap, BUF_MAX, "%s\n\n",
        MAIL_DATA_1);
      }
   if(0 == self_memcmp(tool, "SNMP")){
//      swap = safe_sprintf(swap, BUF_MAX, "%s\n%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n\n",
        swap = safe_sprintf(swap, BUF_MAX, "%s\n%s%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n\n",
        MAIL_SNMP_11,
        kanshi,
        MAIL_SNMP_12,
        pri_data,
        MAIL_SNMP_13,
        prot,
        MAIL_SNMP_14,
        host,
        MAIL_SNMP_15,
        com,
        MAIL_SNMP_16,
        free1,
        MAIL_SNMP_17,
        data,
        MAIL_SNMP_18,
        info);
      }

   if(0 == self_memcmp(stat, "0") && 0 == self_memcmp(tool, "PORT")){
      if(0 == self_memcmp(prot, "DHW")){
//       swap = safe_sprintf(swap, BUF_MAX, "%s\n%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n\n",
         swap = safe_sprintf(swap, BUF_MAX, "%s\n%s%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n\n",
           MAIL_DHW_11,
           kanshi,
           MAIL_DHW_12,
           pri_data,
           MAIL_DHW_13,
           prot,
           MAIL_DHW_14,
           host,
           MAIL_DHW_15,
           com,
           MAIL_DHW_16,
           timeout,
           MAIL_DHW_17,
           free1,
           MAIL_DHW_18,
           data,
           MAIL_DHW_19,
           info);
         }


      else if(0 == self_memcmp(prot, "MYSQL")){
         swap = safe_sprintf(swap, BUF_MAX, "%s\n%s%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n\n",
           MAIL_MYSQL_11,
           kanshi,
           MAIL_MYSQL_12,
           pri_data,
           MAIL_MYSQL_13,
           prot,
           MAIL_MYSQL_14,
           host,
           MAIL_MYSQL_15,
           com,
           MAIL_MYSQL_16,
           port,
           MAIL_MYSQL_17,
           timeout,
           MAIL_MYSQL_18,
           free1,
           MAIL_MYSQL_19,
           data,
           MAIL_MYSQL_20,
           info);
         }

      else if(0 == self_memcmp(prot, "PGSQL")){
         swap = safe_sprintf(swap, BUF_MAX, "%s\n%s%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n\n",
           MAIL_PGSQL_11,
           kanshi,
           MAIL_PGSQL_12,
           pri_data,
           MAIL_PGSQL_13,
           prot,
           MAIL_PGSQL_14,
           host,
           MAIL_PGSQL_15,
           com,
           MAIL_PGSQL_16,
           port,
           MAIL_PGSQL_17,
           timeout,
           MAIL_PGSQL_18,
           free1,
           MAIL_PGSQL_19,
           data,
           MAIL_PGSQL_20,
           info);
         }



      else if(0 == self_memcmp(prot, "URL")){
//       swap = safe_sprintf(swap, BUF_MAX, "%s\n%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n\n",
         swap = safe_sprintf(swap, BUF_MAX, "%s\n%s%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n\n",
           MAIL_URL_11,
           kanshi,
           MAIL_URL_12,
           pri_data,
           MAIL_URL_13,
           prot,
           MAIL_URL_14,
           host,
           MAIL_URL_15,
           port,
           MAIL_URL_16,
           timeout,
           MAIL_URL_17,
           free1,
           MAIL_URL_18,
           data,
           MAIL_URL_19,
           info);
         }
      else if(0 == self_memcmp(prot, "SMTP")){
//       swap = safe_sprintf(swap, BUF_MAX, "%s\n%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n\n",
         swap = safe_sprintf(swap, BUF_MAX, "%s\n%s%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n\n",
           MAIL_SMTP_11,
           kanshi,
           MAIL_SMTP_12,
           pri_data,
           MAIL_SMTP_13,
           prot,
           MAIL_SMTP_14,
           host,
           MAIL_SMTP_15,
           port,
           MAIL_SMTP_16,
           timeout,
           MAIL_SMTP_17,
           free1,
           MAIL_SMTP_18,
           data,
           MAIL_SMTP_19,
           info);
         }
      else if(0 == self_memcmp(prot, "SNTP")){
//       swap = safe_sprintf(swap, BUF_MAX, "%s\n%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n\n",
         swap = safe_sprintf(swap, BUF_MAX, "%s\n%s%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n\n",
           MAIL_NTP_11,
           kanshi,
           MAIL_NTP_12,
           pri_data,
           MAIL_NTP_13,
           prot,
           MAIL_NTP_14,
           host,
           MAIL_NTP_15,
           port,
           MAIL_NTP_16,
           timeout,
           MAIL_NTP_17,
           free1,
           MAIL_NTP_18,
           data,
           MAIL_NTP_19,
           info);
         }

      else if(0 == self_memcmp(prot, "ICMP")){
         swap = safe_sprintf(swap, BUF_MAX, "%s\n%s%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n\n",
           MAIL_ICMP_11,
           kanshi,
           MAIL_ICMP_12,
           pri_data,
           MAIL_ICMP_13,
           prot,
           MAIL_ICMP_14,
           host,
           MAIL_ICMP_15,
           port,
           MAIL_ICMP_16,
           timeout,
           MAIL_ICMP_17,
           free1,
           MAIL_ICMP_18,
           data,
           MAIL_ICMP_19,
           info);
         }
      else{
//       swap = safe_sprintf(swap, BUF_MAX, "%s\n%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n\n",
         swap = safe_sprintf(swap, BUF_MAX, "%s\n%s%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n\n",
           MAIL_PORT_11,
           kanshi,
           MAIL_PORT_12,
           pri_data,
           MAIL_PORT_13,
           prot,
           MAIL_PORT_14,
           host,
           MAIL_PORT_15,
           port,
           MAIL_PORT_16,
           timeout,
           MAIL_PORT_17,
           free1,
           MAIL_PORT_18,
           data,
           MAIL_PORT_19,
           info);
         }
      }

   if(0 == self_memcmp(stat, "1") && 0 == self_memcmp(tool, "PORT")){

      if(0 == self_memcmp(prot, "DHW")){
//       swap = safe_sprintf(swap, BUF_MAX, "%s\n%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n\n",
         swap = safe_sprintf(swap, BUF_MAX, "%s\n%s%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n\n",
           MAIL_DHW_21,
           kanshi,
           MAIL_DHW_22,
           pri_data,
           MAIL_DHW_23,
           prot,
           MAIL_DHW_24,
           host,
           MAIL_DHW_25,
           com,
           MAIL_DHW_26,
           timeout,
           MAIL_DHW_27,
           free1,
           MAIL_DHW_28,
           data,
           MAIL_DHW_29,
           info);
         }


      else if(0 == self_memcmp(prot, "MYSQL")){
         swap = safe_sprintf(swap, BUF_MAX, "%s\n%s%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n\n",
           MAIL_MYSQL_21,
           kanshi,
           MAIL_MYSQL_22,
           pri_data,
           MAIL_MYSQL_23,
           prot,
           MAIL_MYSQL_24,
           host,
           MAIL_MYSQL_25,
           com,
           MAIL_MYSQL_26,
           port,
           MAIL_MYSQL_27,
           timeout,
           MAIL_MYSQL_28,
           free1,
           MAIL_MYSQL_29,
           data,
           MAIL_MYSQL_30,
           info);
         }


      else if(0 == self_memcmp(prot, "PGSQL")){
         swap = safe_sprintf(swap, BUF_MAX, "%s\n%s%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n\n",
           MAIL_PGSQL_21,
           kanshi,
           MAIL_PGSQL_22,
           pri_data,
           MAIL_PGSQL_23,
           prot,
           MAIL_PGSQL_24,
           host,
           MAIL_PGSQL_25,
           com,
           MAIL_PGSQL_26,
           port,
           MAIL_PGSQL_27,
           timeout,
           MAIL_PGSQL_28,
           free1,
           MAIL_PGSQL_29,
           data,
           MAIL_PGSQL_30,
           info);
         }



      else if(0 == self_memcmp(prot, "URL")){
//       swap = safe_sprintf(swap, BUF_MAX, "%s\n%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n\n",
         swap = safe_sprintf(swap, BUF_MAX, "%s\n%s%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n\n",
           MAIL_URL_21,
           kanshi,
           MAIL_URL_22,
           pri_data,
           MAIL_URL_23,
           prot,
           MAIL_URL_24,
           host,
           MAIL_URL_25,
           port,
           MAIL_URL_26,
           timeout,
           MAIL_URL_27,
           free1,
           MAIL_URL_28,
           data,
           MAIL_URL_29,
           info);
         }
      else if(0 == self_memcmp(prot, "SMTP")){
//       swap = safe_sprintf(swap, BUF_MAX, "%s\n%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n\n",
         swap = safe_sprintf(swap, BUF_MAX, "%s\n%s%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n\n",
           MAIL_SMTP_21,
           kanshi,
           MAIL_SMTP_22,
           pri_data,
           MAIL_SMTP_23,
           prot,
           MAIL_SMTP_24,
           host,
           MAIL_SMTP_25,
           port,
           MAIL_SMTP_26,
           timeout,
           MAIL_SMTP_27,
           free1,
           MAIL_SMTP_28,
           data,
           MAIL_SMTP_29,
           info);
         }
      else if(0 == self_memcmp(prot, "SNTP")){
//       swap = safe_sprintf(swap, BUF_MAX, "%s\n%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n\n",
         swap = safe_sprintf(swap, BUF_MAX, "%s\n%s%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n\n",
           MAIL_NTP_21,
           kanshi,
           MAIL_NTP_22,
           pri_data,
           MAIL_NTP_23,
           prot,
           MAIL_NTP_24,
           host,
           MAIL_NTP_25,
           port,
           MAIL_NTP_26,
           timeout,
           MAIL_NTP_27,
           free1,
           MAIL_NTP_28,
           data,
           MAIL_NTP_29,
           info);
         }

      else if(0 == self_memcmp(prot, "ICMP")){
         swap = safe_sprintf(swap, BUF_MAX, "%s\n%s%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n\n",
           MAIL_ICMP_21,
           kanshi,
           MAIL_ICMP_22,
           pri_data,
           MAIL_ICMP_23,
           prot,
           MAIL_ICMP_24,
           host,
           MAIL_ICMP_25,
           port,
           MAIL_ICMP_26,
           timeout,
           MAIL_ICMP_27,
           free1,
           MAIL_ICMP_28,
           data,
           MAIL_ICMP_29,
           info);
         }

      else{
//       swap = safe_sprintf(swap, BUF_MAX, "%s\n%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n\n",
         swap = safe_sprintf(swap, BUF_MAX, "%s\n%s%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n\n",
           MAIL_PORT_21,
           kanshi,
           MAIL_PORT_22,
           pri_data,
           MAIL_PORT_23,
           prot,
           MAIL_PORT_24,
           host,
           MAIL_PORT_25,
           port,
           MAIL_PORT_26,
           timeout,
           MAIL_PORT_27,
           free1,
           MAIL_PORT_28,
           data,
           MAIL_PORT_29,
           info);
         }
      }

   if(0 == self_memcmp(stat, "0") && 0 == self_memcmp(tool, "COMAND")){
//    swap = safe_sprintf(swap, BUF_MAX, "%s\n%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n\n",
      swap = safe_sprintf(swap, BUF_MAX, "%s\n%s%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n\n",
        MAIL_COMMAND_11,
        kanshi,
        MAIL_COMMAND_12,
        pri_data,
        MAIL_COMMAND_13,
        com,
        MAIL_COMMAND_14,
        timeout,
        MAIL_COMMAND_15,
        free1,
        MAIL_COMMAND_16,
        data,
        MAIL_COMMAND_17,
        info);
      }

   if(0 == self_memcmp(stat, "1") && 0 == self_memcmp(tool, "COMAND")){
//    swap = safe_sprintf(swap, BUF_MAX, "%s\n%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n\n",
      swap = safe_sprintf(swap, BUF_MAX, "%s\n%s%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n\n",
        MAIL_COMMAND_21,
        kanshi,
        MAIL_COMMAND_22,
        pri_data,
        MAIL_COMMAND_23,
        com,
        MAIL_COMMAND_24,
        timeout,
        MAIL_COMMAND_25,
        free1,
        MAIL_COMMAND_26,
        data,
        MAIL_COMMAND_27,
        info);
      }
   }
else{
   clean_mem(swap, (char)0x00, (int)strlen(swap));
   }

mail_form_end :;

free(tool);
free(utime);
free(year);
free(month);
free(day);
free(hour);
free(minute);
free(seconds);
free(stat);
free(prot);
free(com);
free(host);
free(port);
free(timeout);
free(data);
free(info);
free(plai);
free(free1);
free(free2);
free(free3);
free(free4);

free(kanshi);
free(pri_data);

return swap;
}



