From 5ad6018a889cc4240056ec2236a2cc983a0fda7f Mon Sep 17 00:00:00 2001 From: Glenn Date: Sun, 24 Dec 2023 18:24:07 +0100 Subject: [PATCH] refactor: move SignalSpam specific code to providers/ --- src/config.cr | 30 ++++++++---- src/main.cr | 93 +++++++++++++++++++++++++----------- src/providers/abstract.cr | 6 +++ src/providers/signal_spam.cr | 73 ++++++++++++++++++++++++++++ 4 files changed, 164 insertions(+), 38 deletions(-) create mode 100644 src/providers/abstract.cr create mode 100644 src/providers/signal_spam.cr diff --git a/src/config.cr b/src/config.cr index 1d1878a..23631a9 100644 --- a/src/config.cr +++ b/src/config.cr @@ -1,17 +1,27 @@ +require "yaml" + +# Classe pour la gestion de la configuration +class Authentication + include YAML::Serializable + + @[YAML::Field(key: "username")] + property username : String + + @[YAML::Field(key: "password")] + property password : String + + def initialize(@username, @password) + end +end class Config include YAML::Serializable - class Authentication - include YAML::Serializable - - @[YAML::Field(key: "username")] - property username : String - - @[YAML::Field(key: "password")] - property password : String - end @[YAML::Field(key: "authentication")] - property auth : Authentication + property authentication : Authentication + + def initialize(@authentication) + end end + diff --git a/src/main.cr b/src/main.cr index 5c98102..bb78029 100644 --- a/src/main.cr +++ b/src/main.cr @@ -1,32 +1,69 @@ -require "yaml" +require "./config" +require "./providers/signal_spam" + require "http/client" -require "./config.cr" -require "./version.cr" +require "option_parser" +require "file_utils" +require "mechanize" + +# Nom du script et version +SCRIPT_NAME = "BulkBarrage" +SCRIPT_VERSION = "0.1.0" + +# Classe pour gérer les interactions avec Signal-Spam +class SignalSpamCtl + def initialize + @option_parser = OptionParser.new do |parser| + parser.banner = [ + "#{SCRIPT_NAME} v#{SCRIPT_VERSION}", + "Usage: bulkbarrage [options] [command] [args]" + ].join("\n") + + parser.on("configure", "Initialise the configuration") do + init_config + end + parser.on("report", "Process and report spam email (default: from STDIN)") do + process_email_spam + end + parser.on("--help", "-h", "show this help") do + puts parser + end + end + end + + def parse_arguments(args : Array(String)) + @option_parser.parse(args) + rescue e : OptionParser::Exception + puts e.message + puts @option_parser + exit(1) + end + + + private def init_config + print "Enter username: " + username = gets.try &.strip || "anonymous" + print "Enter password: " + password = gets.try &.strip || "anonymous" + create_config_file(username, password) + puts "Configuration file created." + end + + private def process_email_spam + email_spam = STDIN.gets_to_end + + signalspam = Providers::SignalSpam.new + signalspam.process(email_spam) + + puts "Email spam processed." + end + + private def create_config_file(username : String, password : String) + signalspam = Providers::SignalSpam.new + signalspam.create_config_file(username, password) + end -# Partie 1: Lecture du fichier de configuration YAML -config_path = File.expand_path("~/.config/signalspam/config.yaml", home: ENV["HOME"]) -begin - config = Config.from_yaml(File.read(config_path)) -rescue ex - puts "Error reading or parsing config file: #{ex.message}" - exit(1) end -# Partie 2: Lecture de l'email-spam depuis STDIN -email_spam = STDIN.gets_to_end - -# Partie 3: Authentification sur signal-spam.fr -def authenticate(config : Config) - # Logique d'authentification (à compléter) -end - -# Partie 4: Remplissage et envoi du formulaire -def submit_spam_report(email_spam : String, config : Config) - # Logique pour remplir et envoyer le formulaire (à compléter) -end - -# Exécution des fonctions -authenticate(config) -submit_spam_report(email_spam, config) - -puts "#{SCRIPT_NAME} v#{SCRIPT_VERSION} executed successfully." +client = SignalSpamCtl.new +client.parse_arguments(ARGV) diff --git a/src/providers/abstract.cr b/src/providers/abstract.cr new file mode 100644 index 0000000..d31d16d --- /dev/null +++ b/src/providers/abstract.cr @@ -0,0 +1,6 @@ + +module Providers + abstract class Abstract + abstract def process(email_content : String) + end +end diff --git a/src/providers/signal_spam.cr b/src/providers/signal_spam.cr new file mode 100644 index 0000000..d3ce5e7 --- /dev/null +++ b/src/providers/signal_spam.cr @@ -0,0 +1,73 @@ + +require "../config" +require "./abstract" + +module Providers + class SignalSpam < Abstract + + property config : Config? = nil + + def initialize + end + + def process(email_content : String) + load_config + agent = Mechanize.new + authenticate(agent) + submit_spam_report(agent, email_content) + end + + def create_config_file(username : String, password : String) + auth = Authentication.new(username: username, password: password) + config = Config.new(authentication: auth) + config_path = File.expand_path("~/.config/bulkbarrage/config.yaml", home: ENV["HOME"]) + FileUtils.mkdir_p(File.dirname(config_path)) + File.write(config_path, config.to_yaml) + end + + private def load_config + config_path = File.expand_path("~/.config/bulkbarrage/config.yaml", home: ENV["HOME"]) + begin + @config = Config.from_yaml(File.read(config_path)) + rescue ex + puts "Error reading or parsing config file: #{ex.message}" + exit(1) + end + end + + private def authenticate(agent) + # Visiter la page de connexion pour obtenir le token d'authenticité + login_page = agent.get("https://signalants.signal-spam.fr/login") + form = login_page.forms.first + authenticity_token = form.field_with("authenticity_token").value + puts "Authentication page loaded." + + # Configurer les détails de l'utilisateur + config = @config + return if config.nil? + + form.field_with("user[email_or_login]").value = config.authentication.username + form.field_with("user[password]").value = config.authentication.password + # form.field_with("authenticity_token").value = authenticity_token + + # Soumettre le formulaire + result_page = agent.submit(form) + raise "Unable to authenticate" if result_page.nil? + puts "Authenticated." + + end + + private def submit_spam_report(agent, email_spam : String) + report_page = agent.get("https://signalants.signal-spam.fr/reportings/new") + form = report_page.forms.first + authenticity_token = form.field_with("authenticity_token").value + puts "Reporting page loaded." + + form.field_with("reporting[raw_email]").value = email_spam + result_page = agent.submit(form) + raise "Unable to submit" if result_page.nil? + puts "Reporting sent." + end + + end +end