Expanded alias registration to work on member functions.

git-svn-id: file:///srv/svn/ircclient/trunk@11 a9804ffe-773b-11dd-bd7c-89c3ef1d2733
This commit is contained in:
Correl Roush 2009-05-05 21:10:34 +00:00
parent 0e773a5687
commit d9a1aae58f
4 changed files with 144 additions and 116 deletions

View file

@ -3,7 +3,7 @@
MessageHandler::MessageHandler( IRCClient *irc, QWidget *parent ) : QTabWidget( parent ) { MessageHandler::MessageHandler( IRCClient *irc, QWidget *parent ) : QTabWidget( parent ) {
this->scriptManager = new MIRCScriptManager(); 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; this->irc = irc;
connect( irc, SIGNAL( messageRcvd( QString, QString, QString, QStringList, QString ) ), this, SLOT( messageRcvd( QString, QString, QString, QStringList, QString ) ) ); 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( registered() ), this, SLOT( autoRun() ) );
@ -19,6 +19,15 @@ MessageHandler::MessageHandler( IRCClient *irc, QWidget *parent ) : QTabWidget(
// Set up variables // Set up variables
variables["address"] = "$ip"; 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 ) { void MessageHandler::_addWindow( QString name, QString defaultCmd, bool focusOnOpen ) {
@ -144,10 +153,8 @@ void MessageHandler::connectedDCCChat( QString nickName ) {
void MessageHandler::sentRAW( QString text ) { qDebug() << ">>> " << text.trimmed(); } void MessageHandler::sentRAW( QString text ) { qDebug() << ">>> " << text.trimmed(); }
void MessageHandler::rcvdRAW( QString text ) { qDebug() << "<<< " << text.trimmed(); } void MessageHandler::rcvdRAW( QString text ) { qDebug() << "<<< " << text.trimmed(); }
bool MessageHandler::call_alias(QString alias, QStringList args) { void MessageHandler::alias_connect(QStringList args) {
QStringList newargs; if( args.count() < 1 ) { return; }
if( alias == "server" || alias == "connect" ) {
if( args.count() < 1 ) { return true; }
irc->quit( tr( "Changing servers" ) ); irc->quit( tr( "Changing servers" ) );
QString host = args.takeFirst(); QString host = args.takeFirst();
int port = 0; int port = 0;
@ -166,62 +173,57 @@ bool MessageHandler::call_alias(QString alias, QStringList args) {
nickName, nickName,
userName, userName,
realName ); 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 void MessageHandler::alias_ctcp(QStringList args) {
QString window; if( args.count() < 2 ) { return; }
QStringList windows = newargs[0].split( "," ); QString target = args.takeFirst();
foreach( window, windows ) { irc->ctcp( target, args.join( " " ) );
if( !window.startsWith( "=" ) && this->windows.keys().contains( window ) ) { this->windows[window]->message( irc->getNickName(), args.join( " " ) ); }
} }
} else if( alias == "notice" ) { void MessageHandler::alias_dcc(QStringList args) {
if( args.count() < 2 ) { return true; } if( args.count() < 2 ) { return; }
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 type = args.takeFirst();
QString nick = args.takeFirst(); QString nick = args.takeFirst();
if( type == "chat" ) { if( type == "chat" ) {
startDCC( nick ); startDCC( nick );
} else if( type == "send" ) { } else if( type == "send" ) {
if( args.count() == 0 ) { return true; } if( args.count() == 0 ) { return; }
startDCC( nick, args.join( " " ) ); 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;
} }
return true; void MessageHandler::alias_echo(QStringList args) {
((ChatWindow*)this->currentWidget())->echo(args.join(" "));
} }
void MessageHandler::alias_join(QStringList args) { void MessageHandler::alias_join(QStringList args) {
irc->join( args.join( "," ) ); irc->join( args.join( "," ) );
} }
void MessageHandler::alias_msg(QStringList args) { 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(); }
} }

View file

@ -25,10 +25,16 @@ private:
public: public:
MessageHandler( IRCClient *irc, QWidget *parent = 0 ); 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_join(QStringList args);
void alias_msg(QStringList args); void alias_msg(QStringList args);
void alias_nick(QStringList args);
void alias_notice(QStringList args);
void alias_quit(QStringList args);
public slots: public slots:
bool call_alias(QString alias, QStringList args);
void messageRcvd( QString type, QString src, QString dest, QStringList values, QString text ); void messageRcvd( QString type, QString src, QString dest, QStringList values, QString text );
void messageSentDCC( QString nickName, QString text ); void messageSentDCC( QString nickName, QString text );
void messageRcvdDCC( QString nickName, QString text ); void messageRcvdDCC( QString nickName, QString text );

View file

@ -15,13 +15,15 @@
class MIRCScript; class MIRCScript;
typedef function< void(QStringList) > _MIRCAliasHandler;
class MIRCScriptManager : public QObject { class MIRCScriptManager : public QObject {
Q_OBJECT Q_OBJECT
private: private:
QObject *parent; QObject *parent;
QVector<MIRCScript*> scripts; QVector<MIRCScript*> scripts;
QMap<QString, QString> _variables; QMap<QString, QString> _variables;
QMap<QString,void (*)(QStringList)> internal_aliases; QMap<QString,_MIRCAliasHandler> internal_aliases;
QString _return_value; QString _return_value;
QTextStream *output; QTextStream *output;
MIRCScript* current_script; MIRCScript* current_script;
@ -35,13 +37,19 @@ public:
bool unload(QString filename); bool unload(QString filename);
*/ */
void call_alias(QString alias, QStringList arguments); 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); bool hasVariable(QString variable);
QString variable(QString variable); QString variable(QString variable);
void variable(QString variable, QString value); void variable(QString variable, QString value);
QMap<QString, QString> variables(); QMap<QString, QString> variables();
QString return_value(); 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: public slots:
void return_value(QString value); void return_value(QString value);
signals: signals:

View file

@ -3,6 +3,11 @@
MIRCScriptManager::MIRCScriptManager(QObject *parent) { MIRCScriptManager::MIRCScriptManager(QObject *parent) {
this->parent = parent; this->parent = parent;
line_offset = 0; 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) { bool MIRCScriptManager::load(QString filename) {
@ -37,17 +42,7 @@ void MIRCScriptManager::call_alias(QString alias, QStringList arguments) {
if (internal_aliases.find(alias) != internal_aliases.end()) { if (internal_aliases.find(alias) != internal_aliases.end()) {
internal_aliases[alias](arguments); internal_aliases[alias](arguments);
} else { } else {
// Handle some builtins // Search scripted aliases
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; bool found = false;
int script_index = 0; int script_index = 0;
for (int i = 0; i < scripts.size(); i++) { for (int i = 0; i < scripts.size(); i++) {
@ -69,23 +64,40 @@ void MIRCScriptManager::call_alias(QString alias, QStringList arguments) {
s->run(); s->run();
} else { } else {
qDebug() << "SYNTAX ERROR IN " << alias; qDebug() << "SYNTAX ERROR IN " << alias;
return_value("");
} }
current_script = p; current_script = p;
line_offset = offset; line_offset = offset;
delete s; delete s;
} else { } else {
qDebug() << "UNKNOWN ALIAS" << alias; qDebug() << "UNKNOWN ALIAS" << alias;
return_value("");
emit unknown_alias(alias, arguments); emit unknown_alias(alias, arguments);
} }
} }
} }
}
QString MIRCScriptManager::return_value() { QString MIRCScriptManager::return_value() {
return _return_value; return _return_value;
} }
void MIRCScriptManager::return_value(QString value) { void MIRCScriptManager::return_value(QString value) {
_return_value = 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; 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);
}