- Timestamp:
- 2006-01-14T20:31:59Z (19 years ago)
- Branches:
- master
- Children:
- 0431ea1
- Parents:
- b23c5c7
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
irc.c
rb23c5c7 rde3e100 293 293 int irc_process( irc_t *irc ) 294 294 { 295 char **lines, *temp ;295 char **lines, *temp, **cmd; 296 296 int i; 297 297 298 if( irc->readbuffer != NULL ) { 299 lines = irc_tokenize(irc->readbuffer ); 300 for( i = 0; *lines[i] != '\0'; i++ ) { 301 if( lines[i+1] == NULL ) { 298 if( irc->readbuffer != NULL ) 299 { 300 lines = irc_tokenize( irc->readbuffer ); 301 302 for( i = 0; *lines[i] != '\0'; i ++ ) 303 { 304 if( lines[i+1] == NULL ) 305 { 302 306 temp = g_strdup( lines[i] ); 303 307 g_free( irc->readbuffer ); 304 308 irc->readbuffer = temp; 305 i ++;309 i ++; 306 310 break; 307 311 } 308 if (!irc_process_line(irc, lines[i])) { 312 313 if( ( cmd = irc_parse_line( irc, lines[i] ) ) == NULL ) 314 continue; 315 if( !irc_exec( irc, cmd ) ) 316 { 317 g_free( cmd ); 309 318 g_free( lines ); 310 319 return 0; 311 320 } 312 } 313 if(lines[i]!=NULL) { 314 g_free(irc->readbuffer); 315 irc->readbuffer=NULL; 316 } 321 322 g_free( cmd ); 323 } 324 325 if( lines[i] != NULL ) 326 { 327 g_free( irc->readbuffer ); 328 irc->readbuffer = NULL; 329 } 330 317 331 g_free( lines ); 318 332 } 333 319 334 return 1; 320 335 } … … 326 341 327 342 /* Count the number of elements we're gonna need. */ 328 for(i=0, j=1; buffer[i]!='\0'; i++ ) { 329 if(buffer[i]=='\n' ) 330 if(buffer[i+1]!='\r' && buffer[i+1]!='\n') 331 j++; 343 for( i = 0, j = 1; buffer[i] != '\0'; i ++ ) 344 { 345 if( buffer[i] == '\n' ) 346 if( buffer[i+1] != '\r' && buffer[i+1] != '\n' ) 347 j ++; 332 348 } 333 349 334 350 /* Allocate j+1 elements. */ 335 lines =g_new (char *, j+1);351 lines = g_new( char *, j + 1 ); 336 352 337 353 /* NULL terminate our list. */ 338 lines[j] =NULL;339 340 lines[0] =buffer;354 lines[j] = NULL; 355 356 lines[0] = buffer; 341 357 342 358 /* Split the buffer in several strings, using \r\n as our seperator, where \r is optional. 343 359 * Although this is not in the RFC, some braindead ircds (newnet's) use this, so some clients might too. 344 360 */ 345 for( i=0, j=0; buffer[i]!='\0'; i++) { 346 if(buffer[i]=='\n') { 347 buffer[i]='\0'; 348 349 /* We dont want to read 1 byte before our buffer 350 * and (in rare cases) generate a SIGSEGV. 351 */ 352 if(i!=0) 353 if(buffer[i-1]=='\r') 354 buffer[i-1]='\0'; 355 if(buffer[i+1]!='\r'&&buffer[i+1]!='\n') 356 lines[++j]=buffer+i+1; 357 } 358 } 359 360 return(lines); 361 } 362 363 int irc_process_line( irc_t *irc, char *line ) 361 for( i = 0, j = 0; buffer[i] != '\0'; i ++) 362 { 363 if( buffer[i] == '\n' ) 364 { 365 buffer[i] = '\0'; 366 367 if( i > 0 && buffer[i-1] == '\r' ) 368 buffer[i-1] = '\0'; 369 if( buffer[i+1] != '\r' && buffer[i+1] != '\n' ) 370 lines[++j] = buffer + i + 1; 371 } 372 } 373 374 return( lines ); 375 } 376 377 char **irc_parse_line( irc_t *irc, char *line ) 364 378 { 365 379 int i, j; … … 367 381 368 382 /* Move the line pointer to the start of the command, skipping spaces and the optional prefix. */ 369 if(line[0]==':') { 370 for(i=0; line[i]!=32; i++); 371 line=line+i; 372 } 373 for(i=0; line[i]==32; i++); 374 line=line+i; 375 383 if( line[0] == ':' ) 384 { 385 for( i = 0; line[i] != ' '; i ++ ); 386 line = line + i; 387 } 388 for( i = 0; line[i] == ' '; i ++ ); 389 line = line + i; 390 376 391 /* If we're already at the end of the line, return. If not, we're going to need at least one element. */ 377 if(line[0]=='\0') 378 return 1; 379 else 380 j=1; 381 382 /* Count the number of char **cmd elements we're going to need. */ 383 for(i=0; line[i]!='\0'; i++) { 384 if((line[i]==32) && (line[i+1]!=32) && (line[i+1]!='\0') && (line[i+1]!=':')) 385 j++; 386 else if((line[i]==':') && (line[i+1]!='\0') && (line[i-1]==32)) { 387 j++; 388 break; 389 } 392 if( line[0] == '\0') 393 return NULL; 394 395 /* Count the number of char **cmd elements we're going to need. */ 396 j = 1; 397 for( i = 0; line[i] != '\0'; i ++ ) 398 { 399 if( line[i] == ' ' ) 400 { 401 j ++; 390 402 403 if( line[i+1] == ':' ) 404 break; 405 } 391 406 } 392 407 393 408 /* Allocate the space we need. */ 394 cmd =g_new(char *, j+1);395 cmd[j] =NULL;409 cmd = g_new( char *, j + 1 ); 410 cmd[j] = NULL; 396 411 397 412 /* Do the actual line splitting, format is: … … 400 415 */ 401 416 402 cmd[0] =line;403 for( i=0, j=0; line[i]!='\0'; i++) {404 if((line[i]==32)){405 line[i]='\0';406 if((line[i+1]!=32) && (line[i+1]!='\0') && (line[i+1]!=':'))407 cmd[++j]=line+i+1;408 }409 else if((line[i]==':') && (line[i+1]!='\0') && (line[i-1]=='\0')) {410 cmd[++j]=line+i+1;411 break;412 }413 }414 415 i=irc_exec(irc, cmd);416 g_free(cmd);417 418 return (i);417 cmd[0] = line; 418 for( i = 0, j = 0; line[i] != '\0'; i ++ ) 419 { 420 if( line[i] == ' ' ) 421 { 422 line[i] = '\0'; 423 cmd[++j] = line + i + 1; 424 425 if( line[i+1] == ':' ) 426 { 427 cmd[j] ++; 428 break; 429 } 430 } 431 } 432 433 return cmd; 419 434 } 420 435
Note: See TracChangeset
for help on using the changeset viewer.