// zither_subst.c
// $Id: zither_subst.c,v 1.7 2007/11/14 04:08:53 sendan Exp $
// masashi shimakura


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



/*----------------- STAG-NEG ---------------- */
int stag_neg(private_data_t * pd)
{
int ret, set;
char * getdata;
char * getdata2;
char * tmp;

char * swap = (char *)calloc(IP_LEN, sizeof(char));


swap = safe_memcpy(swap, pd->getdata, BUF_MAX);
getdata = strstr(swap, "DATA: ");

ret = 0;
if(' '!= pd->getdata[0] && NULL!=strstr(pd->getdata," COM: ") &&
NULL!=strstr(pd->getdata," DATA: ") && NULL!=strstr(pd->getdata, " :: ")){ 
   if( 24 >= strlen(pd->rec.user = safe_memcpy(pd->rec.user, strtok_r(pd->getdata, " :: ", &tmp), BUF_MAX))){
      if( 5 >= strlen(pd->rec.com = safe_memcpy(pd->rec.com, strtok_r(NULL, " COM: ", &tmp), BUF_MAX))){
         if((getdata2 = (strchr(getdata, ' ')+1))!=NULL){
            pd->rec.data = safe_memcpy(pd->rec.data, getdata2, BUF_MAX);
            rm_crlf(pd->rec.user, (int)strlen(pd->rec.user));
            rm_crlf(pd->rec.com, (int)strlen(pd->rec.com));
            rm_crlf(pd->rec.data, (int)strlen(pd->rec.data));
            if( 0!=strlen(pd->rec.user)){
               #ifdef DEBUG
               printf("DEBUG: stag_neg(): user %s\n", pd->rec.user);
               printf("DEBUG: stag_neg(): com %s\n", pd->rec.com);
               printf("DEBUG: stag_neg(): data %s\n", pd->rec.data);
               #endif
               set = (int)com_check(pd->rec.com);
               #ifdef DATA_SIZE
               printf("DATA_SIZE: stag_neg(): com_check = %d\n", set);
               #endif
               switch(set){

                  case 0 : ret = socket_send(gd.ms.sk[pd->rec.fdno], 
                                  "500 NG Illegal method.\n", (pd->life.life_int + pd->life.life_los + 15));
                       #ifdef LOG_OUT
                       safe_pthread_mutex_lock(&con_mutex);
                       log_out(gd.fp.con_fd, pd->rec.user,
                         "<Not>", "Illegal method.", " ", (unsigned char *)gd.ms.inip_addr[pd->rec.fdno]);
                       pthread_mutex_unlock(&con_mutex);
                       #endif
                       break; 
                  case 1 : ret = get_logi(pd);             break; /* User */
                  case 2 : ret = put_logi(pd);             break; /* User */
                  case 3 : ret = life_logi(pd);            break; /* User */
                  case 4 : ret = quit_logi(pd);            break; /* General */
                  case 5 : ret = vers_logi(pd);            break; /* General */
                  case 7 : ret = bat_logi(pd);             break; /* User */
                  case 8 : ret = err_logi(pd);             break; /* User */
                  case 9 : ret = set_logi(pd);             break; /* User */
   //             case 10: private_data_list(pd);          break;   
                  case 11: ret = setg_logi(pd);            break; /* administrator */
                  case 12: global_data_list(pd);           break;
   //             case 13 : ret = dif_logi(pd);            break; /* User */
   //             case 14 : ret = undif_logi(pd);          break; /* User */
                  case 13 : ret = up_logi(pd);             break; /* User */
                  case 14 : ret = alget_logi(pd);          break; /* User */
                  case 15 : ret = aupl_logi(pd);           break; /* User */
                  case 16 : ret = slget_logi(pd);          break; /* User */
                  case 17 : ret = midget_logi(pd);         break; /* User */
                  case 18 : ret = midremove_logi(pd);      break; /* User */
                  case 19 : ret = midset_logi(pd);         break; /* User */
                  /* 20 = 'NOT' */

                  default : ret = socket_send(gd.ms.sk[pd->rec.fdno],
                                   "500 NG Illegal method.\n", (pd->life.life_int + pd->life.life_los + 15));
                       #ifdef LOG_OUT
                       safe_pthread_mutex_lock(&con_mutex);
                       log_out(gd.fp.con_fd, pd->rec.user,
                         "<Not>", "Illegal method.", " ", (unsigned char *)gd.ms.inip_addr[pd->rec.fdno]);
                       pthread_mutex_unlock(&con_mutex);
                       #endif
                       break;
                  }
               }
            }
         } else {
             ret = socket_send(gd.ms.sk[pd->rec.fdno], "500 NG data size\n", (pd->life.life_int + pd->life.life_los + 15));
                }
      }
   }
else {
   ret = socket_send(gd.ms.sk[pd->rec.fdno], "500 NG No data!\n", (pd->life.life_int + pd->life.life_los + 15));
   #ifdef LOG_OUT
   safe_pthread_mutex_lock(&con_mutex);
   log_out(gd.fp.con_fd, pd->rec.user,
    "<Not>", "prease check send data.", " ", (unsigned char *)gd.ms.inip_addr[pd->rec.fdno]);
   pthread_mutex_unlock(&con_mutex);
   #endif
   }

free(swap);

return(ret);
}






