mpd: support password protected MPD

- Add MPD module option `password`, and document it.
- Add logic to send the password, directly after connecting to
  MPD.

Fixes: #576
Signed-off-by: Joseph Benden <joe@benden.us>
This commit is contained in:
Joseph Benden 2020-10-19 11:54:36 -07:00
parent f151d435a8
commit 587eb5fdb4
No known key found for this signature in database
GPG Key ID: 713DC1002C5879C2
3 changed files with 19 additions and 3 deletions

View File

@ -1,7 +1,7 @@
#pragma once
#include <mpd/client.h>
#include <fmt/format.h>
#include <mpd/client.h>
#include <spdlog/spdlog.h>
#include <condition_variable>
@ -22,8 +22,9 @@ class MPD : public ALabel {
// Not using unique_ptr since we don't manage the pointer
// (It's either nullptr, or from the config)
const char* server_;
const unsigned port_;
const char* server_;
const unsigned port_;
const std::string password_;
unsigned timeout_;

View File

@ -20,6 +20,10 @@ Addressed by *mpd*
typeof: integer ++
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*: ++
typeof: integer++
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),
server_(nullptr),
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),
connection_(nullptr, &mpd_connection_free),
status_(nullptr, &mpd_status_free),
@ -238,6 +239,16 @@ void waybar::modules::MPD::tryConnect() {
try {
checkErrors(connection_.get());
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) {
spdlog::error("{}: Failed to connect to MPD: {}", module_name_, e.what());
connection_.reset();