bugfixed command parsing, DISPLAY -> USE_NCURSES
[my-code/ivac.git] / src / ivac.c
1 /* ivac.c -- main ivac file
2  *
3  * author: hackbard@hackdaworld.dyndns.org
4  *         frank.zirkelbach@physik.uni-augsburg.de
5  *
6  * Copyright (C) 2004 Frank Zirkelbach
7  *
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.
12  *
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.
17  *
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
21  *
22  */
23
24 // #define USE_NCURSES
25
26 #include "ivac.h"
27
28 int main(int argc,char **argv) {
29
30   /* TESTING BY NOW */
31
32   t_ivac ivac;
33   int i;
34
35   if(argc!=2) {
36     printf("\nusage: %s <username>\n\n",argv[0]);
37     return ERROR;
38   }
39
40   /* set username */
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);
44
45   /* set capabilities (futur: set by check routines) */
46   ivac.g_cap=NETWORK;
47   ivac.av_cap=AUDIO|VIDEO|DUPLEX;
48
49   /* set event timeout */
50   ivac.event.timeout.tv_sec=IVAC_S_SEC;
51   ivac.event.timeout.tv_usec=IVAC_S_USEC;
52   /* event init */
53   event_init(&(ivac.event));
54
55   /* input init */
56   ivac.input.mode=CONTENT_BUFFER;
57   input_init(&(ivac.input));
58
59   /* set listen port (futur: read from config or entered later) */
60   network_set_listen_port(&(ivac.net),IVAC_LISTEN_PORT);
61   /* network init */
62   if(network_init(&(ivac.net))==N_ERROR) {
63     printf("[ivac] use 'fuser -n tcp %d' to determine the process to kill!\n",
64            ivac.net.l_port);
65     ivac_shutdown(&ivac);
66     return ERROR;
67   }
68
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);
72
73   /* display init */
74   display_init(&(ivac.display));
75
76   /* display */
77   ivac_display(&(ivac.display));
78
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);
81
82   // ivac_shutdown(&ivac);
83
84   return SUCCESS;
85 }
86
87 int ivac_shutdown(t_ivac *ivac) {
88
89   network_shutdown(&(ivac->net));
90   input_shutdown(&(ivac->input));
91   event_stop(&(ivac->event));
92 #ifdef USE_NCURSES
93   display_shutdown(&(ivac->display));
94 #endif
95
96   return SUCCESS;
97 }
98
99 int ivac_send_info(int channel,t_ivac *ivac) {
100
101   char data[SEND_N_MAX];
102   int size;
103
104   size=strlen(ivac->username);
105
106   data[0]=IVAC_SEND_NAME;
107   data[1]=size;
108   strncpy(data+2,ivac->username,size);
109   size+=2;
110
111   data[size]=IVAC_SEND_G_CAP;
112   data[size+1]=1;
113   data[size+2]=ivac->g_cap;
114   size+=3;
115
116   data[size]=IVAC_SEND_AV_CAP;
117   data[size+1]=2;
118   data[size+2]=(ivac->av_cap)>>8;
119   data[size+3]=(ivac->av_cap)&0xff;
120   size+=4;
121
122   if(network_send(ivac->net.connection[channel].fd,data,size)==N_ERROR) {
123     puts("[ivac] ivac_send_info failed");
124     return ERROR;
125   }
126
127   return SUCCESS;
128 }
129
130 int ivac_receive_info(int channel,t_ivac *ivac) {
131
132   char data[SEND_N_MAX];
133   int count,length;
134
135   count=0;
136
137   if((length=network_receive(ivac->net.connection[channel].fd,
138                              data,SEND_N_MAX))==N_ERROR) {
139     puts("[ivac] ivac_receive_info failed");
140     return ERROR;
141   }
142
143   while(length-count) {
144     switch(data[count]) {
145       case IVAC_SEND_NAME:
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);
149         break;
150       case IVAC_SEND_G_CAP:
151         ivac->challenger[channel].g_cap=data[count+2];
152         count+=3;
153         break;
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];
157         count+=4;
158         break;
159       default:
160         puts("[ivac] ivac_receive_info, unknown character");
161         return ERROR;
162         break;
163     }
164   }
165
166   return SUCCESS;
167 }
168
169 int ivac_event_cb(t_event *event,void *ptr) {
170  
171   t_ivac *ivac;
172   int channel;
173
174   ivac=(t_ivac *)ptr;
175
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);
181   }
182
183   /* receive info */
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);
188
189   /* user interaction */
190   if(FD_ISSET(0,&(event->rfds)))
191     input_get_event(&(ivac->input),ivac_parse_command,ivac);
192
193   /* display ivac gui */
194 #ifdef USE_NCURSES
195   ivac_display_content(ivac);
196 #endif
197     
198   return SUCCESS;
199 }
200
201 int ivac_regular_cb(t_event *event,void *ptr) {
202
203   /* usual jobs like audio & video transmit ... */
204
205   return SUCCESS;
206 }
207
208 int ivac_parse_command(t_input *input,void *ptr) {
209
210   t_ivac *ivac;
211   int i,j,k;
212   int len;
213   int channel;
214   char *data,valid;
215   char c_str[IVAC_CONSOLE_STRING_LEN];
216   char arg[IVAC_ARG_COUNT][IVAC_ARG_LEN];
217
218   ivac=(t_ivac *)ptr;
219   data=input->content;
220   valid=0;
221
222   printf("%c",data[input->c_count-1]);
223   fflush(NULL);
224
225   /* parse command routines */
226
227   if(data[input->c_count-1]=='\n') {
228
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);
232
233     /* get args */
234
235     len=0;
236     while(data[len]!='\n') len++;
237     i=0; j=0;
238     while((i<len) && (j<IVAC_ARG_COUNT)) {
239       k=0;
240       while((data[i+k]!=' ')&&(data[i+k]!='\n')) {
241         arg[j][k]=data[i+k];
242         k++;
243       }
244       arg[j][k]='\0';
245       j++;
246       /* skip all ' ' */
247       while(data[i+k]==' ') k++;
248       i+=k;
249     }
250
251     /* parse command  aka arg[0] */
252     if(!(strncmp(arg[0],"quit",4))) {
253       valid=1;
254       sprintf(c_str,"ivac shutdown ...");
255       ivac_shutdown(ivac);
256     }
257     if(!(strncmp(arg[0],"set",3))) {
258       valid=1;
259       if((arg[1][0]>='0')&&(arg[1][0]<='9')) channel=atoi(arg[1]);
260       else channel=-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);
264       }
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);
268         }
269         else {
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;
274         }
275       } else snprintf(c_str,IVAC_CONSOLE_STRING_LEN,"unknown argument: '%s'",
276                       arg[1]);
277     }
278     if(!(strncmp(arg[0],"connect",7))) {
279       valid=1;
280       if((arg[1][0]>='0')&&(arg[1][0]<='9')) channel=atoi(arg[1]);
281       else channel=-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);
285         else {
286           if(ivac->net.connection[channel].status&C_IN_USE)
287             sprintf(c_str,"channel %02d: connection in use",channel);
288           else {
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);
292           }
293         }
294       }
295       else sprintf(c_str,"invalid argument: '%s'",arg[1]);
296     } 
297     if(!(strncmp(arg[0],"close",5))) {
298       valid=1;
299       if((arg[1][0]>='0')&&(arg[1][0]<='9')) channel=atoi(arg[1]);
300       else channel=-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);
304         else {
305           ivac->net.connection[channel].status|=C_HANGUP;
306           sprintf(c_str,"channel %02d: connection closed",channel);
307         }
308       }
309       else sprintf(c_str,"invalid argument: '%s'",arg[1]);
310     }
311     if(!(strncmp(arg[0],"select",6))) {
312       valid=1;
313       if((arg[1][0]>='0')&&(arg[1][0]<='9')) channel=atoi(arg[1]);
314       else channel=-1;
315       if((channel>=0)&&(channel<MAX_CONNECTIONS)) {
316         ivac->net.sendmask|=(1<<channel);
317         sprintf(c_str,"selected channel %d",channel);
318       }
319       else if(arg[1][0]='*') {
320         ivac->net.sendmask=0xff;
321         strcpy(c_str,"selected all channels");
322       }
323       else sprintf(c_str,"invalid argument: '%s'",arg[1]);
324     }
325     if(!(strncmp(arg[0],"deselect",8))) {
326       valid=1;
327       if((arg[1][0]>='0')&&(arg[1][0]<='9')) channel=atoi(arg[1]);
328       else channel=-1;
329       if((channel>=0)&&(channel<MAX_CONNECTIONS)) {
330         ivac->net.sendmask&=(~(1<<channel));
331         sprintf(c_str,"deselected channel %d",channel);
332       }
333       else if(arg[1][0]='*') {
334         ivac->net.sendmask=0;
335         strcpy(c_str,"deselected all channels");
336       }
337       else sprintf(c_str,"invalid argument: '%s'",arg[1]);
338     }
339
340     if(!valid)
341       snprintf(c_str,IVAC_CONSOLE_STRING_LEN,"unknown command: '%s'",arg[0]);
342
343     ivac_add_to_monitor(ivac,c_str);
344       
345
346     /* delete content buffer + reset counter */
347     memset(input->content,0,input->c_count-1);
348     input->c_count=0;
349
350   }
351
352   return SUCCESS;
353 }
354
355 int ivac_display_head(t_display *display) {
356
357 #ifdef USE_NCURSES
358   int x,y;
359
360   move(0,0);
361   for(x=0;x<display->max_x;x++) addch('#');
362   mvaddstr(1,0,"##");
363   mvaddstr(1,(display->max_x-4)/2-4,"- ivac -");
364   mvaddstr(1,(display->max_x-2),"##");
365   move(2,0);
366   for(x=0;x<display->max_x;x++) addch('#');
367   refresh();
368 #else
369   puts("#########################################################");
370   puts("##### ivac - -  Copyright (C) 2004 Frank Zirkelbach #####");
371   puts("#########################################################");
372 #endif
373
374   return SUCCESS;
375 }
376
377 int ivac_display_box(t_display *display) {
378  
379 #ifdef USE_NCURSES 
380   int x,y;
381
382   for(y=IVAC_PROMPT_LEN;y<display->max_y-IVAC_PROMPT_LEN;y++) {
383     mvaddch(y,0,'#');
384     mvaddch(y,display->max_x-1,'#');
385   }
386 #endif
387
388   return SUCCESS;
389 }
390
391 int ivac_display_box_content(t_ivac *ivac) {
392
393
394   return SUCCESS;
395 }
396
397 int ivac_display_console(t_display *display) {
398                                              
399   int x,y;
400          
401   move(display->max_y-IVAC_CONSOLE_LEN-IVAC_PROMPT_LEN-1,0);
402   for(x=0;x<display->max_x;x++) addch('#');                         
403                                           
404   return SUCCESS;
405 }
406
407 int ivac_display_console_content(t_ivac *ivac) {
408
409   int x,y;
410   int len;
411
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);
415     for(x=0;x<len;x++)
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(' ');
419   }
420   refresh();
421
422   return SUCCESS;
423 }
424
425 int ivac_display_prompt(t_display *display) {
426
427   int x,y;
428
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('#');
435   refresh();
436
437   return SUCCESS;
438 }
439
440 int ivac_display_prompt_content(t_ivac *ivac) {
441
442   int x,y;
443
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(' ');
448   }
449
450   for(x=0;x<ivac->input.c_count;x++)
451     mvaddch(ivac->display.max_y-2,x+12,ivac->input.content[x]);
452   refresh();
453
454   return SUCCESS;
455 }
456
457 int ivac_display(t_display *display) {
458
459   int x,y;
460
461   /* display head */
462   ivac_display_head(display);
463
464   /* display box */
465   ivac_display_box(display);
466
467   /* display console */
468   ivac_display_console(display);
469
470   /* display command prompt */
471   ivac_display_prompt(display);
472
473   return SUCCESS;
474 }
475
476 int ivac_display_content(t_ivac *ivac) {
477
478   /* display box content */
479   ivac_display_box_content(ivac);
480
481   /* display console content */
482   ivac_display_console_content(ivac);
483
484   /* display prompt content */
485   ivac_display_prompt_content(ivac);
486
487   return SUCCESS;
488 }
489
490 int ivac_add_to_monitor(t_ivac *ivac,char *msg) {
491
492   int i;
493
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);
497
498   for(i=0;i<IVAC_CONSOLE_LEN;i++)
499     printf("[ivac] console line %d: %s\n",i,ivac->console[i]);
500 #ifdef USE_NCURSES
501   ivac_display_console_content(ivac);
502 #endif
503
504   return SUCCESS;
505 }