1 /***************************************************************************
2 * Copyright (C) 2008-2009 by Andrzej Rybczak *
3 * electricityispower@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. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
19 ***************************************************************************/
27 #include <sys/types.h>
31 #elif defined(__FreeBSD__)
35 #include <sys/param.h>
36 #include <sys/sysctl.h>
44 #include "configuration.h"
53 std::string
GetLineValue(const string
&line
, char a
= '"', char b
= '"')
56 int begin
= -1, end
= -1;
57 for (string::const_iterator it
= line
.begin(); it
!= line
.end(); i
++, it
++)
59 if (*it
== a
|| *it
== b
)
67 if (begin
>= 0 && end
>= 0)
68 return line
.substr(begin
, end
-begin
);
73 void HomeFolder(const ScrobbyConfig
&conf
, string
&s
)
76 s
.replace(0, 1, conf
.user_home_folder
);
79 LogLevel
IntoLogLevel(const string
&value
)
81 LogLevel result
= llInfo
;
86 else if (value
== "info")
90 else if (value
== "verbose")
98 void ParseArgv(ScrobbyConfig
&conf
, int argc
, char **argv
)
100 for (int i
= 1; i
< argc
; i
++)
102 if (strcmp(argv
[i
], "--help") == 0)
106 << "scrobby [options] <conf file>\n"
107 << "scrobby [options] (search for ~/.scrobbyconf, then /etc/scrobby.conf)\n\n"
109 << " --help show this help message\n"
110 << " --no-daemon do not detach from console\n"
111 << " --quiet do not log anything\n"
112 << " --verbose verbose logging\n"
113 << " --version print version information\n"
117 else if (strcmp(argv
[i
], "--no-daemon") == 0)
119 conf
.daemonize
= false;
121 else if (strcmp(argv
[i
], "--quiet") == 0)
123 conf
.log_level
= llNone
;
125 else if (strcmp(argv
[i
], "--verbose") == 0)
127 conf
.log_level
= llVerbose
;
129 else if (strcmp(argv
[i
], "--version") == 0)
131 std::cout
<< "scrobby - an audioscrobbler mpd client, version "VERSION
"\n";
136 conf
.file_config
= argv
[i
];
141 bool CheckFiles(ScrobbyConfig
&conf
)
146 g
.open(conf
.file_pid
.c_str());
152 pid_t pid
= StrToInt(strpid
);
155 std::cerr
<< "pid file: " << conf
.file_pid
<< " is invalid, trying to remove...\n";
156 if (unlink(conf
.file_pid
.c_str()) == 0)
158 std::cout
<< "pid file succesfully removed.\n";
162 std::cerr
<< "couldn't remove pid file.\n";
169 struct stat stat_pid
;
170 std::ostringstream proc_file
;
171 proc_file
<< "/proc/" << pid
<< std::ends
;
172 if (!(stat(proc_file
.str().c_str(), &stat_pid
) == -1 && errno
== ENOENT
))
173 # elif defined(__FreeBSD__)
174 char kvm_errbuf
[_POSIX2_LINE_MAX
];
177 struct kinfo_proc
*proc
;
179 hkvm
= kvm_openfiles(NULL
, _PATH_DEVNULL
, NULL
, O_RDONLY
, kvm_errbuf
);
182 proc
= kvm_getprocs(hkvm
, KERN_PROC_PID
, pid
, &cnt
);
187 std::cerr
<< "kvm_openfiles failed with message:" << std::endl
<< kvm_errbuf
<< std::endl
;
192 std::cerr
<< "scrobby is already running with PID " << pid
<< "!\n";
198 f
.open(conf
.file_pid
.c_str(), std::ios_base::app
);
201 std::cerr
<< "Cannot create/open pid file: " << conf
.file_pid
<< std::endl
;
206 f
.open(conf
.file_log
.c_str(), std::ios_base::app
);
209 std::cerr
<< "Cannot create/open log file: " << conf
.file_log
<< std::endl
;
214 f
.open(conf
.file_cache
.c_str(), std::ios_base::app
);
217 std::cerr
<< "Cannot create/open cache file: " << conf
.file_cache
<< std::endl
;
225 void DefaultConfiguration(ScrobbyConfig
&conf
)
227 conf
.user_home_folder
= getenv("HOME") ? getenv("HOME") : "";
229 conf
.mpd_host
= "localhost";
230 conf
.mpd_port
= 6600;
231 conf
.mpd_timeout
= 15;
233 conf
.file_log
= "/var/log/scrobby/scrobby.log";
234 conf
.file_pid
= "/var/run/scrobby/scrobby.pid";
235 conf
.file_cache
= "/var/cache/scrobby/scrobby.cache";
237 conf
.log_level
= llUndefined
;
238 conf
.daemonize
= true;
241 bool ReadConfiguration(ScrobbyConfig
&conf
, const string
&file
)
244 std::ifstream
f(file
.c_str());
252 if (!line
.empty() && line
[0] != '#')
254 v
= GetLineValue(line
);
256 if (line
.find("dedicated_user") != string::npos
)
259 conf
.dedicated_user
= v
;
261 else if (line
.find("mpd_host") != string::npos
)
266 else if (line
.find("mpd_port") != string::npos
)
269 conf
.mpd_port
= StrToInt(v
);
271 else if (line
.find("mpd_timeout") != string::npos
)
274 conf
.mpd_timeout
= StrToInt(v
);
276 else if (line
.find("log_file") != string::npos
)
284 else if (line
.find("pid_file") != string::npos
)
292 else if (line
.find("cache_file") != string::npos
)
300 else if (line
.find("lastfm_user") != string::npos
)
303 conf
.lastfm_user
= v
;
305 else if (line
.find("lastfm_password") != string::npos
)
308 conf
.lastfm_password
= v
;
310 else if (line
.find("lastfm_md5_password") != string::npos
)
313 conf
.lastfm_md5_password
= v
;
315 else if (line
.find("log_level") != string::npos
)
317 if (!v
.empty() && conf
.log_level
== llUndefined
)
318 conf
.log_level
= IntoLogLevel(v
);