Merge pull request #888 from jbenden/mpd-password

mpd: support password protected MPD
pull/879/head^2
Alex 2020-10-20 09:06:32 +02:00 committed by GitHub
commit acf990743e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 3 deletions

View File

@ -1,7 +1,7 @@
#pragma once #pragma once
#include <mpd/client.h>
#include <fmt/format.h> #include <fmt/format.h>
#include <mpd/client.h>
#include <spdlog/spdlog.h> #include <spdlog/spdlog.h>
#include <condition_variable> #include <condition_variable>
@ -24,6 +24,7 @@ class MPD : public ALabel {
// (It's either nullptr, or from the config) // (It's either nullptr, or from the config)
const char* server_; const char* server_;
const unsigned port_; const unsigned port_;
const std::string password_;
unsigned timeout_; unsigned timeout_;

View File

@ -20,6 +20,10 @@ Addressed by *mpd*
typeof: integer ++ typeof: integer ++
The port MPD listens to. If empty, use the default port. The port MPD listens to. If empty, use the default port.
*password*: ++
typeof: string ++
The password required to connect to the MPD server. If empty, no password is sent to MPD.
*interval*: ++ *interval*: ++
typeof: integer++ typeof: integer++
default: 5 ++ default: 5 ++

View File

@ -15,6 +15,7 @@ waybar::modules::MPD::MPD(const std::string& id, const Json::Value& config)
module_name_(id.empty() ? "mpd" : "mpd#" + id), module_name_(id.empty() ? "mpd" : "mpd#" + id),
server_(nullptr), server_(nullptr),
port_(config_["port"].isUInt() ? config["port"].asUInt() : 0), port_(config_["port"].isUInt() ? config["port"].asUInt() : 0),
password_(config_["password"].empty() ? "" : config_["password"].asString()),
timeout_(config_["timeout"].isUInt() ? config_["timeout"].asUInt() * 1'000 : 30'000), timeout_(config_["timeout"].isUInt() ? config_["timeout"].asUInt() * 1'000 : 30'000),
connection_(nullptr, &mpd_connection_free), connection_(nullptr, &mpd_connection_free),
status_(nullptr, &mpd_status_free), status_(nullptr, &mpd_status_free),
@ -238,6 +239,16 @@ void waybar::modules::MPD::tryConnect() {
try { try {
checkErrors(connection_.get()); checkErrors(connection_.get());
spdlog::debug("{}: Connected to MPD", module_name_); spdlog::debug("{}: Connected to MPD", module_name_);
if (!password_.empty()) {
bool res = mpd_run_password(connection_.get(), password_.c_str());
if (!res) {
spdlog::error("{}: Wrong MPD password", module_name_);
connection_.reset();
return;
}
checkErrors(connection_.get());
}
} catch (std::runtime_error& e) { } catch (std::runtime_error& e) {
spdlog::error("{}: Failed to connect to MPD: {}", module_name_, e.what()); spdlog::error("{}: Failed to connect to MPD: {}", module_name_, e.what());
connection_.reset(); connection_.reset();