|
|
|
@ -159,199 +159,6 @@ size_t writeDataFromCurl(void *buffer, size_t size, size_t nmemb, void *pcl)
|
|
|
|
|
return size*nmemb;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class CWebigNotificationThread : public NLMISC::IRunnable
|
|
|
|
|
{
|
|
|
|
|
private:
|
|
|
|
|
CURL *Curl;
|
|
|
|
|
bool _Running;
|
|
|
|
|
IThread *_Thread;
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
|
|
CWebigNotificationThread()
|
|
|
|
|
{
|
|
|
|
|
_Running = false;
|
|
|
|
|
_Thread = NULL;
|
|
|
|
|
curl_global_init(CURL_GLOBAL_ALL);
|
|
|
|
|
|
|
|
|
|
Curl = NULL;
|
|
|
|
|
//nlinfo("ctor CWebigNotificationThread");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void init()
|
|
|
|
|
{
|
|
|
|
|
if (Curl)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Curl = curl_easy_init();
|
|
|
|
|
if(!Curl) return;
|
|
|
|
|
curl_easy_setopt(Curl, CURLOPT_COOKIEFILE, "");
|
|
|
|
|
curl_easy_setopt(Curl, CURLOPT_NOPROGRESS, 1);
|
|
|
|
|
curl_easy_setopt(Curl, CURLOPT_USERAGENT, getUserAgent().c_str());
|
|
|
|
|
curl_easy_setopt(Curl, CURLOPT_FOLLOWLOCATION, 1);
|
|
|
|
|
curl_easy_setopt(Curl, CURLOPT_WRITEFUNCTION, writeDataFromCurl);
|
|
|
|
|
|
|
|
|
|
NLWEB::CCurlCertificates::useCertificates(Curl);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
~CWebigNotificationThread()
|
|
|
|
|
{
|
|
|
|
|
if(Curl)
|
|
|
|
|
{
|
|
|
|
|
curl_easy_cleanup(Curl);
|
|
|
|
|
Curl = NULL;
|
|
|
|
|
}
|
|
|
|
|
if (_Thread)
|
|
|
|
|
{
|
|
|
|
|
_Thread->terminate();
|
|
|
|
|
delete _Thread;
|
|
|
|
|
_Thread = NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void get(const std::string &url)
|
|
|
|
|
{
|
|
|
|
|
if(!Curl) return;
|
|
|
|
|
curlresult.clear();
|
|
|
|
|
//nlinfo("get '%s'", url.c_str());
|
|
|
|
|
curl_easy_setopt(Curl, CURLOPT_URL, url.c_str());
|
|
|
|
|
CURLcode res = curl_easy_perform(Curl);
|
|
|
|
|
long r;
|
|
|
|
|
curl_easy_getinfo(Curl, CURLINFO_RESPONSE_CODE, &r);
|
|
|
|
|
//nlwarning("result : '%s'", curlresult.c_str());
|
|
|
|
|
|
|
|
|
|
char *ch;
|
|
|
|
|
std::string contentType;
|
|
|
|
|
res = curl_easy_getinfo(Curl, CURLINFO_CONTENT_TYPE, &ch);
|
|
|
|
|
if (res == CURLE_OK && ch != NULL)
|
|
|
|
|
{
|
|
|
|
|
contentType = ch;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// "text/lua; charset=utf8"
|
|
|
|
|
if (contentType.find("text/lua") == 0)
|
|
|
|
|
{
|
|
|
|
|
std::string script;
|
|
|
|
|
script = "\nlocal __WEBIG_NOTIF__= true\n" + curlresult;
|
|
|
|
|
CInterfaceManager::getInstance()->queueLuaScript(script);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
nlwarning("Invalid content-type '%s', expected 'text/lua'", contentType.c_str());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string randomString()
|
|
|
|
|
{
|
|
|
|
|
std::string chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
|
|
|
|
std::string s;
|
|
|
|
|
for (int i = 0; i < 32; i++)
|
|
|
|
|
{
|
|
|
|
|
s += chars[uint(frand(float(chars.size())))];
|
|
|
|
|
}
|
|
|
|
|
return s;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void run()
|
|
|
|
|
{
|
|
|
|
|
if (ClientCfg.WebIgNotifInterval == 0)
|
|
|
|
|
{
|
|
|
|
|
_Running = false;
|
|
|
|
|
nlwarning("ClientCfg.WebIgNotifInterval == 0, notification thread not running");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string domain = ClientCfg.WebIgMainDomain;
|
|
|
|
|
uint32 ms = ClientCfg.WebIgNotifInterval*60*1000;
|
|
|
|
|
|
|
|
|
|
_Running = true;
|
|
|
|
|
// first time, we wait a small amount of time to be sure everything is initialized
|
|
|
|
|
nlSleep(30*1000);
|
|
|
|
|
uint c = 0;
|
|
|
|
|
while (_Running)
|
|
|
|
|
{
|
|
|
|
|
string url = domain + "/index.php?app=notif&format=lua&rnd=" + randomString();
|
|
|
|
|
addWebIGParams(url, true);
|
|
|
|
|
get(url);
|
|
|
|
|
|
|
|
|
|
sleepLoop(ms);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void sleepLoop(uint ms)
|
|
|
|
|
{
|
|
|
|
|
// use smaller sleep time so stopThread() will not block too long
|
|
|
|
|
// tick == 100ms
|
|
|
|
|
uint32 ticks = ms / 100;
|
|
|
|
|
while (_Running && ticks > 0) {
|
|
|
|
|
nlSleep(100);
|
|
|
|
|
ticks--;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void startThread()
|
|
|
|
|
{
|
|
|
|
|
// initialize curl outside thread
|
|
|
|
|
init();
|
|
|
|
|
|
|
|
|
|
if (!_Thread)
|
|
|
|
|
{
|
|
|
|
|
_Thread = IThread::create(this);
|
|
|
|
|
nlassert(_Thread != NULL);
|
|
|
|
|
_Thread->start();
|
|
|
|
|
nlwarning("WebIgNotification thread started");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
nlwarning("WebIgNotification thread already started");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void stopThread()
|
|
|
|
|
{
|
|
|
|
|
_Running = false;
|
|
|
|
|
if (_Thread)
|
|
|
|
|
{
|
|
|
|
|
_Thread->wait();
|
|
|
|
|
delete _Thread;
|
|
|
|
|
_Thread = NULL;
|
|
|
|
|
nlwarning("WebIgNotification thread stopped");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
nlwarning("WebIgNotification thread already stopped");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool isRunning() const
|
|
|
|
|
{
|
|
|
|
|
return _Running;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static CWebigNotificationThread webigThread;
|
|
|
|
|
void startWebIgNotificationThread()
|
|
|
|
|
{
|
|
|
|
|
if (!webigThread.isRunning())
|
|
|
|
|
{
|
|
|
|
|
webigThread.startThread();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void stopWebIgNotificationThread()
|
|
|
|
|
{
|
|
|
|
|
if (webigThread.isRunning())
|
|
|
|
|
{
|
|
|
|
|
webigThread.stopThread();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ***************************************************************************
|
|
|
|
|
// ***************************************************************************
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ***************************************************************************
|
|
|
|
|
NLMISC_REGISTER_OBJECT(CViewBase, CGroupHTMLAuth, std::string, "auth_html");
|
|
|
|
|
|
|
|
|
|