From b317b66c1a85de26def5c3371fe04b85f334fe45 Mon Sep 17 00:00:00 2001 From: Gluzskiy Alexandr Date: Sun, 4 Mar 2012 20:29:44 +0200 Subject: math expressions check code --- stopspam.cpp | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 68 insertions(+), 4 deletions(-) mode change 100644 => 100755 stopspam.cpp (limited to 'stopspam.cpp') diff --git a/stopspam.cpp b/stopspam.cpp old mode 100644 new mode 100755 index 8ca9111..7df9a0b --- a/stopspam.cpp +++ b/stopspam.cpp @@ -133,6 +133,7 @@ MIRANDA_HOOK_EVENT(ME_DB_EVENT_FILTER_ADD, w, l) // if message contains right answer... BYTE msg = 1; + static int math_answer = 0; if(gbInvisDisable) { if(CallProtoService(dbei->szModule, PS_GETSTATUS, 0, 0) == ID_STATUS_INVISIBLE) @@ -155,9 +156,9 @@ MIRANDA_HOOK_EVENT(ME_DB_EVENT_FILTER_ADD, w, l) } else { - std::string check(toUTF8(variables_parse(gbAnswer, hContact))), msg(toUTF8(message)); - boost::regex expr(check); - answered = boost::regex_search(msg.begin(), msg.end(), expr); + std::string check(toUTF8(variables_parse(gbAnswer, hContact))), msg(toUTF8(message)); + boost::regex expr(check); + answered = boost::regex_search(msg.begin(), msg.end(), expr); } } if(answered) @@ -219,7 +220,70 @@ MIRANDA_HOOK_EVENT(ME_DB_EVENT_FILTER_ADD, w, l) tstring q; if(gbInfTalkProtection) q += _T("StopSpam automatic message:\r\n"); - q += variables_parse(gbQuestion, hContact); + if(gbMathExpression) + { //parse math expression in question + tstring tmp_question = gbQuestion; + std::list args; + std::list actions; + std::string arg; + tstring::size_type p1 = gbQuestion.find(_T("X")), p2 = 0; + const tstring expr_chars = _T("X+-/*"); + while(expr_chars.find(gbQuestion[p1]) != tstring::npos) + { + p2 = p1; + for(p1 = gbQuestion.find(_T("X"), p1); p1 != tstring::npos; ++p1) + arg += get_random_num(1); +#ifdef UNICODE + tmp_question.replace(p2, arg.size(), toUTF16(arg)); +#else + tmp_question.replace(p2, arg.size(), arg); +#endif + args.push_back(atoi(arg.c_str())); + actions.push_back(gbQuestion[p1]); + ++p1; + } + math_answer = args.front(); + args.pop_front(); + while(!args.empty()) + { + if(!actions.empty()) + { + switch(actions.front()) + { + case _T('+'): + { + math_answer += args.front(); + args.pop_front(); + } + break; + case _T('-'): + { + math_answer -= args.front(); + args.pop_front(); + } + break; + case _T('/'): + { + math_answer /= args.front(); + args.pop_front(); + } + break; + case _T('*'): + { + math_answer *= args.front(); + args.pop_front(); + } + break; + } + actions.pop_front(); + } + else + break; + } + q += variables_parse(tmp_question, hContact); + } + else + q += variables_parse(gbQuestion, hContact); #ifdef _UNICODE char * buf=mir_utf8encodeW(q.c_str()); -- cgit v1.2.3