bugfixes, temp. removed ncurses display funtions, fixed command parser.
[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 DISPLAY
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   /* set username (futur: read from config or entered later) */
36   strcpy(ivac.username,"hackbard");
37   for(i=0;i<IVAC_CONSOLE_LEN;i++)
38     memset(ivac.console[i],0,IVAC_CONSOLE_STRING_LEN);
39
40   /* set capabilities (futur: set by check routines) */
41   ivac.g_cap=NETWORK;
42   ivac.av_cap=AUDIO|VIDEO|DUPLEX;
43
44   /* set event timeout */
45   ivac.event.timeout.tv_sec=IVAC_S_SEC;
46   ivac.event.timeout.tv_usec=IVAC_S_USEC;
47   /* event init */
48   event_init(&(ivac.event));
49
50   /* input init */
51   ivac.input.mode=CONTENT_BUFFER;
52   input_init(&(ivac.input));
53
54   /* set listen port (futur: read from config or entered later) */
55   network_set_listen_port(&(ivac.net),IVAC_LISTEN_PORT);
56   /* network init */
57   if(network_init(&(ivac.net))==N_ERROR) {
58     printf("[ivac] use 'fuser -n tcp %d' to determine the process to kill!\n",
59            ivac.net.l_port);
60     ivac_shutdown(&ivac);
61     return ERROR;
62   }
63
64   /* add listening port + stdin to (read) event system */
65   event_math(ivac.net.l_fd,&(ivac.event),READ,ADD);
66   event_math(0,&(ivac.event),READ,ADD);
67
68   /* display init */
69 #ifdef DISPLAY
70   display_init(&(ivac.display));
71 #endif
72
73   /* display */
74 #ifdef DISPLAY
75   ivac_display(&(ivac.display));
76 #endif
77
78   /* start event system - callbacks used: ivac_event_cb + ivac_regular_cb */
79   event_start(&(ivac.event),(void *)&ivac,ivac_event_cb,ivac_regular_cb);
80
81   // ivac_shutdown(&ivac);
82
83   return SUCCESS;
84 }
85
86 int ivac_shutdown(t_ivac *ivac) {
87
88   network_shutdown(&(ivac->net));
89   input_shutdown(&(ivac->input));
90   event_stop(&(ivac->event));
91 #ifdef DISPLAY
92   display_shutdown(&(ivac->display));
93 #endif
94
95   return SUCCESS;
96 }
97
98 int ivac_send_info(int channel,t_ivac *ivac) {
99
100   char data[SEND_N_MAX];
101   int size;
102
103   size=strlen(ivac->username);
104
105   data[0]=IVAC_SEND_NAME;
106   data[1]=size;
107   strncpy(data+2,ivac->username,size);
108   size+=2;
109
110   data[size]=IVAC_SEND_G_CAP;
111   data[size+1]=1;
112   data[size+2]=ivac->g_cap;
113   size+=3;
114
115   data[size]=IVAC_SEND_AV_CAP;
116   data[size+1]=2;
117   data[size+2]=(ivac->av_cap)>>8;
118   data[size+3]=(ivac->av_cap)&0xff;
119   size+=4;
120
121   if(network_send(ivac->net.connection[channel].fd,data,size)==N_ERROR) {
122     puts("[ivac] ivac_send_info failed");
123     return ERROR;
124   }
125
126   return SUCCESS;
127 }
128
129 int ivac_receive_info(int channel,t_ivac *ivac) {
130
131   char data[SEND_N_MAX];
132   int count,length;
133
134   count=0;
135
136   if((length=network_receive(ivac->net.connection[channel].fd,
137                              data,SEND_N_MAX))==N_ERROR) {
138     puts("[ivac] ivac_receive_info failed");
139     return ERROR;
140   }
141
142   while(length-count) {
143     switch(data[count]) {
144       case IVAC_SEND_NAME:
145         strncpy(ivac->challenger[channel].name,data+count+2,data[count+1]);
146         ivac->challenger[channel].name[data[count+1]]='\0';
147         count+=(data[count+1]+2);
148         break;
149       case IVAC_SEND_G_CAP:
150         ivac->challenger[channel].g_cap=data[count+2];
151         count+=3;
152         break;
153       case IVAC_SEND_AV_CAP:
154         ivac->challenger[channel].av_cap=data[count+2]<<8;
155         ivac->challenger[channel].av_cap|=data[count+3];
156         count+=4;
157         break;
158       default:
159         puts("[ivac] ivac_receive_info, unknown character");
160         return ERROR;
161         break;
162     }
163   }
164
165   return SUCCESS;
166 }
167
168 int ivac_event_cb(t_event *event,void *ptr) {
169  
170   t_ivac *ivac;
171   int channel;
172
173   ivac=(t_ivac *)ptr;
174
175   if(FD_ISSET(ivac->net.l_fd,&(event->rfds))) {
176     /* manage incoming + send info */
177     channel=network_manage_incoming(&(ivac->net));
178     event_math(ivac->net.connection[channel].fd,event,READ,ADD);
179     ivac_send_info(channel,ivac);
180   }
181
182   /* receive info */
183   for(channel=0;channel<MAX_CONNECTIONS;channel++)
184     if(ivac->net.connection[channel].status&C_ESTABL)
185       if(FD_ISSET(ivac->net.connection[channel].fd,&(event->rfds)))
186         ivac_receive_info(channel,ivac);
187
188   /* user interaction */
189   if(FD_ISSET(0,&(event->rfds)))
190     input_get_event(&(ivac->input),ivac_parse_command,ivac);
191
192   /* display ivac gui */
193 #ifdef DISPLAY
194   ivac_display_content(ivac);
195 #endif
196     
197   return SUCCESS;
198 }
199
200 int ivac_regular_cb(t_event *event,void *ptr) {
201
202   /* usual jobs like audio & video transmit ... */
203
204   return SUCCESS;
205 }
206
207 int ivac_parse_command(t_input *input,void *ptr) {
208
209   t_ivac *ivac;
210   int i,j,k;
211   int len;
212   int channel;
213   char *data,valid;
214   char c_str[IVAC_CONSOLE_STRING_LEN];
215   char arg[IVAC_ARG_COUNT][IVAC_ARG_LEN];
216
217   ivac=(t_ivac *)ptr;
218   data=input->content;
219   valid=0;
220
221   printf("%c",data[input->c_count-1]);
222   fflush(NULL);
223
224   /* parse command routines */
225
226   if(data[input->c_count-1]=='\n') {
227
228     /* delete console string + args */
229     memset(c_str,0,IVAC_CONSOLE_STRING_LEN);
230     for(j=0;j<IVAC_ARG_COUNT;j++) memset(arg[j],0,IVAC_ARG_LEN);
231
232     /* get args */
233
234     len=0;
235     while(data[len]!='\n') len++;
236     i=0; j=0;
237     while((i<len) && (j<IVAC_ARG_COUNT)) {
238       k=0;
239       while((data[i+k]!=' ')&&(data[i+k]!='\n')) {
240         arg[j][k]=data[i+k];
241         k++;
242       }
243       arg[j][k]='\0';
244       j++;
245       /* skip all ' ' */
246       while(data[i+k]==' ') k++;
247       i+=k;
248     }
249
250     /* parse command  aka arg[0] */
251     if(!(strncmp(arg[0],"quit",4))) {
252       valid=1;
253       sprintf(c_str,"ivac shutdown ...");
254       ivac_shutdown(ivac);
255     }
256     if(!(strncmp(arg[0],"set",3))) {
257       valid=1;
258       if((arg[1][0]>='0')&&(arg[1][0]<='9')) channel=atoi(arg[1]);
259       else channel=-1;
260       if(!(strncmp(arg[1],"name",4))) {
261         strncpy(ivac->username,arg[2],CHAR_USERNAME);
262         sprintf(c_str,"changed username to %s",ivac->username);
263       }
264       else if((channel>=0)&&(channel<MAX_CONNECTIONS)) {
265         if(ivac->net.connection[channel].status&C_IN_USE) {
266           sprintf(c_str,"channel %02d: connection in use",channel);
267         }
268         else {
269           strncpy(ivac->net.connection[channel].ip,arg[2],IP_DIGITS);
270           ivac->net.connection[channel].port=atoi(arg[3]);
271           sprintf(c_str,"channel %02d: set connection info",channel);
272           ivac->net.connection[channel].status|=C_INFO_A;
273         }
274       } else snprintf(c_str,IVAC_CONSOLE_STRING_LEN,"unknown argument: '%s'",
275                       arg[1]);
276     }
277     if(!(strncmp(arg[0],"connect",7))) {
278       valid=1;
279       if((arg[1][0]>='0')&&(arg[1][0]<='9')) channel=atoi(arg[1]);
280       else channel=-1;
281       if((channel>=0)&&(channel<MAX_CONNECTIONS)) {
282         if(!ivac->net.connection[channel].status&C_INFO_A)
283           sprintf(c_str,"channel %02d: channel not configured",channel);
284         else {
285           if(ivac->net.connection[channel].status&C_IN_USE)
286             sprintf(c_str,"channel %02d: connection in use",channel);
287           else {
288             sprintf(c_str,"channel %02d: trying to connect to %s:%d",channel,
289                     ivac->net.connection[channel].ip,
290                     ivac->net.connection[channel].port);
291           }
292         }
293       }
294       else sprintf(c_str,"invalid argument: '%s'",arg[1]);
295     } 
296     if(!(strncmp(arg[0],"close",5))) {
297       valid=1;
298       if((arg[1][0]>='0')&&(arg[1][0]<='9')) channel=atoi(arg[1]);
299       else channel=-1;
300       if((channel>=0)&&(channel<MAX_CONNECTIONS)) {
301         if(!ivac->net.connection[channel].status&C_ESTABL)
302           sprintf(c_str,"channel %02d: no active connection",channel);
303         else {
304           ivac->net.connection[channel].status|=C_HANGUP;
305           sprintf(c_str,"channel %02d: connection closed",channel);
306         }
307       }
308       else sprintf(c_str,"invalid argument: '%s'",arg[1]);
309     }
310     if(!(strncmp(arg[0],"select",6))) {
311       valid=1;
312       if((arg[1][0]>='0')&&(arg[1][0]<='9')) channel=atoi(arg[1]);
313       else channel=-1;
314       if((channel>=0)&&(channel<MAX_CONNECTIONS)) {
315         ivac->net.sendmask|=(1<<channel);
316         sprintf(c_str,"selected channel %d",channel);
317       }
318       else if(arg[1][0]='*') {
319         ivac->net.sendmask=0xff;
320         strcpy(c_str,"selected all channels");
321       }
322       else sprintf(c_str,"invalid argument: '%s'",arg[1]);
323     }
324     if(!(strncmp(arg[0],"deselect",8))) {
325       valid=1;
326       if((arg[1][0]>='0')&&(arg[1][0]<='9')) channel=atoi(arg[1]);
327       else channel=-1;
328       if((channel>=0)&&(channel<MAX_CONNECTIONS)) {
329         ivac->net.sendmask&=(~(1<<channel));
330         sprintf(c_str,"deselected channel %d",channel);
331       }
332       else if(arg[1][0]='*') {
333         ivac->net.sendmask=0;
334         strcpy(c_str,"deselected all channels");
335       }
336       else sprintf(c_str,"invalid argument: '%s'",arg[1]);
337     }
338
339     if(!valid)
340       snprintf(c_str,IVAC_CONSOLE_STRING_LEN,"unknown command: '%s'",arg[0]);
341
342     ivac_add_to_monitor(ivac,c_str);
343       
344
345     /* delete content buffer + reset counter */
346     memset(input->content,0,input->c_count-1);
347     input->c_count=0;
348
349   }
350
351   return SUCCESS;
352 }
353
354 int ivac_display_head(t_display *display) {
355
356   int x,y;
357
358   move(0,0);
359   for(x=0;x<display->max_x;x++) addch('#');
360   mvaddstr(1,0,"##");
361   mvaddstr(1,(display->max_x-4)/2-4,"- ivac -");
362   mvaddstr(1,(display->max_x-2),"##");
363   move(2,0);
364   for(x=0;x<display->max_x;x++) addch('#');
365   refresh();
366
367   return SUCCESS;
368 }
369
370 int ivac_display_box(t_display *display) {
371   
372   int x,y;
373
374   for(y=IVAC_PROMPT_LEN;y<display->max_y-IVAC_PROMPT_LEN;y++) {
375     mvaddch(y,0,'#');
376     mvaddch(y,display->max_x-1,'#');
377   }
378
379   return SUCCESS;
380 }
381
382 int ivac_display_box_content(t_ivac *ivac) {
383
384
385   return SUCCESS;
386 }
387
388 int ivac_display_console(t_display *display) {
389                                              
390   int x,y;
391          
392   move(display->max_y-IVAC_CONSOLE_LEN-IVAC_PROMPT_LEN-1,0);
393   for(x=0;x<display->max_x;x++) addch('#');                         
394                                           
395   return SUCCESS;
396 }
397
398 int ivac_display_console_content(t_ivac *ivac) {
399
400   int x,y;
401   int len;
402
403   for(y=0;y<IVAC_CONSOLE_LEN;y++) {
404     len=strlen(ivac->console[y]);
405     move(ivac->display.max_y-IVAC_CONSOLE_LEN-IVAC_PROMPT_LEN+y,2);
406     for(x=0;x<len;x++)
407        addch(((ivac->console[y][x]>' ')||(ivac->console[y][x]<='~'))
408                ?ivac->console[y][x]:' ');
409     for(x=len;x<IVAC_CONSOLE_STRING_LEN-4;x++) addch(' ');
410   }
411   refresh();
412
413   return SUCCESS;
414 }
415
416 int ivac_display_prompt(t_display *display) {
417
418   int x,y;
419
420   move(display->max_y-3,0);
421   for(x=0;x<display->max_x;x++) addch('#');
422   mvaddstr(display->max_y-2,0,"## command: ");
423   mvaddstr(display->max_y-2,display->max_x-2,"##");
424   move(display->max_y-1,0);
425   for(x=0;x<display->max_x;x++) addch('#');
426   refresh();
427
428   return SUCCESS;
429 }
430
431 int ivac_display_prompt_content(t_ivac *ivac) {
432
433   int x,y;
434
435   /* delete old command */
436   if(ivac->input.c_count==0) {
437     move(ivac->display.max_y-2,12);
438     for(x=12;x<ivac->display.max_x-1;x++) addch(' ');
439   }
440
441   for(x=0;x<ivac->input.c_count;x++)
442     mvaddch(ivac->display.max_y-2,x+12,ivac->input.content[x]);
443   refresh();
444
445   return SUCCESS;
446 }
447
448 int ivac_display(t_display *display) {
449
450   int x,y;
451
452   /* display head */
453   ivac_display_head(display);
454
455   /* display box */
456   ivac_display_box(display);
457
458   /* display console */
459   ivac_display_console(display);
460
461   /* display command prompt */
462   ivac_display_prompt(display);
463
464   return SUCCESS;
465 }
466
467 int ivac_display_content(t_ivac *ivac) {
468
469   /* display box content */
470   ivac_display_box_content(ivac);
471
472   /* display console content */
473   ivac_display_console_content(ivac);
474
475   /* display prompt content */
476   ivac_display_prompt_content(ivac);
477
478   return SUCCESS;
479 }
480
481 int ivac_add_to_monitor(t_ivac *ivac,char *msg) {
482
483   int i;
484
485   for(i=0;i<IVAC_CONSOLE_LEN-1;i++)
486     memcpy(ivac->console[i],ivac->console[i+1],IVAC_CONSOLE_STRING_LEN);
487   memcpy(ivac->console[IVAC_CONSOLE_LEN-1],msg,IVAC_CONSOLE_STRING_LEN);
488
489   for(i=0;i<IVAC_CONSOLE_LEN;i++)
490     printf("[ivac] console line %d: %s\n",i,ivac->console[i]);
491 #ifdef DISPLAY
492   ivac_display_console_content(ivac);
493 #endif
494
495   return SUCCESS;
496 }