Browse Source

Macro list and more useless stuffs

master
boisso 7 years ago
parent
commit
e4f7f9a532
  1. 26
      k-meleon/Plugins.cpp
  2. 2
      k-meleon/kmeleon_plugin.h
  3. 162
      k-meleon/macros2/functions.h
  4. 60
      k-meleon/macros2/macros.cpp
  5. 48
      k-meleon/macros2/object.h
  6. 33
      k-meleon/macros2/parser.h

26
k-meleon/Plugins.cpp

@ -1920,6 +1920,26 @@ int SetCmdIcon(const char* name, const char* icon, UINT w, UINT h, const char* h
return theApp.skin.mImages->AddIcon(A2CT(icon), A2CT(hot), A2CT(cold), id, w, h);
}
#include "nsIPermissionManager.h"
bool AddPermission(const char* url, const char* type, const char* perm, bool sessionOnly)
{
nsCOMPtr<nsIPermissionManager> man = do_GetService(NS_PERMISSIONMANAGER_CONTRACTID);
if (!man) return false;
uint32_t permission = 0;
if (strcmp(perm, "allow") == 0)
permission = nsIPermissionManager::ALLOW_ACTION;
else if (strcmp(perm, "deny") == 0)
permission = nsIPermissionManager::DENY_ACTION;
else if (strcmp(perm, "prompt") == 0)
permission = nsIPermissionManager::PROMPT_ACTION;
else
return false;
nsCOMPtr<nsIURI> uri;
NewURI(getter_AddRefs(uri), nsDependentCString(url));
return NS_SUCCEEDED(man->Add(uri, type, permission, sessionOnly?nsIPermissionManager::EXPIRE_SESSION:nsIPermissionManager::EXPIRE_NEVER, 0));
}
kmeleonFunctions kmelFuncsUTF8 = {
SendMessage,
GetCommandIDs,
@ -1991,7 +2011,8 @@ kmeleonFunctions kmelFuncsUTF8 = {
GotoHistoryIndex,
RemoveButton,
AddButton,
SetCmdIcon
SetCmdIcon,
AddPermission
};
kmeleonFunctions kmelFuncs = {
@ -2065,7 +2086,8 @@ kmeleonFunctions kmelFuncs = {
GotoHistoryIndex,
RemoveButton,
AddButton,
SetCmdIcon
SetCmdIcon,
AddPermission
};
BOOL CPlugins::TestLoad(LPCTSTR file, const char *description)

2
k-meleon/kmeleon_plugin.h

@ -379,7 +379,7 @@ typedef struct {
bool (*RemoveButton)(const char* name, const char* command);
bool (*AddButton)(const char* name, const char* command, const char* menu);
int (*SetCmdIcon)(const char* name, const char* icon, UINT w, UINT h, const char* hot, const char* cold);
bool (*AddPermission)(const char* url, const char* type, const char* perm, bool sessionOnly);
} kmeleonFunctions;
/*

162
k-meleon/macros2/functions.h

@ -18,6 +18,7 @@
*/
#include <map>
#include <stdio.h>
#include "../Utils.h"
///////////////////////////
@ -25,6 +26,7 @@
#define WRONGARGS 0
#define WRONGTYPE 1
#define NEEDTRUST(data) if (!checkTrust(data)) return Value();
static void DoError(const char* msg, Statement* stat)
{
@ -61,6 +63,25 @@
parseError(WRONGARGS, name, "", n, data->nparam, data->stat);
}
bool checkTrust(FunctionData* data)
{
return false;
}
void invalidOp(FunctionData* data) {
MacroFile* mf = data->stat->getMFile();
if (!mf) return;
const char* msg = kPlugin.kFuncs->Translate("The macro %s has done an invalid operation and will be deactivated.");
int len = strlen(msg) + mf->name.length();
char* buf = new char[len];
sprintf_s(buf, len, msg, mf->name.c_str());
MessageBoxUTF8(data->c.hWnd, buf, "Security", MB_OK|MB_ICONSTOP|MB_TASKMODAL);
delete [] buf;
DisableMacros(mf);
}
bool mustEscape(unsigned char c)
{
static const char chars[]= " #$&+,/:;=?@";
@ -226,6 +247,7 @@
return WritePrivateProfileStringA(CUTF8_to_ANSI(lpAppName), CUTF8_to_ANSI(lpKeyName), CUTF8_to_ANSI(lpString), CUTF8_to_ANSI(filename));
}
//////////////////////////
// API
@ -282,10 +304,14 @@
else if (!strcmpi(type, "string")) preftype = PREF_UNISTRING;
else {
parseError(WRONGTYPE, "setpref", type);
return "";
return false;
}
MString pref = data->getstr(2);
if (pref.find("kmeleon.plugins.macros") != MString::npos && pref.find("trusted") != MString::npos) {
invalidOp(data);
return Value();
}
if (preftype == PREF_UNISTRING) {
MString value = data->getstr(3);
@ -302,7 +328,7 @@
kFuncs->SetPreference(preftype, pref, &value, TRUE);
}
return "";
return true;
}
Value getpref(FunctionData* data)
@ -363,6 +389,10 @@
}
MString pref = data->getstr(2);
if (pref.find("kmeleon.plugins.macros") != MString::npos && pref.find("trusted") != MString::npos) {
invalidOp(data);
return Value();
}
if (preftype == PREF_BOOL)
{
@ -660,7 +690,7 @@
std::string macro = macrolist.substr(0,pos);
while (macro.length()) {
ret = ExecuteMacro(data->c.hWnd, macro, true);
ret = ExecuteMacro(data->c.hWnd, macro.c_str(), &data->c);
pos != macrolist.npos ?
macrolist.erase(0,pos+1) :
@ -998,7 +1028,7 @@
MString file = data->getstr(1);
if (file.empty())
return "";
FILE* f = fopen(data->getstr(1), "r");
FILE* f = _wfopen(data->getstr(1).utf16(), L"r");
if (f) {
char* buffer = new char[32768];
int size = fread(buffer, sizeof(char), 32768-1, f);
@ -1011,6 +1041,50 @@
return "";
}
Value writefile(FunctionData* data)
{
NEEDTRUST(data);
checkArgs(__FUNCTION__, data, 2);
FILE* f = _wfopen(data->getstr(1).utf16(), L"w");
if (!f) return false;
MString buf = data->getstr(2);
size_t result = fwrite(buf.c_str(), sizeof(char), buf.length(), f);
fclose(f);
return result == buf.length();
}
Value appendfile(FunctionData* data)
{
NEEDTRUST(data);
checkArgs(__FUNCTION__, data, 2);
FILE* f = _wfopen(data->getstr(1).utf16(), L"a");
if (!f) return false;
MString buf = data->getstr(2);
size_t result = fwrite(buf.c_str(), sizeof(char), buf.length(), f);
fclose(f);
return result == buf.length();
}
Value renamefile(FunctionData* data)
{
NEEDTRUST(data);
checkArgs(__FUNCTION__, data, 2);
int res = _wrename(data->getstr(1).utf16(), data->getstr(2).utf16());
return res == 0;
}
Value deletefile(FunctionData* data)
{
NEEDTRUST(data);
checkArgs(__FUNCTION__, data, 1);
int res = _wunlink(data->getstr(1).utf16());
return res == 0;
}
Value readreg(FunctionData* data)
{
checkArgs(__FUNCTION__, data, 2);
@ -1503,6 +1577,78 @@
return kPlugin.kFuncs->SetButton(data->getstr(1), kPlugin.kFuncs->GetID(data->getstr(2)), &b);
}
#define MAX_TIMERS 10
#define OFFSET_TIMERS 1000
typedef struct TimerStruct {
std::string macro;
UINT_PTR idEvent;
HWND hWnd;
TimerStruct() : idEvent(0) {}
} TimerStruct;
TimerStruct timers[MAX_TIMERS];
VOID CALLBACK TimerProc(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime)
{
int i;
for (i=0;i<MAX_TIMERS;i++)
if (timers[i].idEvent == idEvent)
break;
if (i >= MAX_TIMERS) return;
ExecuteMacro(timers[i].hWnd, timers[i].macro.c_str());
KillTimer(NULL, idEvent);
}
Value settimer(FunctionData* data)
{
checkArgs(__FUNCTION__, data, 2, 2);
int i = 0;
for (i=0;i<MAX_TIMERS;i++)
if (!timers[i].macro.length())
break;
if (i >= MAX_TIMERS)
return 0;
timers[i].macro = data->getstr(1);
timers[i].hWnd = data->c.hWnd;
timers[i].idEvent = SetTimer(NULL, (UINT_PTR)&timers[i], data->getint(2), TimerProc);
return timers[i].idEvent;
}
Value killtimer(FunctionData* data)
{
checkArgs(__FUNCTION__, data, 1, 1);
int idEvent = data->getint(1);
int i;
for (i=0;i<MAX_TIMERS;i++)
if (timers[i].idEvent == idEvent)
break;
if (i >= MAX_TIMERS) return false;
//if (i<0 || i>=MAX_TIMERS) return false;
KillTimer(NULL, idEvent);
timers[i].macro.empty();
timers[i].idEvent = 0;
return true;
}
/* url, type, perm, sessionOnly */
Value addperm(FunctionData* data)
{
checkArgs(__FUNCTION__, data, 3, 4);
return kPlugin.kFuncs->AddPermission(data->getstr(1), data->getstr(2), data->getstr(3), data->getbool(4));
}
Value fileexists(FunctionData* data)
{
checkArgs(__FUNCTION__, data, 1, 1);
DWORD dwAttrib = GetFileAttributes(data->getstr(1).utf16());
return (dwAttrib != INVALID_FILE_ATTRIBUTES &&
!(dwAttrib & FILE_ATTRIBUTE_DIRECTORY));
}
#ifndef MACROSFUNC_ADD
#define MACROSFUNC_ADD(entry) m->AddSymbol(#entry, ValueFunc((MacroFunction)&entry));
@ -1568,4 +1714,12 @@ void InitFunctions(Mac* m)
MACROSFUNC_ADD(checkbutton);
MACROSFUNC_ADD(setbuttonimg);
MACROSFUNC_ADD(setcmdicon);
MACROSFUNC_ADD(settimer);
MACROSFUNC_ADD(killtimer);
MACROSFUNC_ADD(addperm);
MACROSFUNC_ADD(fileexists);
MACROSFUNC_ADD(writefile);
MACROSFUNC_ADD(renamefile);
MACROSFUNC_ADD(deletefile);
MACROSFUNC_ADD(appendfile);
}

60
k-meleon/macros2/macros.cpp

@ -24,7 +24,7 @@
#include <shellapi.h>
#include <stdlib.h>
#include <string>
#include <vector>
#include <list>
#include <malloc.h>
#define PLUGIN_NAME "Macro Extension Plugin"
@ -36,7 +36,7 @@
#include "..\\strconv.h"
#include "resrc1.h"
#include <afxres.h> // for ID_APP_EXIT
#include "object.h"
BOOL APIENTRY DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved );
void Create(HWND parent);
@ -57,8 +57,10 @@ void CloseFrame(HWND hWnd, LONG windows);
void Quit();
void SetOption(std::string option);
void SetVarExp(int varid, std::string value);
std::string strTrim(const std::string& instr);
std::string ExecuteMacro (HWND hWnd, std::string name, bool haha);
std::string strTrim(const std::string& instr);
std::string ExecuteMacro(HWND hWnd, const char* name, const Context *parent = nullptr);
void DisableMacros(MacroFile* mf);
#define WRONGARGS 0
#define WRONGTYPE 1
@ -166,9 +168,6 @@ int iVarCount = 0;
int bStartup = 1;
std::string sGlobalArg;
std::string ExecuteMacro (HWND hWnd, std::string name, bool haha = false);
#include "object.h"
class Mac {
public:
@ -438,7 +437,7 @@ public:
Value *v = M->FindSymbol("$ARG");
assert(v);
*v = Value(ptr->arg);
ExecuteMacro(hWnd, ptr->macro, false);
ExecuteMacro(hWnd, ptr->macro.c_str(), false);
*v = Value();
return true;
}
@ -462,6 +461,7 @@ int GetMacroState(int id)
int res = 0;
Context c = {NULL};
if (macro->md->menuChecked) {
return -1; // have to set those manually for now
Evaluator e(M, c);
if (e.EvalExpr(macro->md->menuChecked).boolval())
res |= 0x2;
@ -522,7 +522,7 @@ long DoMessage(const char *to, const char *from, const char *subject, long data1
ExecuteMacro((HWND)data1, "OnOpenTab", false);
}
else if (strcmp(subject, "DestroyTab") == 0) {
ExecuteMacro((HWND)data1, "OnCloseTab", false);
ExecuteMacro(data2?(HWND)data2:(HWND)data1, "OnCloseTab", false);
}
else if (strcmp(subject, "Close") == 0) {
Close((HWND)data1);
@ -578,7 +578,19 @@ long DoMessage(const char *to, const char *from, const char *subject, long data1
return 0;
}
std::string ExecuteMacro (HWND hWnd, std::string name, bool haha)
void DisableMacros(MacroFile* mf)
{
char prefload[MAX_PATH+40];
strcpy(prefload, "kmeleon.plugins.macros.modules.");
strcat(prefload, mf->name.c_str());
strcat(prefload, ".load");
int b = 0;
kFuncs->SetPreference(PREF_BOOL, prefload, (void*)&b, TRUE);
mf->loaded = false;
}
std::string ExecuteMacro (HWND hWnd, const char* name, const Context* parent)
{
if (!M) return "";
@ -587,24 +599,20 @@ std::string ExecuteMacro (HWND hWnd, std::string name, bool haha)
return "";
ValueMacro* macro = (ValueMacro*)val;
if (!macro->md) {
if (!macro->md || !macro->md->mf->loaded) {
return "";
}
Context context;
context.hWnd = hWnd;
context.mf = macro->md->mf;
context.origmf = parent ? (parent->origmf ? parent->origmf : parent->mf) : nullptr;
Evaluator eval(M, context);
eval.Evaluate(macro->md);
return "";
}
typedef struct MacroFileDesc {
MString name;
MString desc;
bool user;
} MacroFileDesc;
std::vector<MacroFileDesc> *macroFileList;
std::list<MacroFile> *macroFileList;
bool LoadDir(const TCHAR* macrosDir)
{
@ -679,7 +687,7 @@ BOOL APIENTRY DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserv
int Load() {
kFuncs = kPlugin.kFuncs;
arglist = new ArgList;
macroFileList = new std::vector<MacroFileDesc>;
macroFileList = new std::list<MacroFile>;
M = new Mac();
M->AddSymbol("$ARG", Value());
InitFunctions(M);
@ -861,12 +869,20 @@ void DoRebar(HWND rebarWnd) {
bool LoadMacros(const TCHAR *filename)
{
Parser parser;
int debug = 0;
kPlugin.kFuncs->GetPreference(PREF_BOOL, "kmeleon.plugins.macros.debug", &debug, &debug);
if (!parser.init(M, filename, debug))
static int debug = -1;
if (debug == -1) {
int def = 0;
kPlugin.kFuncs->GetPreference(PREF_BOOL, "kmeleon.plugins.macros.debug", &debug, &def);
}
MacroFile mf((TCHAR*)filename); // Meh
auto iter = macroFileList->insert(macroFileList->begin(), mf);
if (!parser.init(M, *iter, debug))
return false;
parser.parse();
(*iter).loaded = true;
return true;
}

48
k-meleon/macros2/object.h

@ -1,6 +1,6 @@
#include <assert.h>
#include <map>
#include "../strconv.h"
enum Type {
VALUE_NONE,
@ -18,14 +18,18 @@ public:
MString(std::string s) : std::string(s) {}
MString(const char* s) : std::string(s) {}
operator const char* () { return c_str(); }
CUTF8_to_UTF16 utf16() { return CUTF8_to_UTF16(c_str()); }
};
class MacroDef;
class Value;
class MacroFile;
struct FunctionData;
typedef struct {
HWND hWnd;
MacroFile* origmf;
MacroFile* mf;
} Context;
typedef Value (*MacroFunction)(FunctionData*);
@ -374,7 +378,7 @@ public:
class StatList : public MacroNode {
MacroNode* last;
public:
MacroNode* child;
MacroNode* child;
StatList() {
child = last = NULL;
@ -486,6 +490,32 @@ public:
}
};
class MacroFile {
public:
std::string name;
std::string desc;
std::string file;
std::wstring wfile;
bool user;
bool loaded;
bool trusted;
MacroFile(wchar_t* afile) {
wfile = afile;
file = CUTF16_to_UTF8(afile);
CharLowerBuff(afile, wcslen(afile));
wchar_t* pos = wcsrchr(afile, L'.');
if (pos) *pos = 0;
pos = wcsrchr(afile, L'\\');
name = (const char*)CT_to_UTF8(pos?pos+1:afile);
user = false;
loaded = false;
trusted = false;
}
};
class MacroDef : public StatList {
public:
std::string name;
@ -493,10 +523,12 @@ public:
Expression* menuString;
Expression* menuChecked;
Expression* menuGrayed;
MacroFile* mf;
MacroDef() : StatList() {
MacroDef(MacroFile* amf) : StatList() {
t = NODE_MACRO;
macroInfo = menuString = menuChecked = menuGrayed = NULL;
mf = amf;
}
virtual ~MacroDef() {
@ -522,6 +554,7 @@ public:
}
virtual int getLine() { return -1;}
virtual MacroFile* getMFile() { return nullptr;}
virtual const char* getFile() { return "";}
virtual ~Statement() {
@ -533,15 +566,16 @@ public:
class DebugStatement : public Statement {
int mLine;
MString mFile;
MacroFile* mMf;
public:
DebugStatement(StatType type, const char* file, int line) : Statement(type) {
DebugStatement(StatType type, MacroFile* mf, int line) : Statement(type) {
mLine = line;
mFile = file;
mMf = mf;
}
virtual int getLine() { return mLine;}
virtual const char* getFile() { return mFile.c_str();}
virtual MacroFile* getMFile() { return mMf;}
virtual const char* getFile() { return mMf->file.c_str();}
};

33
k-meleon/macros2/parser.h

@ -201,6 +201,7 @@ public:
ValueStr data;
MString errormsg;
MString file;
MString desc;
unsigned line;
@ -360,6 +361,17 @@ public:
switch (*current)
{
case '#':
if (desc.empty()) {
next();
while (isspace(*current) && *current != '\n') next();
if (*current == '-' && *(current+1) == '-') {
while (*current == '-') next();
while ((*current != '-' || *(current+1) != '-') && *current != '\n' && *current != '\r' && *current) {
desc+=*current;
next();
}
}
}
skipline();
continue;
@ -522,7 +534,7 @@ enum UNOP {
class Parser {
public:
MString file;
MacroFile* mf;
Lexer lex;
Mac* m;
char *input;
@ -532,19 +544,20 @@ public:
Parser() { input = NULL; currentMd = NULL; m = NULL; }
~Parser() { if (input) delete [] input; }
bool init(Mac* mac, const TCHAR* srcFile, bool enableDebug = false) {
bool init(Mac* mac, MacroFile &srcFile, bool enableDebug = false) {
struct _stat st;
if (_tstat(srcFile, &st) == -1)
if (_tstat(srcFile.wfile.c_str(), &st) == -1)
return false;
FILE* f = _tfopen(srcFile, _T("r"));
mf = &srcFile;
FILE* f = _tfopen(srcFile.wfile.c_str(), _T("r"));
if (!f) return false;
input = new char[st.st_size+1];
size_t r = fread(input, sizeof(char), st.st_size, f);
input[r] = 0;
file = (const char*)CT_to_UTF8(srcFile);
lex.setfile(file);
lex.setfile(srcFile.file.c_str());
fclose(f);
return _init(mac, input, enableDebug);
}
@ -564,7 +577,7 @@ public:
errmsg += "\n";
errmsg.append(lex.tokenstr, 25);
}
DoError(errmsg.c_str(), file.c_str(), lex.line);
DoError(errmsg.c_str(), mf->file.c_str(), lex.line);
}
void error(const char* msg) {
@ -575,7 +588,7 @@ public:
{
if (!debug)
return new Statement(type);
return new DebugStatement(type, file.c_str(), lex.line);
return new DebugStatement(type, mf, lex.line);
}
bool isunop(TOKEN tk) {
@ -860,7 +873,7 @@ public:
skip(TK_NAME);
skip(TK_BEGIN);
MacroDef* md = new MacroDef();
MacroDef* md = new MacroDef(mf);
currentMd = md;
md->name = macroname;
@ -1073,7 +1086,7 @@ public:
}
} while (lex.token != TK_NONE && lex.token != TK_ERR);
mf->desc = lex.desc;
return lex.errormsg.length()>0;
}
};

Loading…
Cancel
Save