adjust code style of below patch to the rest of the code
[scrobby.git] / src / configuration.cpp
blob0c830e9ac6e5162486a8f48d2f3c693ac2e24dcc
1 /***************************************************************************
2 * Copyright (C) 2008 by Andrzej Rybczak *
3 * electricityispower@gmail.com *
4 * *
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. *
9 * *
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 * *
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 ***************************************************************************/
21 #include <cstdlib>
22 #include <cstring>
23 #include <iostream>
24 #include <fstream>
26 #ifdef __linux__
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <unistd.h>
30 #include <errno.h>
31 #elif defined(__FreeBSD__)
32 #include <fcntl.h>
33 #include <kvm.h>
34 #include <paths.h>
35 #include <sys/param.h>
36 #include <sys/sysctl.h>
37 #include <sys/user.h>
38 #endif
40 #ifdef HAVE_CONFIG_H
41 # include <config.h>
42 #endif
44 #include "configuration.h"
45 #include "misc.h"
47 using std::string;
49 namespace {
50 std::string GetLineValue(const string &line, char a = '"', char b = '"')
52 int i = 0;
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)
58 if (begin < 0)
59 begin = i+1;
60 else
61 end = i;
64 if (begin >= 0 && end >= 0)
65 return line.substr(begin, end-begin);
66 else
67 return "";
70 void HomeFolder(const ScrobbyConfig &conf, string &s)
72 if (s[0] == '~')
73 s.replace(0, 1, conf.user_home_folder);
76 LogLevel IntoLogLevel(const string &value)
78 LogLevel result = llInfo;
79 if (value == "none")
81 result = llNone;
83 else if (value == "info")
85 result = llInfo;
87 else if (value == "verbose")
89 result = llVerbose;
91 return result;
95 void ParseArgv(ScrobbyConfig &conf, int argc, char **argv)
97 for (int i = 1; i < argc; i++)
99 if (strcmp(argv[i], "--help") == 0)
101 std::cout
102 << "usage:\n"
103 << "scrobby [options] <conf file>\n"
104 << "scrobby [options] (search for ~/.scrobbyconf, then /etc/scrobby.conf)\n\n"
105 << "options:\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"
112 exit(0);
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";
129 exit(0);
131 else
133 conf.file_config = argv[i];
138 bool CheckFiles(ScrobbyConfig &conf)
140 std::ofstream f;
141 std::ifstream g;
143 g.open(conf.file_pid.c_str());
144 if (g.is_open())
146 string strpid;
147 getline(g, strpid);
148 g.close();
149 pid_t pid = StrToInt(strpid);
150 if (pid < 1)
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";
157 else
159 std::cerr << "couldn't remove pid file.\n";
160 return false;
163 else
165 # ifdef __linux__
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];
173 kvm_t *hkvm;
174 int cnt = 0;
175 struct kinfo_proc *proc;
177 hkvm = kvm_openfiles(NULL, _PATH_DEVNULL, NULL, O_RDONLY, kvm_errbuf);
178 if (hkvm)
180 proc = kvm_getprocs(hkvm, KERN_PROC_PID, pid, &cnt);
181 kvm_close(hkvm);
183 else
185 std::cerr << "kvm_openfiles failed with message:" << std::endl << kvm_errbuf << std::endl;
187 if (cnt >= 1)
189 # endif
190 std::cerr << "scrobby is already running with PID " << pid << "!\n";
191 return false;
196 f.open(conf.file_pid.c_str(), std::ios_base::app);
197 if (!f.is_open())
199 std::cerr << "Cannot create/open pid file: " << conf.file_pid << std::endl;
200 return false;
202 f.close();
204 f.open(conf.file_log.c_str(), std::ios_base::app);
205 if (!f.is_open())
207 std::cerr << "Cannot create/open log file: " << conf.file_log << std::endl;
208 return false;
210 f.close();
212 f.open(conf.file_cache.c_str(), std::ios_base::app);
213 if (!f.is_open())
215 std::cerr << "Cannot create/open cache file: " << conf.file_cache << std::endl;
216 return false;
218 f.close();
220 return true;
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)
241 string line, v;
242 std::ifstream f(file.c_str());
244 if (!f.is_open())
245 return false;
247 while (!f.eof())
249 getline(f, line);
250 if (!line.empty() && line[0] != '#')
252 v = GetLineValue(line);
254 if (line.find("dedicated_user") != string::npos)
256 if (!line.empty())
257 conf.dedicated_user = v;
259 else if (line.find("mpd_host") != string::npos)
261 if (!line.empty())
262 conf.mpd_host = v;
264 else if (line.find("mpd_port") != string::npos)
266 if (!v.empty())
267 conf.mpd_port = StrToInt(v);
269 else if (line.find("mpd_timeout") != string::npos)
271 if (!v.empty())
272 conf.mpd_timeout = StrToInt(v);
274 else if (line.find("log_file") != string::npos)
276 if (!v.empty())
278 HomeFolder(conf, v);
279 conf.file_log = v;
282 else if (line.find("pid_file") != string::npos)
284 if (!v.empty())
286 HomeFolder(conf, v);
287 conf.file_pid = v;
290 else if (line.find("cache_file") != string::npos)
292 if (!v.empty())
294 HomeFolder(conf, v);
295 conf.file_cache = v;
298 else if (line.find("lastfm_user") != string::npos)
300 if (!v.empty())
301 conf.lastfm_user = v;
303 else if (line.find("lastfm_password") != string::npos)
305 if (!v.empty())
306 conf.lastfm_password = v;
308 else if (line.find("lastfm_md5_password") != string::npos)
310 if (!v.empty())
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);
320 f.close();
321 return true;