Differences From Artifact [08dd759998]:
- File
src/IRCConnection.m
— part of check-in
[2cc7844016]
at
2012-11-28 21:30:19
on branch trunk
— Make sure no newlines from parameters are sent.
Not doing so would allow hijacking a connection. (user: js, size: 15719) [annotate] [blame] [check-ins using]
To Artifact [74ae5fa759]:
- File src/IRCConnection.m — part of check-in [8845b8b00b] at 2013-02-14 00:20:49 on branch trunk — Prefix all ivars with an underscore. (user: js, size: 15777) [annotate] [blame] [check-ins using]
︙ | ︙ | |||
39 40 41 42 43 44 45 | @implementation IRCConnection - init { self = [super init]; @try { | | | > > > > > > > > > > > > | | | | | | | | | | | | | | | | | | | | | | | | | 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 | @implementation IRCConnection - init { self = [super init]; @try { _channels = [[OFMutableDictionary alloc] init]; _port = 6667; } @catch (id e) { [self release]; @throw e; } return self; } - (void)dealloc { [_socket release]; [_server release]; [_nickname release]; [_username release]; [_realname release]; [_channels release]; [super dealloc]; } - (void)setServer: (OFString*)server { OF_SETTER(_server, server, YES, YES) } - (OFString*)server { OF_GETTER(_server, YES) } - (void)setPort: (uint16_t)port { _port = port; } - (uint16_t)port { return _port; } - (void)setNickname: (OFString*)nickname { OF_SETTER(_nickname, nickname, YES, YES) } - (OFString*)nickname { OF_GETTER(_nickname, YES) } - (void)setUsername: (OFString*)username { OF_SETTER(_username, username, YES, YES) } - (OFString*)username { OF_GETTER(_username, YES) } - (void)setRealname: (OFString*)realname { OF_SETTER(_realname, realname, YES, YES) } - (OFString*)realname { OF_GETTER(_realname, YES) } - (void)setDelegate: (id <IRCConnectionDelegate>)delegate { _delegate = delegate; } - (id <IRCConnectionDelegate>)delegate { OF_GETTER(_delegate, NO) } - (OFTCPSocket*)socket { OF_GETTER(_socket, YES) } - (void)connect { OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init]; _socket = [[OFTCPSocket alloc] init]; [_socket connectToHost: _server port: _port]; [self sendLineWithFormat: @"NICK %@", _nickname]; [self sendLineWithFormat: @"USER %@ * 0 :%@", _username, _realname]; [pool release]; } - (void)disconnect { [self disconnectWithReason: nil]; |
︙ | ︙ | |||
167 168 169 170 171 172 173 | reason = [[reason componentsSeparatedByString: @"\n"] firstObject]; if (reason == nil) [self sendLineWithFormat: @"PART %@", channel]; else [self sendLineWithFormat: @"PART %@ :%@", channel, reason]; | | | | | | 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 | reason = [[reason componentsSeparatedByString: @"\n"] firstObject]; if (reason == nil) [self sendLineWithFormat: @"PART %@", channel]; else [self sendLineWithFormat: @"PART %@ :%@", channel, reason]; [_channels removeObjectForKey: channel]; } - (void)sendLine: (OFString*)line { [_delegate connection: self didSendLine: line]; [_socket writeLine: line]; } - (void)sendLineWithFormat: (OFConstantString*)format, ... { OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init]; OFString *line; va_list args; |
︙ | ︙ | |||
225 226 227 228 229 230 231 | reason: (OFString*)reason { reason = [[reason componentsSeparatedByString: @"\n"] firstObject]; [self sendLineWithFormat: @"KICK %@ %@ :%@", channel, user, reason]; } | | | | | | | > | | | | | | | | | 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 | reason: (OFString*)reason { reason = [[reason componentsSeparatedByString: @"\n"] firstObject]; [self sendLineWithFormat: @"KICK %@ %@ :%@", channel, user, reason]; } - (void)changeNicknameTo: (OFString*)nickname { nickname = [[nickname componentsSeparatedByString: @"\n"] firstObject]; [self sendLineWithFormat: @"NICK %@", nickname]; } - (void)IRC_processLine: (OFString*)line { OFArray *components; OFString *action = nil; [_delegate connection: self didReceiveLine: line]; components = [line componentsSeparatedByString: @" "]; /* PING */ if ([components count] == 2 && [[components firstObject] isEqual: @"PING"]) { OFMutableString *s = [[line mutableCopy] autorelease]; [s replaceOccurrencesOfString: @"PING" withString: @"PONG"]; [self sendLine: s]; return; } action = [[components objectAtIndex: 1] uppercaseString]; /* Connected */ if ([action isEqual: @"001"] && [components count] >= 4) { [_delegate connectionWasEstablished: self]; return; } /* JOIN */ if ([action isEqual: @"JOIN"] && [components count] == 3) { OFString *who = [components objectAtIndex: 0]; OFString *where = [components objectAtIndex: 2]; IRCUser *user; OFMutableSet *channel; who = [who substringWithRange: of_range(1, [who length] - 1)]; user = [IRCUser IRCUserWithString: who]; if ([who hasPrefix: [_nickname stringByAppendingString: @"!"]]) { channel = [OFMutableSet set]; [_channels setObject: channel forKey: where]; } else channel = [_channels objectForKey: where]; [channel addObject: [user nickname]]; [_delegate connection: self didSeeUser: user joinChannel: where]; return; } /* NAMES reply */ if ([action isEqual: @"353"] && [components count] >= 6) { OFString *where; OFMutableSet *channel; OFArray *users; size_t pos; OFEnumerator *enumerator; OFString *user; where = [components objectAtIndex: 4]; if ((channel = [_channels objectForKey: where]) == nil) { /* We did not request that */ return; } pos = [[components objectAtIndex: 0] length] + [[components objectAtIndex: 1] length] + [[components objectAtIndex: 2] length] + |
︙ | ︙ | |||
324 325 326 327 328 329 330 | [user hasPrefix: @"%"] || [user hasPrefix: @"*"]) user = [user substringWithRange: of_range(1, [user length] - 1)]; [channel addObject: user]; } | | | | | | | | | | | | | | 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 | [user hasPrefix: @"%"] || [user hasPrefix: @"*"]) user = [user substringWithRange: of_range(1, [user length] - 1)]; [channel addObject: user]; } [_delegate connection: self didReceiveNamesForChannel: where]; return; } /* PART */ if ([action isEqual: @"PART"] && [components count] >= 3) { OFString *who = [components objectAtIndex: 0]; OFString *where = [components objectAtIndex: 2]; IRCUser *user; OFMutableSet *channel; OFString *reason = nil; size_t pos = [who length] + 1 + [[components objectAtIndex: 1] length] + 1 + [where length]; who = [who substringWithRange: of_range(1, [who length] - 1)]; user = [IRCUser IRCUserWithString: who]; channel = [_channels objectForKey: where]; if ([components count] > 3) reason = [line substringWithRange: of_range(pos + 2, [line length] - pos - 2)]; [channel removeObject: [user nickname]]; [_delegate connection: self didSeeUser: user leaveChannel: where reason: reason]; return; } /* KICK */ if ([action isEqual: @"KICK"] && [components count] >= 4) { OFString *who = [components objectAtIndex: 0]; OFString *where = [components objectAtIndex: 2]; OFString *whom = [components objectAtIndex: 3]; IRCUser *user; OFMutableSet *channel; OFString *reason = nil; size_t pos = [who length] + 1 + [[components objectAtIndex: 1] length] + 1 + [where length] + 1 + [whom length]; who = [who substringWithRange: of_range(1, [who length] - 1)]; user = [IRCUser IRCUserWithString: who]; channel = [_channels objectForKey: where]; if ([components count] > 4) reason = [line substringWithRange: of_range(pos + 2, [line length] - pos - 2)]; [channel removeObject: [user nickname]]; [_delegate connection: self didSeeUser: user kickUser: whom channel: where reason: reason]; return; } /* QUIT */ if ([action isEqual: @"QUIT"] && [components count] >= 2) { OFString *who = [components objectAtIndex: 0]; |
︙ | ︙ | |||
406 407 408 409 410 411 412 | who = [who substringWithRange: of_range(1, [who length] - 1)]; user = [IRCUser IRCUserWithString: who]; if ([components count] > 2) reason = [line substringWithRange: of_range(pos + 2, [line length] - pos - 2)]; | | | | | | | | | | | | | | | | | | | | | | | 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 | who = [who substringWithRange: of_range(1, [who length] - 1)]; user = [IRCUser IRCUserWithString: who]; if ([components count] > 2) reason = [line substringWithRange: of_range(pos + 2, [line length] - pos - 2)]; enumerator = [_channels objectEnumerator]; while ((channel = [enumerator nextObject]) != nil) [channel removeObject: [user nickname]]; [_delegate connection: self didSeeUserQuit: user reason: reason]; return; } /* NICK */ if ([action isEqual: @"NICK"] && [components count] == 3) { OFString *who = [components objectAtIndex: 0]; OFString *nickname = [components objectAtIndex: 2]; IRCUser *user; OFEnumerator *enumerator; OFMutableSet *channel; who = [who substringWithRange: of_range(1, [who length] - 1)]; nickname = [nickname substringWithRange: of_range(1, [nickname length] - 1)]; user = [IRCUser IRCUserWithString: who]; if ([[user nickname] isEqual: _nickname]) { [_nickname release]; _nickname = [nickname copy]; } enumerator = [_channels keyEnumerator]; while ((channel = [enumerator nextObject]) != nil) { if ([channel containsObject: [user nickname]]) { [channel removeObject: [user nickname]]; [channel addObject: nickname]; } } [_delegate connection: self didSeeUser: user changeNicknameTo: nickname]; return; } /* PRIVMSG */ if ([action isEqual: @"PRIVMSG"] && [components count] >= 4) { OFString *from = [components objectAtIndex: 0]; OFString *to = [components objectAtIndex: 2]; IRCUser *user; OFString *msg; size_t pos = [from length] + 1 + [[components objectAtIndex: 1] length] + 1 + [to length]; from = [from substringWithRange: of_range(1, [from length] - 1)]; msg = [line substringWithRange: of_range(pos + 2, [line length] - pos - 2)]; user = [IRCUser IRCUserWithString: from]; if (![to isEqual: _nickname]) [_delegate connection: self didReceiveMessage: msg channel: to user: user]; else [_delegate connection: self didReceivePrivateMessage: msg user: user]; return; } /* NOTICE */ |
︙ | ︙ | |||
500 501 502 503 504 505 506 | if (![from containsString: @"!"] || [to isEqual: @"*"]) { /* System message - ignore for now */ return; } user = [IRCUser IRCUserWithString: from]; | | | | | | | | | | | | | | | | | | | | | | | | | | | | < < < < < < < < < < < < | 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 | if (![from containsString: @"!"] || [to isEqual: @"*"]) { /* System message - ignore for now */ return; } user = [IRCUser IRCUserWithString: from]; if (![to isEqual: _nickname]) [_delegate connection: self didReceiveNotice: notice channel: to user: user]; else [_delegate connection: self didReceiveNotice: notice user: user]; return; } } - (void)processLine: (OFString*)line { OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init]; [self IRC_processLine: line]; [pool release]; } - (BOOL)socket: (OFTCPSocket*)socket didReceiveISO88591Line: (OFString*)line exception: (OFException*)exception { if (line != nil) { [self IRC_processLine: line]; [socket asyncReadLineWithTarget: self selector: @selector(connection: didReceiveLine: exception:)]; } return NO; } - (BOOL)socket: (OFTCPSocket*)socket didReceiveLine: (OFString*)line exception: (OFException*)exception { if (line != nil) { [self IRC_processLine: line]; return YES; } if ([exception isKindOfClass: [OFInvalidEncodingException class]]) [socket asyncReadLineWithEncoding: OF_STRING_ENCODING_ISO_8859_1 target: self selector: @selector(socket: didReceiveISO88591Line: exception:)]; return NO; } - (void)handleConnection { [_socket asyncReadLineWithTarget: self selector: @selector(socket:didReceiveLine: exception:)]; } - (OFSet*)usersInChannel: (OFString*)channel { return [[[_channels objectForKey: channel] copy] autorelease]; } @end @implementation OFObject (IRCConnectionDelegate) - (void)connection: (IRCConnection*)connection didReceiveLine: (OFString*)line { |
︙ | ︙ |