📄 kceasyext.cpp
字号:
/*
This file is part of KCeasy (http://www.kceasy.com)
Copyright (C) 2002-2004 Markus Kern <mkern@kceasy.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.
*/
//---------------------------------------------------------------------------
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include "..\engine\Config.h"
#include "..\brand\kceasy.rc"
#include "netfw.h"
#include "exdll.h"
//---------------------------------------------------------------------------
using namespace KCeasyEngine;
#define WM_KCEASY_EXTERNAL_SHUTDOWN (WM_USER + 2)
//---------------------------------------------------------------------------
/* Usage:
* KCeasyExt::CopyINIValuesExcept <src_file> <dst_file> <comma sparated exclusion list>
* Possible return values in $R0:
* "ok"
* "error"
*/
NSISFunction(CopyINIValuesExcept)
{
EXDLL_INIT();
char SrcFile[1024];
char DstFile[1024];
char Exclude[1024];
if(popstring(SrcFile) != 0) {
setuservariable(INST_R0,"error");
return;
}
if(popstring(DstFile) != 0) {
setuservariable(INST_R0,"error");
return;
}
if(popstring(Exclude) != 0) {
setuservariable(INST_R0,"error");
return;
}
TFileConfig* SrcConf = new TFileConfig(SrcFile);
TFileConfig* DstConf = new TFileConfig(DstFile);
if(!SrcConf->Load(true) || !DstConf->Load(true)) {
delete SrcConf;
delete DstConf;
setuservariable(INST_R0,"error");
return;
}
list<string> Excludes = string_split(string(Exclude),",");
list<string> Sections = SrcConf->GetSections();
for(list<string>::iterator si = Sections.begin(); si != Sections.end(); ++si) {
list<string> Names = SrcConf->GetNames((*si));
for(list<string>::iterator ni = Names.begin(); ni != Names.end(); ++ni) {
// make key
string Key = (*si) + "/" + (*ni);
// key must not be in exclude list
list<string>::iterator ei;
for(ei = Excludes.begin(); ei != Excludes.end(); ++ei) {
if(Key == (*ei))
break;
}
if(ei != Excludes.end())
continue; // in exclude list
// value must already exists in destination file
if(!DstConf->ValueExists(Key))
continue;
// copy value
DstConf->SetValue(Key,SrcConf->GetValue(Key));
}
}
DstConf->Save();
delete SrcConf;
delete DstConf;
setuservariable(INST_R0,"ok");
}
/* Usage:
* KCeasyExt::IsKCeasyRunning
* Possible return values in $R0:
* "running"
* "not_running"
*/
NSISFunction(IsKCeasyRunning)
{
EXDLL_INIT();
HANDLE hGlobalMutex;
// is there an instance of KCeasy running?
if((hGlobalMutex = OpenMutex(SYNCHRONIZE,FALSE,BRAND_GLOBAL_MUTEX_NAME)) != NULL) {
// running
CloseHandle(hGlobalMutex);
setuservariable(INST_R0,"running");
return;
}
// not running
setuservariable(INST_R0,"not_running");
}
/* Usage:
* KCeasyExt::ShutdownKCeasy <timeout in seconds>
* Possible return values in $R0:
* "shutdown"
* "timeout"
* "error"
*/
NSISFunction(ShutdownKCeasy)
{
EXDLL_INIT();
int Timeout;
char buf[1024];
HWND hMsgTargetWnd;
HANDLE hGlobalMutex;
if(popstring(buf) != 0) {
setuservariable(INST_R0,"error");
return;
}
if((Timeout = atoi(buf)) == 0) {
setuservariable(INST_R0,"error");
return;
}
// send close message
if((hMsgTargetWnd = FindWindow("MessageTarget",BRAND_EXTERNAL_MSG_TARGET)) == NULL) {
setuservariable(INST_R0,"error");
return;
}
PostMessage(hMsgTargetWnd,WM_KCEASY_EXTERNAL_SHUTDOWN,0,0);
// wait for shutdown if possible
if((hGlobalMutex = OpenMutex(SYNCHRONIZE,FALSE,BRAND_GLOBAL_MUTEX_NAME)) != NULL) {
DWORD Ret = WaitForSingleObject(hGlobalMutex,Timeout*1000);
CloseHandle(hGlobalMutex);
if(Ret == WAIT_FAILED) {
setuservariable(INST_R0,"timeout");
return;
} else if(Ret == WAIT_TIMEOUT) {
setuservariable(INST_R0,"timeout");
return;
}
setuservariable(INST_R0,"shutdown");
return;
}
// or simply sleep for timeout period
Sleep(Timeout*1000);
setuservariable(INST_R0,"timeout");
}
/* Usage:
* KCeasyExt::CmdLineContains <token>
* Possible return values in $R0:
* "found"
* "not_found"
*/
NSISFunction(CmdLineContains)
{
EXDLL_INIT();
char token[1024];
char *cmdline;
if(popstring(token) != 0) {
setuservariable(INST_R0,"not_found");
return;
}
if((cmdline = getuservariable(INST_CMDLINE)) == NULL) {
setuservariable(INST_R0,"not_found");
return;
}
list<string> tokens = string_split(string(cmdline)," ");
for(list<string>::iterator itr=tokens.begin(); itr != tokens.end(); ++itr) {
if((*itr) == token) {
setuservariable(INST_R0,"found");
return;
}
}
setuservariable(INST_R0,"not_found");
}
/* Usage:
* KCeasyExt::AddPlugins <gift config path> <colon sparated list of plugins>
* Possible return values in $R0:
* "ok"
* "error"
* Makes sure passed plugins are in giftd.conf
*/
NSISFunction(AddPlugins)
{
EXDLL_INIT();
char File[1024];
char Networks[1024];
if(popstring(File) != 0) {
setuservariable(INST_R0,"error");
return;
}
if(popstring(Networks) != 0) {
setuservariable(INST_R0,"error");
return;
}
TFileConfig* GiftConf = new TFileConfig(File);
if(!GiftConf->Load(true)) {
delete GiftConf;
setuservariable(INST_R0,"error");
return;
}
list<string> NewList = string_split(string(Networks),":");
list<string> OldList = string_split(GiftConf->GetValue("main/plugins"),":");
// add all networks in NewList to OldList without duplicates
for(list<string>::iterator ni = NewList.begin(); ni != NewList.end(); ++ni) {
if(string_trim(*ni) == "")
continue;
list<string>::iterator oi = OldList.begin();
for(; oi != OldList.end(); ++oi)
if(*oi == *ni)
break;
if(oi == OldList.end())
OldList.push_back(*ni);
}
GiftConf->SetValue("main/plugins", string_join(OldList,":"));
GiftConf->Save();
delete GiftConf;
setuservariable(INST_R0,"ok");
}
/*
* Firewall helpers
*/
static BSTR CStrToBStr(const char* CStr)
{
int len;
BSTR BStr = NULL;
if((len = MultiByteToWideChar(CP_ACP,0,CStr,-1,NULL,0)) > 0) {
WCHAR* WStr;
if((WStr = (WCHAR*)malloc(len*sizeof(WCHAR))) != NULL) {
if(MultiByteToWideChar(CP_ACP,0,CStr,-1,WStr,len) > 0) {
BStr = SysAllocString(WStr);
}
free(WStr);
}
}
return BStr;
}
static INetFwAuthorizedApplications* GetFirewallApps()
{
INetFwAuthorizedApplications* Apps = NULL;
HRESULT hr;
// Create an instance of the firewall settings manager.
INetFwMgr* Manager;
hr = CoCreateInstance(__uuidof(NetFwMgr),NULL,CLSCTX_INPROC_SERVER,
__uuidof(INetFwMgr),(void**)&Manager);
if(SUCCEEDED(hr) && Manager) {
// Retrieve the local firewall policy.
INetFwPolicy* Policy;
hr = Manager->get_LocalPolicy(&Policy);
if(SUCCEEDED(hr) && Policy) {
// Retrieve the firewall profile currently in effect
INetFwProfile* Profile;
hr = Policy->get_CurrentProfile(&Profile);
if(SUCCEEDED(hr) && Profile) {
// Retrieve the authorized application collection
hr = Profile->get_AuthorizedApplications(&Apps);
if(FAILED(hr)) {
Apps = NULL;
}
Profile->Release();
}
Policy->Release();
}
Manager->Release();
}
return Apps;
}
/* Usage:
* KCeasyExt::AddFirwallException <executable image path> <name>
* Possible return values in $R0:
* "ok"
* "error"
* Adds executable file to Win XP SP2 firewall exception list under name
*/
NSISFunction(AddFirewallException)
{
EXDLL_INIT();
char Path[1024];
char Name[1024];
BSTR BPath, BName;
HRESULT hr;
bool Succeeded = false;
if(popstring(Path) != 0 || (BPath = CStrToBStr(Path)) == NULL) {
setuservariable(INST_R0,"error");
return;
}
if(popstring(Name) != 0 || (BName = CStrToBStr(Name)) == NULL) {
SysFreeString(BPath);
setuservariable(INST_R0,"error");
return;
}
// Get authorized application collection
INetFwAuthorizedApplications* Apps;
if((Apps = GetFirewallApps()) != NULL) {
// Create an instance of an authorized application
INetFwAuthorizedApplication* App;
hr = CoCreateInstance(__uuidof(NetFwAuthorizedApplication),NULL,CLSCTX_INPROC_SERVER,
__uuidof(INetFwAuthorizedApplication),(void**)&App);
if(SUCCEEDED(hr) && App) {
// Set the process image file name
if(SUCCEEDED(App->put_ProcessImageFileName(BPath)) &&
SUCCEEDED(App->put_Name(BName)) &&
SUCCEEDED(App->put_Enabled(true)))
{
// Add the application to the collection
hr = Apps->Add(App);
if(SUCCEEDED(hr))
Succeeded = true;
}
App->Release();
}
Apps->Release();
}
SysFreeString(BPath);
SysFreeString(BName);
setuservariable(INST_R0,Succeeded ? "ok" : "error");
}
/* Usage:
* KCeasyExt::RemoveFirwallException <executable image path>
* Possible return values in $R0:
* "ok"
* "error"
* Removes executable file from Win XP SP2 firewall exception list
*/
NSISFunction(RemoveFirewallException)
{
EXDLL_INIT();
char Path[1024];
BSTR BPath;
HRESULT hr;
bool Succeeded = false;
if(popstring(Path) != 0 || (BPath = CStrToBStr(Path)) == NULL) {
setuservariable(INST_R0,"error");
return;
}
// Get authorized application collection
INetFwAuthorizedApplications* Apps;
if((Apps = GetFirewallApps()) != NULL) {
// Remove image
hr = Apps->Remove(BPath);
if(SUCCEEDED(hr))
Succeeded = true;
Apps->Release();
}
SysFreeString(BPath);
setuservariable(INST_R0,Succeeded ? "ok" : "error");
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -