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 <openssl/evp.h>
30 #include "configuration.h"
33 extern ScrobbyConfig config
;
35 size_t write_data(char *buffer
, size_t size
, size_t nmemb
, std::string data
)
37 size_t result
= size
* nmemb
;
38 data
.append(buffer
, result
);
44 if (config
.dedicated_user
.empty() || getuid() != 0)
48 if (!(userinfo
= getpwnam(config
.dedicated_user
.c_str())))
50 std::cerr
<< "user " << config
.dedicated_user
<< " not found!\n";
53 if (setgid(userinfo
->pw_gid
) == -1)
55 std::cerr
<< "cannot set gid for user " << config
.dedicated_user
<< ": " << strerror(errno
) << std::endl
;
58 if (setuid(userinfo
->pw_uid
) == -1)
60 std::cerr
<< "cannot change to uid of user " << config
.dedicated_user
<< ": " << strerror(errno
) << std::endl
;
63 config
.user_home_folder
= userinfo
->pw_dir
? userinfo
->pw_dir
: "";
71 std::ofstream
f(config
.file_pid
.c_str(), std::ios_base::trunc
);
85 std::ofstream
f(config
.file_cache
.c_str(), std::ios::trunc
);
89 void GetCachedSongs(std::vector
<std::string
> &v
)
91 std::ifstream
f(config
.file_cache
.c_str());
104 void Cache(const std::string
&s
)
106 std::ofstream
f(config
.file_cache
.c_str(), std::ios::app
);
114 void Log(LogLevel ll
, const char *format
, ...)
116 if (config
.log_level
< ll
)
118 FILE *f
= fopen(config
.file_log
.c_str(), "a");
121 perror("Cannot open log file!\n");
124 fprintf(f
, "[%s] ", DateTime().c_str());
126 va_start(list
, format
);
127 vfprintf(f
, format
, list
);
133 void ignore_newlines(std::string
&s
)
135 for (size_t i
= s
.find("\n"); i
!= std::string::npos
; i
= s
.find("\n"))
139 std::string
md5sum(const std::string
&s
)
141 unsigned char md_value
[EVP_MAX_MD_SIZE
];
145 EVP_DigestInit(&mdctx
, EVP_md5());
146 EVP_DigestUpdate(&mdctx
, s
.c_str(), s
.length());
147 EVP_DigestFinal_ex(&mdctx
, md_value
, &md_len
);
148 EVP_MD_CTX_cleanup(&mdctx
);
150 char result
[md_len
*2+1];
151 for (unsigned i
= 0; i
< md_len
; i
++)
152 sprintf(&result
[i
*2], "%02x", md_value
[i
]);
153 result
[md_len
*2] = 0;
158 std::string
DateTime()
165 result
[strftime(result
, 31, "%Y/%m/%d %X", t
)] = 0;
169 int StrToInt(const std::string
&s
)
171 return atoi(s
.c_str());