diff --git a/bin/fm-discord-lib.sh b/bin/fm-discord-lib.sh index fb655b113ec..76a5ab31170 100644 --- a/bin/fm-discord-lib.sh +++ b/bin/fm-discord-lib.sh @@ -115,6 +115,33 @@ fm_discord_load_config() { FM_DISCORD_AUTHORIZED_USERS=$(fmx_env_get FM_DISCORD_AUTHORIZED_USER_IDS "$env_file") fi export FM_DISCORD_AUTHORIZED_USERS + + # Captain command channels: an explicit opt-in list of channels where an + # authorized captain's plain message (no @mention) is a Firstmate request. + # This is NOT the polling allowlist: the allowlist sets where we poll, this + # sets where a mention-free message is treated as a command. A command channel + # that is also excluded is dropped with an explicit diagnostic (exclusion + # wins) rather than silently chosen. + if [ -n "${FM_DISCORD_COMMAND_CHANNELS+x}" ]; then + FM_DISCORD_COMMAND_CHANNEL_IDS=${FM_DISCORD_COMMAND_CHANNELS-} + else + FM_DISCORD_COMMAND_CHANNEL_IDS=$(fmx_env_get FM_DISCORD_COMMAND_CHANNELS "$env_file") + fi + if [ -n "$FM_DISCORD_COMMAND_CHANNEL_IDS" ] && [ -n "$FM_DISCORD_EXCLUDES" ]; then + local cmd_keep="" cmd_id="" + local -a cmd_ids + IFS=',' read -r -a cmd_ids <<< "$FM_DISCORD_COMMAND_CHANNEL_IDS" + for cmd_id in "${cmd_ids[@]}"; do + cmd_id=${cmd_id//[[:space:]]/} + [ -n "$cmd_id" ] || continue + case ",$FM_DISCORD_EXCLUDES," in + *",$cmd_id,"*) printf 'fm-discord: command channel %s is also excluded; exclusion wins\n' "$cmd_id" >&2 ;; + *) cmd_keep=${cmd_keep:+$cmd_keep,}$cmd_id ;; + esac + done + FM_DISCORD_COMMAND_CHANNEL_IDS=$cmd_keep + fi + export FM_DISCORD_COMMAND_CHANNEL_IDS } # Check if a request_id or context is from self-hosted Discord diff --git a/bin/fm-discord-poll.js b/bin/fm-discord-poll.js index e235eedc1c2..f660a2ce6b0 100755 --- a/bin/fm-discord-poll.js +++ b/bin/fm-discord-poll.js @@ -34,6 +34,16 @@ const excludeIds = excludeConfig const allowDMs = (process.env.FM_DISCORD_ALLOW_DMS || process.env.FM_DISCORD_DMS || "true").toLowerCase() !== "false"; const authorizedUserIds = new Set((process.env.FM_DISCORD_AUTHORIZED_USER_IDS || "").split(",").map((s) => s.trim()).filter(Boolean)); +// Captain command channels: an explicit opt-in list where an authorized +// captain's plain message (no @mention) is an inbound Firstmate request. This is +// NOT the polling allowlist. An unauthorized author is still ignored. +const commandChannels = new Set( + (process.env.FM_DISCORD_COMMAND_CHANNELS || process.env.FM_DISCORD_COMMAND_CHANNEL_IDS || "") + .split(",") + .map((s) => s.trim()) + .filter(Boolean), +); + const cursorDir = join(stateDir, "x-context"); function cursorFile(chId) { return join(cursorDir, `discord-cursor-${chId}.json`); @@ -241,18 +251,22 @@ async function main() { continue; } - // Check if mentioned, a DM, or a reply to one of the bot's own - // messages. A reply to the bot's message is an inbound command - // even without an explicit @mention: the captain answers a bot - // post by replying to it, and Discord does not add a mention for - // that. Dropping it is the silent no-reaction bug this guards. + // Check if mentioned, a DM, a reply to one of the bot's own + // messages, or a plain message in a captain command channel. A + // reply to the bot's message is an inbound command even without + // an explicit @mention: the captain answers a bot post by + // replying to it, and Discord does not add a mention for that. + // A command channel is the same idea for a dedicated console + // channel: the captain types naturally, no mention needed. Both + // still require an authorized author. const isDM = !msg.guild_id; if (isDM && !allowDMs) continue; const isMentioned = Array.isArray(msg.mentions) && msg.mentions.some((m) => m.id === botId); const contentHasBotMention = msg.content && (msg.content.includes(`<@${botId}>`) || msg.content.includes(`<@!${botId}>`)); const isReplyToBot = msg.referenced_message?.author?.id === botId; + const isCommandChannel = commandChannels.has(chId) && authorizedUserIds.has(msg.author?.id); - if (!isDM && !isMentioned && !contentHasBotMention && !isReplyToBot) { + if (!isDM && !isMentioned && !contentHasBotMention && !isReplyToBot && !isCommandChannel) { continue; } diff --git a/bin/fm-discord-poll.sh b/bin/fm-discord-poll.sh index 4ec7210fe70..cd989a6eb25 100755 --- a/bin/fm-discord-poll.sh +++ b/bin/fm-discord-poll.sh @@ -38,6 +38,7 @@ export FM_DISCORD_CHANNELS="${FM_DISCORD_CHANNELS:-}" export FM_DISCORD_EXCLUDES="${FM_DISCORD_EXCLUDES-}" export FM_DISCORD_ALLOW_DMS="${FM_DISCORD_DMS:-true}" export FM_DISCORD_AUTHORIZED_USER_IDS="${FM_DISCORD_AUTHORIZED_USERS:-}" +export FM_DISCORD_COMMAND_CHANNELS="${FM_DISCORD_COMMAND_CHANNEL_IDS:-}" # Only a token-gated run can capture anything, so the wake library is loaded # only here: without a token this stays a hard no-op that touches no state. diff --git a/docs/configuration.md b/docs/configuration.md index 560abbdd8de..363006330ad 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -697,6 +697,7 @@ Optional configuration variables in `.env`: An explicit allowlist overrides this built-in default with a warning; an explicitly configured exclusion still wins over an allowlist and produces a warning. - `FM_DISCORD_ALLOW_DMS`: `true` or `false` (defaults to `true`). - `FM_DISCORD_AUTHORIZED_USER_IDS`: comma-separated Discord user IDs allowed to answer pushed decisions, including the captain and any trusted operators; unset or empty authorizes nobody. +- `FM_DISCORD_COMMAND_CHANNELS`: optional comma-separated channel IDs where an **authorized** user's plain message (no `@mention`) is an inbound Firstmate request. This is separate from the polling allowlist (`FM_DISCORD_CHANNEL_ID`/`FM_DISCORD_ALLOWED_CHANNELS`): the bot must poll the channel, and the channel must also be explicitly listed here for mention-free capture. The built-in exclusion for `1551134713727426570` is overridden by explicitly allowlisting that channel, but an explicitly configured exclusion takes precedence over both lists. A command channel still excluded after polling configuration is dropped with a `fm-discord: command channel … is also excluded; exclusion wins` diagnostic. Unauthorized authors and bot messages are ignored. Replies and follow-ups for self-hosted Discord mentions post directly to Discord's REST API using `FM_DISCORD_BOT_TOKEN`. Quiet-mode status reports also use the first configured channel; the report command refuses to run during away mode. diff --git a/tests/fm-discord-selfhosted.test.sh b/tests/fm-discord-selfhosted.test.sh index e2dd96b825f..a65da136448 100755 --- a/tests/fm-discord-selfhosted.test.sh +++ b/tests/fm-discord-selfhosted.test.sh @@ -301,6 +301,99 @@ test_bootstrap_activation() { pass "fm-bootstrap handles FM_DISCORD_BOT_TOKEN activation and artifact generation" } +# Captain command channel: an authorized captain's plain message (no @mention) in +# an explicitly opted-in channel is an inbound Firstmate request. +test_command_channel_plain_message_is_captured() { + local home wake_out inbox + home="$TMP_ROOT/command-channel" + mkdir -p "$home/state" + make_fake_discord_node "$home" + wake_out=$(FM_TEST_REAL_NODE=$(command -v node) \ + FM_DISCORD_FAKE_MESSAGES='[{"id":"1352000000000003001","channel_id":"1000000000000000009","guild_id":"1000000000000000000","author":{"id":"8000000000000000001","username":"captain"},"mentions":[],"content":"PR 94 다시 검사해","attachments":[]}]' \ + PATH="$home/fake-bin:$BASE_PATH" FM_HOME="$home" FM_STATE_OVERRIDE="$home/state" \ + FM_DISCORD_BOT_TOKEN=fake-test-token FM_DISCORD_CHANNEL_ID="1000000000000000009" \ + FM_DISCORD_AUTHORIZED_USER_IDS="8000000000000000001" FM_DISCORD_COMMAND_CHANNELS="1000000000000000009" \ + "$ROOT/bin/fm-discord-poll.sh") + assert_equals "x-mention discord-sh-1352000000000003001" "$wake_out" "command-channel plain message wakes firstmate" + inbox="$home/state/x-inbox/discord-sh-1352000000000003001.json" + assert_present "$inbox" "command-channel message captured" + assert_equals "PR 94 다시 검사해" "$(jq -r '.text' "$inbox")" "plain text preserved without a mention" + assert_equals "discord-selfhosted" "$(jq -r '.source' "$inbox")" "command-channel input normalizes to the canonical source" + pass "an authorized captain's plain message in a command channel is captured" +} + +test_command_channel_unauthorized_author_is_ignored() { + local home wake_out + home="$TMP_ROOT/command-channel-unauth" + mkdir -p "$home/state" + make_fake_discord_node "$home" + wake_out=$(FM_TEST_REAL_NODE=$(command -v node) \ + FM_DISCORD_FAKE_MESSAGES='[{"id":"1352000000000003002","channel_id":"1000000000000000009","guild_id":"1000000000000000000","author":{"id":"8000000000000000999","username":"stranger"},"mentions":[],"content":"do something","attachments":[]}]' \ + PATH="$home/fake-bin:$BASE_PATH" FM_HOME="$home" FM_STATE_OVERRIDE="$home/state" \ + FM_DISCORD_BOT_TOKEN=fake-test-token FM_DISCORD_CHANNEL_ID="1000000000000000009" \ + FM_DISCORD_AUTHORIZED_USER_IDS="8000000000000000001" FM_DISCORD_COMMAND_CHANNELS="1000000000000000009" \ + "$ROOT/bin/fm-discord-poll.sh") + [ -z "$wake_out" ] || fail "unauthorized author must not wake (got: $wake_out)" + assert_absent "$home/state/x-inbox/discord-sh-1352000000000003002.json" "unauthorized author not captured" + pass "an unauthorized author is ignored even in a command channel" +} + +test_command_channel_plain_message_ignored_in_ordinary_channel() { + local home wake_out + home="$TMP_ROOT/command-channel-ordinary" + mkdir -p "$home/state" + make_fake_discord_node "$home" + wake_out=$(FM_TEST_REAL_NODE=$(command -v node) \ + FM_DISCORD_FAKE_MESSAGES='[{"id":"1352000000000003003","channel_id":"1000000000000000008","guild_id":"1000000000000000000","author":{"id":"8000000000000000001","username":"captain"},"mentions":[],"content":"no mention here","attachments":[]}]' \ + PATH="$home/fake-bin:$BASE_PATH" FM_HOME="$home" FM_STATE_OVERRIDE="$home/state" \ + FM_DISCORD_BOT_TOKEN=fake-test-token FM_DISCORD_CHANNEL_ID="1000000000000000008" \ + FM_DISCORD_AUTHORIZED_USER_IDS="8000000000000000001" FM_DISCORD_COMMAND_CHANNELS="1000000000000000009" \ + "$ROOT/bin/fm-discord-poll.sh") + [ -z "$wake_out" ] || fail "ordinary channel plain message must not wake (got: $wake_out)" + assert_absent "$home/state/x-inbox/discord-sh-1352000000000003003.json" "ordinary channel plain message not captured" + pass "a plain message in an ordinary channel is still ignored" +} + +test_command_channel_duplicate_poll_is_idempotent() { + local home count + home="$TMP_ROOT/command-channel-idem" + mkdir -p "$home/state" + make_fake_discord_node "$home" + run_poll() { + FM_TEST_REAL_NODE=$(command -v node) \ + FM_DISCORD_FAKE_MESSAGES='[{"id":"1352000000000003004","channel_id":"1000000000000000009","guild_id":"1000000000000000000","author":{"id":"8000000000000000001","username":"captain"},"mentions":[],"content":"twice","attachments":[]}]' \ + PATH="$home/fake-bin:$BASE_PATH" FM_HOME="$home" FM_STATE_OVERRIDE="$home/state" \ + FM_DISCORD_BOT_TOKEN=fake-test-token FM_DISCORD_CHANNEL_ID="1000000000000000009" \ + FM_DISCORD_AUTHORIZED_USER_IDS="8000000000000000001" FM_DISCORD_COMMAND_CHANNELS="1000000000000000009" \ + "$ROOT/bin/fm-discord-poll.sh" >/dev/null + } + run_poll + run_poll + count=0 + for inbox_file in "$home/state/x-inbox"/*discord-sh-1352000000000003004*; do + [ -e "$inbox_file" ] && count=$((count + 1)) + done + assert_equals "1" "$count" "a re-polled command-channel message yields exactly one inbox record" + pass "command-channel capture is idempotent across re-polls" +} + +test_command_channel_conflict_with_exclusion_is_reported() { + local home wake_out + home="$TMP_ROOT/command-channel-conflict" + mkdir -p "$home/state" + make_fake_discord_node "$home" + wake_out=$(FM_TEST_REAL_NODE=$(command -v node) \ + FM_DISCORD_FAKE_MESSAGES='[{"id":"1352000000000003005","channel_id":"1000000000000000009","guild_id":"1000000000000000000","author":{"id":"8000000000000000001","username":"captain"},"mentions":[],"content":"conflict","attachments":[]}]' \ + PATH="$home/fake-bin:$BASE_PATH" FM_HOME="$home" FM_STATE_OVERRIDE="$home/state" \ + FM_DISCORD_BOT_TOKEN=fake-test-token FM_DISCORD_CHANNEL_ID="1000000000000000009" \ + FM_DISCORD_AUTHORIZED_USER_IDS="8000000000000000001" FM_DISCORD_COMMAND_CHANNELS="1000000000000000009" \ + FM_DISCORD_EXCLUDE_CHANNELS="1000000000000000009" \ + "$ROOT/bin/fm-discord-poll.sh" 2>"$home/warnings.log") + [ -z "$wake_out" ] || fail "an excluded command channel must not capture (got: $wake_out)" + assert_contains "$(cat "$home/warnings.log")" "command channel 1000000000000000009 is also excluded; exclusion wins" "command/exclusion conflict is reported" + pass "an excluded command channel fails safe with an explicit diagnostic" +} + test_poll_no_token_is_hard_noop test_ingestion_payload_shape_and_wake test_default_dm_discovery @@ -313,3 +406,8 @@ test_reply_to_member_without_mention_is_ignored test_dm_reply_is_polled_with_configured_channel test_out_of_band_mention_enqueues_durable_wake test_bootstrap_activation +test_command_channel_plain_message_is_captured +test_command_channel_unauthorized_author_is_ignored +test_command_channel_plain_message_ignored_in_ordinary_channel +test_command_channel_duplicate_poll_is_idempotent +test_command_channel_conflict_with_exclusion_is_reported