|
| 1 | +#include "server_core.hpp" |
| 2 | +#include "server_processor.hpp" |
| 3 | + |
| 4 | +#include <networkprotocoldsl_uv/libuvserverwrapper.hpp> |
| 5 | + |
| 6 | +#include <CLI/CLI.hpp> |
| 7 | + |
| 8 | +#include <condition_variable> |
| 9 | +#include <csignal> |
| 10 | +#include <future> |
| 11 | +#include <iostream> |
| 12 | +#include <mutex> |
| 13 | +#include <unordered_map> |
| 14 | +#include <unordered_set> |
| 15 | + |
| 16 | +namespace { |
| 17 | + |
| 18 | +std::mutex signal_mtx; |
| 19 | +std::condition_variable signal_cv; |
| 20 | +bool stop_signal_received = false; |
| 21 | + |
| 22 | +void signal_handler(int signum) { |
| 23 | + std::lock_guard<std::mutex> lock(signal_mtx); |
| 24 | + stop_signal_received = true; |
| 25 | + std::cerr << "Received signal: " << signum << std::endl; |
| 26 | + signal_cv.notify_one(); |
| 27 | +} |
| 28 | + |
| 29 | +} // anonymous namespace |
| 30 | + |
| 31 | +namespace smtpserver { |
| 32 | + |
| 33 | +void parse_email_list( |
| 34 | + const std::string &str, |
| 35 | + std::unordered_map<std::string, std::unordered_set<std::string>> &map) { |
| 36 | + auto pos = str.find('@'); |
| 37 | + if (pos == std::string::npos) { |
| 38 | + throw CLI::ValidationError("Invalid format for email"); |
| 39 | + } |
| 40 | + map[str.substr(pos + 1)].insert(str.substr(0, pos)); |
| 41 | +} |
| 42 | + |
| 43 | +std::optional<int> |
| 44 | +main_server(const std::span<const char *> &args, |
| 45 | + const networkprotocoldsl::InterpretedProgram &program, |
| 46 | + networkprotocoldsl_uv::AsyncWorkQueue &async_queue) { |
| 47 | + // Setup CLI11 parser. |
| 48 | + ServerConfiguration config; |
| 49 | + |
| 50 | + // add some sample values to the configuration |
| 51 | + config.valid_recipients["example.com"].insert("user1"); |
| 52 | + config.valid_recipients["example.com"].insert("user2"); |
| 53 | + config.valid_recipients["other.com"].insert("user3"); |
| 54 | + config.blocked_senders["bad.com"].insert("user"); |
| 55 | + config.blocked_client_domains.insert("bad.com"); |
| 56 | + |
| 57 | + CLI::App app{"Server configuration"}; |
| 58 | + app.add_option("--bind-ip", config.bind_ip, "IP address to bind") |
| 59 | + ->default_val("127.0.0.1"); |
| 60 | + app.add_option("--bind-port", config.bind_port, "Port to bind") |
| 61 | + ->default_val(8080); |
| 62 | + app.add_option("--server-name", config.server_name, "Server name") |
| 63 | + ->default_val("testsrv"); |
| 64 | + app.add_option("--maildir", config.maildir, "Mail directory") |
| 65 | + ->default_val("./maildir/"); |
| 66 | + app.add_option("--valid-recipients", config.valid_recipients, |
| 67 | + "Valid recipients"); |
| 68 | + app.add_option("--blocked-senders", config.blocked_senders, |
| 69 | + "Blocked senders"); |
| 70 | + app.add_option("--blocked-client-domains", config.blocked_client_domains, |
| 71 | + "Blocked client domains"); |
| 72 | + |
| 73 | + // Convert span to vector (skip program name). |
| 74 | + std::vector<std::string> args_cli(++args.begin(), args.end()); |
| 75 | + try { |
| 76 | + app.parse(args_cli); |
| 77 | + } catch (const CLI::ParseError &e) { |
| 78 | + return app.exit(e); |
| 79 | + } |
| 80 | + |
| 81 | + std::cerr << "Binding to IP: " << config.bind_ip |
| 82 | + << ", Port: " << config.bind_port << std::endl; |
| 83 | + |
| 84 | + // Get the server callbacks. |
| 85 | + auto server_callbacks = get_sever_callbacks(config); |
| 86 | + |
| 87 | + // Create server wrapper and start the server. |
| 88 | + networkprotocoldsl_uv::LibuvServerWrapper server_wrapper( |
| 89 | + program, server_callbacks, async_queue); |
| 90 | + auto bind_future = server_wrapper.start(config.bind_ip, config.bind_port); |
| 91 | + bind_future.wait(); |
| 92 | + auto bind_result = bind_future.get(); |
| 93 | + if (std::holds_alternative<std::string>(bind_result)) { |
| 94 | + std::cerr << "Bind failed: " << std::get<std::string>(bind_result) |
| 95 | + << std::endl; |
| 96 | + return std::nullopt; |
| 97 | + } |
| 98 | + std::cerr << "Server started on descriptor: " << std::get<int>(bind_result) |
| 99 | + << std::endl; |
| 100 | + |
| 101 | + // Setup signal handling for SIGINT. |
| 102 | + std::signal(SIGINT, signal_handler); |
| 103 | + std::cerr << "Server is running; press Ctrl+C to stop." << std::endl; |
| 104 | + |
| 105 | + { |
| 106 | + std::unique_lock<std::mutex> lock(signal_mtx); |
| 107 | + signal_cv.wait(lock, [] { return stop_signal_received; }); |
| 108 | + } |
| 109 | + |
| 110 | + std::cerr << "Stopping server... " << std::flush; |
| 111 | + server_wrapper.stop(); |
| 112 | + |
| 113 | + std::cerr << "done" << std::endl; |
| 114 | + |
| 115 | + return 0; |
| 116 | +} |
| 117 | + |
| 118 | +} // namespace smtpserver |
0 commit comments