3 * (c) 2005 by Avuton Olrich <avuton@gmail.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include "libmpdclient.h"
27 #define DEFAULT_HOST "localhost"
28 #define DEFAULT_PORT "6600"
29 #define MPD_TIMEOUT 60
32 static void parse_password(const char *host
, int *password_len
, int *parsed_len
)
34 /* parse password and host */
35 char *ret
= strstr(host
, "@");
38 if (ret
&& len
== 0) {
42 *parsed_len
+= len
+ 1;
47 void printErrorAndExit(mpd_Connection
* conn
)
50 fprintf(stderr
, "error: %s\n", conn
->errorStr
);
55 mpd_Connection
*setup_connection(char * host
, char * port
)
63 if(!host
|| strlen(host
)<1) {
64 if ((test
= getenv("MPD_HOST")) && test
) {
71 if(!port
|| strlen(port
)<1) {
72 if ((test
= getenv("MPD_PORT")) && test
) {
79 iport
= strtol(port
, &test
, 10);
81 if (iport
<= 0 || *test
!= '\0') {
83 "MPD_PORT \"%s\" is not a positive integer\n", port
);
87 parse_password(host
, &password_len
, &parsed_len
);
89 conn
= mpd_newConnection(host
+ parsed_len
, iport
, (float)MPD_TIMEOUT
);
91 printErrorAndExit(conn
);
94 mpd_sendPasswordCommand(conn
, &host
[password_len
]);
95 printErrorAndExit(conn
);
96 mpd_finishCommand(conn
);
97 printErrorAndExit(conn
);