Public Access
IRC: join after NickServ login, keyed auto-join, SASL fallback
- With NickServ, auto-join waits for the logged-in reply (900) or 2 s
at most, so registered-only IRC channels let us in
- A failed SASL login falls back to NickServ (its own password, or the
SASL account and password)
- IDENTIFY names the account explicitly, and once logged in on a
fallback nick, REGAIN takes ours back from a stale session
- Auto-join entries take keys ("#private key, #public"); keys from
/join are reused when rejoining; keyed channels go first in JOIN
- Serial irc dump: whole Buffers, and which login is configured
(never the secrets)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EhqxQ49eCju4CzKYNjZzwT
This commit is contained in:
+18
-13
@@ -36,7 +36,8 @@ std::string IrcConfig::validate() const {
|
||||
if (port < 1 || port > 65535) return "Port must be 1 to 65535";
|
||||
if (!validNick(nick)) return "Nick: letters, digits and []\\`_^{|}- only, not starting with a digit";
|
||||
for (auto& c : autojoin)
|
||||
if (c.size() < 2 || (c[0] != '#' && c[0] != '&')) return "IRC channels start with # or &";
|
||||
if (c.channel.size() < 2 || (c.channel[0] != '#' && c.channel[0] != '&'))
|
||||
return "Auto-join: IRC channels start with # or &, each followed by its key if it has one";
|
||||
return "";
|
||||
}
|
||||
|
||||
@@ -56,23 +57,27 @@ std::string IrcConfig::save() {
|
||||
return "";
|
||||
}
|
||||
|
||||
std::vector<std::string> IrcConfig::parseChannels(const std::string& text) {
|
||||
std::vector<std::string> out;
|
||||
std::string current;
|
||||
for (char c : text + " ") {
|
||||
if (c == ' ' || c == ',') {
|
||||
if (!current.empty()) out.push_back(current);
|
||||
current.clear();
|
||||
} else {
|
||||
current += c;
|
||||
}
|
||||
std::vector<IrcChannel> IrcConfig::parseChannels(const std::string& text) {
|
||||
std::vector<IrcChannel> out;
|
||||
std::string word;
|
||||
auto take = [&]() {
|
||||
if (word.empty()) return;
|
||||
bool channel = word[0] == '#' || word[0] == '&';
|
||||
if (!channel && !out.empty() && out.back().key.empty()) out.back().key = word;
|
||||
else out.push_back({word, ""}); // a stray word fails validation as a channel
|
||||
word.clear();
|
||||
};
|
||||
for (char c : text) {
|
||||
if (c == ' ' || c == ',') take();
|
||||
else word += c;
|
||||
}
|
||||
take();
|
||||
return out;
|
||||
}
|
||||
|
||||
std::string IrcConfig::formatChannels(const std::vector<std::string>& channels) {
|
||||
std::string IrcConfig::formatChannels(const std::vector<IrcChannel>& channels) {
|
||||
std::string out;
|
||||
for (auto& c : channels) out += (out.empty() ? "" : " ") + c;
|
||||
for (auto& c : channels) out += (out.empty() ? "" : ", ") + c.channel + (c.key.empty() ? "" : " " + c.key);
|
||||
return out;
|
||||
}
|
||||
|
||||
|
||||
@@ -7,6 +7,11 @@
|
||||
|
||||
namespace roro {
|
||||
|
||||
struct IrcChannel {
|
||||
std::string channel; // "#roro"
|
||||
std::string key; // empty when the IRC channel has none
|
||||
};
|
||||
|
||||
// The one IRC server the IRC Service connects to, persisted in internal flash.
|
||||
class IrcConfig {
|
||||
public:
|
||||
@@ -20,7 +25,7 @@ class IrcConfig {
|
||||
std::string nick;
|
||||
std::string saslUser, saslPassword; // SASL PLAIN when both are set
|
||||
std::string nickservPassword; // otherwise IDENTIFY with NickServ, if set
|
||||
std::vector<std::string> autojoin;
|
||||
std::vector<IrcChannel> autojoin;
|
||||
|
||||
// A copy is a draft the UI can edit freely; copySettingsFrom() applies one.
|
||||
IrcConfig(const IrcConfig&) = default;
|
||||
@@ -41,8 +46,10 @@ class IrcConfig {
|
||||
std::string save(); // empty on success, otherwise why it was refused
|
||||
std::string validate() const;
|
||||
|
||||
static std::vector<std::string> parseChannels(const std::string& text);
|
||||
static std::string formatChannels(const std::vector<std::string>& channels);
|
||||
// "#private key, #public": a word starting with # or & is an IRC channel, the word after it
|
||||
// (if it doesn't) its key. Spaces or commas separate entries, so "#a #b" also reads.
|
||||
static std::vector<IrcChannel> parseChannels(const std::string& text);
|
||||
static std::string formatChannels(const std::vector<IrcChannel>& channels);
|
||||
|
||||
private:
|
||||
KeyValueStore& store_;
|
||||
|
||||
+59
-12
@@ -37,6 +37,13 @@ std::pair<std::string, std::string> firstWord(const std::string& text) {
|
||||
|
||||
IrcSession::IrcSession(const IrcConfig& config) : config_(config), nick_(config.nick) {
|
||||
buffers_.push_back({config.host, IrcBuffer::Type::Server, {}, 0, false, true, ""});
|
||||
for (auto& c : config.autojoin)
|
||||
if (!c.key.empty()) keys_[lower(c.channel)] = c.key;
|
||||
}
|
||||
|
||||
void IrcSession::tick(uint32_t nowMs) {
|
||||
nowMs_ = nowMs;
|
||||
if (joinsHeld_ && nowMs - heldSinceMs_ >= kNickservWaitMs) joinChannels();
|
||||
}
|
||||
|
||||
int IrcSession::totalUnread() const {
|
||||
@@ -108,6 +115,8 @@ void IrcSession::add(int b, IrcLine::Kind kind, const std::string& nick, const s
|
||||
|
||||
void IrcSession::connected(int64_t) {
|
||||
registered_ = false;
|
||||
joinsHeld_ = false;
|
||||
saslFailed_ = false;
|
||||
quit_ = false;
|
||||
nick_ = config_.nick;
|
||||
if (!config_.saslUser.empty() && !config_.saslPassword.empty()) send("CAP REQ :sasl");
|
||||
@@ -130,18 +139,49 @@ void IrcSession::onWelcome(const IrcMessage& m, int64_t utc) {
|
||||
registered_ = true;
|
||||
nick_ = m.param(0);
|
||||
info(0, "Connected to " + config_.host + " as " + nick_, utc);
|
||||
if (!config_.nickservPassword.empty() && config_.saslUser.empty())
|
||||
send(IrcMessage::serialize("PRIVMSG", {"NickServ", "IDENTIFY " + config_.nickservPassword}));
|
||||
|
||||
std::vector<std::string> channels = config_.autojoin;
|
||||
for (auto& c : rejoin_)
|
||||
if (std::find(channels.begin(), channels.end(), c) == channels.end()) channels.push_back(c);
|
||||
rejoin_.clear();
|
||||
if (!channels.empty()) {
|
||||
std::string list;
|
||||
for (auto& c : channels) list += (list.empty() ? "" : ",") + c;
|
||||
send(IrcMessage::serialize("JOIN", {list}));
|
||||
// NickServ when it's the configured login, or as the fallback when SASL failed.
|
||||
// The account is named explicitly: we may be on a fallback nick if a stale session holds ours.
|
||||
bool sasl = !config_.saslUser.empty() && !config_.saslPassword.empty();
|
||||
std::string account = sasl ? config_.saslUser : config_.nick;
|
||||
std::string identify;
|
||||
if (!config_.nickservPassword.empty() && (!sasl || saslFailed_)) identify = account + " " + config_.nickservPassword;
|
||||
else if (sasl && saslFailed_) identify = account + " " + config_.saslPassword;
|
||||
if (!identify.empty()) {
|
||||
// IRC channels for registered users only would refuse us until NickServ has logged us in.
|
||||
send(IrcMessage::serialize("PRIVMSG", {"NickServ", "IDENTIFY " + identify}));
|
||||
joinsHeld_ = true;
|
||||
heldSinceMs_ = nowMs_;
|
||||
return;
|
||||
}
|
||||
joinChannels();
|
||||
}
|
||||
|
||||
void IrcSession::joinChannels() {
|
||||
joinsHeld_ = false;
|
||||
std::vector<std::string> channels;
|
||||
auto addOnce = [&](const std::string& c) {
|
||||
for (auto& existing : channels)
|
||||
if (lower(existing) == lower(c)) return;
|
||||
channels.push_back(c);
|
||||
};
|
||||
for (auto& c : config_.autojoin) addOnce(c.channel);
|
||||
for (auto& c : rejoin_) addOnce(c);
|
||||
rejoin_.clear();
|
||||
if (channels.empty()) return;
|
||||
|
||||
// JOIN #keyed,#open key: IRC pairs keys with the first channels listed.
|
||||
std::string keyed, open, keys;
|
||||
for (auto& c : channels) {
|
||||
auto k = keys_.find(lower(c));
|
||||
if (k != keys_.end()) {
|
||||
keyed += (keyed.empty() ? "" : ",") + c;
|
||||
keys += (keys.empty() ? "" : ",") + k->second;
|
||||
} else {
|
||||
open += (open.empty() ? "" : ",") + c;
|
||||
}
|
||||
}
|
||||
std::string list = keyed + (!keyed.empty() && !open.empty() ? "," : "") + open;
|
||||
send(keys.empty() ? IrcMessage::serialize("JOIN", {list}) : IrcMessage::serialize("JOIN", {list, keys}));
|
||||
}
|
||||
|
||||
void IrcSession::receive(const std::string& raw, int64_t utc) {
|
||||
@@ -163,11 +203,17 @@ void IrcSession::receive(const std::string& raw, int64_t utc) {
|
||||
} else if (cmd == "AUTHENTICATE" && m.param(0) == "+") {
|
||||
std::string user = config_.saslUser;
|
||||
send("AUTHENTICATE " + base64Encode(user + '\0' + user + '\0' + config_.saslPassword));
|
||||
} else if (cmd == "900") {
|
||||
// Logged in. Take our nick back from a stale session, then join.
|
||||
if (lower(nick_) != lower(config_.nick))
|
||||
send(IrcMessage::serialize("PRIVMSG", {"NickServ", "REGAIN " + config_.nick}));
|
||||
if (joinsHeld_) joinChannels();
|
||||
} else if (cmd == "903") {
|
||||
info(0, "SASL login succeeded", utc);
|
||||
send("CAP END");
|
||||
} else if (cmd == "904" || cmd == "905" || cmd == "906" || cmd == "902") {
|
||||
info(0, "SASL login failed: " + m.param(m.params.size() - 1), utc);
|
||||
info(0, "SASL login failed: " + m.param(m.params.size() - 1) + " (trying NickServ instead)", utc);
|
||||
saslFailed_ = true;
|
||||
send("CAP END");
|
||||
} else if (cmd == "PRIVMSG" || cmd == "NOTICE") {
|
||||
onPrivmsg(m, utc, cmd == "NOTICE");
|
||||
@@ -284,6 +330,7 @@ void IrcSession::command(int b, const std::string& text, int64_t utc) {
|
||||
auto [channel, key] = firstWord(rest);
|
||||
if (channel.empty()) return info(b, "Usage: /join #channel", utc);
|
||||
if (!isChannel(channel)) channel = "#" + channel;
|
||||
if (!key.empty()) keys_[lower(channel)] = key; // reused when rejoining
|
||||
send(key.empty() ? IrcMessage::serialize("JOIN", {channel}) : IrcMessage::serialize("JOIN", {channel, key}));
|
||||
} else if (verb == "part") {
|
||||
std::string channel = inChannel ? buf.name : "";
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
#include <cstdint>
|
||||
#include <deque>
|
||||
#include <map>
|
||||
#include <set>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
@@ -53,6 +54,12 @@ class IrcSession {
|
||||
|
||||
explicit IrcSession(const IrcConfig& config);
|
||||
|
||||
// Joins wait this long for NickServ to confirm a login before going ahead anyway.
|
||||
static constexpr uint32_t kNickservWaitMs = 2000;
|
||||
|
||||
// Uptime in ms, called regularly: releases joins held back for NickServ.
|
||||
void tick(uint32_t nowMs);
|
||||
|
||||
void connected(int64_t utc);
|
||||
void disconnected(int64_t utc, const std::string& reason);
|
||||
void receive(const std::string& raw, int64_t utc);
|
||||
@@ -83,6 +90,7 @@ class IrcSession {
|
||||
bool mentionsMe(const std::string& text) const;
|
||||
void onPrivmsg(const IrcMessage& m, int64_t utc, bool notice);
|
||||
void onWelcome(const IrcMessage& m, int64_t utc);
|
||||
void joinChannels();
|
||||
void command(int buffer, const std::string& text, int64_t utc);
|
||||
void say(int buffer, const std::string& text, int64_t utc, bool action);
|
||||
|
||||
@@ -94,6 +102,11 @@ class IrcSession {
|
||||
int viewing_ = -1;
|
||||
std::set<std::string> namesRequested_;
|
||||
std::vector<std::string> rejoin_; // IRC channels to join again after a reconnect
|
||||
std::map<std::string, std::string> keys_; // lower-case IRC channel -> key, from config and /join
|
||||
bool joinsHeld_ = false; // waiting for NickServ before joining
|
||||
bool saslFailed_ = false;
|
||||
uint32_t heldSinceMs_ = 0;
|
||||
uint32_t nowMs_ = 0;
|
||||
IrcEffects effects_;
|
||||
uint32_t revision_ = 0;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user