1 /***************************************************************************
2 * Copyright (C) 2008 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 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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"
50 std::string
GetLineValue(const string
&line
, char a
= '"', char b
= '"')
53 int begin
= -1, end
= -1;
54 for (string::const_iterator it
= line
.begin(); it
!= line
.end(); i
++, it
++)
56 if (*it
== a
|| *it
== b
)
64 if (begin
>= 0 && end
>= 0)
65 return line
.substr(begin
, end
-begin
);
70 void HomeFolder(const ScrobbyConfig
&conf
, string
&s
)
73 s
.replace(0, 1, conf
.user_home_folder
);
76 LogLevel
IntoLogLevel(const string
&value
)
78 LogLevel result
= llInfo
;
83 else if (value
== "info")
87 else if (value
== "verbose")
95 void ParseArgv(ScrobbyConfig
&conf
, int argc
, char **argv
)
97 for (int i
= 1; i
< argc
; i
++)
99 if (strcmp(argv
[i
], "--help") == 0)
103 << "scrobby [options] <conf file>\n"
104 << "scrobby [options] (search for ~/.scrobbyconf, then /etc/scrobby.conf)\n\n"
106 << " --help show this help message\n"
107 << " --no-daemon do not detach from console\n"
108 << " --quiet do not log anything\n"
109 << " --verbose verbose logging\n"
110 << " --version print version information\n"
114 else if (strcmp(argv
[i
], "--no-daemon") == 0)
116 conf
.daemonize
= false;
118 else if (strcmp(argv
[i
], "--quiet") == 0)
120 conf
.log_level
= llNone
;
122 else if (strcmp(argv
[i
], "--verbose") == 0)
124 conf
.log_level
= llVerbose
;
126 else if (strcmp(argv
[i
], "--version") == 0)
128 std::cout
<< "scrobby - an audioscrobbler mpd client, version "VERSION
"\n";
133 conf
.file_config
= argv
[i
];
138 bool CheckFiles(ScrobbyConfig
&conf
)
143 g
.open(conf
.file_pid
.c_str());
149 pid_t pid
= StrToInt(strpid
);
152 std::cerr
<< "pid file: " << conf
.file_pid
<< " is invalid, trying to remove...\n";
153 if (unlink(conf
.file_pid
.c_str()) == 0)
155 std::cout
<< "pid file succesfully removed.\n";
159 std::cerr
<< "couldn't remove pid file.\n";
166 struct stat stat_pid
;
167 std::ostringstream proc_file
;
168 proc_file
<< "/proc/" << pid
<< std::ends
;
169 if (!(stat(proc_file
.str().c_str(), &stat_pid
) == -1 && errno
== ENOENT
))
171 # elif defined(__FreeBSD__)
172 char kvm_errbuf
[_POSIX2_LINE_MAX
];
175 struct kinfo_proc
*proc
;
177 hkvm
= kvm_openfiles(NULL
, _PATH_DEVNULL
, NULL
, O_RDONLY
, kvm_errbuf
);
180 proc
= kvm_getprocs(hkvm
, KERN_PROC_PID
, pid
, &cnt
);
185 std::cerr
<< "kvm_openfiles failed with message:" << std::endl
<< kvm_errbuf
<< std::endl
;
190 std::cerr
<< "scrobby is already running with PID " << pid
<< "!\n";
196 f
.open(conf
.file_pid
.c_str(), std::ios_base::app
);
199 std::cerr
<< "Cannot create/open pid file: " << conf
.file_pid
<< std::endl
;
204 f
.open(conf
.file_log
.c_str(), std::ios_base::app
);
207 std::cerr
<< "Cannot create/open log file: " << conf
.file_log
<< std::endl
;
212 f
.open(conf
.file_cache
.c_str(), std::ios_base::app
);
215 std::cerr
<< "Cannot create/open cache file: " << conf
.file_cache
<< std::endl
;
223 void DefaultConfiguration(ScrobbyConfig
&conf
)
225 conf
.user_home_folder
= getenv("HOME") ? getenv("HOME") : "";
227 conf
.mpd_host
= "localhost";
228 conf
.mpd_port
= 6600;
229 conf
.mpd_timeout
= 15;
231 conf
.file_log
= "/var/log/scrobby/scrobby.log";
232 conf
.file_pid
= "/var/run/scrobby/scrobby.pid";
233 conf
.file_cache
= "/var/cache/scrobby/scrobby.cache";
235 conf
.log_level
= llUndefined
;
236 conf
.daemonize
= true;
239 bool ReadConfiguration(ScrobbyConfig
&conf
, const string
&file
)
242 std::ifstream
f(file
.c_str());
250 if (!line
.empty() && line
[0] != '#')
252 v
= GetLineValue(line
);
254 if (line
.find("dedicated_user") != string::npos
)
257 conf
.dedicated_user
= v
;
259 else if (line
.find("mpd_host") != string::npos
)
264 else if (line
.find("mpd_port") != string::npos
)
267 conf
.mpd_port
= StrToInt(v
);
269 else if (line
.find("mpd_timeout") != string::npos
)
272 conf
.mpd_timeout
= StrToInt(v
);
274 else if (line
.find("log_file") != string::npos
)
282 else if (line
.find("pid_file") != string::npos
)
290 else if (line
.find("cache_file") != string::npos
)
298 else if (line
.find("lastfm_user") != string::npos
)
301 conf
.lastfm_user
= v
;
303 else if (line
.find("lastfm_password") != string::npos
)
306 conf
.lastfm_password
= v
;
308 else if (line
.find("lastfm_md5_password") != string::npos
)
311 conf
.lastfm_md5_password
= v
;
313 else if (line
.find("log_level") != string::npos
)
315 if (!v
.empty() && conf
.log_level
== llUndefined
)
316 conf
.log_level
= IntoLogLevel(v
);