diff options
author | iximeow <me@iximeow.net> | 2017-12-31 04:54:17 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-12-31 04:54:17 -0800 |
commit | 0668cdd7d5e800d8c7e8b1c0853f8b747179356f (patch) | |
tree | 9b4ade0ad1d036df2c5f50016720d48355f50e5e /src/commands | |
parent | 9979821629fc8943ad50b06e934a83a24b352a7f (diff) | |
parent | 935c78ce7d2aaabca269d81cff3459cef1084fbc (diff) |
Merge pull request #5 from iximeow/dm-fixup
clean up DM support, add thing to send DMs
Diffstat (limited to 'src/commands')
-rw-r--r-- | src/commands/dm.rs | 53 | ||||
-rw-r--r-- | src/commands/mod.rs | 2 |
2 files changed, 55 insertions, 0 deletions
diff --git a/src/commands/dm.rs b/src/commands/dm.rs new file mode 100644 index 0000000..95f65b7 --- /dev/null +++ b/src/commands/dm.rs @@ -0,0 +1,53 @@ +use display::DisplayInfo; +use tw; +use ::Queryer; + +use tw::TweetId; + +use commands::Command; + +static DM_CREATE_URL: &str = "https://api.twitter.com/1.1/direct_messages/new.json"; + +pub static DM: Command = Command { + keyword: "dm", + params: 1, + exec: dm, + param_str: " <user_handle>", + help_str: "Send DM to <user_handle>" +}; + +fn dm(line: String, tweeter: &mut tw::TwitterCache, queryer: &mut Queryer, display_info: &mut DisplayInfo) { + let user_profile = match tweeter.current_profile().map(|profile| profile.to_owned()) { + Some(profile) => profile, + None => { + display_info.status("To send a DM you must be an authenticated user.".to_owned()); + return; + } + }; + let mut text: String = line.trim().to_string(); + let text_bare = match text.find(" ") { + None => "".to_owned(), + Some(id_end_idx) => { + text.split_off(id_end_idx + 1) + } + }; + let dm_text = text_bare.trim(); + let handle_chars = text.trim().chars().collect::<Vec<char>>(); + let normalized_handle = if handle_chars[0] == '@' { + handle_chars[1..].to_vec() + } else { + handle_chars + }.into_iter().collect::<String>(); + + let encoded = ::url_encode(dm_text); + let result = match tweeter.current_profile() { + Some(user_profile) => { + queryer.do_api_post(&format!("{}?text={}&screen_name={}", DM_CREATE_URL, encoded, normalized_handle), &tweeter.app_key, &user_profile.creds) + }, + None => Err("No logged in user to DM as".to_owned()) + }; + match result { + Ok(_) => (), + Err(e) => display_info.status(e) + } +} diff --git a/src/commands/mod.rs b/src/commands/mod.rs index e8a725b..8108a78 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -10,6 +10,7 @@ pub struct Command { pub help_str: &'static str } +pub mod dm; pub mod help; pub mod auth; pub mod show_cache; @@ -23,6 +24,7 @@ pub mod thread; pub mod profile; pub static COMMANDS: &[&Command] = &[ + &dm::DM, &profile::PROFILE, &profile::PROFILES, &help::HELP, |