1 /* ivac.c -- main ivac file
3 * author: hackbard@hackdaworld.dyndns.org
4 * frank.zirkelbach@physik.uni-augsburg.de
6 * Copyright (C) 2004 Frank Zirkelbach
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 // #define USE_NCURSES
28 int main(int argc,char **argv) {
36 printf("\nusage: %s <username>\n\n",argv[0]);
41 strncpy(ivac.username,argv[1],CHAR_USERNAME);
42 for(i=0;i<IVAC_CONSOLE_LEN;i++)
43 memset(ivac.console[i],0,IVAC_CONSOLE_STRING_LEN);
45 /* set capabilities (futur: set by check routines) */
47 ivac.av_cap=AUDIO|VIDEO|DUPLEX;
49 /* set event timeout */
50 ivac.event.timeout.tv_sec=IVAC_S_SEC;
51 ivac.event.timeout.tv_usec=IVAC_S_USEC;
53 event_init(&(ivac.event));
56 ivac.input.mode=CONTENT_BUFFER;
57 input_init(&(ivac.input));
59 /* set listen port (futur: read from config or entered later) */
60 network_set_listen_port(&(ivac.net),IVAC_LISTEN_PORT);
62 if(network_init(&(ivac.net))==N_ERROR) {
63 printf("[ivac] use 'fuser -n tcp %d' to determine the process to kill!\n",
69 /* add listening port + stdin to (read) event system */
70 event_math(ivac.net.l_fd,&(ivac.event),READ,ADD);
71 event_math(0,&(ivac.event),READ,ADD);
74 display_init(&(ivac.display));
77 ivac_display(&(ivac.display));
79 /* start event system - callbacks used: ivac_event_cb + ivac_regular_cb */
80 event_start(&(ivac.event),(void *)&ivac,ivac_event_cb,ivac_regular_cb);
82 // ivac_shutdown(&ivac);
87 int ivac_shutdown(t_ivac *ivac) {
89 network_shutdown(&(ivac->net));
90 input_shutdown(&(ivac->input));
91 event_stop(&(ivac->event));
93 display_shutdown(&(ivac->display));
99 int ivac_send_info(int channel,t_ivac *ivac) {
101 char data[SEND_N_MAX];
104 size=strlen(ivac->username);
106 data[0]=IVAC_SEND_NAME;
108 strncpy(data+2,ivac->username,size);
111 data[size]=IVAC_SEND_G_CAP;
113 data[size+2]=ivac->g_cap;
116 data[size]=IVAC_SEND_AV_CAP;
118 data[size+2]=(ivac->av_cap)>>8;
119 data[size+3]=(ivac->av_cap)&0xff;
122 if(network_send(ivac->net.connection[channel].fd,data,size)==N_ERROR) {
123 puts("[ivac] ivac_send_info failed");
130 int ivac_receive_info(int channel,t_ivac *ivac) {
132 char data[SEND_N_MAX];
137 if((length=network_receive(ivac->net.connection[channel].fd,
138 data,SEND_N_MAX))==N_ERROR) {
139 puts("[ivac] ivac_receive_info failed");
143 while(length-count) {
144 switch(data[count]) {
146 strncpy(ivac->challenger[channel].name,data+count+2,data[count+1]);
147 ivac->challenger[channel].name[data[count+1]]='\0';
148 count+=(data[count+1]+2);
150 case IVAC_SEND_G_CAP:
151 ivac->challenger[channel].g_cap=data[count+2];
154 case IVAC_SEND_AV_CAP:
155 ivac->challenger[channel].av_cap=data[count+2]<<8;
156 ivac->challenger[channel].av_cap|=data[count+3];
160 puts("[ivac] ivac_receive_info, unknown character");
169 int ivac_event_cb(t_event *event,void *ptr) {
176 if(FD_ISSET(ivac->net.l_fd,&(event->rfds))) {
177 /* manage incoming + send info */
178 channel=network_manage_incoming(&(ivac->net));
179 event_math(ivac->net.connection[channel].fd,event,READ,ADD);
180 ivac_send_info(channel,ivac);
184 for(channel=0;channel<MAX_CONNECTIONS;channel++)
185 if(ivac->net.connection[channel].status&C_ESTABL)
186 if(FD_ISSET(ivac->net.connection[channel].fd,&(event->rfds)))
187 ivac_receive_info(channel,ivac);
189 /* user interaction */
190 if(FD_ISSET(0,&(event->rfds)))
191 input_get_event(&(ivac->input),ivac_parse_command,ivac);
193 /* display ivac gui */
195 ivac_display_content(ivac);
201 int ivac_regular_cb(t_event *event,void *ptr) {
203 /* usual jobs like audio & video transmit ... */
208 int ivac_parse_command(t_input *input,void *ptr) {
215 char c_str[IVAC_CONSOLE_STRING_LEN];
216 char arg[IVAC_ARG_COUNT][IVAC_ARG_LEN];
222 printf("%c",data[input->c_count-1]);
225 /* parse command routines */
227 if(data[input->c_count-1]=='\n') {
229 /* delete console string + args */
230 memset(c_str,0,IVAC_CONSOLE_STRING_LEN);
231 for(j=0;j<IVAC_ARG_COUNT;j++) memset(arg[j],0,IVAC_ARG_LEN);
236 while(data[len]!='\n') len++;
238 while((i<len) && (j<IVAC_ARG_COUNT)) {
240 while((data[i+k]!=' ')&&(data[i+k]!='\n')) {
247 while(data[i+k]==' ') k++;
251 /* parse command aka arg[0] */
252 if(!(strncmp(arg[0],"quit",4))) {
254 sprintf(c_str,"ivac shutdown ...");
257 if(!(strncmp(arg[0],"set",3))) {
259 if((arg[1][0]>='0')&&(arg[1][0]<='9')) channel=atoi(arg[1]);
261 if(!(strncmp(arg[1],"name",4))) {
262 strncpy(ivac->username,arg[2],CHAR_USERNAME);
263 sprintf(c_str,"changed username to %s",ivac->username);
265 else if((channel>=0)&&(channel<MAX_CONNECTIONS)) {
266 if(ivac->net.connection[channel].status&C_IN_USE) {
267 sprintf(c_str,"channel %02d: connection in use",channel);
270 strncpy(ivac->net.connection[channel].ip,arg[2],IP_DIGITS);
271 ivac->net.connection[channel].port=atoi(arg[3]);
272 sprintf(c_str,"channel %02d: set connection info",channel);
273 ivac->net.connection[channel].status|=C_INFO_A;
275 } else snprintf(c_str,IVAC_CONSOLE_STRING_LEN,"unknown argument: '%s'",
278 if(!(strncmp(arg[0],"connect",7))) {
280 if((arg[1][0]>='0')&&(arg[1][0]<='9')) channel=atoi(arg[1]);
282 if((channel>=0)&&(channel<MAX_CONNECTIONS)) {
283 if(!(ivac->net.connection[channel].status&C_INFO_A))
284 sprintf(c_str,"channel %02d: channel not configured",channel);
286 if(ivac->net.connection[channel].status&C_IN_USE)
287 sprintf(c_str,"channel %02d: connection in use",channel);
289 sprintf(c_str,"channel %02d: trying to connect to %s:%d",channel,
290 ivac->net.connection[channel].ip,
291 ivac->net.connection[channel].port);
295 else sprintf(c_str,"invalid argument: '%s'",arg[1]);
297 if(!(strncmp(arg[0],"close",5))) {
299 if((arg[1][0]>='0')&&(arg[1][0]<='9')) channel=atoi(arg[1]);
301 if((channel>=0)&&(channel<MAX_CONNECTIONS)) {
302 if(!(ivac->net.connection[channel].status&C_ESTABL))
303 sprintf(c_str,"channel %02d: no active connection",channel);
305 ivac->net.connection[channel].status|=C_HANGUP;
306 sprintf(c_str,"channel %02d: connection closed",channel);
309 else sprintf(c_str,"invalid argument: '%s'",arg[1]);
311 if(!(strncmp(arg[0],"select",6))) {
313 if((arg[1][0]>='0')&&(arg[1][0]<='9')) channel=atoi(arg[1]);
315 if((channel>=0)&&(channel<MAX_CONNECTIONS)) {
316 ivac->net.sendmask|=(1<<channel);
317 sprintf(c_str,"selected channel %d",channel);
319 else if(arg[1][0]='*') {
320 ivac->net.sendmask=0xff;
321 strcpy(c_str,"selected all channels");
323 else sprintf(c_str,"invalid argument: '%s'",arg[1]);
325 if(!(strncmp(arg[0],"deselect",8))) {
327 if((arg[1][0]>='0')&&(arg[1][0]<='9')) channel=atoi(arg[1]);
329 if((channel>=0)&&(channel<MAX_CONNECTIONS)) {
330 ivac->net.sendmask&=(~(1<<channel));
331 sprintf(c_str,"deselected channel %d",channel);
333 else if(arg[1][0]='*') {
334 ivac->net.sendmask=0;
335 strcpy(c_str,"deselected all channels");
337 else sprintf(c_str,"invalid argument: '%s'",arg[1]);
341 snprintf(c_str,IVAC_CONSOLE_STRING_LEN,"unknown command: '%s'",arg[0]);
343 ivac_add_to_monitor(ivac,c_str);
346 /* delete content buffer + reset counter */
347 memset(input->content,0,input->c_count-1);
355 int ivac_display_head(t_display *display) {
361 for(x=0;x<display->max_x;x++) addch('#');
363 mvaddstr(1,(display->max_x-4)/2-4,"- ivac -");
364 mvaddstr(1,(display->max_x-2),"##");
366 for(x=0;x<display->max_x;x++) addch('#');
369 puts("#########################################################");
370 puts("##### ivac - - Copyright (C) 2004 Frank Zirkelbach #####");
371 puts("#########################################################");
377 int ivac_display_box(t_display *display) {
382 for(y=IVAC_PROMPT_LEN;y<display->max_y-IVAC_PROMPT_LEN;y++) {
384 mvaddch(y,display->max_x-1,'#');
391 int ivac_display_box_content(t_ivac *ivac) {
397 int ivac_display_console(t_display *display) {
401 move(display->max_y-IVAC_CONSOLE_LEN-IVAC_PROMPT_LEN-1,0);
402 for(x=0;x<display->max_x;x++) addch('#');
407 int ivac_display_console_content(t_ivac *ivac) {
412 for(y=0;y<IVAC_CONSOLE_LEN;y++) {
413 len=strlen(ivac->console[y]);
414 move(ivac->display.max_y-IVAC_CONSOLE_LEN-IVAC_PROMPT_LEN+y,2);
416 addch(((ivac->console[y][x]>' ')||(ivac->console[y][x]<='~'))
417 ?ivac->console[y][x]:' ');
418 for(x=len;x<IVAC_CONSOLE_STRING_LEN-4;x++) addch(' ');
425 int ivac_display_prompt(t_display *display) {
429 move(display->max_y-3,0);
430 for(x=0;x<display->max_x;x++) addch('#');
431 mvaddstr(display->max_y-2,0,"## command: ");
432 mvaddstr(display->max_y-2,display->max_x-2,"##");
433 move(display->max_y-1,0);
434 for(x=0;x<display->max_x;x++) addch('#');
440 int ivac_display_prompt_content(t_ivac *ivac) {
444 /* delete old command */
445 if(ivac->input.c_count==0) {
446 move(ivac->display.max_y-2,12);
447 for(x=12;x<ivac->display.max_x-1;x++) addch(' ');
450 for(x=0;x<ivac->input.c_count;x++)
451 mvaddch(ivac->display.max_y-2,x+12,ivac->input.content[x]);
457 int ivac_display(t_display *display) {
462 ivac_display_head(display);
465 ivac_display_box(display);
467 /* display console */
468 ivac_display_console(display);
470 /* display command prompt */
471 ivac_display_prompt(display);
476 int ivac_display_content(t_ivac *ivac) {
478 /* display box content */
479 ivac_display_box_content(ivac);
481 /* display console content */
482 ivac_display_console_content(ivac);
484 /* display prompt content */
485 ivac_display_prompt_content(ivac);
490 int ivac_add_to_monitor(t_ivac *ivac,char *msg) {
494 for(i=0;i<IVAC_CONSOLE_LEN-1;i++)
495 memcpy(ivac->console[i],ivac->console[i+1],IVAC_CONSOLE_STRING_LEN);
496 memcpy(ivac->console[IVAC_CONSOLE_LEN-1],msg,IVAC_CONSOLE_STRING_LEN);
498 for(i=0;i<IVAC_CONSOLE_LEN;i++)
499 printf("[ivac] console line %d: %s\n",i,ivac->console[i]);
501 ivac_display_console_content(ivac);