X-Git-Url: https://hackdaworld.org/gitweb/?a=blobdiff_plain;f=src%2Fivac.c;h=8b8acbc50923ad9b58fd6340546a838479455c72;hb=2fb65b9beceb0945b593e28332507aa1b7c3fd90;hp=c567f280d8c11b4e2ed1bb420517ebbfba54d0ad;hpb=4546108a81317af1135683e85b9340715e585339;p=my-code%2Fivac.git diff --git a/src/ivac.c b/src/ivac.c index c567f28..8b8acbc 100644 --- a/src/ivac.c +++ b/src/ivac.c @@ -21,6 +21,8 @@ * */ +// #define DISPLAY + #include "ivac.h" int main(int argc,char **argv) { @@ -28,33 +30,138 @@ int main(int argc,char **argv) { /* TESTING BY NOW */ t_ivac ivac; + int i; /* set username (futur: read from config or entered later) */ strcpy(ivac.username,"hackbard"); + for(i=0;inet)); + input_shutdown(&(ivac->input)); + event_stop(&(ivac->event)); +#ifdef DISPLAY + display_shutdown(&(ivac->display)); +#endif + + return SUCCESS; +} + +int ivac_send_info(int channel,t_ivac *ivac) { + + char data[SEND_N_MAX]; + int size; + + size=strlen(ivac->username); + + data[0]=IVAC_SEND_NAME; + data[1]=size; + strncpy(data+2,ivac->username,size); + size+=2; + + data[size]=IVAC_SEND_G_CAP; + data[size+1]=1; + data[size+2]=ivac->g_cap; + size+=3; + + data[size]=IVAC_SEND_AV_CAP; + data[size+1]=2; + data[size+2]=(ivac->av_cap)>>8; + data[size+3]=(ivac->av_cap)&0xff; + size+=4; + + if(network_send(ivac->net.connection[channel].fd,data,size)==N_ERROR) { + puts("[ivac] ivac_send_info failed"); + return ERROR; + } + + return SUCCESS; +} + +int ivac_receive_info(int channel,t_ivac *ivac) { + + char data[SEND_N_MAX]; + int count,length; + + count=0; + + if((length=network_receive(ivac->net.connection[channel].fd, + data,SEND_N_MAX))==N_ERROR) { + puts("[ivac] ivac_receive_info failed"); + return ERROR; + } + + while(length-count) { + switch(data[count]) { + case IVAC_SEND_NAME: + strncpy(ivac->challenger[channel].name,data+count+2,data[count+1]); + ivac->challenger[channel].name[data[count+1]]='\0'; + count+=(data[count+1]+2); + break; + case IVAC_SEND_G_CAP: + ivac->challenger[channel].g_cap=data[count+2]; + count+=3; + break; + case IVAC_SEND_AV_CAP: + ivac->challenger[channel].av_cap=data[count+2]<<8; + ivac->challenger[channel].av_cap|=data[count+3]; + count+=4; + break; + default: + puts("[ivac] ivac_receive_info, unknown character"); + return ERROR; + break; + } + } + return SUCCESS; } @@ -69,17 +176,23 @@ int ivac_event_cb(t_event *event,void *ptr) { /* manage incoming + send info */ channel=network_manage_incoming(&(ivac->net)); event_math(ivac->net.connection[channel].fd,event,READ,ADD); - send_info(channel,&(ivac->net),ivac->username); + ivac_send_info(channel,ivac); } /* receive info */ for(channel=0;channelnet.connection[channel].status&C_ESTABL) if(FD_ISSET(ivac->net.connection[channel].fd,&(event->rfds))) - receive_info(channel,&(ivac->net)); + ivac_receive_info(channel,ivac); /* user interaction */ - if(FD_ISSET(0,&(event->rfds))) input_get_char(ivac); + if(FD_ISSET(0,&(event->rfds))) + input_get_event(&(ivac->input),ivac_parse_command,ivac); + + /* display ivac gui */ +#ifdef DISPLAY + ivac_display_content(ivac); +#endif return SUCCESS; } @@ -90,3 +203,294 @@ int ivac_regular_cb(t_event *event,void *ptr) { return SUCCESS; } + +int ivac_parse_command(t_input *input,void *ptr) { + + t_ivac *ivac; + int i,j,k; + int len; + int channel; + char *data,valid; + char c_str[IVAC_CONSOLE_STRING_LEN]; + char arg[IVAC_ARG_COUNT][IVAC_ARG_LEN]; + + ivac=(t_ivac *)ptr; + data=input->content; + valid=0; + + printf("%c",data[input->c_count-1]); + fflush(NULL); + + /* parse command routines */ + + if(data[input->c_count-1]=='\n') { + + /* delete console string + args */ + memset(c_str,0,IVAC_CONSOLE_STRING_LEN); + for(j=0;j='0')&&(arg[1][0]<='9')) channel=atoi(arg[1]); + else channel=-1; + if(!(strncmp(arg[1],"name",4))) { + strncpy(ivac->username,arg[2],CHAR_USERNAME); + sprintf(c_str,"changed username to %s",ivac->username); + } + else if((channel>=0)&&(channelnet.connection[channel].status&C_IN_USE) { + sprintf(c_str,"channel %02d: connection in use",channel); + } + else { + strncpy(ivac->net.connection[channel].ip,arg[2],IP_DIGITS); + ivac->net.connection[channel].port=atoi(arg[3]); + sprintf(c_str,"channel %02d: set connection info",channel); + ivac->net.connection[channel].status|=C_INFO_A; + } + } else snprintf(c_str,IVAC_CONSOLE_STRING_LEN,"unknown argument: '%s'", + arg[1]); + } + if(!(strncmp(arg[0],"connect",7))) { + valid=1; + if((arg[1][0]>='0')&&(arg[1][0]<='9')) channel=atoi(arg[1]); + else channel=-1; + if((channel>=0)&&(channelnet.connection[channel].status&C_INFO_A) + sprintf(c_str,"channel %02d: channel not configured",channel); + else { + if(ivac->net.connection[channel].status&C_IN_USE) + sprintf(c_str,"channel %02d: connection in use",channel); + else { + sprintf(c_str,"channel %02d: trying to connect to %s:%d",channel, + ivac->net.connection[channel].ip, + ivac->net.connection[channel].port); + } + } + } + else sprintf(c_str,"invalid argument: '%s'",arg[1]); + } + if(!(strncmp(arg[0],"close",5))) { + valid=1; + if((arg[1][0]>='0')&&(arg[1][0]<='9')) channel=atoi(arg[1]); + else channel=-1; + if((channel>=0)&&(channelnet.connection[channel].status&C_ESTABL) + sprintf(c_str,"channel %02d: no active connection",channel); + else { + ivac->net.connection[channel].status|=C_HANGUP; + sprintf(c_str,"channel %02d: connection closed",channel); + } + } + else sprintf(c_str,"invalid argument: '%s'",arg[1]); + } + if(!(strncmp(arg[0],"select",6))) { + valid=1; + if((arg[1][0]>='0')&&(arg[1][0]<='9')) channel=atoi(arg[1]); + else channel=-1; + if((channel>=0)&&(channelnet.sendmask|=(1<net.sendmask=0xff; + strcpy(c_str,"selected all channels"); + } + else sprintf(c_str,"invalid argument: '%s'",arg[1]); + } + if(!(strncmp(arg[0],"deselect",8))) { + valid=1; + if((arg[1][0]>='0')&&(arg[1][0]<='9')) channel=atoi(arg[1]); + else channel=-1; + if((channel>=0)&&(channelnet.sendmask&=(~(1<net.sendmask=0; + strcpy(c_str,"deselected all channels"); + } + else sprintf(c_str,"invalid argument: '%s'",arg[1]); + } + + if(!valid) + snprintf(c_str,IVAC_CONSOLE_STRING_LEN,"unknown command: '%s'",arg[0]); + + ivac_add_to_monitor(ivac,c_str); + + + /* delete content buffer + reset counter */ + memset(input->content,0,input->c_count-1); + input->c_count=0; + + } + + return SUCCESS; +} + +int ivac_display_head(t_display *display) { + + int x,y; + + move(0,0); + for(x=0;xmax_x;x++) addch('#'); + mvaddstr(1,0,"##"); + mvaddstr(1,(display->max_x-4)/2-4,"- ivac -"); + mvaddstr(1,(display->max_x-2),"##"); + move(2,0); + for(x=0;xmax_x;x++) addch('#'); + refresh(); + + return SUCCESS; +} + +int ivac_display_box(t_display *display) { + + int x,y; + + for(y=IVAC_PROMPT_LEN;ymax_y-IVAC_PROMPT_LEN;y++) { + mvaddch(y,0,'#'); + mvaddch(y,display->max_x-1,'#'); + } + + return SUCCESS; +} + +int ivac_display_box_content(t_ivac *ivac) { + + + return SUCCESS; +} + +int ivac_display_console(t_display *display) { + + int x,y; + + move(display->max_y-IVAC_CONSOLE_LEN-IVAC_PROMPT_LEN-1,0); + for(x=0;xmax_x;x++) addch('#'); + + return SUCCESS; +} + +int ivac_display_console_content(t_ivac *ivac) { + + int x,y; + int len; + + for(y=0;yconsole[y]); + move(ivac->display.max_y-IVAC_CONSOLE_LEN-IVAC_PROMPT_LEN+y,2); + for(x=0;xconsole[y][x]>' ')||(ivac->console[y][x]<='~')) + ?ivac->console[y][x]:' '); + for(x=len;xmax_y-3,0); + for(x=0;xmax_x;x++) addch('#'); + mvaddstr(display->max_y-2,0,"## command: "); + mvaddstr(display->max_y-2,display->max_x-2,"##"); + move(display->max_y-1,0); + for(x=0;xmax_x;x++) addch('#'); + refresh(); + + return SUCCESS; +} + +int ivac_display_prompt_content(t_ivac *ivac) { + + int x,y; + + /* delete old command */ + if(ivac->input.c_count==0) { + move(ivac->display.max_y-2,12); + for(x=12;xdisplay.max_x-1;x++) addch(' '); + } + + for(x=0;xinput.c_count;x++) + mvaddch(ivac->display.max_y-2,x+12,ivac->input.content[x]); + refresh(); + + return SUCCESS; +} + +int ivac_display(t_display *display) { + + int x,y; + + /* display head */ + ivac_display_head(display); + + /* display box */ + ivac_display_box(display); + + /* display console */ + ivac_display_console(display); + + /* display command prompt */ + ivac_display_prompt(display); + + return SUCCESS; +} + +int ivac_display_content(t_ivac *ivac) { + + /* display box content */ + ivac_display_box_content(ivac); + + /* display console content */ + ivac_display_console_content(ivac); + + /* display prompt content */ + ivac_display_prompt_content(ivac); + + return SUCCESS; +} + +int ivac_add_to_monitor(t_ivac *ivac,char *msg) { + + int i; + + for(i=0;iconsole[i],ivac->console[i+1],IVAC_CONSOLE_STRING_LEN); + memcpy(ivac->console[IVAC_CONSOLE_LEN-1],msg,IVAC_CONSOLE_STRING_LEN); + + for(i=0;iconsole[i]); +#ifdef DISPLAY + ivac_display_console_content(ivac); +#endif + + return SUCCESS; +}