The complete source code of IdealIRC
http://www.idealirc.org/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
214 lines
5.8 KiB
214 lines
5.8 KiB
#include "ScriptGeneralUtils.h"
|
|
#include "Script/Builtin/Error.h"
|
|
#include "Script/ValueExtract.h"
|
|
#include "Script/ScriptException.h"
|
|
#include "Script/Manager.h"
|
|
#include "IRC.h"
|
|
#include "IWin/IWinStatus.h"
|
|
#include "MdiManager.h"
|
|
|
|
#include <QInputDialog>
|
|
#include <QMessageBox>
|
|
#include <QDateTime>
|
|
#include <random>
|
|
|
|
using namespace Builtin::Error;
|
|
|
|
namespace ScriptFunctions {
|
|
|
|
ValueHolder input(Script&, std::vector<ValueHolder>& args)
|
|
{
|
|
if (args.size() < 2 || args.size() > 3)
|
|
throwInsufficientParameters("input", args.size(), 2);
|
|
|
|
if (args[0].getType() != ValueHolder::Type::String)
|
|
throwNotAString("input", 1);
|
|
|
|
if (args[1].getType() != ValueHolder::Type::String)
|
|
throwNotAString("input", 2);
|
|
|
|
QString defaultText;
|
|
if (args.size() == 3) {
|
|
if (args[2].getType() != ValueHolder::Type::String)
|
|
throwNotAString("input", 2);
|
|
defaultText = ValueExtract(args[2]).toString().c_str();
|
|
}
|
|
|
|
QString title(ValueExtract(args[0]).toString().c_str());
|
|
QString message(ValueExtract(args[1]).toString().c_str());
|
|
|
|
bool ok = false;
|
|
QString input = QInputDialog::getText(nullptr, title, message, QLineEdit::Normal, defaultText, &ok);
|
|
if (!ok)
|
|
return ValueHolder();
|
|
return input.toStdString();
|
|
}
|
|
|
|
ValueHolder showmessage(Script&, std::vector<ValueHolder>& args)
|
|
{
|
|
if (args.size() != 4)
|
|
throwInsufficientParameters("showmessage", args.size(), 4);
|
|
|
|
if (args[0].getType() != ValueHolder::Type::String)
|
|
throwNotAString("showmessage", 1);
|
|
|
|
if (args[1].getType() != ValueHolder::Type::String)
|
|
throwNotAString("showmessage", 2);
|
|
|
|
if (args[2].getType() != ValueHolder::Type::String)
|
|
throwNotAString("showmessage", 3);
|
|
|
|
if (args[3].getType() != ValueHolder::Type::String)
|
|
throwNotAString("showmessage", 4);
|
|
|
|
QString title(ValueExtract(args[0]).toString().c_str());
|
|
QString message(ValueExtract(args[1]).toString().c_str());
|
|
QString type(ValueExtract(args[2]).toString().c_str());
|
|
QString buttons(ValueExtract(args[3]).toString().c_str());
|
|
|
|
QMessageBox::StandardButtons dlgbtns = QMessageBox::NoButton;
|
|
for (QChar c : buttons) {
|
|
switch (c.toLatin1()) {
|
|
case 'y':
|
|
dlgbtns |= QMessageBox::Yes;
|
|
break;
|
|
case 'n':
|
|
dlgbtns |= QMessageBox::No;
|
|
break;
|
|
case 'o':
|
|
dlgbtns |= QMessageBox::Ok;
|
|
break;
|
|
case 'c':
|
|
dlgbtns |= QMessageBox::Cancel;
|
|
break;
|
|
case 'a':
|
|
dlgbtns |= QMessageBox::Abort;
|
|
break;
|
|
case 'r':
|
|
dlgbtns |= QMessageBox::Retry;
|
|
break;
|
|
case 'i':
|
|
dlgbtns |= QMessageBox::Ignore;
|
|
break;
|
|
default:
|
|
{
|
|
std::string cs;
|
|
cs += c.toLatin1();
|
|
throw ScriptException("showmessage: Unknown button type: " + cs);
|
|
}
|
|
}
|
|
}
|
|
|
|
QMessageBox::StandardButtons btnPressed;
|
|
if (type == "question")
|
|
btnPressed = QMessageBox::question(nullptr, title, message, dlgbtns);
|
|
else if (type == "info")
|
|
btnPressed = QMessageBox::information(nullptr, title, message, dlgbtns);
|
|
else if (type == "warn")
|
|
btnPressed = QMessageBox::warning(nullptr, title, message, dlgbtns);
|
|
else if (type == "crit")
|
|
btnPressed = QMessageBox::critical(nullptr, title, message, dlgbtns);
|
|
else
|
|
throw ScriptException("showmessage: Unknown message box type: " + type.toStdString());
|
|
|
|
switch (btnPressed) {
|
|
case QMessageBox::Yes:
|
|
return std::string("y");
|
|
case QMessageBox::No:
|
|
return std::string("n");
|
|
case QMessageBox::Abort:
|
|
return std::string("a");
|
|
case QMessageBox::Retry:
|
|
return std::string("r");
|
|
case QMessageBox::Ignore:
|
|
return std::string("i");
|
|
case QMessageBox::Ok:
|
|
return std::string("o");
|
|
case QMessageBox::Cancel: [[fallthrough]];
|
|
default:
|
|
return std::string("c");
|
|
}
|
|
}
|
|
|
|
ValueHolder context(Script&, std::vector<ValueHolder>& args)
|
|
{
|
|
if (!args.empty())
|
|
throwInsufficientParameters("context", args.size(), 0);
|
|
|
|
const auto& connection = ScriptManager::getContext()->getConnection();
|
|
|
|
ValueArray ret;
|
|
|
|
ret.emplace("online", new ValueHolder{ connection.isOnline() });
|
|
ret.emplace("ssl", new ValueHolder{ connection.isSSL() });
|
|
|
|
ret.emplace("host", new ValueHolder{ connection.getHostname() });
|
|
ret.emplace("port", new ValueHolder{ connection.getPort() });
|
|
|
|
ret.emplace("nickname", new ValueHolder{ connection.getNickname() });
|
|
|
|
return ValueHolder(ret);
|
|
}
|
|
|
|
ValueHolder unixts(Script&, std::vector<ValueHolder>& args)
|
|
{
|
|
if (args.empty())
|
|
throwInsufficientParameters("unixts", args.size(), 1);
|
|
|
|
qint64 ts = ValueExtract(args[0]).toInt();
|
|
auto datetime = QDateTime::fromSecsSinceEpoch(ts);
|
|
if (args.size() > 1) {
|
|
QString format = ValueExtract(args[1]).toString().c_str();
|
|
return datetime.toString(format).toStdString();
|
|
}
|
|
else
|
|
return datetime.toString(Qt::ISODate).toStdString();
|
|
}
|
|
|
|
ValueHolder unixnow(Script&, std::vector<ValueHolder>& /*args*/)
|
|
{
|
|
return static_cast<int>( QDateTime::currentSecsSinceEpoch() );
|
|
}
|
|
|
|
ValueHolder now(Script&, std::vector<ValueHolder>& args)
|
|
{
|
|
if (args.empty())
|
|
return QDateTime::currentDateTime().toString(Qt::ISODate).toStdString();
|
|
else {
|
|
const auto format = QString::fromStdString( ValueExtract(args[0]).toString() );
|
|
const auto dt = QDateTime::currentDateTime();
|
|
const auto dtStr = dt.toString(format);
|
|
return dtStr.toStdString();
|
|
}
|
|
}
|
|
|
|
ValueHolder subwintype(Script&, std::vector<ValueHolder>& args)
|
|
{
|
|
if (!args.empty())
|
|
throwInsufficientParameters("subwintype", args.size(), 0);
|
|
|
|
const auto& mdiman = MdiManager::instance();
|
|
const auto* cwin = mdiman.currentWindow();
|
|
|
|
IWin::Type wintype = IWin::Type::Undefined;
|
|
if (cwin)
|
|
wintype = cwin->getType();
|
|
|
|
return IWin::TypeString.at(wintype).toStdString();
|
|
}
|
|
|
|
ValueHolder subwinname(Script&, std::vector<ValueHolder>& args)
|
|
{
|
|
if (!args.empty())
|
|
throwInsufficientParameters("subwinname", args.size(), 0);
|
|
|
|
const auto& mdiman = MdiManager::instance();
|
|
const auto* cwin = mdiman.currentWindow();
|
|
|
|
if (!cwin)
|
|
return ValueHolder{}; // void
|
|
|
|
return cwin->getButtonText().toStdString();
|
|
}
|
|
|
|
}
|
|
|