Exemplo n.º 1
0
bool CWRequest::IsServerRunning(string sSessionID, int iTimeoutMS, string* pErrorMessage)
{
    if(sSessionID == "")
    {
        if(pErrorMessage != NULL) *pErrorMessage = "Session ID is empty.";
        return false;
    }

    // read server pid
    string instance_filename = TempDir() + sSessionID + ".server";
    int fd = -1;
    int file_size = 0;
    if(!FileOpenExistingAndLock(instance_filename, iTimeoutMS, &fd, &file_size, pErrorMessage))
        return false;

    int server_pid = 0;
    if(!FileReadInt(fd, &server_pid, pErrorMessage))
    {
        FileUnlockAndClose(fd, NULL);
        return false;
    }
    FileUnlockAndClose(fd, NULL);

    // check if server is running
    if(!IsProcessRunning(server_pid))
    {
        if(pErrorMessage != NULL) *pErrorMessage = "Server is not running.";

        // if server is not running, delete server pid file and server queue
        FileDelete(instance_filename, NULL);

        string queue_filename = TempDir() + sSessionID + ".queue";
        if(FileExists(queue_filename))
            FileDelete(queue_filename, NULL);

        return false;
    }

    return true;
}
Exemplo n.º 2
0
bool SendMail(string sFrom, string sTo, string sSubject, string sBody, bool bIsHTML, string sSendMailPath, string* pErrorMessage)
{
    string message = "";

    if(sFrom != "") message.append("From: " + sFrom + "\r\n");
    if(sTo != "") message.append("To: " + sTo + "\r\n");
    if(sSubject != "") message.append("Subject: " + sSubject + "\r\n");
    if(bIsHTML)
        message.append("Content-Type: text/html\r\n");
    else
        message.append("Content-Type: text/plain\r\n");

    if(sBody != "") message.append(sBody);

    string temp_file_name = TempDir() + CreateID(sFrom + sTo) + ".tmp";
    if(!FileSaveString(temp_file_name, message, 0, pErrorMessage))
        return false;

	string sendmail_path = "";
	if(sSendMailPath == "")
		sendmail_path = DEFAULT_SENDMAIL_PATH;
	else
		sendmail_path = sSendMailPath;

    string arguments = "";
    arguments.append("-t < ");
    arguments.append("\"" + temp_file_name + "\"");
    if(!Execute(sendmail_path, arguments, true, pErrorMessage))
    {
        FileDelete(temp_file_name, NULL);
        return false;
    }

    FileDelete(temp_file_name, NULL);

    return true;
}
Exemplo n.º 3
0
void CDownloadQueue::LoadMetFiles(const CPath& path)
{
	AddLogLineNS(CFormat(_("Loading temp files from %s.")) % path.GetPrintable());
	
	std::vector<CPath> files;

	// Locate part-files to be loaded
	CDirIterator TempDir(path);
	CPath fileName = TempDir.GetFirstFile(CDirIterator::File, wxT("*.part.met"));
	while (fileName.IsOk()) {
		files.push_back(path.JoinPaths(fileName));

		fileName = TempDir.GetNextFile();
	}

	// Loading in order makes it easier to figure which
	// file is broken in case of crashes, or the like.
	std::sort(files.begin(), files.end());

	// Load part-files	
	for ( size_t i = 0; i < files.size(); i++ ) {
		AddLogLineNS(CFormat(_("Loading PartFile %u of %u")) % (i + 1) % files.size());
		fileName = files[i].GetFullName();
		CPartFile *toadd = new CPartFile();
		bool result = toadd->LoadPartFile(path, fileName) != 0;
		if (!result) {
			// Try from backup
			result = toadd->LoadPartFile(path, fileName, true) != 0;
		}
		if (result && !IsFileExisting(toadd->GetFileHash())) {
			{
				wxMutexLocker lock(m_mutex);
				m_filelist.push_back(toadd);
			}
			NotifyObservers(EventType(EventType::INSERTED, toadd));
			Notify_DownloadCtrlAddFile(toadd);
		} else {
			wxString msg;
			if (result) {
				msg << CFormat(wxT("WARNING: Duplicate partfile with hash '%s' found, skipping: %s"))
					% toadd->GetFileHash().Encode() % fileName;
			} else {
				// If result is false, then reading of both the primary and the backup .met failed
				AddLogLineN(_("ERROR: Failed to load backup file. Search http://forum.amule.org for .part.met recovery solutions."));
				msg << CFormat(wxT("ERROR: Failed to load PartFile '%s'")) % fileName;
			}
			AddLogLineCS(msg);

			// Delete the partfile object in the end.
			delete toadd;
		}
	}
	AddLogLineNS(_("All PartFiles Loaded."));
	
	if ( GetFileCount() == 0 ) {
		AddLogLineN(_("No part files found"));
	} else {
		AddLogLineN(CFormat(wxPLURAL("Found %u part file", "Found %u part files", GetFileCount())) % GetFileCount());

		DoSortByPriority();
		CheckDiskspace( path );
		Notify_ShowUpdateCatTabTitles();
	}
}
Exemplo n.º 4
0
bool CWRequest::ReceiveFromBridge(string sSessionID, string* pRequestID, bool* pSetResponseCookie, bool* pSecure, bool* pKeepAlive, bool bLongPolling, int iTimeoutMS, CWCallbackFunction pCallback, const void* pCallbackArg, int iCallbackIntervalMS, string* pErrorMessage)
{
    if(sSessionID == "")
    {
        if(pErrorMessage != NULL) *pErrorMessage = "session_id is empty.";
        return false;
    }

    Clear();

    // ---
    // read request id from queue
    // ---
    string queue_filename = "";
    if(bLongPolling)
    {
        queue_filename = TempDir() + sSessionID + ".queue_lp";

        // if no long polling request is waiting, return false
        int queue_size = 0;
        if(!FileExists(queue_filename, &queue_size))
            return false;

        if(queue_size <= 0)
            return false;
    }
    else
        queue_filename = TempDir() + sSessionID + ".queue";

    char* buffer = NULL;
    int buffer_size = 0;
    if(!PopData(queue_filename, (void**)&buffer, &buffer_size, iTimeoutMS, pCallback, pCallbackArg, iCallbackIntervalMS, pErrorMessage))
        return false;

    string request_id(buffer, buffer_size);
    free(buffer);

    // ---
    // read request from file
    // ---
    string request_filename = TempDir() + request_id + ".request";

    int fd = -1;
    int file_size = 0;
    if(!FileOpenExistingAndLock(request_filename, REQUEST_RECEIVE_TIMEOUT, &fd, &file_size, pErrorMessage))
        return false;

    // query
    if(!FileReadStringList(fd, Query, pErrorMessage))
    {
        FileUnlockAndClose(fd, NULL);
        return false;
    }

    // post
    if(!FileReadStringList(fd, Post, pErrorMessage))
    {
        FileUnlockAndClose(fd, NULL);
        return false;
    }

    // cookie
    if(!FileReadStringList(fd, Cookie, pErrorMessage))
    {
        FileUnlockAndClose(fd, NULL);
        return false;
    }

    // env
    if(!FileReadStringList(fd, Env, pErrorMessage))
    {
        FileUnlockAndClose(fd, NULL);
        return false;
    }

    // unlock file
    FileUnlockAndClose(fd, NULL);

    // delete file
    FileDelete(request_filename, NULL);

    if(Cookie->GetValue("session_id") != sSessionID)
    {
        Cookie->SetValue("session_id", sSessionID);
        *pSetResponseCookie = true;
    }
    else
        *pSetResponseCookie = false;

    if(Env->GetValue("HTTPS") == "on")
        *pSecure = true;
    else
        *pSecure = false;

    if(pKeepAlive != NULL) *pKeepAlive = GetValue("keep_alive") == "1" || GetValue("keep_alive") == "true";

    *pRequestID = request_id;

    return true;
}
Exemplo n.º 5
0
bool CWRequest::SendToServer(string sServerPath, string* pSessionID, string sRequestID, bool bLongPolling, bool bKeepAlive, string* pErrorMessage)
{
    string session_id = *pSessionID;

    if(session_id == "" || !IsServerRunning(session_id, INSTANCE_CHECK_TIMEOUT, pErrorMessage))
    {
        if(bLongPolling || bKeepAlive)
            return false;

        if(!StartServer(sServerPath, INSTANCE_START_TIMEOUT, &session_id, pErrorMessage))
            return false;
    }

    // ---
    // write request to file
    // ---
    string request_filename = TempDir() + sRequestID + ".request";

    int fd = -1;
    if(!FileCreateNonExistingAndLock(request_filename, REQUEST_WRITE_TIMEOUT, &fd, pErrorMessage))
        return false;

    // query
    if(!FileWriteStringList(fd, Query, pErrorMessage))
    {
        FileUnlockAndClose(fd, NULL);
        return false;
    }

    // post
    if(!FileWriteStringList(fd, Post, pErrorMessage))
    {
        FileUnlockAndClose(fd, NULL);
        return false;
    }

    // cookie
    if(!FileWriteStringList(fd, Cookie, pErrorMessage))
    {
        FileUnlockAndClose(fd, NULL);
        return false;
    }

    // env
    if(!FileWriteStringList(fd, Env, pErrorMessage))
    {
        FileUnlockAndClose(fd, NULL);
        return false;
    }

    FileUnlockAndClose(fd, NULL);

    // request type
    string queue_filename = "";
    if(bLongPolling)
        queue_filename = TempDir() + session_id + ".queue_lp";
    else
        queue_filename = TempDir() + session_id + ".queue";

    // ---
    // register request in queue
    // ---
    if(!PushData(queue_filename, (char*)sRequestID.c_str(), sRequestID.size(), REQUEST_SEND_TIMEOUT, pErrorMessage))
        return false;

    *pSessionID = session_id;

    Clear();

    return true;
}
Exemplo n.º 6
0
bool CWRequest::ReceiveFromWeb(string* pSessionID, string* pRequestID, bool* pThisIsCookieCheck, bool* pCookieCheckOk, bool* pLongPolling, bool* pKeepAlive, string* pErrorMessage)
{
    Clear();

    CGI_varlist* query = 0;
    CGI_varlist* post = 0;
    CGI_varlist* cookie = 0;

    string file_upload_template = TempDir() + "cgi-upload-XXXXXX";

    query = CGI_get_query(NULL);
    post = CGI_get_post(NULL, file_upload_template.c_str());
    cookie = CGI_get_cookie(NULL);

    if(query != 0)
    {
        const char *name = 0;
        CGI_value  *value = 0;
        for(name = CGI_first_name(query); name != 0; name = CGI_next_name(query))
        {
            value = CGI_lookup_all(query, 0);
            for(int i = 0; value[i] != 0; i++)
            {
                string name_value = "";
                name_value = name_value + name;
                name_value = name_value + "=";
                name_value = name_value + value[i];
                Query->Add(name_value);
            }
        }
    }

    if(post != 0)
    {
        const char *name = 0;
        CGI_value  *value = 0;
        for(name = CGI_first_name(post); name != 0; name = CGI_next_name(post))
        {
            value = CGI_lookup_all(post, 0);
            for(int i = 0; value[i] != 0; i++)
            {
                string name_value = "";
                name_value = name_value + name;
                name_value = name_value + "=";
                name_value = name_value + value[i];
                Post->Add(name_value);
            }
        }
    }

    if(cookie != 0)
    {
        const char *name = 0;
        CGI_value  *value = 0;
        for(name = CGI_first_name(cookie); name != 0; name = CGI_next_name(cookie))
        {
            value = CGI_lookup_all(cookie, 0);
            for(int i = 0; value[i] != 0; i++)
            {
                string name_value = "";
                name_value = name_value + name;
                name_value = name_value + "=";
                name_value = name_value + value[i];
                Cookie->Add(name_value);
            }
        }
    }

    CGI_free_varlist(cookie);
    CGI_free_varlist(post);
    CGI_free_varlist(query);

    // environment variables
    Env->SetValue("SERVER_NAME", GetEnv("SERVER_NAME"));
    Env->SetValue("SERVER_PORT", GetEnv("SERVER_PORT"));
    Env->SetValue("SCRIPT_NAME", GetEnv("SCRIPT_NAME"));
    Env->SetValue("REQUEST_METHOD", GetEnv("REQUEST_METHOD"));
    Env->SetValue("DOCUMENT_ROOT", GetEnv("DOCUMENT_ROOT"));
    Env->SetValue("REMOTE_ADDR", GetEnv("REMOTE_ADDR"));
    Env->SetValue("HTTP_USER_AGENT", GetEnv("HTTP_USER_AGENT"));
    Env->SetValue("HTTP_REFERER", GetEnv("HTTP_REFERER"));
    Env->SetValue("HTTP_COOKIE", GetEnv("HTTP_COOKIE"));
    Env->SetValue("CONTENT_TYPE", GetEnv("CONTENT_TYPE"));
    Env->SetValue("HTTPS", GetEnv("HTTPS"));

    // read and return session_id
    string session_id = Cookie->GetValue("session_id");
    string request_id = CreateRequestID();
    bool this_is_cookie_check = GetValue("cookie_check") == "1" || GetValue("cookie_check") == "true";
    DeleteNameValue("cookie_check");
    bool cookie_check_ok = Cookie->GetValue("cookie_check_data") == "123";
    bool long_polling = GetValue("long_polling") == "1" || GetValue("long_polling") == "true";
    bool keep_alive = GetValue("keep_alive") == "1" || GetValue("keep_alive") == "true";

    if(pSessionID != NULL) *pSessionID = session_id;
    if(pRequestID != NULL) *pRequestID = request_id;
    if(pThisIsCookieCheck != NULL) *pThisIsCookieCheck = this_is_cookie_check;
    if(pCookieCheckOk != NULL) *pCookieCheckOk = cookie_check_ok;
    if(pLongPolling != NULL) *pLongPolling = long_polling;
    if(pKeepAlive != NULL) *pKeepAlive = keep_alive;

    return true;
}
Exemplo n.º 7
0
int		Panel::ProcessKey(int Key, unsigned int ControlState) {
	switch (Key) {
		case VK_F3:
			if (ControlState == PKF_SHIFT) {
				return	true;
			}
			break;
		case VK_F4:
			switch (ControlState) {
				case 0:
					DlgShutdown();
					return true;
				case PKF_SHIFT:
					return	true;
			}
			break;
		case VK_F6:
			switch (ControlState) {
				case 0:
					if (DlgConnection()) {
						psi.Control(this, FCTL_UPDATEPANEL, 0, nullptr);
						psi.Control(this, FCTL_REDRAWPANEL, 0, nullptr);
					}
					return	true;
				case PKF_SHIFT:
					try {
						m_conn->Open(nullptr);
					} catch (WinError &e) {
						farebox_code(e.code(), e.where().c_str());
					}
					psi.Control(this, FCTL_UPDATEPANEL, 0, nullptr);
					psi.Control(this, FCTL_REDRAWPANEL, 0, nullptr);
					return	true;
			}
			break;
		case VK_F7:
			switch (ControlState) {
				case 0:
					AutoUTF cmd = L"mstsc.exe";
					if (!Empty(m_conn->host())) {
						cmd += L" /v:";
						cmd += m_conn->host();
					}
					ExecCMD(cmd);
					return true;
			}
			break;
	}
	if ((ControlState == 0 && (Key == VK_F3 || Key == VK_F5 || Key == VK_F8)) ||
		(ControlState == PKF_SHIFT && (Key == VK_F7 || Key == VK_F8))) {
		FarPnl pInfo(this, FCTL_GETPANELINFO);
		WinTS::iterator m_cur;
		if (pInfo.ItemsNumber() && pInfo.CurrentItem() &&
			(m_cur = std::find(m_ts.begin(), m_ts.end(), pInfo[pInfo.CurrentItem()].FindData.nFileSize)) != m_ts.end()) {
			if (ControlState == 0 && Key == VK_F3) {
				AutoUTF	tmp(TempFile(TempDir()));
				HANDLE	hfile = ::CreateFileW(tmp.c_str(), GENERIC_WRITE, 0, nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr);
				if (hfile != INVALID_HANDLE_VALUE) {
					FileWrite(hfile, Info(m_cur));
					::CloseHandle(hfile);
					psi.Viewer(tmp.c_str(), nullptr, 0, 0, -1, -1,
							   VF_DELETEONLYFILEONCLOSE | VF_ENABLE_F6 | VF_DISABLEHISTORY |
							   VF_NONMODAL | VF_IMMEDIATERETURN, CP_AUTODETECT);
				}
				return	true;
			} else if (ControlState == 0 && Key == VK_F5) {
				DlgMessage(m_cur->id());
			} else 	if (ControlState == PKF_SHIFT && Key == VK_F7) {
				WinTSession::ConnectLocal(m_cur->id());
			} else 	if (ControlState == 0 && Key == VK_F8) {
				if (farquestion(GetMsg(txtAreYouSure), GetMsg(txtDisconnectSession)))
					WinTSession::Disconnect(m_cur->id(), m_conn->host());
			} else if (ControlState == PKF_SHIFT && Key == VK_F8) {
				if (farquestion(GetMsg(txtAreYouSure), GetMsg(txtLogoffSession)))
					WinTSession::LogOff(m_cur->id(), m_conn->host());
			}
			psi.Control(this, FCTL_UPDATEPANEL, 0, nullptr);
			psi.Control(this, FCTL_REDRAWPANEL, 0, nullptr);
			return	true;
		}
	}
	return	false;
}