xmaster 1.6.6

Enterprise-grade X/Twitter CLI — post, reply, like, retweet, DM, search, and more
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
pub mod post;
pub mod engage;
pub mod social;
pub mod timeline;
pub mod search;
pub mod search_ai;
pub mod dm;
pub mod config_cmd;
pub mod agent_info;
pub mod update;
pub mod user;
pub mod thread;
pub mod metrics;
pub mod lists;
pub mod moderation;
pub mod rate_limits;
pub mod analyze;
pub mod track;
pub mod report;
pub mod suggest;
pub mod schedule;
pub mod bookmarks_cmd;
pub mod engage_recommend;
pub mod engage_inbox;
pub mod skill_cmd;
pub mod replies;
pub mod read_post;
pub mod inspire;
pub mod tweet_engagement;
pub mod quotes;
pub mod amplifiers;
pub mod volume;
pub mod article;

use crate::cli::{ArticleCommands, Cli, Commands, ConfigCommands, DmCommands, EngageCommands, WatchlistCommands, ListCommands, TrackCommands, ReportCommands, SuggestCommands, ScheduleCommands, BookmarkCommands, SkillCommands};
use crate::context::AppContext;
use crate::errors::XmasterError;
use crate::output::OutputFormat;
use std::sync::Arc;

/// Extract the top-level command name as a lowercase string for allowlisting.
fn command_name(cmd: &Commands) -> &'static str {
    match cmd {
        Commands::Post { .. } => "post",
        Commands::Delete { .. } => "delete",
        Commands::Like { .. } => "like",
        Commands::Unlike { .. } => "unlike",
        Commands::Retweet { .. } => "retweet",
        Commands::Unretweet { .. } => "unretweet",
        Commands::Bookmark { .. } => "bookmark",
        Commands::Unbookmark { .. } => "unbookmark",
        Commands::Follow { .. } => "follow",
        Commands::Unfollow { .. } => "unfollow",
        Commands::Dm { .. } => "dm",
        Commands::Timeline { .. } => "timeline",
        Commands::Mentions { .. } => "mentions",
        Commands::Search { .. } => "search",
        Commands::SearchAi { .. } => "search-ai",
        Commands::Trending { .. } => "trending",
        Commands::User { .. } => "user",
        Commands::Me => "me",
        Commands::Bookmarks { .. } => "bookmarks",
        Commands::Followers { .. } => "followers",
        Commands::Following { .. } => "following",
        Commands::Config { .. } => "config",
        Commands::AgentInfo => "agent-info",
        Commands::Engage { .. } => "engage",
        Commands::Update { .. } => "update",
        Commands::Star => "star",
        Commands::Thread { .. } => "thread",
        Commands::Article { .. } => "article",
        Commands::Reply { .. } => "reply",
        Commands::Metrics { .. } => "metrics",
        Commands::Lists { .. } => "lists",
        Commands::HideReply { .. } => "hide-reply",
        Commands::UnhideReply { .. } => "unhide-reply",
        Commands::Read { .. } => "read",
        Commands::Replies { .. } => "replies",
        Commands::Likers { .. } => "likers",
        Commands::Retweeters { .. } => "retweeters",
        Commands::Quotes { .. } => "quotes",
        Commands::Users { .. } => "users",
        Commands::Volume { .. } => "volume",
        Commands::Amplifiers { .. } => "amplifiers",
        Commands::RateLimits => "rate-limits",
        Commands::Block { .. } => "block",
        Commands::Unblock { .. } => "unblock",
        Commands::Mute { .. } => "mute",
        Commands::Unmute { .. } => "unmute",
        Commands::Analyze { .. } => "analyze",
        Commands::Inspire { .. } => "inspire",
        Commands::Track { .. } => "track",
        Commands::Report { .. } => "report",
        Commands::Suggest { .. } => "suggest",
        Commands::Schedule { .. } => "schedule",
        Commands::Skill { .. } => "skill",
    }
}

/// Check XMASTER_ALLOW_COMMANDS / XMASTER_DENY_COMMANDS env vars.
/// Deny takes precedence over allow. Agent-info and config are always
/// allowed so operators can still introspect and reconfigure.
fn check_command_access(cmd_name: &str) -> Result<(), XmasterError> {
    // Always allow introspection + configuration commands
    if matches!(cmd_name, "agent-info" | "config" | "rate-limits" | "update" | "skill") {
        return Ok(());
    }

    if let Ok(deny) = std::env::var("XMASTER_DENY_COMMANDS") {
        let denied: Vec<&str> = deny.split(',').map(|s| s.trim()).collect();
        if denied.iter().any(|d| d.eq_ignore_ascii_case(cmd_name)) {
            return Err(XmasterError::CommandDenied(format!(
                "'{cmd_name}' is blocked by XMASTER_DENY_COMMANDS"
            )));
        }
    }

    if let Ok(allow) = std::env::var("XMASTER_ALLOW_COMMANDS") {
        let allowed: Vec<&str> = allow.split(',').map(|s| s.trim()).collect();
        if !allowed.iter().any(|a| a.eq_ignore_ascii_case(cmd_name)) {
            return Err(XmasterError::CommandDenied(format!(
                "'{cmd_name}' is not in XMASTER_ALLOW_COMMANDS"
            )));
        }
    }

    Ok(())
}

#[cfg(test)]
mod access_tests {
    use super::*;
    use std::sync::Mutex;

    static ENV_LOCK: Mutex<()> = Mutex::new(());

    fn clear_access_env() {
        std::env::remove_var("XMASTER_ALLOW_COMMANDS");
        std::env::remove_var("XMASTER_DENY_COMMANDS");
    }

    #[test]
    fn deny_blocks_command() {
        let _guard = ENV_LOCK.lock().unwrap();
        clear_access_env();
        std::env::set_var("XMASTER_DENY_COMMANDS", "post,delete");
        let r = check_command_access("post");
        assert!(r.is_err());
        assert!(r.unwrap_err().to_string().contains("blocked"));
        clear_access_env();
    }

    #[test]
    fn deny_allows_unlisted() {
        let _guard = ENV_LOCK.lock().unwrap();
        clear_access_env();
        std::env::set_var("XMASTER_DENY_COMMANDS", "post,delete");
        assert!(check_command_access("search").is_ok());
        clear_access_env();
    }

    #[test]
    fn allow_restricts_to_listed() {
        let _guard = ENV_LOCK.lock().unwrap();
        clear_access_env();
        std::env::set_var("XMASTER_ALLOW_COMMANDS", "search,read,metrics");
        assert!(check_command_access("search").is_ok());
        let r = check_command_access("post");
        assert!(r.is_err());
        assert!(r.unwrap_err().to_string().contains("not in"));
        clear_access_env();
    }

    #[test]
    fn introspection_always_allowed() {
        let _guard = ENV_LOCK.lock().unwrap();
        clear_access_env();
        std::env::set_var("XMASTER_DENY_COMMANDS", "agent-info,config");
        assert!(check_command_access("agent-info").is_ok());
        assert!(check_command_access("config").is_ok());
        clear_access_env();
    }

    #[test]
    fn deny_takes_precedence_over_allow() {
        let _guard = ENV_LOCK.lock().unwrap();
        clear_access_env();
        std::env::set_var("XMASTER_ALLOW_COMMANDS", "post,search");
        std::env::set_var("XMASTER_DENY_COMMANDS", "post");
        let r = check_command_access("post");
        assert!(r.is_err());
        clear_access_env();
    }
}

pub async fn dispatch(
    ctx: Arc<AppContext>,
    cli: &Cli,
    format: OutputFormat,
) -> Result<(), XmasterError> {
    // Command access guard — check allowlist/denylist before any work.
    let cmd_name = command_name(&cli.command);
    check_command_access(cmd_name)?;

    match &cli.command {
        Commands::Post { text, reply_to, quote, media, poll, poll_duration } => {
            post::execute(ctx, format, text, reply_to.as_deref(), quote.as_deref(), media, poll.as_deref(), *poll_duration).await
        }
        Commands::Delete { id } => engage::delete(ctx, format, id).await,
        Commands::Like { id } => engage::like(ctx, format, id).await,
        Commands::Unlike { id } => engage::unlike(ctx, format, id).await,
        Commands::Retweet { id } => engage::retweet(ctx, format, id).await,
        Commands::Unretweet { id } => engage::unretweet(ctx, format, id).await,
        Commands::Bookmark { id } => engage::bookmark(ctx, format, id).await,
        Commands::Unbookmark { id } => engage::unbookmark(ctx, format, id).await,
        Commands::Follow { username } => social::follow(ctx, format, username).await,
        Commands::Unfollow { username } => social::unfollow(ctx, format, username).await,
        Commands::Dm { action } => match action {
            DmCommands::Send { username, text } => dm::send(ctx, format, username, text).await,
            DmCommands::Inbox { count } => dm::inbox(ctx, format, *count).await,
            DmCommands::Thread { id, count } => dm::thread(ctx, format, id, *count).await,
        },
        Commands::Timeline { user, count, since, before, sort } => timeline::timeline(ctx, format, user.as_deref(), *count, since.as_deref(), before.as_deref(), sort.as_deref()).await,
        Commands::Mentions { count, since_id } => timeline::mentions(ctx, format, *count, since_id.as_deref()).await,
        Commands::Search { query, mode, count, since, before } => search::execute(ctx, format, query, mode, *count, since.as_deref(), before.as_deref()).await,
        Commands::SearchAi { query, count, from_date, to_date } => {
            search_ai::execute(ctx, format, query, *count, from_date.as_deref(), to_date.as_deref()).await
        }
        Commands::Trending { region, category, personalized } => {
            if *personalized {
                search_ai::personalized_trends(ctx, format).await
            } else {
                search_ai::trending(ctx, format, region.as_deref(), category.as_deref()).await
            }
        }
        Commands::User { username } => user::info(ctx, format, username).await,
        Commands::Me => user::me(ctx, format).await,
        Commands::Bookmarks { action } => match action {
            BookmarkCommands::List { count, unread } => bookmarks_cmd::list(ctx, format, *count, *unread).await,
            BookmarkCommands::Sync { count } => bookmarks_cmd::sync(ctx, format, *count).await,
            BookmarkCommands::Search { query } => bookmarks_cmd::search(format, query).await,
            BookmarkCommands::Export { output, unread } => bookmarks_cmd::export(format, output.as_deref(), *unread).await,
            BookmarkCommands::Digest { days } => bookmarks_cmd::digest(format, *days).await,
            BookmarkCommands::Stats => bookmarks_cmd::stats(format).await,
            BookmarkCommands::Folders => bookmarks_cmd::folders(ctx, format).await,
            BookmarkCommands::Folder { id, count } => bookmarks_cmd::folder(ctx, format, id, *count).await,
        },
        Commands::Followers { username, count } => social::followers(ctx, format, username, *count).await,
        Commands::Following { username, count } => social::following(ctx, format, username, *count).await,
        Commands::Config { action } => match action {
            ConfigCommands::Show => config_cmd::show(ctx, format).await,
            ConfigCommands::Get { key } => config_cmd::get(format, key).await,
            ConfigCommands::Set { key, value } => config_cmd::set(format, key, value).await,
            ConfigCommands::Check => config_cmd::check(ctx, format).await,
            ConfigCommands::Guide => { config_cmd::guide(format).await }
            ConfigCommands::Auth => { config_cmd::auth(ctx, format).await }
            ConfigCommands::WebLogin => { config_cmd::web_login(format).await }
        },
        Commands::AgentInfo => {
            agent_info::execute(format);
            Ok(())
        }
        Commands::Engage { action } => match action {
            EngageCommands::Inbox { id, count, quote_reply_count } => {
                engage_inbox::execute(ctx, format, id, *count, *quote_reply_count).await
            }
            EngageCommands::Recommend { topic, min_followers, count } => {
                engage_recommend::recommend(ctx, format, topic.as_deref(), *min_followers, *count).await
            }
            EngageCommands::Feed { topics, min_followers, max_age_mins, count } => {
                engage_recommend::feed(ctx, format, topics, *min_followers, *max_age_mins, *count).await
            }
            EngageCommands::Watchlist { action } => match action {
                WatchlistCommands::Add { username, topic } => {
                    engage_recommend::watchlist_add(ctx, format, username, topic.as_deref()).await
                }
                WatchlistCommands::List => engage_recommend::watchlist_list(format).await,
                WatchlistCommands::Remove { username } => engage_recommend::watchlist_remove(format, username).await,
            },
            EngageCommands::HotTargets { days, min_imps, min_profile_clicks, min_samples, count, sort } => {
                engage_recommend::hot_targets(format, *days, *min_imps, *min_profile_clicks, *min_samples, *count, sort).await
            }
            EngageCommands::Swarm { id, min_followers, max_followers, count } => {
                engage_recommend::swarm(ctx, format, id, *min_followers, *max_followers, *count).await
            }
        },
        Commands::Update { check } => update::execute(*check).await,
        Commands::Star => {
            crate::star_nudge::open_star_page();
            Ok(())
        }
        Commands::Thread { texts, media } => thread::execute(ctx, format, texts, media).await,
        Commands::Article { action } => match action {
            ArticleCommands::Preview {
                input,
                output,
                title,
                subtitle,
                header_image,
                author,
                handle,
                avatar,
                audience,
                open,
            } => {
                article::preview(
                    format,
                    input,
                    output.as_deref(),
                    title.as_deref(),
                    subtitle.as_deref(),
                    header_image.as_deref(),
                    author,
                    handle,
                    avatar.as_deref(),
                    audience,
                    *open,
                )
                .await
            }
            ArticleCommands::Draft {
                input,
                title,
                header_image,
            } => {
                article::draft(
                    ctx,
                    format,
                    input,
                    title.as_deref(),
                    header_image.as_deref(),
                )
                .await
            }
        },
        Commands::Reply { id, text, media } => {
            post::execute(ctx, format, text, Some(id.as_str()), None, media, None, 1440).await
        }
        Commands::Metrics { ids } => metrics::execute_batch(ctx, format, ids).await,
        Commands::Lists { action } => match action {
            ListCommands::Create { name, description } => {
                lists::create(ctx, format, name, description.as_deref()).await
            }
            ListCommands::Delete { id } => lists::delete(ctx, format, id).await,
            ListCommands::Add { list_id, username } => {
                lists::add_member(ctx, format, list_id, username).await
            }
            ListCommands::Remove { list_id, username } => {
                lists::remove_member(ctx, format, list_id, username).await
            }
            ListCommands::Timeline { list_id, count } => {
                lists::timeline(ctx, format, list_id, *count).await
            }
            ListCommands::Members { list_id, count } => {
                lists::members(ctx, format, list_id, *count).await
            }
            ListCommands::Mine { count } => lists::mine(ctx, format, *count).await,
        },
        Commands::HideReply { id } => moderation::hide_reply(ctx, format, id).await,
        Commands::UnhideReply { id } => moderation::unhide_reply(ctx, format, id).await,
        Commands::Read { id } => read_post::execute(ctx, format, id).await,
        Commands::Replies { id, count } => replies::execute(ctx, format, id, *count).await,
        Commands::Likers { id, count } => tweet_engagement::likers(ctx, format, id, *count).await,
        Commands::Retweeters { id, count } => tweet_engagement::retweeters(ctx, format, id, *count).await,
        Commands::Quotes { id, count } => quotes::execute(ctx, format, id, *count).await,
        Commands::Users { usernames } => user::bulk(ctx, format, usernames).await,
        Commands::Volume { query, granularity } => volume::execute(ctx, format, query, granularity).await,
        Commands::Amplifiers { count } => amplifiers::execute(ctx, format, *count).await,
        Commands::RateLimits => rate_limits::execute(ctx, format).await,
        Commands::Block { username } => moderation::block(ctx, format, username).await,
        Commands::Unblock { username } => moderation::unblock(ctx, format, username).await,
        Commands::Mute { username } => moderation::mute(ctx, format, username).await,
        Commands::Unmute { username } => moderation::unmute(ctx, format, username).await,
        Commands::Analyze { text, goal, reply_to } => analyze::execute(ctx, format, text, goal.as_deref(), reply_to.as_deref()).await,
        Commands::Inspire { topic, author, min_likes, min_chars, long, count } => {
            // --long is sugar for --min-chars 500. Explicit --min-chars wins if both passed.
            let mc = min_chars.or(if *long { Some(500) } else { None });
            inspire::execute(ctx, format, topic.as_deref(), author.as_deref(), *min_likes, mc, *count).await
        },
        Commands::Track { action } => match action {
            TrackCommands::Run => track::track_run(ctx, format).await,
            TrackCommands::Status => track::track_status(ctx, format).await,
            TrackCommands::Followers => track::track_followers(ctx, format).await,
            TrackCommands::Growth { days } => track::follower_growth(ctx, format, *days).await,
        },
        Commands::Report { action } => match action {
            ReportCommands::Daily => report::daily(ctx, format).await,
            ReportCommands::Weekly => report::weekly(ctx, format).await,
        },
        Commands::Suggest { action } => match action {
            SuggestCommands::BestTime => suggest::best_time(ctx, format).await,
            SuggestCommands::NextPost => suggest::next_post(ctx, format).await,
        },
        Commands::Schedule { action } => match action {
            ScheduleCommands::Add { content, at, reply_to, quote, media } => {
                schedule::add(ctx, format, content, at, reply_to.as_deref(), quote.as_deref(), media).await
            }
            ScheduleCommands::List { status } => schedule::list(format, status.as_deref()).await,
            ScheduleCommands::Cancel { id } => schedule::cancel(format, id).await,
            ScheduleCommands::Reschedule { id, at } => schedule::reschedule(format, id, at).await,
            ScheduleCommands::Fire => schedule::fire(ctx, format).await,
            ScheduleCommands::Setup => schedule::setup(format).await,
        },
        Commands::Skill { action } => match action {
            SkillCommands::Install => skill_cmd::install(format).await,
            SkillCommands::Update => skill_cmd::update(format).await,
            SkillCommands::Status => skill_cmd::status(format).await,
        },
    }
}