From a3d33d3e680f19c0d61911d64d9e4c30a07f55ab Mon Sep 17 00:00:00 2001 From: Your Name Date: Sun, 3 May 2020 20:06:12 +0200 Subject: Promote example app to proper command line client. --- Cargo.toml | 4 +- examples/zot/abconfig.rs | 38 ---------- examples/zot/abook.rs | 29 -------- examples/zot/channel_stream.rs | 104 --------------------------- examples/zot/group.rs | 70 ------------------ examples/zot/item.rs | 57 --------------- examples/zot/mod.rs | 7 -- examples/zot/network_stream.rs | 104 --------------------------- examples/zot/xchan.rs | 41 ----------- examples/zotcli.rs | 147 -------------------------------------- src/bin/zot/main.rs | 147 ++++++++++++++++++++++++++++++++++++++ src/bin/zot/zot/abconfig.rs | 38 ++++++++++ src/bin/zot/zot/abook.rs | 29 ++++++++ src/bin/zot/zot/channel_stream.rs | 104 +++++++++++++++++++++++++++ src/bin/zot/zot/group.rs | 70 ++++++++++++++++++ src/bin/zot/zot/item.rs | 57 +++++++++++++++ src/bin/zot/zot/mod.rs | 7 ++ src/bin/zot/zot/network_stream.rs | 104 +++++++++++++++++++++++++++ src/bin/zot/zot/xchan.rs | 41 +++++++++++ 19 files changed, 599 insertions(+), 599 deletions(-) delete mode 100644 examples/zot/abconfig.rs delete mode 100644 examples/zot/abook.rs delete mode 100644 examples/zot/channel_stream.rs delete mode 100644 examples/zot/group.rs delete mode 100644 examples/zot/item.rs delete mode 100644 examples/zot/mod.rs delete mode 100644 examples/zot/network_stream.rs delete mode 100644 examples/zot/xchan.rs delete mode 100644 examples/zotcli.rs create mode 100644 src/bin/zot/main.rs create mode 100644 src/bin/zot/zot/abconfig.rs create mode 100644 src/bin/zot/zot/abook.rs create mode 100644 src/bin/zot/zot/channel_stream.rs create mode 100644 src/bin/zot/zot/group.rs create mode 100644 src/bin/zot/zot/item.rs create mode 100644 src/bin/zot/zot/mod.rs create mode 100644 src/bin/zot/zot/network_stream.rs create mode 100644 src/bin/zot/zot/xchan.rs diff --git a/Cargo.toml b/Cargo.toml index d985740..c88cdd0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,6 +21,8 @@ authors = ["haraldei"] edition = "2018" [dependencies] +clap = "2.33.0" +dotenv = "0.13" reqwest = "0.9.1" serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" @@ -29,7 +31,5 @@ url = "2.1" [dev-dependencies] base64 = "0.11.0" -clap = "2.33.0" -dotenv = "0.13" mockito = "0.25" serde_json = "1.0" diff --git a/examples/zot/abconfig.rs b/examples/zot/abconfig.rs deleted file mode 100644 index 2e357e4..0000000 --- a/examples/zot/abconfig.rs +++ /dev/null @@ -1,38 +0,0 @@ -/* Example Zot API command line utility, part of zotapi. - * Copyright (C) 2018 Harald Eilertsen - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -use zotapi; - -pub fn fetch(client: &zotapi::Client) { - match zotapi::ABConfig::z().fetch(&client) { - Ok(v) => { - println!("Id: Chan: Cat: Key: Val: xchan:"); - for entry in v { - println!("{:6} {:6} {:15} {:15} {:4} {}", - entry.id, - entry.chan, - entry.cat, - entry.k, - entry.v, - entry.xchan); - } - } - Err(e) => { - println!("{:?}", e); - } - } -} diff --git a/examples/zot/abook.rs b/examples/zot/abook.rs deleted file mode 100644 index 97025c3..0000000 --- a/examples/zot/abook.rs +++ /dev/null @@ -1,29 +0,0 @@ -/* Example Zot API command line utility, part of zotapi. - * Copyright (C) 2018 Harald Eilertsen - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -pub fn fetch(client: &zotapi::Client, _raw: bool) { - match zotapi::Abook::z().fetch(&client) { - Ok(abooks) => { - for b in abooks { - println!("{:?}", b); - } - } - Err(e) => { - println!("{:?}", e); - } - } -} diff --git a/examples/zot/channel_stream.rs b/examples/zot/channel_stream.rs deleted file mode 100644 index bad7152..0000000 --- a/examples/zot/channel_stream.rs +++ /dev/null @@ -1,104 +0,0 @@ -/* Example Zot API command line utility, part of zotapi. - * Copyright (C) 2018 Harald Eilertsen - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -use zotapi; - -use serde_json; -use std::iter::Iterator; - -pub fn fetch(client: &zotapi::Client, raw: bool) { - match zotapi::channel_stream().fetch(&client) { - Ok(payload) => { - if raw { - println!("{}", payload); - } else { - list(&payload); - } - } - Err(e) => { - println!("Error getting channel stream: {:?}", e); - } - } -} - -fn list(payload: &str) { - match serde_json::from_str(&payload) { - Ok(serde_json::Value::Array(v)) => { - for item in v.into_iter() { - print_item(&item); - println!("-----"); - } - } - Ok(_) => println!("Wrong type returned, expected an array."), - Err(e) => { - println!("Error: {}", e); - } - } -} - -fn get_str(v: &serde_json::Value) -> &str { - v.as_str().unwrap_or("") -} - -fn get_verb(item: &serde_json::Value) -> &str { - match get_str(&item["verb"]).split("/").last() { - Some("post") => "posted", - Some("like") => "liked", - Some("share") => "shared", - Some("dislike") => "disliked", - _ => "unknown verb", - } -} - -fn get_object_type(item: &serde_json::Value) -> &str { - get_str(&item["object_type"]) - .split("/") - .last() - .unwrap_or("unknown object") -} - -fn get_tags(item: &serde_json::Value) -> String { - match item["tags"] { - serde_json::Value::Array(ref v) => v - .iter() - .map(|t| { - let prefix = match get_str(&t["type"]) { - "hashtag" => "#", - "forum" => "!", - "mention" => "@", - _ => "", - }; - format!("{}{}", prefix, get_str(&t["tag"])) - }) - .collect::>() - .join(", "), - serde_json::Value::Null => String::new(), - _ => String::from("invalid tags, expected array..."), - } -} - -fn print_item(item: &serde_json::Value) { - let author = &item["author"]; - print!("{} ", get_str(&item["created"])); - print!("{} ", get_str(&author["name"])); - println!("{} a {}", get_verb(&item), get_object_type(&item)); - println!("URL: {}", get_str(&author["url"])); - println!("Proto: {}", get_str(&author["network"])); - println!("Title: {}", get_str(&item["title"])); - println!("Tags : {}", get_tags(&item)); - println!("Message:\n{}", get_str(&item["body"])); -} diff --git a/examples/zot/group.rs b/examples/zot/group.rs deleted file mode 100644 index 9264cdc..0000000 --- a/examples/zot/group.rs +++ /dev/null @@ -1,70 +0,0 @@ -/* Example Zot API command line utility, part of zotapi. - * Copyright (C) 2018 Harald Eilertsen - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -use serde_json::{from_str, Value}; - -pub fn list(data: &str) { - if let Ok(Value::Array(groups)) = from_str(&data) { - println!("Id | Group name | uid | flags | hash"); - println!("----+------------------+-----+-------+-------------------------"); - - for group in groups { - let mut flags = String::new(); - - if let Some(visible) = group["visible"].as_u64() { - if visible != 0 { - flags += "v"; - } - } - - if let Some(deleted) = group["deleted"].as_u64() { - if deleted != 0 { - flags += "d"; - } - } - - print!( - "{:>3} | {:16} | {:>3} | {:5} | {}\n", - group["id"].as_u64().unwrap(), - group["gname"].as_str().unwrap(), - group["uid"].as_u64().unwrap(), - flags, - group["hash"].as_str().unwrap() - ); - } - } else { - eprintln!("Invalid data"); - } -} - -pub fn list_members(data: &str) { - if let Ok(Value::Array(members)) = from_str(&data) { - println!("Id | Name | Address"); - println!("----+-------------------------------+------------------------"); - - for member in members { - println!( - "{:>3} | {:29} | {}", - member["id"].as_u64().unwrap(), - member["xchan_name"].as_str().unwrap(), - member["xchan_addr"].as_str().unwrap() - ); - } - } else { - eprintln!("Invalid data"); - } -} diff --git a/examples/zot/item.rs b/examples/zot/item.rs deleted file mode 100644 index 648a9f0..0000000 --- a/examples/zot/item.rs +++ /dev/null @@ -1,57 +0,0 @@ -/* Example Zot API command line utility, part of zotapi. - * Copyright (C) 2018 Harald Eilertsen - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -use clap::ArgMatches; -use zotapi; - -pub fn post(client: &zotapi::Client, args: &ArgMatches) { - let mut msg = zotapi::item(); - let body: String; - - if let Some(file) = dbg!(args.value_of("FILE")) { - let buf = std::fs::read(file).expect("Invalid file name"); - body = String::from_utf8(buf).expect("File contained invalid char data."); - msg.body(&body); - } - - if let Some(title) = dbg!(args.value_of("TITLE")) { - msg.title(title); - } - - if let Some(file) = dbg!(args.value_of("ATTACH")) { - msg.file(file); - } - - if let Some(groups) = dbg!(args.value_of("GROUP")) { - for g in groups.split(",") { - msg.group_allow(g); - } - } - - match msg.create(&client) { - Ok(res) => { - if res.success { - println!("New item with id {} posted successfully!", res.item_id); - } else { - println!("The item could not be posted."); - } - } - Err(e) => { - println!("Error posting message: {:?}", e); - } - } -} diff --git a/examples/zot/mod.rs b/examples/zot/mod.rs deleted file mode 100644 index 706f496..0000000 --- a/examples/zot/mod.rs +++ /dev/null @@ -1,7 +0,0 @@ -pub mod abconfig; -pub mod abook; -pub mod channel_stream; -pub mod group; -pub mod item; -pub mod network_stream; -pub mod xchan; diff --git a/examples/zot/network_stream.rs b/examples/zot/network_stream.rs deleted file mode 100644 index 0e6edb4..0000000 --- a/examples/zot/network_stream.rs +++ /dev/null @@ -1,104 +0,0 @@ -/* Example Zot API command line utility, part of zotapi. - * Copyright (C) 2018 Harald Eilertsen - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -use zotapi; - -use serde_json; -use std::iter::Iterator; - -pub fn fetch(client: &zotapi::Client, raw: bool) { - match zotapi::network_stream().fetch(&client) { - Ok(payload) => { - if raw { - println!("{}", payload); - } else { - list(&payload); - } - } - Err(e) => { - println!("Error getting channel stream: {:?}", e); - } - } -} - -fn list(payload: &str) { - match serde_json::from_str(&payload) { - Ok(serde_json::Value::Array(v)) => { - for item in v.into_iter() { - print_item(&item); - println!("-----"); - } - } - Ok(_) => println!("Wrong type returned, expected an array."), - Err(e) => { - println!("Error: {}", e); - } - } -} - -fn get_str(v: &serde_json::Value) -> &str { - v.as_str().unwrap_or("") -} - -fn get_verb(item: &serde_json::Value) -> &str { - match get_str(&item["verb"]).split("/").last() { - Some("post") => "posted", - Some("like") => "liked", - Some("share") => "shared", - Some("dislike") => "disliked", - _ => "unknown verb", - } -} - -fn get_object_type(item: &serde_json::Value) -> &str { - get_str(&item["object_type"]) - .split("/") - .last() - .unwrap_or("unknown object") -} - -fn get_tags(item: &serde_json::Value) -> String { - match item["tags"] { - serde_json::Value::Array(ref v) => v - .iter() - .map(|t| { - let prefix = match get_str(&t["type"]) { - "hashtag" => "#", - "forum" => "!", - "mention" => "@", - _ => "", - }; - format!("{}{}", prefix, get_str(&t["tag"])) - }) - .collect::>() - .join(", "), - serde_json::Value::Null => String::new(), - _ => String::from("invalid tags, expected array..."), - } -} - -fn print_item(item: &serde_json::Value) { - let author = &item["author"]; - print!("{} ", get_str(&item["created"])); - print!("{} ", get_str(&author["name"])); - println!("{} a {}", get_verb(&item), get_object_type(&item)); - println!("URL: {}", get_str(&author["url"])); - println!("Proto: {}", get_str(&author["network"])); - println!("Title: {}", get_str(&item["title"])); - println!("Tags : {}", get_tags(&item)); - println!("Message:\n{}", get_str(&item["body"])); -} diff --git a/examples/zot/xchan.rs b/examples/zot/xchan.rs deleted file mode 100644 index 088d49c..0000000 --- a/examples/zot/xchan.rs +++ /dev/null @@ -1,41 +0,0 @@ -/* Example Zot API command line utility, part of zotapi. - * Copyright (C) 2018 - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -use zotapi; - -pub enum Type { - Addr, - Hash, - GUID, -} - -pub fn fetch(client: &zotapi::Client, _raw: bool, t: Type, id: &str) { - let res = match t { - Type::Addr => zotapi::XChan::z().by_address(&id).fetch(&client), - Type::Hash => zotapi::XChan::z().by_hash(&id).fetch(&client), - Type::GUID => zotapi::XChan::z().by_guid(&id).fetch(&client), - }; - - match res { - Ok(payload) => { - println!("{:?}", payload); - } - Err(e) => { - println!("{:?}", e); - } - } -} diff --git a/examples/zotcli.rs b/examples/zotcli.rs deleted file mode 100644 index 0be566f..0000000 --- a/examples/zotcli.rs +++ /dev/null @@ -1,147 +0,0 @@ -/* Example Zot API command line utility, part of zotapi. - * Copyright (C) 2018 Harald Eilertsen - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -use clap::{clap_app, crate_authors, crate_description, crate_name, crate_version}; -use dotenv::dotenv; -use std::env; -use std::str::FromStr; - -mod zot; - -fn main() { - dotenv().ok(); - let site = env::var("HZ_SITE").expect("SITE variable expected"); - let user = env::var("HZ_USER").expect("USER variable expected"); - let password = env::var("HZ_PASSWORD").expect("PASSWORD variable expected"); - - let matches = clap_app!(app => - (name: crate_name!()) - (version: crate_version!()) - (author: crate_authors!()) - (about: crate_description!()) - (@subcommand channel => - (about: "Fetch the channel stream") - (@arg raw: --raw "Display raw json payload") - ) - (@subcommand network => - (about: "Fetch the network stream") - (@arg raw: --raw "Display raw json payload") - ) - (@subcommand abook => - (about: "Fetch address book/contact info") - (@arg raw: --raw "Display raw json payload") - ) - (@subcommand abconfig => - (about: "Fetch abconfig") - ) - (@subcommand group => - (about: "Fetch privacy groups") - (@arg raw: --raw "Display raw json payload") - (@group selector => - (@arg ID: --id +takes_value "Fetch members of group ") - (@arg GNAME: --name +takes_value "Fetch members of group ") - ) - ) - (@subcommand xchan => - (about: "Fetch xchan info") - (@arg raw: --raw "Display raw json payload") - (@arg addr: --addr "ID is given as webbie (default)") - (@arg hash: --hash "ID is given as xchan hash") - (@arg guid: --guid "ID is given as a GUID") - (@arg ID: +required "id (webbie, hash or GUID) of xchan to fetch") - ) - (@subcommand post => - (about: "Post a new message") - (@arg FILE: "A text file containing the body of the message") - (@arg TITLE: --title +takes_value "Set a title for the message") - (@arg ATTACH: --attach [FILE] "Attach a file or image to the post") - (@arg GROUP: --group [groups] "Limit distribution to the specified group(s) separated by comma") - ) - ) - .get_matches(); - - let client = zotapi::client(&site, &user, &password); - - match matches.subcommand() { - ("channel", Some(m)) => { - let raw = m.is_present("raw"); - zot::channel_stream::fetch(&client, raw); - } - ("network", Some(m)) => { - let raw = m.is_present("raw"); - zot::network_stream::fetch(&client, raw); - } - ("abconfig", _) => { - zot::abconfig::fetch(&client); - } - ("abook", Some(m)) => { - let raw = m.is_present("raw"); - zot::abook::fetch(&client, raw); - } - ("group", Some(m)) => { - if let Some(id) = m.value_of("ID") { - let res = zotapi::group_members() - .by_group_id(u64::from_str(id).unwrap()) - .fetch(&client) - .unwrap(); - - if m.is_present("raw") { - println!("{}", res); - } else { - zot::group::list_members(&res); - } - } else if let Some(gname) = m.value_of("GNAME") { - let res = zotapi::group_members() - .by_group_name(gname) - .fetch(&client) - .unwrap(); - - if m.is_present("raw") { - println!("{}", res); - } else { - zot::group::list_members(&res); - } - } else { - let res = zotapi::group().fetch(&client).unwrap(); - - if m.is_present("raw") { - println!("{}", res); - } else { - zot::group::list(&res); - } - } - } - ("xchan", Some(m)) => { - let raw = m.is_present("raw"); - let t = if m.is_present("guid") { - zot::xchan::Type::GUID - } else if m.is_present("hash") { - zot::xchan::Type::Hash - } else { - zot::xchan::Type::Addr - }; - - zot::xchan::fetch(&client, raw, t, m.value_of("ID").unwrap()); - } - ("post", Some(m)) => { - zot::item::post(&client, m); - } - _ => { - println!("{}", matches.usage()); - } - } -} diff --git a/src/bin/zot/main.rs b/src/bin/zot/main.rs new file mode 100644 index 0000000..5828f44 --- /dev/null +++ b/src/bin/zot/main.rs @@ -0,0 +1,147 @@ +/* Example Zot API command line utility, part of zotapi. + * Copyright (C) 2018 Harald Eilertsen + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +use clap::{clap_app, crate_authors, crate_version}; +use dotenv::dotenv; +use std::env; +use std::str::FromStr; + +mod zot; + +fn main() { + dotenv().ok(); + let site = env::var("HZ_SITE").expect("SITE variable expected"); + let user = env::var("HZ_USER").expect("USER variable expected"); + let password = env::var("HZ_PASSWORD").expect("PASSWORD variable expected"); + + let matches = clap_app!(app => + (name: "zot") + (version: crate_version!()) + (author: crate_authors!()) + (about: "zotapi command line client") + (@subcommand channel => + (about: "Fetch the channel stream") + (@arg raw: --raw "Display raw json payload") + ) + (@subcommand network => + (about: "Fetch the network stream") + (@arg raw: --raw "Display raw json payload") + ) + (@subcommand abook => + (about: "Fetch address book/contact info") + (@arg raw: --raw "Display raw json payload") + ) + (@subcommand abconfig => + (about: "Fetch abconfig") + ) + (@subcommand group => + (about: "Fetch privacy groups") + (@arg raw: --raw "Display raw json payload") + (@group selector => + (@arg ID: --id +takes_value "Fetch members of group ") + (@arg GNAME: --name +takes_value "Fetch members of group ") + ) + ) + (@subcommand xchan => + (about: "Fetch xchan info") + (@arg raw: --raw "Display raw json payload") + (@arg addr: --addr "ID is given as webbie (default)") + (@arg hash: --hash "ID is given as xchan hash") + (@arg guid: --guid "ID is given as a GUID") + (@arg ID: +required "id (webbie, hash or GUID) of xchan to fetch") + ) + (@subcommand post => + (about: "Post a new message") + (@arg FILE: "A text file containing the body of the message") + (@arg TITLE: --title +takes_value "Set a title for the message") + (@arg ATTACH: --attach [FILE] "Attach a file or image to the post") + (@arg GROUP: --group [groups] "Limit distribution to the specified group(s) separated by comma") + ) + ) + .get_matches(); + + let client = zotapi::client(&site, &user, &password); + + match matches.subcommand() { + ("channel", Some(m)) => { + let raw = m.is_present("raw"); + zot::channel_stream::fetch(&client, raw); + } + ("network", Some(m)) => { + let raw = m.is_present("raw"); + zot::network_stream::fetch(&client, raw); + } + ("abconfig", _) => { + zot::abconfig::fetch(&client); + } + ("abook", Some(m)) => { + let raw = m.is_present("raw"); + zot::abook::fetch(&client, raw); + } + ("group", Some(m)) => { + if let Some(id) = m.value_of("ID") { + let res = zotapi::group_members() + .by_group_id(u64::from_str(id).unwrap()) + .fetch(&client) + .unwrap(); + + if m.is_present("raw") { + println!("{}", res); + } else { + zot::group::list_members(&res); + } + } else if let Some(gname) = m.value_of("GNAME") { + let res = zotapi::group_members() + .by_group_name(gname) + .fetch(&client) + .unwrap(); + + if m.is_present("raw") { + println!("{}", res); + } else { + zot::group::list_members(&res); + } + } else { + let res = zotapi::group().fetch(&client).unwrap(); + + if m.is_present("raw") { + println!("{}", res); + } else { + zot::group::list(&res); + } + } + } + ("xchan", Some(m)) => { + let raw = m.is_present("raw"); + let t = if m.is_present("guid") { + zot::xchan::Type::GUID + } else if m.is_present("hash") { + zot::xchan::Type::Hash + } else { + zot::xchan::Type::Addr + }; + + zot::xchan::fetch(&client, raw, t, m.value_of("ID").unwrap()); + } + ("post", Some(m)) => { + zot::item::post(&client, m); + } + _ => { + println!("{}", matches.usage()); + } + } +} diff --git a/src/bin/zot/zot/abconfig.rs b/src/bin/zot/zot/abconfig.rs new file mode 100644 index 0000000..2e357e4 --- /dev/null +++ b/src/bin/zot/zot/abconfig.rs @@ -0,0 +1,38 @@ +/* Example Zot API command line utility, part of zotapi. + * Copyright (C) 2018 Harald Eilertsen + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +use zotapi; + +pub fn fetch(client: &zotapi::Client) { + match zotapi::ABConfig::z().fetch(&client) { + Ok(v) => { + println!("Id: Chan: Cat: Key: Val: xchan:"); + for entry in v { + println!("{:6} {:6} {:15} {:15} {:4} {}", + entry.id, + entry.chan, + entry.cat, + entry.k, + entry.v, + entry.xchan); + } + } + Err(e) => { + println!("{:?}", e); + } + } +} diff --git a/src/bin/zot/zot/abook.rs b/src/bin/zot/zot/abook.rs new file mode 100644 index 0000000..97025c3 --- /dev/null +++ b/src/bin/zot/zot/abook.rs @@ -0,0 +1,29 @@ +/* Example Zot API command line utility, part of zotapi. + * Copyright (C) 2018 Harald Eilertsen + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +pub fn fetch(client: &zotapi::Client, _raw: bool) { + match zotapi::Abook::z().fetch(&client) { + Ok(abooks) => { + for b in abooks { + println!("{:?}", b); + } + } + Err(e) => { + println!("{:?}", e); + } + } +} diff --git a/src/bin/zot/zot/channel_stream.rs b/src/bin/zot/zot/channel_stream.rs new file mode 100644 index 0000000..bad7152 --- /dev/null +++ b/src/bin/zot/zot/channel_stream.rs @@ -0,0 +1,104 @@ +/* Example Zot API command line utility, part of zotapi. + * Copyright (C) 2018 Harald Eilertsen + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +use zotapi; + +use serde_json; +use std::iter::Iterator; + +pub fn fetch(client: &zotapi::Client, raw: bool) { + match zotapi::channel_stream().fetch(&client) { + Ok(payload) => { + if raw { + println!("{}", payload); + } else { + list(&payload); + } + } + Err(e) => { + println!("Error getting channel stream: {:?}", e); + } + } +} + +fn list(payload: &str) { + match serde_json::from_str(&payload) { + Ok(serde_json::Value::Array(v)) => { + for item in v.into_iter() { + print_item(&item); + println!("-----"); + } + } + Ok(_) => println!("Wrong type returned, expected an array."), + Err(e) => { + println!("Error: {}", e); + } + } +} + +fn get_str(v: &serde_json::Value) -> &str { + v.as_str().unwrap_or("") +} + +fn get_verb(item: &serde_json::Value) -> &str { + match get_str(&item["verb"]).split("/").last() { + Some("post") => "posted", + Some("like") => "liked", + Some("share") => "shared", + Some("dislike") => "disliked", + _ => "unknown verb", + } +} + +fn get_object_type(item: &serde_json::Value) -> &str { + get_str(&item["object_type"]) + .split("/") + .last() + .unwrap_or("unknown object") +} + +fn get_tags(item: &serde_json::Value) -> String { + match item["tags"] { + serde_json::Value::Array(ref v) => v + .iter() + .map(|t| { + let prefix = match get_str(&t["type"]) { + "hashtag" => "#", + "forum" => "!", + "mention" => "@", + _ => "", + }; + format!("{}{}", prefix, get_str(&t["tag"])) + }) + .collect::>() + .join(", "), + serde_json::Value::Null => String::new(), + _ => String::from("invalid tags, expected array..."), + } +} + +fn print_item(item: &serde_json::Value) { + let author = &item["author"]; + print!("{} ", get_str(&item["created"])); + print!("{} ", get_str(&author["name"])); + println!("{} a {}", get_verb(&item), get_object_type(&item)); + println!("URL: {}", get_str(&author["url"])); + println!("Proto: {}", get_str(&author["network"])); + println!("Title: {}", get_str(&item["title"])); + println!("Tags : {}", get_tags(&item)); + println!("Message:\n{}", get_str(&item["body"])); +} diff --git a/src/bin/zot/zot/group.rs b/src/bin/zot/zot/group.rs new file mode 100644 index 0000000..9264cdc --- /dev/null +++ b/src/bin/zot/zot/group.rs @@ -0,0 +1,70 @@ +/* Example Zot API command line utility, part of zotapi. + * Copyright (C) 2018 Harald Eilertsen + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +use serde_json::{from_str, Value}; + +pub fn list(data: &str) { + if let Ok(Value::Array(groups)) = from_str(&data) { + println!("Id | Group name | uid | flags | hash"); + println!("----+------------------+-----+-------+-------------------------"); + + for group in groups { + let mut flags = String::new(); + + if let Some(visible) = group["visible"].as_u64() { + if visible != 0 { + flags += "v"; + } + } + + if let Some(deleted) = group["deleted"].as_u64() { + if deleted != 0 { + flags += "d"; + } + } + + print!( + "{:>3} | {:16} | {:>3} | {:5} | {}\n", + group["id"].as_u64().unwrap(), + group["gname"].as_str().unwrap(), + group["uid"].as_u64().unwrap(), + flags, + group["hash"].as_str().unwrap() + ); + } + } else { + eprintln!("Invalid data"); + } +} + +pub fn list_members(data: &str) { + if let Ok(Value::Array(members)) = from_str(&data) { + println!("Id | Name | Address"); + println!("----+-------------------------------+------------------------"); + + for member in members { + println!( + "{:>3} | {:29} | {}", + member["id"].as_u64().unwrap(), + member["xchan_name"].as_str().unwrap(), + member["xchan_addr"].as_str().unwrap() + ); + } + } else { + eprintln!("Invalid data"); + } +} diff --git a/src/bin/zot/zot/item.rs b/src/bin/zot/zot/item.rs new file mode 100644 index 0000000..648a9f0 --- /dev/null +++ b/src/bin/zot/zot/item.rs @@ -0,0 +1,57 @@ +/* Example Zot API command line utility, part of zotapi. + * Copyright (C) 2018 Harald Eilertsen + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +use clap::ArgMatches; +use zotapi; + +pub fn post(client: &zotapi::Client, args: &ArgMatches) { + let mut msg = zotapi::item(); + let body: String; + + if let Some(file) = dbg!(args.value_of("FILE")) { + let buf = std::fs::read(file).expect("Invalid file name"); + body = String::from_utf8(buf).expect("File contained invalid char data."); + msg.body(&body); + } + + if let Some(title) = dbg!(args.value_of("TITLE")) { + msg.title(title); + } + + if let Some(file) = dbg!(args.value_of("ATTACH")) { + msg.file(file); + } + + if let Some(groups) = dbg!(args.value_of("GROUP")) { + for g in groups.split(",") { + msg.group_allow(g); + } + } + + match msg.create(&client) { + Ok(res) => { + if res.success { + println!("New item with id {} posted successfully!", res.item_id); + } else { + println!("The item could not be posted."); + } + } + Err(e) => { + println!("Error posting message: {:?}", e); + } + } +} diff --git a/src/bin/zot/zot/mod.rs b/src/bin/zot/zot/mod.rs new file mode 100644 index 0000000..706f496 --- /dev/null +++ b/src/bin/zot/zot/mod.rs @@ -0,0 +1,7 @@ +pub mod abconfig; +pub mod abook; +pub mod channel_stream; +pub mod group; +pub mod item; +pub mod network_stream; +pub mod xchan; diff --git a/src/bin/zot/zot/network_stream.rs b/src/bin/zot/zot/network_stream.rs new file mode 100644 index 0000000..0e6edb4 --- /dev/null +++ b/src/bin/zot/zot/network_stream.rs @@ -0,0 +1,104 @@ +/* Example Zot API command line utility, part of zotapi. + * Copyright (C) 2018 Harald Eilertsen + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +use zotapi; + +use serde_json; +use std::iter::Iterator; + +pub fn fetch(client: &zotapi::Client, raw: bool) { + match zotapi::network_stream().fetch(&client) { + Ok(payload) => { + if raw { + println!("{}", payload); + } else { + list(&payload); + } + } + Err(e) => { + println!("Error getting channel stream: {:?}", e); + } + } +} + +fn list(payload: &str) { + match serde_json::from_str(&payload) { + Ok(serde_json::Value::Array(v)) => { + for item in v.into_iter() { + print_item(&item); + println!("-----"); + } + } + Ok(_) => println!("Wrong type returned, expected an array."), + Err(e) => { + println!("Error: {}", e); + } + } +} + +fn get_str(v: &serde_json::Value) -> &str { + v.as_str().unwrap_or("") +} + +fn get_verb(item: &serde_json::Value) -> &str { + match get_str(&item["verb"]).split("/").last() { + Some("post") => "posted", + Some("like") => "liked", + Some("share") => "shared", + Some("dislike") => "disliked", + _ => "unknown verb", + } +} + +fn get_object_type(item: &serde_json::Value) -> &str { + get_str(&item["object_type"]) + .split("/") + .last() + .unwrap_or("unknown object") +} + +fn get_tags(item: &serde_json::Value) -> String { + match item["tags"] { + serde_json::Value::Array(ref v) => v + .iter() + .map(|t| { + let prefix = match get_str(&t["type"]) { + "hashtag" => "#", + "forum" => "!", + "mention" => "@", + _ => "", + }; + format!("{}{}", prefix, get_str(&t["tag"])) + }) + .collect::>() + .join(", "), + serde_json::Value::Null => String::new(), + _ => String::from("invalid tags, expected array..."), + } +} + +fn print_item(item: &serde_json::Value) { + let author = &item["author"]; + print!("{} ", get_str(&item["created"])); + print!("{} ", get_str(&author["name"])); + println!("{} a {}", get_verb(&item), get_object_type(&item)); + println!("URL: {}", get_str(&author["url"])); + println!("Proto: {}", get_str(&author["network"])); + println!("Title: {}", get_str(&item["title"])); + println!("Tags : {}", get_tags(&item)); + println!("Message:\n{}", get_str(&item["body"])); +} diff --git a/src/bin/zot/zot/xchan.rs b/src/bin/zot/zot/xchan.rs new file mode 100644 index 0000000..088d49c --- /dev/null +++ b/src/bin/zot/zot/xchan.rs @@ -0,0 +1,41 @@ +/* Example Zot API command line utility, part of zotapi. + * Copyright (C) 2018 + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +use zotapi; + +pub enum Type { + Addr, + Hash, + GUID, +} + +pub fn fetch(client: &zotapi::Client, _raw: bool, t: Type, id: &str) { + let res = match t { + Type::Addr => zotapi::XChan::z().by_address(&id).fetch(&client), + Type::Hash => zotapi::XChan::z().by_hash(&id).fetch(&client), + Type::GUID => zotapi::XChan::z().by_guid(&id).fetch(&client), + }; + + match res { + Ok(payload) => { + println!("{:?}", payload); + } + Err(e) => { + println!("{:?}", e); + } + } +} -- cgit v1.2.3