diff irccdctl/main.cpp @ 644:aae6d5a2b28d

Irccd: change how configuration is loaded
author David Demelier <markand@malikania.fr>
date Fri, 23 Mar 2018 14:00:03 +0100
parents 7e2d0739f37c
children 4a13a016ea4f
line wrap: on
line diff
--- a/irccdctl/main.cpp	Wed Mar 21 19:45:55 2018 +0100
+++ b/irccdctl/main.cpp	Fri Mar 23 14:00:03 2018 +0100
@@ -122,7 +122,6 @@
 {
     std::unique_ptr<connection> conn;
     std::string host;
-    std::uint16_t port;
     ini::section::const_iterator it;
 
     if ((it = sc.find("host")) == sc.end())
@@ -133,16 +132,19 @@
     if ((it = sc.find("port")) == sc.end())
         throw std::invalid_argument("missing port parameter");
 
-    port = string_util::to_uint<std::uint16_t>(it->value());
+    const auto port = string_util::to_uint<std::uint16_t>(it->value());
+
+    if (!port)
+        throw std::invalid_argument("invalid port parameter");
 
     if ((it = sc.find("ssl")) != sc.end() && string_util::is_boolean(it->value()))
 #if defined(HAVE_SSL)
-        conn = std::make_unique<tls_connection>(service, ctx, host, port);
+        conn = std::make_unique<tls_connection>(service, ctx, host, *port);
 #else
         throw std::runtime_error("SSL disabled");
 #endif
     else
-        conn = std::make_unique<ip_connection>(service, host, port);
+        conn = std::make_unique<ip_connection>(service, host, *port);
 
     return conn;
 }
@@ -305,9 +307,12 @@
     if ((it = options.find("-p")) == options.end() && (it = options.find("--port")) == options.end())
         throw std::invalid_argument("missing port argument (-p or --port)");
 
-    auto port = string_util::to_uint<std::uint16_t>(it->second);
+    const auto port = string_util::to_uint<std::uint16_t>(it->second);
 
-    return std::make_unique<ip_connection>(service, host, port);
+    if (!port)
+        throw std::invalid_argument("invalid port argument");
+
+    return std::make_unique<ip_connection>(service, host, *port);
 }
 
 /*