1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
|
/*
* Copyright © 2004-2008 Jens Oknelid, paskharen@gmail.com
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* In addition, as a special exception, compiling, linking, and/or
* using OpenSSL with this program is allowed.
*/
#include "WulforUtil.hh"
#include <glib/gi18n.h>
#include <glib/gstdio.h>
#include <client/ClientManager.h>
#include <client/Util.h>
#include <iostream>
#include <arpa/inet.h>
#include <ifaddrs.h>
#include <fcntl.h>
using namespace std;
std::vector<std::string> WulforUtil::charsets;
const std::string WulforUtil::magnetSignature = "magnet:?xt=urn:tree:tiger:";
vector<int> WulforUtil::splitString(const string &str, const string &delimiter)
{
string::size_type loc, len, pos = 0;
vector<int> array;
if (!str.empty() && !delimiter.empty())
{
while ((loc = str.find(delimiter, pos)) != string::npos)
{
len = loc - pos;
array.push_back(Util::toInt(str.substr(pos, len)));
pos = loc + delimiter.size();
}
len = str.size() - pos;
array.push_back(Util::toInt(str.substr(pos, len)));
}
return array;
}
string WulforUtil::linuxSeparator(const string &ps)
{
string str = ps;
for (string::iterator it = str.begin(); it != str.end(); ++it)
if ((*it) == '\\')
(*it) = '/';
return str;
}
string WulforUtil::windowsSeparator(const string &ps)
{
string str = ps;
for (string::iterator it = str.begin(); it != str.end(); ++it)
if ((*it) == '/')
(*it) = '\\';
return str;
}
vector<string> WulforUtil::getLocalIPs()
{
vector<string> addresses;
struct ifaddrs *ifap;
if (getifaddrs(&ifap) == 0)
{
struct ifaddrs *interface = ifap;
while (interface)
{
struct sockaddr *ifa_addr = interface->ifa_addr;
if (ifa_addr && ifa_addr->sa_family == AF_INET)
{
struct sockaddr_in *addr_in = (struct sockaddr_in *)ifa_addr;
string address = inet_ntoa(addr_in->sin_addr);
if (address != "127.0.0.1")
addresses.push_back(address);
}
interface = interface->ifa_next;
}
freeifaddrs(ifap);
}
return addresses;
}
string WulforUtil::getNicks(const string &cid)
{
return getNicks(CID(cid));
}
string WulforUtil::getNicks(const CID& cid)
{
return Util::toString(ClientManager::getInstance()->getNicks(cid));
}
string WulforUtil::getNicks(const User::Ptr& user)
{
return getNicks(user->getCID());
}
string WulforUtil::getHubNames(const string &cid)
{
return getHubNames(CID(cid));
}
string WulforUtil::getHubNames(const CID& cid)
{
StringList hubs = ClientManager::getInstance()->getHubNames(cid);
if (hubs.empty())
return _("Offline");
else
return Util::toString(hubs);
}
string WulforUtil::getHubNames(const User::Ptr& user)
{
return getHubNames(user->getCID());
}
StringList WulforUtil::getHubAddress(const CID& cid)
{
return ClientManager::getInstance()->getHubs(cid);
}
StringList WulforUtil::getHubAddress(const User::Ptr& user)
{
return getHubAddress(user->getCID());
}
string WulforUtil::getTextFromMenu(GtkMenuItem *item)
{
string text;
GtkWidget *child = gtk_bin_get_child(GTK_BIN(item));
if (child && GTK_IS_LABEL(child))
text = gtk_label_get_text(GTK_LABEL(child));
return text;
}
vector<string>& WulforUtil::getCharsets()
{
if (charsets.size() == 0)
{
charsets.push_back(_("System default"));
charsets.push_back(_("UTF-8 (Unicode)"));
charsets.push_back(_("CP1252 (Western Europe)"));
charsets.push_back(_("ISO-8859-2 (Central Europe)"));
charsets.push_back(_("ISO-8859-7 (Greek)"));
charsets.push_back(_("ISO-8859-8 (Hebrew)"));
charsets.push_back(_("ISO-8859-9 (Turkish)"));
charsets.push_back(_("ISO-2022-JP (Japanese)"));
charsets.push_back(_("SJIS (Japanese)"));
charsets.push_back(_("CP949 (Korean)"));
charsets.push_back(_("KOI8-R (Cyrillic)"));
charsets.push_back(_("CP1251 (Cyrillic)"));
charsets.push_back(_("CP1256 (Arabic)"));
charsets.push_back(_("CP1257 (Baltic)"));
charsets.push_back(_("GB18030 (Chinese)"));
charsets.push_back(_("TIS-620 (Thai)"));
}
return charsets;
}
void WulforUtil::openURI(const std::string &uri)
{
GError* error = NULL;
gchar *argv[3];
#if defined(__APPLE__)
argv[0] = (gchar *)"open";
#elif defined(_WIN32)
argv[0] = (gchar *)"start";
#else
argv[0] = (gchar *)"xdg-open";
#endif
argv[1] = (gchar *)Text::fromUtf8(uri).c_str();
argv[2] = NULL;
g_spawn_async(NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL, NULL, &error);
if (error != NULL)
{
cerr << "Failed to open URI: " << error->message << endl;
g_error_free(error);
}
}
string WulforUtil::makeMagnet(const string &name, const int64_t size, const string &tth)
{
if (name.empty() || tth.empty())
return string();
// other clients can return paths with different separators, so we should catch both cases
string::size_type i = name.find_last_of("/\\");
string path = (i != string::npos) ? name.substr(i + 1) : name;
return magnetSignature + tth + "&xl=" + Util::toString(size) + "&dn=" + Util::encodeURI(path);
}
bool WulforUtil::splitMagnet(const string &magnet, string &name, int64_t &size, string &tth)
{
name = _("Unknown");
size = 0;
tth = _("Unknown");
if (!isMagnet(magnet.c_str()) || magnet.size() <= magnetSignature.length())
return FALSE;
string::size_type nextpos = 0;
for (string::size_type pos = magnetSignature.length(); pos < magnet.size(); pos = nextpos + 1)
{
nextpos = magnet.find('&', pos);
if (nextpos == string::npos)
nextpos = magnet.size();
if (pos == magnetSignature.length())
tth = magnet.substr(magnetSignature.length(), nextpos - magnetSignature.length());
else if (magnet.compare(pos, 3, "xl=") == 0)
size = Util::toInt64(magnet.substr(pos + 3, nextpos - pos - 3));
else if (magnet.compare(pos, 3, "dn=") == 0)
name = Util::encodeURI(magnet.substr(pos + 3, nextpos - pos - 3), TRUE);
}
return TRUE;
}
bool WulforUtil::isMagnet(const string &text)
{
return g_ascii_strncasecmp(text.c_str(), magnetSignature.c_str(), magnetSignature.length()) == 0;
}
bool WulforUtil::isLink(const string &text)
{
return g_ascii_strncasecmp(text.c_str(), "http://", 7) == 0 ||
g_ascii_strncasecmp(text.c_str(), "https://", 8) == 0 ||
g_ascii_strncasecmp(text.c_str(), "www.", 4) == 0 ||
g_ascii_strncasecmp(text.c_str(), "ftp://", 6) == 0 ||
g_ascii_strncasecmp(text.c_str(), "sftp://", 7) == 0 ||
g_ascii_strncasecmp(text.c_str(), "irc://", 6) == 0 ||
g_ascii_strncasecmp(text.c_str(), "ircs://", 7) == 0 ||
g_ascii_strncasecmp(text.c_str(), "im:", 3) == 0 ||
g_ascii_strncasecmp(text.c_str(), "mailto:", 7) == 0 ||
g_ascii_strncasecmp(text.c_str(), "news:", 5) == 0;
}
bool WulforUtil::isHubURL(const string &text)
{
return g_ascii_strncasecmp(text.c_str(), "dchub://", 8) == 0 ||
g_ascii_strncasecmp(text.c_str(), "adc://", 6) == 0 ||
g_ascii_strncasecmp(text.c_str(), "adcs://", 7) == 0;
}
bool WulforUtil::profileIsLocked()
{
static bool profileIsLocked = false;
if (profileIsLocked)
return TRUE;
// We can't use Util::getConfigPath() since the core has not been started yet.
// Also, Util::getConfigPath() is utf8 and we need system encoding for g_open().
char *home = getenv("HOME");
string configPath = home ? string(home) + "/.dc++/" : "/tmp/";
string profileLockingFile = configPath + "profile.lck";
int flags = O_WRONLY | O_CREAT;
int mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
int fd = g_open(profileLockingFile.c_str(), flags, mode);
if (fd != -1) // read error
{
struct flock lock;
lock.l_start = 0;
lock.l_len = 0;
lock.l_type = F_WRLCK;
lock.l_whence = SEEK_SET;
struct flock testlock = lock;
if (fcntl(fd, F_GETLK, &testlock) != -1) // Locking not supported
{
if (fcntl(fd, F_SETLK, &lock) == -1)
profileIsLocked = true;
}
}
return profileIsLocked;
}
|