From d9a1aae58f1a9a7ba1896debe58357fe5cc0d7d6 Mon Sep 17 00:00:00 2001 From: Correl Roush Date: Tue, 5 May 2009 21:10:34 +0000 Subject: [PATCH] Expanded alias registration to work on member functions. git-svn-id: file:///srv/svn/ircclient/trunk@11 a9804ffe-773b-11dd-bd7c-89c3ef1d2733 --- guiapp/messagehandler.cpp | 152 +++++++++++++++++++------------------- guiapp/messagehandler.h | 8 +- mirc/includes/mirc.h | 12 ++- mirc/src/mirc.cpp | 88 ++++++++++++---------- 4 files changed, 144 insertions(+), 116 deletions(-) diff --git a/guiapp/messagehandler.cpp b/guiapp/messagehandler.cpp index cb46ddd..996b7fe 100644 --- a/guiapp/messagehandler.cpp +++ b/guiapp/messagehandler.cpp @@ -3,7 +3,7 @@ MessageHandler::MessageHandler( IRCClient *irc, QWidget *parent ) : QTabWidget( parent ) { this->scriptManager = new MIRCScriptManager(); - connect(this->scriptManager, SIGNAL( unknown_alias( QString, QStringList ) ), this, SLOT( call_alias( QString, QStringList ) ) ); + //connect(this->scriptManager, SIGNAL( unknown_alias( QString, QStringList ) ), this, SLOT( call_alias( QString, QStringList ) ) ); 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() ) ); @@ -19,6 +19,15 @@ MessageHandler::MessageHandler( IRCClient *irc, QWidget *parent ) : QTabWidget( // Set up variables variables["address"] = "$ip"; + // Register aliases + this->scriptManager->register_alias("dcc", bind(&MessageHandler::alias_dcc, this, _1)); + this->scriptManager->register_alias("echo", bind(&MessageHandler::alias_echo, this, _1)); + 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)); + this->scriptManager->register_alias("quit", bind(&MessageHandler::alias_quit, this, _1)); } void MessageHandler::_addWindow( QString name, QString defaultCmd, bool focusOnOpen ) { @@ -144,84 +153,77 @@ void MessageHandler::connectedDCCChat( QString nickName ) { void MessageHandler::sentRAW( QString text ) { qDebug() << ">>> " << text.trimmed(); } void MessageHandler::rcvdRAW( QString text ) { qDebug() << "<<< " << text.trimmed(); } -bool MessageHandler::call_alias(QString alias, QStringList args) { - QStringList newargs; - if( alias == "server" || alias == "connect" ) { - if( args.count() < 1 ) { return true; } - 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 ); - } else if( alias == "nick" ) { - if( args.count() < 1 ) { return true; } - irc->nick( args[0] ); - } else if( alias == "msg" || alias == "privmsg" ) { - if( args.count() < 2 ) { return true; } - newargs += args.takeFirst(); - if( newargs[0].startsWith( "=") ) { - // DCC Chat - ((DCCChatServer*)DCCServers[DCCNicks[newargs[0].right( newargs[0].length() - 1 )]])->sendText( args.join( " " ) ); - } else { - irc->msg( newargs[0], args.join( " " ) ); - } - // Update existing chat windows with the sent message - QString window; - QStringList windows = newargs[0].split( "," ); - foreach( window, windows ) { - if( !window.startsWith( "=" ) && this->windows.keys().contains( window ) ) { this->windows[window]->message( irc->getNickName(), args.join( " " ) ); } - } - } else if( alias == "notice" ) { - if( args.count() < 2 ) { return true; } - QString dest = args.takeFirst(); - irc->notice( dest, args.join( " " ) ); - } else if( alias == "ctcp" ) { - if( args.count() < 2 ) { return true; } - newargs += args.takeFirst(); - irc->ctcp( newargs[0], args.join( " " ) ); - } else if( alias == "join" ) { - irc->join( args.join( "," ) ); - } else if( alias == "set" ) { - QString var = args.takeFirst(); - variables[var] = args.join( " " ); - } else if( alias == "dcc" ) { - if( args.count() < 2 ) { return true; } - QString type = args.takeFirst(); - QString nick = args.takeFirst(); - if( type == "chat" ) { - startDCC( nick ); - } else if( type == "send" ) { - if( args.count() == 0 ) { return true; } - startDCC( nick, args.join( " " ) ); - } - } else if( alias == "quit" || alias == "q" ) { - if( args.count() > 0 ) { irc->quit( args.join( " " ) ); } - else { irc->quit(); } - } else if(alias == "echo") { - windows["status"]->echo(args.join(" ")); - } else { - //TODO: Script error! Unknown alias.... WHEREVER WE ARE IN WHICHEVER SCRIPT IT IS THAT WE'RE RUNNING!! - //BUT WAIT!!!!! NO!!!! The script manager is the only thing handling that!!! - //IN FACT, ALL THE ABOVE ALIASES BELONG IN THE IRC CLIENT LIBRARY, REALLY! PERHAPS? - return false; +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(); } - return true; + 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( " " ) ); + } +} +void MessageHandler::alias_echo(QStringList args) { + ((ChatWindow*)this->currentWidget())->echo(args.join(" ")); } void MessageHandler::alias_join(QStringList args) { irc->join( args.join( "," ) ); } void MessageHandler::alias_msg(QStringList args) { + 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) { + if( args.count() > 1 ) { + 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( " " ) ); +} +void MessageHandler::alias_quit(QStringList args) { + if( args.count() > 0 ) { irc->quit( args.join( " " ) ); } + else { irc->quit(); } } diff --git a/guiapp/messagehandler.h b/guiapp/messagehandler.h index 1561ac9..3b9dd71 100644 --- a/guiapp/messagehandler.h +++ b/guiapp/messagehandler.h @@ -25,10 +25,16 @@ private: public: MessageHandler( IRCClient *irc, QWidget *parent = 0 ); + void alias_connect(QStringList args); + void alias_ctcp(QStringList args); + void alias_dcc(QStringList args); + void alias_echo(QStringList args); void alias_join(QStringList args); void alias_msg(QStringList args); + void alias_nick(QStringList args); + void alias_notice(QStringList args); + void alias_quit(QStringList args); public slots: - bool call_alias(QString alias, QStringList args); void messageRcvd( QString type, QString src, QString dest, QStringList values, QString text ); void messageSentDCC( QString nickName, QString text ); void messageRcvdDCC( QString nickName, QString text ); diff --git a/mirc/includes/mirc.h b/mirc/includes/mirc.h index f8c95dc..9ef153a 100644 --- a/mirc/includes/mirc.h +++ b/mirc/includes/mirc.h @@ -15,13 +15,15 @@ class MIRCScript; +typedef function< void(QStringList) > _MIRCAliasHandler; + class MIRCScriptManager : public QObject { Q_OBJECT private: QObject *parent; QVector scripts; QMap _variables; - QMap internal_aliases; + QMap internal_aliases; QString _return_value; QTextStream *output; MIRCScript* current_script; @@ -35,13 +37,19 @@ public: bool unload(QString filename); */ void call_alias(QString alias, QStringList arguments); - bool register_alias(QString alias, void (*fn)(QStringList)); + bool register_alias(QString alias, _MIRCAliasHandler fn); bool hasVariable(QString variable); QString variable(QString variable); void variable(QString variable, QString value); QMap variables(); QString return_value(); + + // Builtin aliases + void _alias_line(QStringList); + void _alias_lower(QStringList arguments); + void _alias_return(QStringList arguments); + void _alias_version(QStringList); public slots: void return_value(QString value); signals: diff --git a/mirc/src/mirc.cpp b/mirc/src/mirc.cpp index dd751cf..45cd88c 100644 --- a/mirc/src/mirc.cpp +++ b/mirc/src/mirc.cpp @@ -3,6 +3,11 @@ MIRCScriptManager::MIRCScriptManager(QObject *parent) { this->parent = parent; line_offset = 0; + + register_alias("line", bind(&MIRCScriptManager::_alias_line, this, _1)); + register_alias("lower", bind(&MIRCScriptManager::_alias_lower, this, _1)); + register_alias("return", bind(&MIRCScriptManager::_alias_return, this, _1)); + register_alias("version", bind(&MIRCScriptManager::_alias_version, this, _1)); } bool MIRCScriptManager::load(QString filename) { @@ -37,46 +42,37 @@ void MIRCScriptManager::call_alias(QString alias, QStringList arguments) { if (internal_aliases.find(alias) != internal_aliases.end()) { internal_aliases[alias](arguments); } else { - // Handle some builtins - if (alias == "line") { - return_value(QString::number(current_script->line() + line_offset)); - } else if (alias == "lower") { - return_value(arguments.join(" ").toLower()); - } else if (alias == "return") { - return_value(arguments.join(" ")); - //TODO: Somehow terminate the currently running script - } else if (alias == "version") { - return_value(MIRC_VERSION); - } else { - bool found = false; - int script_index = 0; - for (int i = 0; i < scripts.size(); i++) { - QMap aliases = scripts.at(i)->aliases(); - if (aliases.find(alias) != aliases.end()) { - found = true; - script_index = i; - break; - } + // Search scripted aliases + bool found = false; + int script_index = 0; + for (int i = 0; i < scripts.size(); i++) { + QMap aliases = scripts.at(i)->aliases(); + if (aliases.find(alias) != aliases.end()) { + found = true; + script_index = i; + break; } - if (found) { - MIRCScript *s = new MIRCScript(this); - MIRCScript *p = current_script; - int offset = line_offset; - line_offset = scripts.at(script_index)->aliases()[alias].line; - current_script = s; - QString code = scripts.at(script_index)->code(alias); - if (s->parse(code)) { - s->run(); - } else { - qDebug() << "SYNTAX ERROR IN " << alias; - } - current_script = p; - line_offset = offset; - delete s; + } + if (found) { + MIRCScript *s = new MIRCScript(this); + MIRCScript *p = current_script; + int offset = line_offset; + line_offset = scripts.at(script_index)->aliases()[alias].line; + current_script = s; + QString code = scripts.at(script_index)->code(alias); + if (s->parse(code)) { + s->run(); } else { - qDebug() << "UNKNOWN ALIAS" << alias; - emit unknown_alias(alias, arguments); + qDebug() << "SYNTAX ERROR IN " << alias; + return_value(""); } + current_script = p; + line_offset = offset; + delete s; + } else { + qDebug() << "UNKNOWN ALIAS" << alias; + return_value(""); + emit unknown_alias(alias, arguments); } } } @@ -86,6 +82,22 @@ QString MIRCScriptManager::return_value() { void MIRCScriptManager::return_value(QString value) { _return_value = value; } -bool MIRCScriptManager::register_alias(QString alias, void (*fn)(QStringList)) { +bool MIRCScriptManager::register_alias(QString alias, _MIRCAliasHandler fn) { internal_aliases[alias] = fn; } + +// Built-in aliases + +void MIRCScriptManager::_alias_line(QStringList) { + return_value(QString::number(current_script->line() + line_offset)); +} +void MIRCScriptManager::_alias_lower(QStringList arguments) { + return_value(arguments.join(" ").toLower()); +} +void MIRCScriptManager::_alias_return(QStringList arguments) { + return_value(arguments.join(" ")); + //TODO: Somehow terminate the currently running script +} +void MIRCScriptManager::_alias_version(QStringList) { + return_value(MIRC_VERSION); +}