Skip to content

Commit

Permalink
irc: add --host option
Browse files Browse the repository at this point in the history
This is _only_ intended for podman containers.

Do NOT expose this ricochet-irc to a LAN or WAN.
--host only works if --i-know-what-i-am-doing is also specified.
  • Loading branch information
wfr committed Jan 30, 2025
1 parent 8e135a0 commit 2a9b73d
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 7 deletions.
16 changes: 13 additions & 3 deletions src/irc/IrcServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,14 @@
#include <QException>
#include <QDebug>

IrcServer::IrcServer(QObject *parent, uint16_t port, const QString& password)
: QObject(parent), m_port(port), m_password(password)
IrcServer::IrcServer(QObject *parent,
const QHostAddress& host,
uint16_t port,
const QString& password)
: QObject(parent),
m_host(host),
m_port(port),
m_password(password)
{
}

Expand All @@ -62,7 +68,7 @@ IrcServer::~IrcServer()
bool IrcServer::run()
{
tcpServer = new QTcpServer(this);
if(tcpServer->listen(QHostAddress::LocalHost, m_port))
if(tcpServer->listen(m_host, m_port))
{
connect(tcpServer,
SIGNAL(newConnection()),
Expand Down Expand Up @@ -133,6 +139,10 @@ const QString IrcServer::getWelcomeMessage()
return QString(QStringLiteral("Welcome to a local IRC server!"));
}

const QHostAddress& IrcServer::host() {
return m_host;
}

uint16_t IrcServer::port() {
return m_port;
}
Expand Down
3 changes: 3 additions & 0 deletions src/irc/IrcServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class IrcServer : public QObject
Q_OBJECT
public:
explicit IrcServer(QObject *parent = 0,
const QHostAddress& host = QHostAddress::LocalHost,
uint16_t port = 6667,
const QString& password = QStringLiteral(""));
~IrcServer();
Expand All @@ -73,6 +74,7 @@ class IrcServer : public QObject

virtual const QString getWelcomeMessage();

const QHostAddress& host();
uint16_t port();
const QString& password() const;

Expand Down Expand Up @@ -112,6 +114,7 @@ public slots:


protected:
QHostAddress m_host;
uint16_t m_port;
QString m_password;
QString welcome_message;
Expand Down
6 changes: 4 additions & 2 deletions src/irc/RicochetIrcServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,11 @@


RicochetIrcServer::RicochetIrcServer(QObject *parent,
uint16_t port, const QString& password,
const QHostAddress& host,
uint16_t port,
const QString& password,
const QString& control_channel_name)
: IrcServer(parent, port, password),
: IrcServer(parent, host, port, password),
control_channel_name(control_channel_name)
{

Expand Down
1 change: 1 addition & 0 deletions src/irc/RicochetIrcServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class RicochetIrcServer : public IrcServer
Q_OBJECT
public:
explicit RicochetIrcServer(QObject *parent = nullptr,
const QHostAddress& host = QHostAddress::LocalHost,
uint16_t port = 6667,
const QString& password = QStringLiteral(""),
const QString& control_channel_name = QStringLiteral("#ricochet"));
Expand Down
5 changes: 3 additions & 2 deletions src/irc/RicochetIrcServerTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@ RicochetIrcServerTask::RicochetIrcServerTask(QCoreApplication* app)
: QObject(app), irc_server(nullptr)
{
SettingsObject settings;
QHostAddress host(settings.read("irc.host", "127.0.0.1").toString());
uint16_t port = static_cast<uint16_t>(settings.read("irc.port", 6667).toInt());
QString password = settings.read("irc.password", QLatin1String("")).toString();
assert(password.length());

irc_server = new RicochetIrcServer(this, port, password);
irc_server = new RicochetIrcServer(this, host, port, password);
}

void RicochetIrcServerTask::run()
Expand All @@ -42,7 +43,7 @@ void RicochetIrcServerTask::run()

std::cout << std::endl;
std::cout << "## IRC server started:" << std::endl;
std::cout << "Host: 127.0.0.1" << std::endl;
std::cout << "Host: " << irc_server->host().toString() << std::endl;
std::cout << "Port: " << irc_server->port() << std::endl;
std::cout << "Password: " << irc_server->password().toUtf8().data() << std::endl;
std::cout << std::endl;
Expand Down
18 changes: 18 additions & 0 deletions src/irc/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,14 @@ static bool initSettings(SettingsFile *settings, QLockFile **lockFile, QString &
QStringLiteral("config-path"),
defaultConfigPath);
parser.addOption(opt_config_path);
QCommandLineOption opt_irc_host(QStringLiteral("host"),
QCoreApplication::translate("irc", "Set IRC server host."),
QStringLiteral("host"),
QStringLiteral("127.0.0.1"));
parser.addOption(opt_irc_host);
QCommandLineOption opt_irc_expert(QStringLiteral("i-know-what-i-am-doing"),
QCoreApplication::translate("irc", "Allow --host option"));
parser.addOption(opt_irc_expert);
QCommandLineOption opt_irc_port(QStringLiteral("port"),
QCoreApplication::translate("irc", "Set IRC server port."),
QStringLiteral("port"),
Expand Down Expand Up @@ -344,6 +352,16 @@ static bool initSettings(SettingsFile *settings, QLockFile **lockFile, QString &
}

// IRC settings
if(parser.isSet(opt_irc_host)) {
if (!parser.isSet(opt_irc_expert) && parser.value("host") != "127.0.0.1") {
errorMessage = QStringLiteral("--host requires --i-know-what-i-am-doing");
return false;
}
QHostAddress host(parser.value("host"));
settings->root()->write("irc.host", host.toString());
qDebug() << "IRC server host is " << host;
}

if(parser.isSet(opt_irc_port)) {
bool ok;
int port = parser.value(QStringLiteral("port")).toInt(&ok);
Expand Down

0 comments on commit 2a9b73d

Please sign in to comment.