Exemplo n.º 1
0
/*
 * Sends a password to the mpd server
 * FIXME: should retrieve updated privileges list?
 */
bool
Control::sendpassword(string pw)
{
	EXIT_IDLE;

	return mpd_run_password(conn->h(), pw.c_str());
}
Exemplo n.º 2
0
void Connection::SendPassword()
{
	assert(m_connection);
	noidle();
	assert(!m_command_list_active);
	mpd_run_password(m_connection.get(), m_password.c_str());
	checkErrors();
}
Exemplo n.º 3
0
static int mpd_update()
{
    struct timeval now;

    /* reread every 1000 msec only */
    gettimeofday(&now, NULL);
    int timedelta = (now.tv_sec - timestamp.tv_sec) * 1000 + (now.tv_usec - timestamp.tv_usec) / 1000;

    if (timedelta > 0 && timedelta < waittime) {
	/* debug("[MPD] waittime not reached...\n"); */
	return 1;
    }

    /* check if configured */
    if (configure_mpd() < 0) {
	return -1;
    }

    /* check if connected */
    if (conn == NULL || mpd_connection_get_error(conn) != MPD_ERROR_SUCCESS) {
	if (conn) {
	    if (errorcnt < ERROR_DISPLAY)
		mpd_printerror("reconnect");
	} else
	    debug("[MPD] initialize connect to [%s]:[%i]", host, iport);
    }
    if (!conn) {
	conn = mpd_connection_new(host, iport, TIMEOUT_IN_S * 1000);
	if (conn == NULL || mpd_connection_get_error(conn) != MPD_ERROR_SUCCESS) {
	    if (conn) {
		if (errorcnt < ERROR_DISPLAY)
		    mpd_printerror("connect");
	    }
	    if (errorcnt == ERROR_DISPLAY)
		error("[MPD] stop logging, until connection is fixed!");
	    errorcnt++;
	    gettimeofday(&timestamp, NULL);
	    return -1;
	}

	if (*pw && !mpd_run_password(conn, pw)) {
	    errorcnt++;
	    mpd_printerror("run_password");
	    return -1;
	}
	errorcnt = 0;
	debug("[MPD] connection fixed...");
    }

    mpd_query_status(conn);
    mpd_query_stats(conn);

    gettimeofday(&timestamp, NULL);
    return 1;
}
Exemplo n.º 4
0
void mpd_poll(struct mg_server *s)
{
    switch (mpd.conn_state) {
        case MPD_DISCONNECTED:
            /* Try to connect */
            fprintf(stdout, "MPD Connecting to %s:%d\n", mpd.host, mpd.port);
            mpd.conn = mpd_connection_new(mpd.host, mpd.port, 3000);
            if (mpd.conn == NULL) {
                fprintf(stderr, "Out of memory.");
                mpd.conn_state = MPD_FAILURE;
                return;
            }

            if (mpd_connection_get_error(mpd.conn) != MPD_ERROR_SUCCESS) {
                fprintf(stderr, "MPD connection: %s\n", mpd_connection_get_error_message(mpd.conn));
                mg_iterate_over_connections(s, mpd_notify_callback, 
                    (void *)mpd_connection_get_error_message(mpd.conn));
                mpd.conn_state = MPD_FAILURE;
                return;
            }

            if(mpd.password && !mpd_run_password(mpd.conn, mpd.password))
            {
                fprintf(stderr, "MPD connection: %s\n", mpd_connection_get_error_message(mpd.conn));
                mg_iterate_over_connections(s, mpd_notify_callback, 
                    (void *)mpd_connection_get_error_message(mpd.conn));
                mpd.conn_state = MPD_FAILURE;
                return;
            }

            fprintf(stderr, "MPD connected.\n");
            mpd_connection_set_timeout(mpd.conn, 10000);
            mpd.conn_state = MPD_CONNECTED;
            break;

        case MPD_FAILURE:
            fprintf(stderr, "MPD connection failed.\n");

        case MPD_DISCONNECT:
        case MPD_RECONNECT:
            if(mpd.conn != NULL)
                mpd_connection_free(mpd.conn);
            mpd.conn = NULL;
            mpd.conn_state = MPD_DISCONNECTED;
            break;

        case MPD_CONNECTED:
            mpd.buf_size = mpd_put_state(mpd.buf, &mpd.song_id, &mpd.queue_version);
            mg_iterate_over_connections(s, mpd_notify_callback, NULL);
            break;
    }
}
Exemplo n.º 5
0
/*
 * pgmpc_init
 *
 * Initialize connection to mpd server.
 */
static void
pgmpc_init(void)
{
	Assert(mpd_conn == NULL);

	/* Establish connection to mpd server */
	mpd_conn = mpd_connection_new(mpd_host, mpd_port, mpd_timeout * 1000);
	if (mpd_connection_get_error(mpd_conn) != MPD_ERROR_SUCCESS)
		pgmpc_print_error();

	/* Send password if any */
	if (mpd_password[0])
	{
		if (!mpd_run_password(mpd_conn, mpd_password))
			pgmpc_print_error();
	}
}
Exemplo n.º 6
0
bool
CMPD::Connect()
{
    if(_conn)
        mpd_connection_free(_conn);

	_conn = mpd_connection_new(Config->getMHost().c_str(), Config->getMPort(), 0);
    _connected = _conn && mpd_connection_get_error(_conn) == MPD_ERROR_SUCCESS;

    if(_connected && Config->getMPassword().size() > 0) {
        _connected &= mpd_run_password(_conn, Config->getMPassword().c_str());
    }

    if(_connected)
        mpd_run_subscribe(_conn, "mpdas");

	return _connected;
}
Exemplo n.º 7
0
Arquivo: mpd.cpp Projeto: hrkfdn/mpdas
bool CMPD::Connect()
{
    if(_conn)
        mpd_connection_free(_conn);

    _conn = mpd_connection_new(_cfg->Get("host").c_str(), _cfg->GetInt("port"), 0);
    _connected = _conn && mpd_connection_get_error(_conn) == MPD_ERROR_SUCCESS;

    if(_connected && _cfg->Get("mpdpassword").size() > 0) {
        _connected &= mpd_run_password(_conn, _cfg->Get("mpdpassword").c_str());
    }
    else if(!_connected) {
        eprintf("MPD connection error: %s", mpd_connection_get_error_message(_conn));
    }

    if(_connected)
        mpd_run_subscribe(_conn, "mpdas");

    return _connected;
}
Exemplo n.º 8
0
int
main(int argc, char *argv[])
{
  char *password = NULL;
  struct mpd_connection *conn = mpd_connection_new(mpd_check_host(NULL, &password), 0, 0);

  if (mpd_connection_get_error(conn) != MPD_ERROR_SUCCESS)
  {
    fprintf(stderr,"%s: %s\n", argv[0], mpd_connection_get_error_message(conn));
    return 1;
  }

  if (password != NULL)
  {
    if (mpd_run_password(conn, password) == false)
    {
      fprintf(stderr, "%s: mpd_run_password %s\n", argv[0],
	  mpd_connection_get_error_message(conn));
      free(password);
      return 2;
    }
  }

  if (notify_init("MPD_Notification") == 0)
  {
    fprintf(stderr, "%s: Cannot initialize libnotify.\n", argv[0]);
    return 3;
  }

  infinite_loop(conn);

  mpd_connection_free(conn);
  free(password);
  notify_uninit();

  return 0;
}
Exemplo n.º 9
0
int main(int argc, char *argv[]) {
	int opts;

	const char   *short_opts  = "t:r:b:a:p:s:h";
	struct option long_opts[] = {
		{ "title",	required_argument,	0, 't' },
		{ "artist",	required_argument,	0, 'r' },
		{ "album",	required_argument,	0, 'b' },
		{ "addr",	required_argument,	0, 'a' },
		{ "port",	required_argument,	0, 'p' },
		{ "secret",	required_argument,	0, 's' },
		{ "help",	no_argument,		0, 'h' },
		{ 0,		0,			0,  0  }
	};

	unsigned int id = 0;

	struct mpd_song *song = NULL;
	struct mpd_connection *mpd = NULL;

	char *mpd_addr = getenv("MPD_HOST");
	int   mpd_port = getenv("MPD_PORT") ? atoi(getenv("MPD_PORT")) : 0;
	char *mpd_pass = NULL;

	char *title  = NULL;
	char *artist = NULL;
	char *album  = NULL;

	if (argc == 1) {
		help();
		return -1;
	}

	while ((opts = getopt_long(argc, argv, short_opts, long_opts, 0)) != -1) {
		switch (opts) {
			case 't': { title = optarg;		break;     }
			case 'r': { artist = optarg;		break;     }
			case 'b': { album = optarg;		break;     }
			case 'a': { mpd_addr = optarg;		break;     }
			case 'p': { mpd_port = atoi(optarg);	break;     }
			case 's': { mpd_pass = optarg;		break;     }
			default :
			case 'h': { help();			return -1; }
		}
	}

	mpd = mpd_connection_new(mpd_addr, mpd_port, 30000);
	CHECK_MPD_CONN(mpd);

	if (mpd_pass != NULL) {
		mpd_run_password(mpd, mpd_pass);
		CHECK_MPD_CONN(mpd);
	}

	mpd_search_queue_songs(mpd, false);

	if (title) {
		mpd_search_add_tag_constraint(
			mpd, MPD_OPERATOR_DEFAULT,
			MPD_TAG_TITLE, title
		);
	}

	if (artist) {
		mpd_search_add_tag_constraint(
			mpd, MPD_OPERATOR_DEFAULT,
			MPD_TAG_ARTIST, artist
		);
	}

	if (album) {
		mpd_search_add_tag_constraint(
			mpd, MPD_OPERATOR_DEFAULT,
			MPD_TAG_ALBUM, album
		);
	}

	mpd_search_commit(mpd);

	song = mpd_recv_song(mpd);

	mpd_response_finish(mpd);

	if (song) {
		id = mpd_song_get_id(song);

		mpd_song_free(song);

		mpd_run_play_id(mpd, id);
		CHECK_MPD_CONN(mpd);
	}

	mpd_connection_free(mpd);

	return 0;
}
Exemplo n.º 10
0
/* Establish a connection with the MPD server
 * If the connection failed, the "conn" is set to NULL
 */
void Player::connectToMpd()
{
    QSettings settings;
    unsigned int retryPeriod;
    QString host;
    unsigned int port;
    unsigned int timeout;
    QString password;

    EMS_LOAD_SETTINGS(retryPeriod, "player/retry_period", EMS_MPD_CONNECTION_RETRY_PERIOD, UInt);
    EMS_LOAD_SETTINGS(host, "player/host", EMS_MPD_IP, String);
    EMS_LOAD_SETTINGS(timeout, "player/timeout", EMS_MPD_TIMEOUT, UInt);
    EMS_LOAD_SETTINGS(port, "player/port", EMS_MPD_PORT, UInt);
    EMS_LOAD_SETTINGS(password, "player/password", EMS_MPD_PASSWORD, String);

    if (conn != NULL)
    {
        disconnectToMpd();
    }

    /* Try to connect until it success */
    while (!conn && !isInterruptionRequested())
    {
        qDebug() << "Connecting to " << QString("%1:%2... (timeout %3ms)").arg(host).arg(port).arg(timeout);
        conn = mpd_connection_new(host.toStdString().c_str(), port, timeout);

        if (conn == NULL)
        {
            qCritical() << "MPD Connection failed.";
        }
        else if (mpd_connection_get_error(conn) != MPD_ERROR_SUCCESS)
        {
            QString errorMessage = QString::fromUtf8(mpd_connection_get_error_message(conn));
            qCritical() << "MPD Connection failed : " << errorMessage;
            mpd_connection_free(conn);
            conn = NULL;
        }
        else if (!password.isEmpty())
        {
            qDebug() << "Setting password...";
            if (!mpd_run_password(conn, password.toStdString().c_str()))
            {
                qCritical() << "Setting MPD password failed.";
                mpd_connection_free(conn);
                conn = NULL;
            }
        }

        if (!conn)
        {
            qCritical() << "Retrying within " << QString("%1 ms").arg(retryPeriod);
            for (unsigned int i=0; i<retryPeriod; i++)
            {
                if (!isInterruptionRequested())
                    usleep(1000);
                else
                    break;
            }
        }
    }

    qDebug() << "Connected to " << host;
}
Exemplo n.º 11
0
struct mpd_connection *
mpd_connection_new(const char *host, unsigned port, unsigned timeout_ms)
{
	struct mpd_connection *connection = malloc(sizeof(*connection));
	bool success;
	int fd;
	const char *line;
	char *password = NULL;


	if (connection == NULL)
		return NULL;

	mpd_error_init(&connection->error);
	connection->async = NULL;
	connection->parser = NULL;
	connection->receiving = false;
	connection->sending_command_list = false;
	connection->pair_state = PAIR_STATE_NONE;
	connection->request = NULL;

	if (!mpd_socket_global_init(&connection->error))
		return connection;

	if (timeout_ms == 0)
		/* 30s is the default */
		timeout_ms = 30000;

	mpd_connection_set_timeout(connection, timeout_ms);

	host = mpd_check_host(host, &password);
	port = mpd_check_port(port);

	fd = mpd_connect(host, port, &connection->timeout, &connection->error);
	if (fd < 0) {
		free(password);
		return connection;
	}

	connection->async = mpd_async_new(fd);
	if (connection->async == NULL) {
		free(password);
		mpd_socket_close(fd);
		mpd_error_code(&connection->error, MPD_ERROR_OOM);
		return connection;
	}

	connection->parser = mpd_parser_new();
	if (connection->parser == NULL) {
		free(password);
		mpd_error_code(&connection->error, MPD_ERROR_OOM);
		return connection;
	}

	line = mpd_sync_recv_line(connection->async, &connection->timeout);
	if (line == NULL) {
		free(password);
		mpd_connection_sync_error(connection);
		return connection;
	}

	success = mpd_parse_welcome(connection, line);

	if (password != NULL) {
		if (success)
			mpd_run_password(connection, password);
		free(password);
	}

	return connection;
}
Exemplo n.º 12
0
void print_mpd(yajl_gen json_gen, char *buffer, const char *host, int port, const char *password, const char *format) {
        const char *walk;
        char *outwalk = buffer;
        static char titlebuf[40];
        static char artistbuf[40];
        static char albumbuf[40];
        static char trackbuf[10];
        static char datebuf[10];

        static struct mpd_connection *conn;
        struct mpd_status *status = NULL;
        enum mpd_state state;
        struct mpd_song *song;

        /* First run */
        if (conn == NULL) {
                conn = mpd_connection_new(host, port, 1500);
                if (conn == NULL) {
                        START_COLOR("color_bad");
                        outwalk += sprintf(outwalk, "%s", "ERROR");
                        goto print_end;
                }

                if (mpd_connection_get_error(conn) != MPD_ERROR_SUCCESS) {
                        START_COLOR("color_bad");
                        outwalk += sprintf(outwalk, "%s", "CONNECT ERROR");
                        mpd_connection_free(conn);
                        conn = NULL;
                        goto print_end;
                }

                if (password != NULL && strcmp(password, "") != 0 &&
                                !mpd_run_password(conn, password)) {
                        START_COLOR("color_bad");
                        outwalk += sprintf(outwalk, "%s", "PASS ERROR");
                        mpd_connection_free(conn);
                        conn = NULL;
                        goto print_end;
                }
        }

        if ((status = mpd_run_status(conn))) {
                state = mpd_status_get_state(status);
        }

        if (!status || (state != MPD_STATE_PLAY && state != MPD_STATE_PAUSE)) {
                START_COLOR("color_bad");
                outwalk += sprintf(outwalk, "%s", "Stopped");
                mpd_connection_free(conn);
                conn = NULL;
                goto print_end;
        }

        mpd_status_free(status);

        if (state == MPD_STATE_PLAY)
                START_COLOR("color_good");
        else if (state == MPD_STATE_PAUSE)
                START_COLOR("color_degraded");

        song = mpd_run_current_song(conn);

        COPY_CROP(titlebuf, mpd_song_get_tag(song, MPD_TAG_TITLE, 0));
        COPY_CROP(artistbuf, mpd_song_get_tag(song, MPD_TAG_ARTIST, 0));
        COPY_CROP(albumbuf, mpd_song_get_tag(song, MPD_TAG_ALBUM, 0));
        COPY_CROP(trackbuf, mpd_song_get_tag(song, MPD_TAG_TRACK, 0));
        COPY_CROP(datebuf, mpd_song_get_tag(song, MPD_TAG_DATE, 0));

        mpd_song_free(song);

        for (walk = format; *walk != '\0'; walk++) {
                if (*walk != '%') {
                        *(outwalk++) = *walk;
                        continue;
                }

                if (BEGINS_WITH(walk+1, "title")) {
                        if (*titlebuf)
                                outwalk += sprintf(outwalk, "%s", titlebuf);
                        walk += strlen("title");
                }
                else if (BEGINS_WITH(walk+1, "artist")) {
                        if (*artistbuf)
                                outwalk += sprintf(outwalk, "%s", artistbuf);
                        walk += strlen("artist");
                }
                else if (BEGINS_WITH(walk+1, "album")) {
                        if (*albumbuf)
                                outwalk += sprintf(outwalk, "%s", albumbuf);
                        walk += strlen("album");
                }
                else if (BEGINS_WITH(walk+1, "track")) {
                        if (*trackbuf)
                                outwalk += sprintf(outwalk, "%s", trackbuf);
                        walk += strlen("track");
                }
                else if (BEGINS_WITH(walk+1, "date")) {
                        if (*datebuf)
                                outwalk += sprintf(outwalk, "%s", datebuf);
                        walk += strlen("date");
                }
        }

print_end:

        END_COLOR;
        OUTPUT_FULL_TEXT(buffer);

        return;
}