Changeset 3079
- Timestamp:
- 12/24/05 18:56:59 (3 years ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
branches/cocoa-networking/Chat Core/MVIRCChatConnection.h
r3076 r3079 27 27 @interface MVChatConnection (MVIRCChatConnectionPrivate) 28 28 - (void) _readNextMessageFromServer; 29 - (void) _handleCommand:(id) command parameters:(NSArray *) parameters fromSender:(NSString *) sender username:(NSString *) user host:(NSString *) host; 29 30 30 31 + (const char *) _flattenedIRCStringForMessage:(NSAttributedString *) message withEncoding:(NSStringEncoding) enc andChatFormat:(MVChatMessageFormat) format; branches/cocoa-networking/Chat Core/MVIRCChatConnection.m
r3078 r3079 1424 1424 [_chatConnection writeData:data withTimeout:-1. tag:0]; 1425 1425 [data release]; 1426 1427 [[NSNotificationCenter defaultCenter] postNotificationName:MVChatConnectionGotRawMessageNotification object:self userInfo:[NSDictionary dictionaryWithObjectsAndKeys:raw, @"message", [NSNumber numberWithBool:YES], @"outbound", nil]]; 1426 1428 } 1427 1429 … … 1589 1591 1590 1592 - (void) socket:(AsyncSocket *) sock didConnectToHost:(NSString *) host port:(UInt16) port { 1593 if( [[self password] length] ) [self sendRawMessageWithFormat:@"PASS %@", [self password]]; 1594 [self sendRawMessageWithFormat:@"NICK %@", [self nickname]]; 1595 [self sendRawMessageWithFormat:@"USER %@ %@ %@ :%@", [self username], [[NSHost currentHost] name], [self server], [self realName]]; 1591 1596 [self _didConnect]; 1592 1597 [self _readNextMessageFromServer]; 1593 }1594 1595 - (void) _readNextMessageFromServer {1596 static NSData *delimeter = nil;1597 if( ! delimeter ) delimeter = [[NSData allocWithZone:nil] initWithBytes:"\x0D\x0A" length:2];1598 [_chatConnection readDataToData:delimeter withTimeout:-1. tag:0];1599 1598 } 1600 1599 … … 1609 1608 char *command = NULL; 1610 1609 char *currentParameter = NULL; 1611 NSMutableArray *parameters = [[NSMutableArray allocWithZone:nil] initWithCapacity:10]; 1610 1611 NSString *senderString = nil; 1612 NSString *usernameString = nil; 1613 NSString *hostString = nil; 1614 id commandObject = nil; 1615 NSMutableArray *parameters = [[NSMutableArray allocWithZone:nil] initWithCapacity:15]; 1616 1617 // Parsing as defined in 2.3.1 at http://www.irchelp.org/irchelp/rfc/rfc2812.txt 1612 1618 1613 1619 if( len <= 2 || len > 512 ) 1614 1620 goto end; // bad message 1615 1621 1622 #define checkAndMarkIfDone() if( *line == '\r' || *line == '\f' || *line == '\0' ) done = YES 1623 #define consumeWhitespace() while( *line == ' ' && ! done ) line++ 1624 #define notEndOfLine() *line != '\r' && *line != '\f' && *line != '\0' && ! done 1625 1616 1626 BOOL done = NO; 1617 if( *line != '\r' && ! done) {1627 if( notEndOfLine() ) { 1618 1628 if( *line == ':' ) { 1619 1629 // prefix: ':' <sender> [ '!' <user> ] [ '@' <host> ] ' ' { ' ' } 1620 1630 sender = ++line; 1621 while( *line != '\r'&& *line != ' ' && *line != '!' && *line != '@' ) line++;1622 if( *line == '\r' ) done = YES;1631 while( notEndOfLine() && *line != ' ' && *line != '!' && *line != '@' ) line++; 1632 checkAndMarkIfDone(); 1623 1633 1624 1634 if( *line == '!' ) { 1625 1635 *line++ = '\0'; 1626 1636 user = line; 1627 while( *line != '\r' && *line != ' ' && *line != '@' && ! done) line++;1628 if( *line == '\r' ) done = YES;1637 while( notEndOfLine() && *line != ' ' && *line != '@' ) line++; 1638 checkAndMarkIfDone(); 1629 1639 if( *line != '@' ) *line = '\0'; 1630 1640 } … … 1633 1643 *line++ = '\0'; 1634 1644 host = line; 1635 while( *line != '\r' && *line != ' ' && ! done) line++;1636 if( *line == '\r' ) done = YES;1645 while( notEndOfLine() && *line != ' ' ) line++; 1646 checkAndMarkIfDone(); 1637 1647 *line = '\0'; 1638 1648 } 1639 1649 1640 if( *line == ' ' ) { 1641 *line++ = '\0'; 1642 while( *line == ' ' && ! done ) line++; 1643 } 1650 *line++ = '\0'; 1651 consumeWhitespace(); 1644 1652 } 1645 1653 1646 if( *line != '\r' && ! done) {1654 if( notEndOfLine() ) { 1647 1655 // command: <letter> { <letter> } | <number> <number> <number> 1648 1656 // letter: 'a' ... 'z' | 'A' ... 'Z' 1649 1657 // number: '0' ... '9' 1650 1658 command = line; 1651 while( *line != '\r' && *line != ' ' && ! done ) line++; 1652 if( *line == ' ' ) { 1653 *line++ = '\0'; 1654 while( *line == ' ' && ! done ) line++; 1655 } 1659 while( notEndOfLine() && *line != ' ' ) line++; 1660 *line++ = '\0'; 1661 consumeWhitespace(); 1656 1662 } 1657 1663 1658 NSString *param = nil;1659 while( *line != '\r' && ! done) {1664 id param = nil; 1665 while( notEndOfLine() ) { 1660 1666 // params: [ ':' <trailing data> | <letter> { <letter> } ] [ ' ' { ' ' } ] [ <params> ] 1661 1667 currentParameter = NULL; … … 1663 1669 if( *line == ':' ) { 1664 1670 currentParameter = ++line; 1665 while( *line != '\r' && ! done) line++;1671 while( notEndOfLine() ) line++; 1666 1672 *line = '\0'; 1667 1673 done = YES; 1674 param = [[NSData allocWithZone:nil] initWithBytes:currentParameter length:(line - currentParameter)]; 1668 1675 } else { 1669 1676 currentParameter = line; 1670 while( *line != '\r' && *line != ' ' && ! done) line++;1671 if( *line == '\r' ) done = YES;1677 while( notEndOfLine() && *line != ' ' ) line++; 1678 checkAndMarkIfDone(); 1672 1679 *line++ = '\0'; 1680 param = [[NSString allocWithZone:nil] initWithBytes:currentParameter encoding:[self encoding]]; 1673 1681 } 1674 1682 1675 if( currentParameter )1676 param = [NSString stringWithBytes:currentParameter encoding:[self encoding]];1677 1683 if( param ) [parameters addObject:param]; 1678 1679 while( *line == ' ' && ! done ) line++; 1684 [param release]; 1685 1686 consumeWhitespace(); 1680 1687 } 1681 1688 } 1682 1689 1690 #undef checkAndMarkIfDone() 1691 #undef consumeWhitespace() 1692 #undef notEndOfLine() 1693 1683 1694 end: 1684 NSLog(@"%s %s %s %s %@", sender, user, host, command, [parameters description] ); 1685 1686 NSNotification *note = [NSNotification notificationWithName:MVChatConnectionGotRawMessageNotification object:self userInfo:[NSDictionary dictionaryWithObjectsAndKeys:rawString, @"message", [NSNumber numberWithBool:NO], @"outbound", nil]]; 1687 [[NSNotificationCenter defaultCenter] postNotification:note]; 1695 if( sender ) senderString = [[NSString allocWithZone:nil] initWithBytes:sender encoding:[self encoding]]; 1696 if( user ) usernameString = [[NSString allocWithZone:nil] initWithBytes:user encoding:[self encoding]]; 1697 if( host ) hostString = [[NSString allocWithZone:nil] initWithBytes:host encoding:[self encoding]]; 1698 if( command ) { 1699 if( strlen( command ) == 3 && isdigit( command[0] ) && isdigit( command[1] ) && isdigit( command[2] ) ) { 1700 unsigned long commandNumber = strtoul( command, NULL, 10 ); 1701 commandObject = [[NSNumber allocWithZone:nil] initWithUnsignedLong:commandNumber]; 1702 } else commandObject = [[NSString allocWithZone:nil] initWithBytes:command encoding:[self encoding]]; 1703 } 1704 1705 [self _handleCommand:commandObject parameters:parameters fromSender:senderString username:usernameString host:hostString]; 1706 1707 [[NSNotificationCenter defaultCenter] postNotificationName:MVChatConnectionGotRawMessageNotification object:self userInfo:[NSDictionary dictionaryWithObjectsAndKeys:rawString, @"message", [NSNumber numberWithBool:NO], @"outbound", nil]]; 1688 1708 [rawString release]; 1689 1709 1710 [senderString release]; 1711 [usernameString release]; 1712 [hostString release]; 1713 [commandObject release]; 1690 1714 [parameters release]; 1715 1691 1716 [self _readNextMessageFromServer]; 1717 } 1718 1719 - (void) _readNextMessageFromServer { 1720 static NSData *delimeter = nil; 1721 if( ! delimeter ) delimeter = [[NSData allocWithZone:nil] initWithBytes:"\x0D\x0A" length:2]; 1722 [_chatConnection readDataToData:delimeter withTimeout:-1. tag:0]; 1723 } 1724 1725 - (void) _handleCommand:(id) command parameters:(NSArray *) parameters fromSender:(NSString *) sender username:(NSString *) user host:(NSString *) host { 1726 NSLog(@"%@ %@ %@ %@ %@", sender, user, host, command, [parameters description] ); 1692 1727 } 1693 1728
