2008-08-31 09:08:24 +00:00
|
|
|
#include "messagehandler.h"
|
|
|
|
#include <QFileInfo>
|
|
|
|
|
|
|
|
MessageHandler::MessageHandler( IRCClient *irc, QWidget *parent ) : QTabWidget( parent ) {
|
2009-04-17 05:05:43 +00:00
|
|
|
this->scriptManager = new MIRCScriptManager();
|
2009-05-05 21:10:34 +00:00
|
|
|
//connect(this->scriptManager, SIGNAL( unknown_alias( QString, QStringList ) ), this, SLOT( call_alias( QString, QStringList ) ) );
|
2008-08-31 09:08:24 +00:00
|
|
|
this->irc = irc;
|
|
|
|
connect( irc, SIGNAL( messageRcvd( QString, QString, QString, QStringList, QString ) ), this, SLOT( messageRcvd( QString, QString, QString, QStringList, QString ) ) );
|
|
|
|
connect( irc, SIGNAL( registered() ), this, SLOT( autoRun() ) );
|
|
|
|
connect( irc, SIGNAL( sentRAW( QString ) ), this, SLOT( sentRAW( QString ) ) );
|
|
|
|
connect( irc, SIGNAL( rcvdRAW( QString ) ), this, SLOT( rcvdRAW( QString ) ) );
|
|
|
|
this->setTabPosition( QTabWidget::South );
|
2009-05-08 20:58:19 +00:00
|
|
|
_addWindow( "status", "say", true );
|
2008-08-31 09:08:24 +00:00
|
|
|
|
|
|
|
DCCPortMin = 1024;
|
|
|
|
DCCPortMax = 5000;
|
|
|
|
|
|
|
|
DCCSendAhead = true;
|
|
|
|
|
|
|
|
// Set up variables
|
|
|
|
variables["address"] = "$ip";
|
2009-05-05 21:10:34 +00:00
|
|
|
// Register aliases
|
|
|
|
this->scriptManager->register_alias("dcc", bind(&MessageHandler::alias_dcc, this, _1));
|
|
|
|
this->scriptManager->register_alias("echo", bind(&MessageHandler::alias_echo, this, _1));
|
2009-05-05 21:29:32 +00:00
|
|
|
this->scriptManager->register_alias("ip", bind(&MessageHandler::alias_ip, this, _1));
|
2009-05-05 21:10:34 +00:00
|
|
|
this->scriptManager->register_alias("join", bind(&MessageHandler::alias_join, this, _1));
|
|
|
|
this->scriptManager->register_alias("msg", bind(&MessageHandler::alias_msg, this, _1));
|
|
|
|
this->scriptManager->register_alias("nick", bind(&MessageHandler::alias_nick, this, _1));
|
|
|
|
this->scriptManager->register_alias("notice", bind(&MessageHandler::alias_notice, this, _1));
|
|
|
|
this->scriptManager->register_alias("privmsg", bind(&MessageHandler::alias_msg, this, _1));
|
2009-05-05 21:29:32 +00:00
|
|
|
this->scriptManager->register_alias("say", bind(&MessageHandler::alias_say, this, _1));
|
2009-05-05 21:10:34 +00:00
|
|
|
this->scriptManager->register_alias("quit", bind(&MessageHandler::alias_quit, this, _1));
|
2008-08-31 09:08:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MessageHandler::_addWindow( QString name, QString defaultCmd, bool focusOnOpen ) {
|
|
|
|
if( windows.keys().contains( name ) ) { return; }
|
|
|
|
int index = this->addTab( new ChatWindow(), name );
|
|
|
|
windows[name] = (ChatWindow*)this->widget( index );
|
|
|
|
windows[name]->setDefaultCmd( defaultCmd );
|
|
|
|
connect( windows[name], SIGNAL( doCmd( QString ) ), this, SLOT( processCmd( QString ) ) );
|
|
|
|
windows[name]->setupUi( windows[name] );
|
|
|
|
if( focusOnOpen ) {
|
|
|
|
this->setCurrentIndex( index );
|
|
|
|
windows[name]->input->setFocus();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QString MessageHandler::parseVar( QString text ) {
|
|
|
|
if( !text.startsWith( '$' ) ) { return text; }
|
|
|
|
QString var = text.right( text.length() - 1 ).toLower();
|
|
|
|
QString value;
|
|
|
|
|
|
|
|
if( var == "ip" ) { value = irc->getIPAddress(); }
|
|
|
|
if( var == "nick" || var == "nickname" ) { value = irc->getNickName(); }
|
|
|
|
|
|
|
|
if( value.isNull() && variables.keys().contains( var ) ) { value = variables[var].toString(); }
|
|
|
|
return ( value.isNull() || value == text ) ? text : parseVar( value ); // Allows for recursive vars, while avoiding an infinite loop
|
|
|
|
}
|
|
|
|
|
|
|
|
void MessageHandler::messageRcvd( QString type, QString src, QString dest, QStringList values, QString text ) {
|
|
|
|
QString window = "status";
|
|
|
|
QString source = "server";
|
|
|
|
QString message = text;
|
|
|
|
|
|
|
|
if( src.startsWith( ':' ) ) { src = src.right( src.length() - 1 ); }
|
|
|
|
|
|
|
|
// Handle special messages first
|
|
|
|
if( type == "JOIN" ) {
|
2009-05-08 20:58:19 +00:00
|
|
|
_addWindow( text, "say", true );
|
2008-08-31 09:08:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if( !dest.isEmpty() && dest.startsWith( '#' ) ) {
|
|
|
|
window = dest;
|
|
|
|
}
|
|
|
|
if( !src.isEmpty() && src.contains( '!' ) && src.contains( '@' ) ) {
|
|
|
|
QStringList split = src.split( '!' );
|
|
|
|
source = split[0];
|
|
|
|
if( dest == irc->getNickName() && type == "PRIVMSG" ) {
|
|
|
|
window = source;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( type == "CTCP" ) {
|
|
|
|
values = text.split( " " );
|
|
|
|
QString ctcpCommand = values.takeFirst().toUpper();
|
|
|
|
if( ctcpCommand == "VERSION" ) {
|
|
|
|
irc->notice( source, tr( "Atma2 IRC Client %1" ).arg( 0.01 ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//emit dispatchMessage( window, src, message );
|
2009-05-08 20:58:19 +00:00
|
|
|
_addWindow( window, "say" );
|
2008-08-31 09:08:24 +00:00
|
|
|
windows[window]->message( source, message );
|
|
|
|
}
|
|
|
|
|
|
|
|
void MessageHandler::messageSentDCC( QString nickName, QString text ) {
|
|
|
|
QString window = nickName.prepend( "=" );
|
2009-05-08 20:58:19 +00:00
|
|
|
_addWindow( window, "say" );
|
2008-08-31 09:08:24 +00:00
|
|
|
windows[window]->message( irc->getNickName(), text );
|
|
|
|
}
|
|
|
|
|
|
|
|
void MessageHandler::messageRcvdDCC( QString nickName, QString text ) {
|
|
|
|
QString window = "=" + nickName;
|
2009-05-08 20:58:19 +00:00
|
|
|
_addWindow( window, "say" );
|
2008-08-31 09:08:24 +00:00
|
|
|
windows[window]->message( nickName, text );
|
|
|
|
}
|
|
|
|
|
|
|
|
void MessageHandler::processCmd( QString cmdText ) {
|
|
|
|
qDebug() << cmdText;
|
2009-04-17 05:05:43 +00:00
|
|
|
|
|
|
|
MIRCScript *script = new MIRCScript(this->scriptManager);
|
|
|
|
script->parse(cmdText);
|
|
|
|
script->run();
|
|
|
|
}
|
|
|
|
void MessageHandler::autoRun() {
|
|
|
|
//irc->join( "#pinet" );
|
|
|
|
}
|
|
|
|
|
|
|
|
void MessageHandler::startDCC( QString nickName, QString fileName ) {
|
|
|
|
for( int i = DCCPortMin; i <= DCCPortMax; i++ ) {
|
|
|
|
if( !DCCServers.keys().contains( i ) ) {
|
|
|
|
QString message = "DCC %1";
|
|
|
|
if( fileName.isEmpty() ) {
|
|
|
|
// Chat session
|
|
|
|
DCCServers[i] = new DCCChatServer( nickName );
|
|
|
|
connect( ((DCCChatServer*)DCCServers[i]), SIGNAL( connectedChat( QString ) ), this, SLOT( connectedDCCChat( QString ) ) );
|
|
|
|
DCCNicks[nickName] = i;
|
|
|
|
message = message.arg( "CHAT chat %1 %2" );
|
|
|
|
} else {
|
|
|
|
// File Send
|
|
|
|
QFileInfo fileInfo = QFileInfo( fileName );
|
|
|
|
if( !fileInfo.exists() ) { return; }
|
|
|
|
DCCServers[i] = new DCCFileServer( fileName, DCCSendAhead );
|
|
|
|
message = message.arg( "SEND %1 %3 %2" ).arg( fileInfo.fileName() ).arg( fileInfo.size() ).arg( "%1 %2" );
|
|
|
|
}
|
|
|
|
bool success = DCCServers[i]->listen( QHostAddress::Any, i );
|
|
|
|
qDebug() << QString( "Listen on port %1: %2" ).arg( i ).arg( success );
|
|
|
|
message = message.arg( irc->localHost.toIPv4Address() ).arg( i );
|
|
|
|
qDebug() << message;
|
|
|
|
irc->ctcp( nickName, message );
|
|
|
|
break;
|
2008-08-31 09:08:24 +00:00
|
|
|
}
|
|
|
|
}
|
2009-04-17 05:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MessageHandler::connectedDCCChat( QString nickName ) {
|
|
|
|
DCCChatServer *dcc = ((DCCChatServer*)DCCServers[DCCNicks[nickName]]);
|
|
|
|
connect( dcc, SIGNAL( rcvdText( QString, QString ) ), this, SLOT( messageRcvdDCC( QString, QString ) ) );
|
|
|
|
connect( dcc, SIGNAL( sentText( QString, QString ) ), this, SLOT( messageSentDCC( QString, QString ) ) );
|
|
|
|
QString window = nickName.prepend( "=" );
|
2009-05-08 20:58:19 +00:00
|
|
|
_addWindow( window, "say" );
|
2009-04-17 05:05:43 +00:00
|
|
|
windows[window]->echo( "DCC Chat Connected" );
|
|
|
|
}
|
|
|
|
|
|
|
|
void MessageHandler::sentRAW( QString text ) { qDebug() << ">>> " << text.trimmed(); }
|
|
|
|
void MessageHandler::rcvdRAW( QString text ) { qDebug() << "<<< " << text.trimmed(); }
|
|
|
|
|
2009-05-05 21:10:34 +00:00
|
|
|
void MessageHandler::alias_connect(QStringList args) {
|
|
|
|
if( args.count() < 1 ) { return; }
|
|
|
|
irc->quit( tr( "Changing servers" ) );
|
|
|
|
QString host = args.takeFirst();
|
|
|
|
int port = 0;
|
|
|
|
if( host.contains( ':' ) ) {
|
|
|
|
QStringList splitHost = host.split( ':' );
|
|
|
|
host = splitHost.takeFirst();
|
|
|
|
port = splitHost.join( "" ).toInt();
|
|
|
|
}
|
|
|
|
port = port > 0 ? port : 6667;
|
|
|
|
QString nickName = args.count() > 0 ? args.takeFirst() : irc->getNickName();
|
|
|
|
QString userName = args.count() > 0 ? args.takeFirst() : irc->getUserName();
|
|
|
|
QString realName = args.count() > 0 ? args.join( " " ) : irc->getRealName();
|
|
|
|
irc->connectAndRegister(
|
|
|
|
host, // Host
|
|
|
|
port > 0 ? port : 6667, // Port
|
|
|
|
nickName,
|
|
|
|
userName,
|
|
|
|
realName );
|
|
|
|
}
|
|
|
|
void MessageHandler::alias_ctcp(QStringList args) {
|
|
|
|
if( args.count() < 2 ) { return; }
|
|
|
|
QString target = args.takeFirst();
|
|
|
|
irc->ctcp( target, args.join( " " ) );
|
|
|
|
}
|
|
|
|
void MessageHandler::alias_dcc(QStringList args) {
|
|
|
|
if( args.count() < 2 ) { return; }
|
|
|
|
QString type = args.takeFirst();
|
|
|
|
QString nick = args.takeFirst();
|
|
|
|
if( type == "chat" ) {
|
|
|
|
startDCC( nick );
|
|
|
|
} else if( type == "send" ) {
|
|
|
|
if( args.count() == 0 ) { return; }
|
|
|
|
startDCC( nick, args.join( " " ) );
|
2008-08-31 09:08:24 +00:00
|
|
|
}
|
2009-05-05 21:10:34 +00:00
|
|
|
}
|
|
|
|
void MessageHandler::alias_echo(QStringList args) {
|
|
|
|
((ChatWindow*)this->currentWidget())->echo(args.join(" "));
|
2008-08-31 09:08:24 +00:00
|
|
|
}
|
2009-05-05 21:29:32 +00:00
|
|
|
void MessageHandler::alias_ip(QStringList args) {
|
|
|
|
scriptManager->return_value(irc->getIPAddress());
|
|
|
|
}
|
2009-04-17 05:05:43 +00:00
|
|
|
void MessageHandler::alias_join(QStringList args) {
|
|
|
|
irc->join( args.join( "," ) );
|
2008-08-31 09:08:24 +00:00
|
|
|
}
|
2009-04-17 05:05:43 +00:00
|
|
|
void MessageHandler::alias_msg(QStringList args) {
|
2009-05-05 21:10:34 +00:00
|
|
|
if( args.count() < 2 ) { return; }
|
|
|
|
QString target = args.takeFirst();
|
|
|
|
if( target.startsWith( "=") ) {
|
|
|
|
// DCC Chat
|
|
|
|
((DCCChatServer*)DCCServers[DCCNicks[target.right( target.length() - 1 )]])->sendText( args.join( " " ) );
|
|
|
|
} else {
|
|
|
|
irc->msg( target, args.join( " " ) );
|
|
|
|
}
|
|
|
|
// Update existing chat windows with the sent message
|
|
|
|
QString window;
|
|
|
|
QStringList windows = target.split( "," );
|
|
|
|
foreach( window, windows ) {
|
|
|
|
if( !window.startsWith( "=" ) && this->windows.keys().contains( window ) ) { this->windows[window]->message( irc->getNickName(), args.join( " " ) ); }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void MessageHandler::alias_nick(QStringList args) {
|
2009-05-08 20:58:19 +00:00
|
|
|
if( args.count() > 0 ) {
|
2009-05-05 21:10:34 +00:00
|
|
|
irc->nick( args[0] );
|
|
|
|
}
|
|
|
|
this->scriptManager->return_value(irc->getNickName());
|
|
|
|
}
|
|
|
|
void MessageHandler::alias_notice(QStringList args) {
|
|
|
|
if( args.count() < 2 ) { return; }
|
|
|
|
QString dest = args.takeFirst();
|
|
|
|
irc->notice( dest, args.join( " " ) );
|
|
|
|
}
|
2009-05-05 21:29:32 +00:00
|
|
|
void MessageHandler::alias_say(QStringList args) {
|
|
|
|
QString target = this->tabText(this->currentIndex());
|
|
|
|
if (target != "status") {
|
|
|
|
alias_msg(QStringList(target) + args);
|
|
|
|
}
|
|
|
|
}
|
2009-05-05 21:10:34 +00:00
|
|
|
void MessageHandler::alias_quit(QStringList args) {
|
|
|
|
if( args.count() > 0 ) { irc->quit( args.join( " " ) ); }
|
|
|
|
else { irc->quit(); }
|
2008-08-31 09:08:24 +00:00
|
|
|
}
|