Browse Source

Add function declaration, return and break

Add download and date function
Enable trust check
master
boisso 7 years ago
parent
commit
bfdf7ce558
  1. 72
      k-meleon/macros2/functions.h
  2. 36
      k-meleon/macros2/macros.cpp
  3. 3
      k-meleon/macros2/object.h
  4. 15
      k-meleon/macros2/parser.h

72
k-meleon/macros2/functions.h

@ -29,6 +29,8 @@
#define WRONGTYPE 1
#define NEEDTRUST(data) if (!checkTrust(data)) return Value();
Value ExecuteFunction(const char* name);
static void DoError(const char* msg, Statement* stat)
{
DoError(msg, stat?stat->getFile():"", stat?stat->getLine():-1);
@ -66,8 +68,43 @@
bool checkTrust(FunctionData* data)
{
return true; // TODO: to enable later
return data->c.mf->trusted && (!data->c.origmf || data->c.origmf->trusted);
if (data->c.mf->denied)
return false;
if (data->c.mf->trusted && (!data->c.origmf || data->c.origmf->trusted))
return true;
const char* msg = kPlugin.kFuncs->Translate("The macro \"%s\" want to make change to your computer. Do you want to allow it?\nOnly allow changes if you trust the author of the macro.");
int len = strlen(msg) + data->c.mf->name.length();
char* buf = new char[len];
sprintf_s(buf, len, msg, data->c.mf->name.c_str());
int res = MessageBoxUTF8(data->c.hWnd, buf, "Security", MB_YESNO|MB_ICONQUESTION|MB_TASKMODAL);
delete [] buf;
if (res == IDYES) {
char pref[MAX_PATH+40];
sprintf_s(pref, "kmeleon.plugins.macros.modules.%s.trusted", data->c.mf->name.c_str());
int b = 1;
kFuncs->SetPreference(PREF_INT, pref, (void*)&b, TRUE);
data->c.mf->trusted = true;
sprintf_s(pref, "kmeleon.plugins.macros.modules.%s.checksum", data->c.mf->name.c_str());
MD5 md5;
md5.digestFile(CUTF8_to_UTF16(data->c.mf->file.c_str()));
kPlugin.kFuncs->SetPreference(PREF_STRING, pref, md5.digestChars, TRUE);
return true;
} else {
char pref[MAX_PATH+40];
sprintf_s(pref, "kmeleon.plugins.macros.modules.%s.trusted", data->c.mf->name.c_str());
int b = 2;
kFuncs->SetPreference(PREF_INT, pref, (void*)&b, TRUE);
data->c.mf->denied = true;
return false;
}
}
void invalidOp(FunctionData* data) {
@ -1785,6 +1822,35 @@
return std::time(nullptr);
}
Value date(FunctionData* data)
{
std::time_t time;
int t = data->getint(2,-1);
time = t>=0 ? t : std::time(nullptr);
char buf[100];
std::strftime(buf, sizeof(buf), data->getstr(1), std::localtime(&time));
return buf;
}
void OnDownload(const char* url, const char* path, int result, void* data) {
ExecuteFunction((char*)data);
delete data;
}
Value download(FunctionData* data)
{
NEEDTRUST(data);
checkArgs(__FUNCTION__, data, 2, 3);
void* cdata = (void*)strdup(data->getstr(3));
if (!kPlugin.kFuncs->Download(data->getstr(1), data->getstr(2), &OnDownload, cdata)) {
delete cdata;
return false;
}
return true;
}
#define FUNCSYMBOL(X) "!"#X
#ifndef MACROSFUNC_ADD
@ -1864,4 +1930,6 @@ void InitFunctions(Mac* m)
MACROSFUNC_ADD(logmsg);
MACROSFUNC_ADD(popupmenu);
MACROSFUNC_ADD(time);
MACROSFUNC_ADD(date);
MACROSFUNC_ADD(download);
}

36
k-meleon/macros2/macros.cpp

@ -595,6 +595,30 @@ void DisableMacros(MacroFile* mf)
mf->loaded = false;
}
Value ExecuteFunction(const char* name)
{
if (!M) return "";
std::string funcname = "!";
funcname += name;
Value* val = M->FindSymbol(funcname);
if (!val || !val->isufunction())
return "";
ValueFunc* f = (ValueFunc*)val;
f->uf->tds.emptyvar();
/*Expression* param = static_cast<Expression*>(expr->firstParam);
for (auto it = expr->v->uf->params.begin(); it != expr->v->uf->params.end(); it++) {
if (!param) break;
**it = EvalExpr(param);
param = static_cast<Expression*>(param->next);
}*/
Value v, ret;
Context context = {0};
Evaluator eval(M, context);
eval.Evaluate(f->uf, &ret);
return ret;
}
std::string ExecuteMacro (HWND hWnd, const char* name, const Context* parent)
{
if (!M) return "";
@ -891,10 +915,17 @@ MacroFile* LoadMacros(const TCHAR *filename)
m->loaded = true;
char pref[MAX_PATH+40] = {0};
sprintf_s(pref, "kmeleon.plugins.macros.modules.%s.denied", m->name.c_str());
kPlugin.kFuncs->GetPreference(PREF_BOOL, pref, &m->denied, &m->denied);
if (m->denied) return m;
sprintf_s(pref, "kmeleon.plugins.macros.modules.%s.trusted", m->name.c_str());
int trust = 0;
kPlugin.kFuncs->GetPreference(PREF_BOOL, pref, &trust, &trust);
if (trust) {
kPlugin.kFuncs->GetPreference(PREF_INT, pref, &trust, &trust);
if (trust == 2) {
m->denied = true;
} else if (trust == 1) {
char checksum[33];
sprintf_s(pref, "kmeleon.plugins.macros.modules.%s.checksum", m->name.c_str());
long len = kPlugin.kFuncs->GetPreference(PREF_STRING, pref, 0, 0);
@ -907,6 +938,7 @@ MacroFile* LoadMacros(const TCHAR *filename)
m->trusted = true;
}
}
return m;
}

3
k-meleon/macros2/object.h

@ -155,6 +155,7 @@ public:
bool isstring() const{ return t == VALUE_STRING; }
bool ismacro() const { return t == VALUE_MACRO; }
bool isfunction() const { return t == VALUE_FUNCTION; }
bool isufunction() const { return t == VALUE_UFUNCTION; }
bool isvalid() const { return t != VALUE_NONE; }
MInt operator +(Value& right ){ return intval() + right.intval(); }
@ -649,6 +650,7 @@ public:
bool user;
bool loaded;
bool trusted;
bool denied;
Mac m;
MacroFile(wchar_t* afile) {
@ -665,6 +667,7 @@ public:
user = false;
loaded = false;
trusted = false;
denied = false;
}
};

15
k-meleon/macros2/parser.h

@ -101,6 +101,13 @@ static const char* TokenList[] = {
"<menu checked>",
"<menu grayed>",
"<macro info>",
"<button check>",
"null",
"function",
"return",
"const",
"break",
"=",
">",
"<",
@ -666,6 +673,14 @@ public:
((ExprValue*)e)->Set(m->AddSymbol(std::string("#NULL"), Value()));
break;
/*case TK_MENUGRAY:
e = currentMd->menuGrayed;
break;
case TK_MENUCHECK:
e = currentMd->menuChecked;
break;*/
default :
error("Invalid expression.", lex.token);
while (lex.token != TK_NONE && lex.token != TK_SEP)

Loading…
Cancel
Save