1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 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 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 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 327 328 329 330 331 332 333 334 335 336 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 412 413 414 415 416 417 418 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 500 501 502 503 504 505 506 507 508 509 510 511 512 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 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806
|
#include <qdialog.h>
#include <qwidget.h>
#include <qlistbox.h>
#include <qlineedit.h>
#include <qmultilineedit.h>
#include <qpushbutton.h>
#include <qtabwidget.h>
#include <qevent.h>
#include <qapplication.h>
#include <iostream>
#include "chatbox.h"
#include "globals.h"
#include "preferences.h"
#include "utility.h"
ChatBox::ChatBox() : QDialog()
{
setCaption( "Kascade IRC Client" );
resize( 650, 400 );
setMinimumSize( 350, 200 );
names = new QListBox( this );
input = new QLineEdit( this );
closeButton = new QPushButton( this );
channels = new QTabWidget( this );
closeButton->setText( "close" );
sysmsgarea = new QMultiLineEdit( this );
channels->addTab( sysmsgarea, "system" );
sysmsgarea->setWordWrap( QMultiLineEdit::WidgetWidth );
sysmsgarea->setReadOnly( TRUE );
QObject::connect( input, SIGNAL( returnPressed() ), this, SLOT( textEntered() ) );
QObject::connect( closeButton, SIGNAL( clicked() ), this, SLOT( closeChannel() ) );
QObject::connect( names, SIGNAL( selected( int ) ), this, SLOT( createPrivateChannel( int ) ) );
socket = new QSocket();
QObject::connect( socket, SIGNAL( connected() ), this, SLOT( connected() ) );
QObject::connect( socket, SIGNAL( readyRead() ), this, SLOT( incoming() ) );
QObject::connect( channels, SIGNAL( selected( const QString & ) ), this, SLOT( selected( const QString & ) ) );
setFont( config->ircFont() );
state = DISCONNECTED;
nicknamenr = 1;
}
void ChatBox::selected( const QString &title )
{
if( title != "system" )
gotoPage( pageForChannel( channelForTitle( (const char *)title ) ) );
}
void ChatBox::gotoPage( QWidget *page )
{
names->clear();
if( page != sysmsgarea )
{
string name;
for( unsigned int n = 0; n < namevv[vectorIndex( page )].size(); n++ )
{
name = namevv[ vectorIndex( page ) ][n];
if( name[0] == '@' )
names->insertItem( name.c_str(), 0 );
else
names->insertItem( name.c_str() );
}
}
channels->showPage( page );
}
void ChatBox::login()
{
if( state == DISCONNECTED )
{
printLine( "** connecting to server " + config->ircServer() + ", port 6666", currentArea() );
myapp->processEvents();
// socket->setMode( QSocket::Ascii );
socket->connectToHost( config->ircServer().c_str(), 6666 );
}
}
void ChatBox::join( string title, string channel = "" )
{
if( channel == "" )
channel = title;
input->setFocus();
show();
setActiveWindow();
if( vectorIndex( channel ) != -1 )
return;
// determine title
int exists = TRUE;
while( exists )
{
exists = FALSE;
for( unsigned int n = 0; n < channelandtitlev.size(); n += 2 )
if( channelandtitlev[n+1] == title )
{
title += "*";
exists = TRUE;
break;
}
}
channelandtitlev.push_back( channel );
channelandtitlev.push_back( title );
if( state == CONNECTED )
transmitLine( "JOIN " + channel );
else
{
autojoin = channel;
login();
}
}
void ChatBox::disconnect()
{
if( state == DISCONNECTED )
return;
socket->close();
state = DISCONNECTED;
nicknamenr = 1;
for( int n = pagev.size()-1; n >= 0; n-- )
removePage( pagev[n] );
printLine( "** disconnected from server", sysmsgarea );
}
int ChatBox::vectorIndex( string channel )
{
for( unsigned int n = 0; n < channelv.size(); n++ )
if( channelv[n] == channel )
return n;
return -1;
}
int ChatBox::vectorIndex( QWidget *page )
{
for( unsigned int n = 0; n < pagev.size(); n++ )
if( pagev[n] == page )
return n;
return -1;
}
string ChatBox::currentChannel()
{
if( vectorIndex( channels->currentPage() ) != -1 )
return channelv[vectorIndex( channels->currentPage() )];
return "";
}
QWidget *ChatBox::pageForChannel( string channel )
{
if( vectorIndex( channel ) != -1 )
return pagev[vectorIndex( channel )];
return 0;
}
string ChatBox::titleForChannel( string channel )
{
for( unsigned int n = 0; n < channelandtitlev.size(); n += 2 )
if( channelandtitlev[n] == channel )
return channelandtitlev[n+1];
return channel;
}
string ChatBox::channelForTitle( string title )
{
for( unsigned int n = 0; n < channelandtitlev.size(); n += 2 )
if( channelandtitlev[n+1] == title )
return channelandtitlev[n];
return title;
}
string ChatBox::tfc( string channel )
{
string title = titleForChannel( channel );
if( title[0] == '#' )
return title;
return "'" + title + "'";
}
QMultiLineEdit *ChatBox::areaForChannel( string channel )
{
return (QMultiLineEdit *)pageForChannel( channel );
}
QMultiLineEdit *ChatBox::currentArea()
{
return (QMultiLineEdit *)(channels->currentPage());
}
void ChatBox::connected()
{
printLine( "** connected, logging in..", currentArea() );
if( !tryNickUser() )
{
printLine( "** no nicknames specified. edit preferences.", sysmsgarea );
printLine( "** disconnected", sysmsgarea );
nicknamenr = 1;
}
}
int ChatBox::tryNickUser()
{
if( nicknamenr == 5 )
return FALSE;
while( ( nicknamenr < 5 ) &&
( ( user = config->ircNickname( nicknamenr++ ) ) == "" ) );
if( user == "" )
return FALSE;
transmitLine( "NICK " + user );
transmitLine( "USER " + user + " localhost " + config->ircServer() + " :" + user );
return TRUE;
}
int ChatBox::transmitLine( string s )
{
//cout << "sending " + s << endl;
return (socket->writeBlock( ( s + "\n\r" ).c_str(), s.length()+2 ) != -1 );
}
void ChatBox::incoming()
{
while( socket->canReadLine() )
{
string s = (const char *)( socket->readLine() );
while( ( s[s.length()-1] == 0xd ) ||
( s[s.length()-1] == 0xa ) )
s = s.substr( 0, s.length()-1 );
s = trim( s );
//cout << "## " << s << endl;
int k;
string origin, code, params;
origin = s.substr( 0, k = s.find_first_of( ' ' ) );
s = s.substr( k+1, s.length()-k-1 );
if( ( k = s.find_first_of( ' ' ) ) != -1 )
{
code = s.substr( 0, k );
params = s.substr( k+1, s.length()-k-1 );
}
else
code = s;
processMessage( origin, code, params );
}
}
void ChatBox::processMessage( string origin, string code, string params )
{
string person;
if( origin.find_first_of( '!' ) != -1 )
person = origin.substr( 1, origin.find_first_of( '!' )-1 );
if( code == "001" )
{
state = CONNECTED;
printLine( "** you are logged in as " + user, sysmsgarea );
if( autojoin != "" )
{
transmitLine( "JOIN " + autojoin );
autojoin = "";
}
}
else if( origin == "PING" )
transmitLine( "PONG " + code );
else if( code == "JOIN" )
{
string channel = params.substr( 1, params.find_first_of( ' ' )-1 );
if( person == user )
{
int exists = FALSE;
unsigned int n;
for( n = 0; n < channelv.size(); n++ )
if( channelv[n] == channel )
{
exists = TRUE;
break;
}
if( !exists )
{
channels->removePage( sysmsgarea );
createChannel( channel );
printLine( "** you have joined channel " + tfc( channel ), areaForChannel( channel ) );
}
else
{
joinedv[n] = TRUE;
gotoPage( pageForChannel( channel ) );
printLine( "** you have rejoined channel " + tfc( channel ), areaForChannel( channel ) );
}
}
else
{
namevv[ vectorIndex( channel ) ].push_back( person );
gotoPage( channels->currentPage() );
broadcast( "** " + person + " has joined channel " + tfc( channel ) );
}
}
else if( code == "KICK" )
{
int k;
string channel = params.substr( 0, k = params.find_first_of( ' ' ) );
string kicked = params.substr( k+1, params.find_first_of( ' ', k+1 )-k-1 );
if( kicked == user )
{
joinedv[ vectorIndex( channel ) ] = FALSE;
namevv[ vectorIndex( channel ) ].clear();
gotoPage( channels->currentPage() );
if( person == user )
broadcast( "** you have kicked yourself from channel " + tfc( channel ) );
else
broadcast( "** " + person + " has kicked you from channel " + tfc( channel ) );
}
else
{
removeName( kicked, channel );
if( kicked == person )
broadcast( "** " + person + " has kicked themselves from channel " + tfc( channel ) );
else if( person == user )
broadcast( "** you have kicked " + kicked + " from channel " + tfc( channel ) );
else
broadcast( "** " + kicked + " has been kicked by " + person + " from channel " + tfc( channel ) );
}
}
else if( code == "NICK" )
{
string nick = params.substr( 1, params.length()-1 );
if( person == user )
{
broadcast( "** you are now known as " + nick );
user = nick;
}
else
broadcast( "** " + person + " is now known as " + nick );
for( unsigned int m = 0; m < namevv.size(); m++ )
for( unsigned int n = 0; n < namevv[m].size(); n++ )
if( namevv[m][n] == person )
{
namevv[m][n] = nick;
break;
}
else if( namevv[m][n] == "@" + person )
{
namevv[m][n] = "@" + nick;
break;
}
for( unsigned int n = 0; n < channelv.size(); n++ )
if( channelv[n] == "$" + person )
{
channels->changeTab( pageForChannel( "$" + person ), ("$" + nick).c_str() );
channelv[n] = "$" + nick;
}
gotoPage( channels->currentPage() );
}
else if( code == "PART" )
{
string channel = params.substr( 0, params.find_first_of( ' ' ) );
// cout << "channel" << channel << "!" << endl;
if( person == user )
removePage( pageForChannel( channel ) );
else
{
removeName( person, channel );
broadcast( "** " + person + " has left channel " + tfc( channel ) );
}
}
else if( code == "QUIT" )
{
for( unsigned int n = 0; n < channelv.size(); n++ )
removeName( person, channelv[n] );
broadcast( "** " + person + " has quit irc" );
}
else if( code == "MODE" )
{
int k;
string channel = params.substr( 0, k = params.find_first_of( ' ' ) );
if( channel[0] == '#' )
{
origin = origin.substr( 1, origin.find_first_of( '!' )-1 );
char flag = params[k+1];
string modes = params.substr( k+2, params.find_first_of( ' ', k+2 )-k-2 );
k = params.find_first_of( ' ', k+2 );
string person = params.substr( k+1, params.length()-k-1 );
/*cout << "origin " << origin << endl;
cout << "channel " << channel << endl;
cout << "flag " << flag << endl;
cout << "modes " << modes << endl;
cout << "person " << person << endl; */
vector<string> personv;
dirToVec( person, personv, ' ', FALSE );
vector<string> &namev = namevv[ vectorIndex( channel ) ];
if( flag == '+' )
{
if( modes.find_first_of( 'o' ) != -1 )
for( unsigned int n = 0; n < personv.size(); n++ )
giveOpStatus( origin, personv[n], namev );
}
else
{
if( modes.find_first_of( 'o' ) != -1 )
for( unsigned int n = 0; n < personv.size(); n++ )
removeOpStatus( origin, personv[n], namev );
}
gotoPage( channels->currentPage() );
}
}
else if( code == "PRIVMSG" )
{
int k;
string channel = params.substr( 0, k = params.find_first_of( ' ' ) );
string msg = params.substr( k+2, params.length()-k-2 );
string finalmsg;
if( ( msg[0] == 1 ) &&
( msg.find( "ACTION " ) == 1 ) )
finalmsg = "** " + person + msg.substr( 7, msg.length()-8 );
else
finalmsg = person + "> " + msg;
if( channel[0] == '#' )
printLine( finalmsg, areaForChannel( channel ) );
else
{
if( vectorIndex( "$" + person ) == -1 )
createChannel( "$" + person );
if( person != user )
printLine( finalmsg, areaForChannel( "$" + person ) );
}
}
else if( code == ERR_NICKNAMEINUSE )
{
int i = params.find_first_of( ' ' )+1;
int j = params.find_first_of( ' ', i );
string nick = params.substr( i, j-i );
printLine( "** nickname " + nick + " already in use", currentArea() );
if( ( state == DISCONNECTED ) &&
( !tryNickUser() ) )
{
nicknamenr = 1;
printLine( "** all specified nicknames are in use. edit preferences.", sysmsgarea );
printLine( "** disconnected", sysmsgarea );
}
}
else if( code == RPL_NAMREPLY )
{
int k = params.find_first_of( '#' );
string chann = params.substr( k, params.find_first_of( ' ', k+2 )-k );
int index = vectorIndex( chann );
k = params.find_first_of( ':' );
params = params.substr( k+1, params.length()-k-1 );
namevv[index].erase( namevv[index].begin(), namevv[index].end() );
while( ( k = params.find_first_of( ' ' ) ) != -1 )
{
namevv[index].push_back( params.substr( 0, k ) );
params = params.substr( k+1, params.length()-k-1 );
}
namevv[index].push_back( params );
gotoPage( pageForChannel( chann ) );
}
}
void ChatBox::giveOpStatus( string origin, string person, vector<string> &namev )
{
for( unsigned int n = 0; n < namev.size(); n++ )
if( namev[n] == person )
{
namev[n] = "@" + person;
break;
}
if( origin == user )
printLine( "** you give operator status to " + person, currentArea() );
else if( person == user )
printLine( "** " + origin + " gives you operator status", currentArea() );
else
printLine( "** " + origin + " gives operator status to " + person, currentArea() );
}
void ChatBox::removeOpStatus( string origin, string person, vector<string> &namev )
{
for( unsigned int n = 0; n < namev.size(); n++ )
if( namev[n] == "@" + person )
{
namev[n] = person;
break;
}
if( ( origin == user ) && ( person == user ) )
printLine( "** you remove operator status from yourself", currentArea() );
else if( origin == user )
printLine( "** you remove operator status from " + person, currentArea() );
else if( person == user )
printLine( "** " + origin + " removes your operator status", currentArea() );
else
printLine( "** " + origin + " removes operator status from " + person, currentArea() );
}
void ChatBox::removeName( string person, string channel )
{
//cout << person << "!" << channel << "!" << endl;
vector<string> &namev = namevv[ vectorIndex( channel ) ];
for( unsigned int n = 0; n < namev.size(); n++ )
{
//cout << "!" << namev[n] << endl;
if( ( namev[n] == person ) ||
( namev[n] == "@" + person ) )
{
namev.erase( namev.begin()+n, namev.begin()+n+1 );
break;
}
}
gotoPage( channels->currentPage() );
}
void ChatBox::broadcast( string msg )
{
printLine( msg, sysmsgarea );
for( unsigned int n = 0; n < pagev.size(); n++ )
printLine( msg, (QMultiLineEdit *)pagev[n] );
}
void ChatBox::textEntered()
{
if( state == DISCONNECTED )
{
printLine( "** you are not connected", sysmsgarea );
input->clear();
return;
}
string s = (const char *)input->text();
if( s == "" )
return;
if( ( s == "/help" ) || ( s == "/HELP" ) )
{
QMultiLineEdit *area = (QMultiLineEdit *)(channels->currentPage() );
printLine( "", area );
printLine( "** Kascade IRC Client - version 1.0", area );
printLine( "**", area );
printLine( "** This is a very simple IRC Client. You can use another IRC Client", area );
printLine( "** with the Kascade Browser if you like, by editing the preferences.", area );
printLine( "**", area );
printLine( "** Command-list: join, part, kick, op(er), deop, me, msg, nick", area );
printLine( "** Examples: /join #startrek, /op janeway, /kick neelix, /me rules, /part", area );
printLine( "** Double-click on someone's name to chat privately.", area );
printLine( "**", area );
printLine( "** Please send bug-reports and suggestions to: m.dufour@student.tudelft.nl", area );
printLine( "** Have fun!", area );
}
else if( s[0] == '/' )
{
s.erase( 0, 1 );
int k;
string command;
string params;
if( ( k = s.find_first_of( ' ' ) ) == -1 )
command = toUC( s );
else
{
command = toUC( s.substr( 0, k ) );
params = s.substr( k+1, s.length()-k-1 );
}
if( ( command == "OP" ) ||
( command == "OPER" ) )
transmitLine( "MODE " + currentChannel() + " +o " + params );
if( command == "DEOP" )
transmitLine( "MODE " + currentChannel() + " -o " + params );
else if( command == "PART" )
{
if( currentChannel()[0] == '$' )
removePage( channels->currentPage() );
else
transmitLine( command + " " + currentChannel() + " " + params );
}
else if( command == "KICK" )
transmitLine( command + " " + currentChannel() + " " + params );
else if( command == "ME" )
{
printLine( "** " + user + " " + params, currentArea() );
if( currentChannel()[0] == '$' )
transmitLine( "PRIVMSG " + currentChannel().substr( 1, currentChannel().length()-1 ) +
" :" + (char)1 + "ACTION " + params + (char)1 );
else
transmitLine( "PRIVMSG " + currentChannel() + " :" + (char)1 + "ACTION " + params + (char)1 );
}
else if( command == "MSG" )
{
int k = params.find_first_of( ' ' );
if( k != -1 )
{
string person = params.substr( 0, k );
string msg = params.substr( k+1, params.length()-k-1 );
transmitLine( "PRIVMSG " + person + " :" + msg );
}
}
else if( ( command == "JOIN" ) || ( command == "NICK" ) )
transmitLine( s );
}
else if ( channels->currentPage() != sysmsgarea )
{
if( joinedv[ vectorIndex( channels->currentPage() ) ] == TRUE )
{
printLine( user + "> " + s, currentArea() );
if( currentChannel()[0] == '$' )
transmitLine( "PRIVMSG " + currentChannel().substr( 1, currentChannel().length()-1 ) + " :" + s );
else
transmitLine( "PRIVMSG " + currentChannel() + " :" + s );
}
else
printLine( "** try rejoining the channel first", currentArea() );
}
else
printLine( "** try to join a channel first", sysmsgarea );
input->clear();
}
void ChatBox::printLine( string s, QMultiLineEdit *textarea )
{
if( textarea != 0 )
{
//textarea->insertLine( s.c_str() );
if( textarea->text() == "" )
textarea->setText( s.c_str() );
else
textarea->append( s.c_str() );
textarea->setCursorPosition( textarea->numLines(), 0 );
}
// else
// cout << s << endl;
}
void ChatBox::closeChannel()
{
if( channels->currentPage() == sysmsgarea )
hide();
if( currentChannel()[0] == '#' )
transmitLine( "PART " + currentChannel() );
removePage( channels->currentPage() );
}
void ChatBox::removePage( QWidget *page )
{
if( page == 0 )
return;
for( unsigned int n = 0; n < pagev.size(); n++ )
if( pagev[n] == page )
{
pagev.erase( pagev.begin()+n, pagev.begin()+n+1 );
channelv.erase( channelv.begin()+n, channelv.begin()+n+1 );
namevv.erase( namevv.begin()+n, namevv.begin()+n+1 );
joinedv.erase( joinedv.begin()+n, joinedv.begin()+n+1 );
break;
}
channels->removePage( page );
if( channels->currentPage() == 0 )
{
channels->addTab( sysmsgarea, "system" );
gotoPage( sysmsgarea );
printLine( "** you have no channels open. use /join #channel to join one.", sysmsgarea );
}
}
void ChatBox::createPrivateChannel( int name )
{
string channel = (const char *)(names->item( name )->text());
if( channel[0] == '@' )
channel.erase( 0, 1 );
channel = "$" + channel;
createChannel( channel );
}
void ChatBox::createChannel( string channel )
{
QMultiLineEdit *chatarea = new QMultiLineEdit( this );
chatarea->setWordWrap( QMultiLineEdit::WidgetWidth );
chatarea->setReadOnly( TRUE );
if( channel[0] == '$' )
channels->addTab( chatarea, channel.c_str() );
else
channels->addTab( chatarea, titleForChannel( channel ).c_str() );
channelv.push_back( channel );
pagev.push_back( chatarea );
joinedv.push_back( TRUE );
vector<string> namev;
namevv.push_back( namev );
gotoPage( chatarea );
closeButton->setText( "close" );
}
void ChatBox::resizeEvent( QResizeEvent * )
{
int h = this->height(), w = this->width();
closeButton->setGeometry( w-120, h-70, 110, 30);
names->setGeometry( w-120, 33, 110, h-113 );
if( channels != 0 )
channels->setGeometry( 10, 10, w-140, h-50 );
input->setGeometry( 10, h-30, w-20, 20 );
}
|