From 427bd27968fb52ee17963978c90c0b6ea387e8a3 Mon Sep 17 00:00:00 2001 From: murmeldin Date: Sat, 3 Aug 2024 17:13:31 +0200 Subject: [PATCH] mediawiki function cleanup, login token working and to-do list in main --- Cargo.toml | 4 +- src/config_check.rs | 2 +- src/main.rs | 24 +++++++++--- src/mediawiki.rs | 90 +++++++++++++++++++++++++-------------------- 4 files changed, 74 insertions(+), 46 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 7c9e0a9..cff3d7a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,7 +9,7 @@ chrono = "0.4.38" regex = "1.10.5" futures = "0.3.30" headers = "0.4.0" -reqwest = {version = "0.12.5", features = ["blocking"]} +reqwest = { version = "0.12.5", default-features = false, features = ["blocking", "rustls-tls"] } lettre = "0.11.7" rand = "0.9.0-alpha.1" rusqlite = "0.31.0" @@ -17,3 +17,5 @@ uuid = { version = "1.10.0", features = ["v4"] } log = "0.4.22" clap = "4.5.11" rpassword = "7.3.1" +serde = {version = "1.0.204", features = ["derive"]} +serde_json = "1.0.122" diff --git a/src/config_check.rs b/src/config_check.rs index 4340691..84b41ba 100644 --- a/src/config_check.rs +++ b/src/config_check.rs @@ -50,7 +50,7 @@ const CONFIG_SPEC: CfgSpec<'static> = CfgSpec { CfgField::Default { key: "api-user", default: "PlenumBot@PlenumBot-PW1", description: "API Username that was generated in the mediawiki by the PlenumBot-Account" }, CfgField::Password { key: "api-secret", description: "API Secret that was generated in the mediawiki by the PlenumBot-Account. Example: \"PlenumBot-PW1@mgeg89bgit09q8bjblt5tn5ojj2d4gy\"" }, CfgField::Default { key: "server-url", default: "https://wiki.berlin.ccc.de/" , description: "Link that is needed for getting the session token" } - ], + ], }, // TODO: Matrix, CfgGroup { name: "text", diff --git a/src/main.rs b/src/main.rs index d5b7405..f18e62c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -18,18 +18,26 @@ Pad-ins-Wiki-und-versenden-Skript • Pad in MediaWiki-Format umwandeln • Neue Wiki-Seite erstellen und dort das umgewandelte Pad hochladen */ +/* *TO-DO LIST* +main.rs +- [ ] Add logic for top_anzahl in main.rs:172 +MediaWiki +- [ ] Add "get_csrf_token-function" for getting a write token to allow write operations in the wiki (murmeldin) +- [ ] Add "create_page" function for creating new pages in the wiki that is called on every day after plenum (murmeldin) +- [ ] Add "modify_plenum_main_page" function for creating new Links on the Plenum main page whenever the create_page function is being called (murmeldin) -// future improvements: -// - search ADJ_TIMEYWIMEY to find places that need adjusting if the bot might run late -// (that's an incomplete list, but tag things as you notice them…) +future improvements: +- search ADJ_TIMEYWIMEY to find places that need adjusting if the bot might run late +(that's an incomplete list, but tag things as you notice them…) + + */ // Import other .rs files as modules mod key_value; use key_value::KeyValueStore as KV; mod hedgedoc; use hedgedoc::HedgeDoc; mod mediawiki; - mod config_check; pub mod variables_and_settings; @@ -49,6 +57,7 @@ use uuid::Uuid; use lettre::message::{header, SinglePart}; use lettre::transport::smtp::authentication::Credentials; use lettre::{Message, SmtpTransport, Transport}; +use reqwest::blocking::Client; const FALLBACK_TEMPLATE: &str = variables_and_settings::FALLBACK_TEMPLATE; @@ -165,9 +174,14 @@ fn main() -> Result<(), Box> { let mut message_id: Option = None; // let in_3_days_is_plenum = true; - + //TEMPORÄR ANFANG: BEI PRODUCTION MUSS DAS HIER RAUS let top_anzahl: i32 = 0; // Muss noch gecodet werden let yesterday_was_plenum = true; // Das ist nur zu Testzwecken, kommt noch weg + + let auth_result = mediawiki::get_login_token(&Client::new(), &config.get("wiki-http-user").expect("HTML User not found DB!"), &config.get("wiki-http-pass").expect("HTML Password not found DB!"))?; + println!("---AUTH RESULT:---\n{}\n-----------", auth_result); + // TEMPORÄR ENDE + let pad_content = hedgedoc.download(¤t_pad_id).expect("Fehler beim Download des Pads!"); let pad_content_without_top_instructions = try_to_remove_top_instructions(pad_content); println!("Pad-content geladen!"); diff --git a/src/mediawiki.rs b/src/mediawiki.rs index df7d3ff..fdc95f2 100644 --- a/src/mediawiki.rs +++ b/src/mediawiki.rs @@ -1,67 +1,79 @@ use std::error::Error; use std::fs::File; use std::io::Read; +use clap::builder::Str; +use pandoc::{PandocError, PandocOutput}; use reqwest; +use reqwest::blocking::Client; +use reqwest::tls; +use serde::Deserialize; + +#[derive(Deserialize)] +struct QueryResponse { + batchcomplete: String, + query: QueryTokens, +} + +#[derive(Deserialize)] +struct QueryTokens { + tokens: Tokens, +} + +#[derive(Deserialize)] +struct Tokens { + logintoken: String, +} pub fn pad_ins_wiki(old_pad_content: String) { - convert_markdown_to_mediawiki_and_save_as_txt(old_pad_content); - + convert_md_to_mediawiki(old_pad_content); // Textdatei wieder einlesen - let mut file = - File::open("pandoc-output.txt").expect("Fehler beim öffnen der MediaWiki-Textdatei!"); - let mut contents = String::new(); - file.read_to_string(&mut contents).expect("Fehler beim auslesen der MediaWiki-Textdatei!"); + // Passwörter aus Datenbank lesen (ToBeDone) + /* let plenum_bot_user = String::from("PlenumBot@PlenumBot-PW1"); let plenum_bot_pw = String::from("**OLD_API_PW_REMOVED**"); let login_token = login_to_mediawiki(plenum_bot_user.clone(), plenum_bot_pw.clone()) .expect("Fehler beim Einloggen!"); println!("plenum_bot_user: {plenum_bot_user}, plenum_bot_pw: {plenum_bot_pw}, login_token: {login_token}") + + */ } -fn convert_markdown_to_mediawiki_and_save_as_txt(old_pad_content: String) { +/// Converts one file type into another using pandoc and saves the result as a txt file +fn pandoc_convert(old_pad_content: String, output_filepath: &str, input_format: pandoc::InputFormat, output_format: pandoc::OutputFormat) -> Result { //Convert Markdown into Mediawiki // Vanilla pandoc Befehl: pandoc --from markdown --to mediawiki --no-highlight let mut p = pandoc::new(); p.set_input(pandoc::InputKind::Pipe(old_pad_content)); - p.set_input_format(pandoc::InputFormat::Markdown, vec![]); - p.set_output(pandoc::OutputKind::File("./pandoc-output.txt".parse().unwrap())); - p.set_output_format(pandoc::OutputFormat::MediaWiki, vec![]); + p.set_input_format(input_format, vec![]); + p.set_output(pandoc::OutputKind::File(output_filepath.parse().unwrap())); + p.set_output_format(output_format, vec![]); p.execute() - .expect("Fehler beim Umwandeln des und speichern des Pads in eine mediawiki-Textdatei"); } -fn login_to_mediawiki( - plenum_bot_user: String, plenum_bot_pw: String, -) -> Result> { - //let mut map = HashMap::new(); - //map.insert("logintoken", "result"); - let username = "cccb-wiki"; - let password = "**OLD_PW_REMOVED**"; - let auth_header_value = format!("{}:{}", username, password); - // let auth_header_value = format!("Basic {}", Engine::encode(&auth_value, ())); - /* NOCH NICHT GEFIXT - let client = reqwest::blocking::Client::new(); - let resp = client - .get("https://wiki.berlin.ccc.de/api.php?action=query&meta=tokens&type=login&format=json") - .send()? - .text()?; - //let response = client - // .post("https://wiki.berlin.ccc.de/api.php?action=query&meta=tokens&type=login&format=json") - // .form(&[("Username", "cccb-wiki"), ("Password", "**OLD_PW_REMOVED**")]) - // .send() - // .unwrap(); - //.json(&map); - let html_source = resp.text()?; - //let login_token: String = map.get("logintoken").unwrap().to_string().clone(); - println!("---HTML:---\n{}\n-----------", html_source); +/// Reads a text file from a specified path and returns it as a String +fn read_txt_file (filepath: &str) -> String { + let mut file = + File::open(filepath).expect(&*format!("Fehler beim öffnen der Textdatei mit Pfad {filepath}!")); + let mut contents = String::new(); + file.read_to_string(&mut contents).expect("Fehler beim auslesen der MediaWiki-Textdatei!").to_string() +} - */ - Ok(String::from("unimplemented")) +/// Takes a Sting in the Markdown format and returns a String in the mediawiki Format +fn convert_md_to_mediawiki (old_pad_content: String) -> String { + let output_filepath: &str = "./pandoc_mediawiki.txt"; + pandoc_convert(old_pad_content, output_filepath, pandoc::InputFormat::Markdown, pandoc::OutputFormat::MediaWiki).expect("Fehler beim Umwandeln des und speichern des Pads in eine mediawiki-Textdatei"); + read_txt_file(output_filepath) } /// Authenticate with username and password on http basic auth -fn auth() { - +pub fn get_login_token(client: &Client, http_user: &str, http_pass: &String) -> Result> { + let resp = client + .get("https://wiki.berlin.ccc.de/api.php?action=query&meta=tokens&type=login&format=json") + .basic_auth(http_user, Some(http_pass)) + .send()? + .text()?; + let response_deserialized: QueryResponse = serde_json::from_str(&resp)?; + Ok(response_deserialized.query.tokens.logintoken) } \ No newline at end of file