summaryrefslogtreecommitdiff
path: root/plugins/StopSpamMod/src/utilities.cpp
diff options
context:
space:
mode:
authorGeorge Hazan <george.hazan@gmail.com>2015-08-18 11:49:54 +0000
committerGeorge Hazan <george.hazan@gmail.com>2015-08-18 11:49:54 +0000
commit878d72910cccf4f84c7cb45bb4c11134920f3166 (patch)
tree251fba89632c389309ff9b2451850aa949efb8d0 /plugins/StopSpamMod/src/utilities.cpp
parent871410044ecbac0d2dd67a7c98f8bcd2df9410eb (diff)
- naming conflict;
- warning fixes; - code cleaning git-svn-id: http://svn.miranda-ng.org/main/trunk@14988 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c
Diffstat (limited to 'plugins/StopSpamMod/src/utilities.cpp')
-rwxr-xr-xplugins/StopSpamMod/src/utilities.cpp188
1 files changed, 87 insertions, 101 deletions
diff --git a/plugins/StopSpamMod/src/utilities.cpp b/plugins/StopSpamMod/src/utilities.cpp
index bbbc910ed0..2cad3a82a6 100755
--- a/plugins/StopSpamMod/src/utilities.cpp
+++ b/plugins/StopSpamMod/src/utilities.cpp
@@ -15,17 +15,16 @@
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
-
#include "stdafx.h"
tstring DBGetContactSettingStringPAN(MCONTACT hContact, char const * szModule, char const * szSetting, tstring errorValue)
{
DBVARIANT dbv;
//if(db_get(hContact, szModule, szSetting, &dbv))
- if(db_get_ts(hContact, szModule, szSetting, &dbv))
+ if (db_get_ts(hContact, szModule, szSetting, &dbv))
return errorValue;
-// if(DBVT_TCHAR == dbv.type )
- errorValue = dbv.ptszVal;
+ // if(DBVT_TCHAR == dbv.type )
+ errorValue = dbv.ptszVal;
db_free(&dbv);
return errorValue;
}
@@ -34,10 +33,10 @@ std::string DBGetContactSettingStringPAN_A(MCONTACT hContact, char const * szMod
{
DBVARIANT dbv;
//if(db_get(hContact, szModule, szSetting, &dbv))
- if(db_get_s(hContact, szModule, szSetting, &dbv))
+ if (db_get_s(hContact, szModule, szSetting, &dbv))
return errorValue;
-// if(DBVT_ASCIIZ == dbv.type )
- errorValue = dbv.pszVal;
+ // if(DBVT_ASCIIZ == dbv.type )
+ errorValue = dbv.pszVal;
db_free(&dbv);
return errorValue;
}
@@ -50,7 +49,7 @@ tstring &GetDlgItemString(HWND hwnd, int id)
GetWindowText(h, buf, len + 1);
static tstring s;
s = buf;
- delete []buf;
+ delete[]buf;
return s;
}
@@ -78,26 +77,27 @@ int CreateCListGroup(TCHAR* szGroupName)
void DeleteCListGroupsByName(TCHAR* szGroupName)
{
BYTE ConfirmDelete = db_get_b(NULL, "CList", "ConfirmDelete", SETTING_CONFIRMDELETE_DEFAULT);
- if(ConfirmDelete)
+ if (ConfirmDelete)
db_set_b(NULL, "CList", "ConfirmDelete", 0);
TCHAR *szGroup;
- for (int i=1; (szGroup = pcli->pfnGetGroupName(i, NULL)) != NULL; i++)
- if( !mir_wstrcmp(szGroupName, szGroup))
+ for (int i = 1; (szGroup = pcli->pfnGetGroupName(i, NULL)) != NULL; i++)
+ if (!mir_wstrcmp(szGroupName, szGroup))
CallService(MS_CLIST_GROUPDELETE, i, 0);
- if(ConfirmDelete)
+ if (ConfirmDelete)
db_set_b(NULL, "CList", "ConfirmDelete", ConfirmDelete);
}
-int RemoveTmp(WPARAM,LPARAM)
+int RemoveTmp(WPARAM, LPARAM)
{
void CleanThread();
CleanThread();
return 0;
}
-tstring variables_parse(tstring const &tstrFormat, MCONTACT hContact){
+tstring variables_parse(tstring const &tstrFormat, MCONTACT hContact)
+{
if (gbVarsServiceExist) {
FORMATINFO fi;
TCHAR *tszParsed;
@@ -142,27 +142,25 @@ const int Stricmp(const TCHAR *str, const TCHAR *substr)
TCHAR* ReqGetText(DBEVENTINFO* dbei)
{
- if ( !dbei->pBlob )
+ if (!dbei->pBlob)
return 0;
- char * ptr=(char *)&dbei->pBlob[sizeof(DWORD)*2];
- int len=dbei->cbBlob-sizeof(DWORD)*2;
- int i=0;
+ char * ptr = (char *)&dbei->pBlob[sizeof(DWORD) * 2];
+ int len = dbei->cbBlob - sizeof(DWORD) * 2;
+ int i = 0;
- while(len && (i<4))
- {
- if(!ptr[0]) i++;
+ while (len && (i < 4)) {
+ if (!ptr[0]) i++;
ptr++;
len--;
};
- if(len)
- {
- char * tstr=(char *)mir_alloc(len+1);
+ if (len) {
+ char * tstr = (char *)mir_alloc(len + 1);
memcpy(tstr, ptr, len);
- tstr[len]=0;
+ tstr[len] = 0;
WCHAR* msg = NULL;
- msg=(dbei->flags&DBEF_UTF)?mir_utf8decodeW(tstr):mir_a2u(tstr);
+ msg = (dbei->flags&DBEF_UTF) ? mir_utf8decodeW(tstr) : mir_a2u(tstr);
mir_free(tstr);
return (TCHAR *)msg;
};
@@ -171,8 +169,8 @@ TCHAR* ReqGetText(DBEVENTINFO* dbei)
BOOL IsUrlContains(TCHAR * Str)
{
- const int CountUrl=11;
- const TCHAR URL[CountUrl][5]=
+ const int CountUrl = 11;
+ const TCHAR URL[CountUrl][5] =
{
L"http",
L"www",
@@ -187,12 +185,11 @@ BOOL IsUrlContains(TCHAR * Str)
L".tv"
};
- if(Str && mir_tstrlen(Str)>0) {
+ if (Str && mir_tstrlen(Str) > 0) {
TCHAR *StrLower = NEWTSTR_MALLOC(Str);
CharLowerBuff(StrLower, (int)mir_tstrlen(StrLower));
- for (int i=0; i<CountUrl; i++)
- if(_tcsstr (StrLower, URL[i]))
- {
+ for (int i = 0; i < CountUrl; i++)
+ if (_tcsstr(StrLower, URL[i])) {
mir_free(StrLower);
return 1;
}
@@ -204,8 +201,8 @@ BOOL IsUrlContains(TCHAR * Str)
tstring GetContactUid(MCONTACT hContact, tstring Protocol)
{
tstring Uid;
- TCHAR dUid[32]={0};
- char aUid[32]={0};
+ TCHAR dUid[32] = { 0 };
+ char aUid[32] = { 0 };
char *szProto = mir_utf8encodeW(Protocol.c_str());
CONTACTINFO ci;
memset(&ci, 0, sizeof(ci));
@@ -215,19 +212,19 @@ tstring GetContactUid(MCONTACT hContact, tstring Protocol)
ci.cbSize = sizeof(ci);
ci.dwFlag = CNF_DISPLAYUID | CNF_TCHAR;
- if (!CallService(MS_CONTACT_GETCONTACTINFO, 0, (LPARAM) & ci)) {
+ if (!CallService(MS_CONTACT_GETCONTACTINFO, 0, (LPARAM)& ci)) {
switch (ci.type) {
- case CNFT_ASCIIZ:
- Uid=ci.pszVal;
- mir_free((void *)ci.pszVal);
+ case CNFT_ASCIIZ:
+ Uid = ci.pszVal;
+ mir_free((void *)ci.pszVal);
break;
- case CNFT_DWORD:
- _itoa_s(ci.dVal,aUid,32,10);
- OemToChar(aUid, dUid);
- Uid=dUid;
+ case CNFT_DWORD:
+ _itoa_s(ci.dVal, aUid, 32, 10);
+ OemToChar(aUid, dUid);
+ Uid = dUid;
break;
- default:
- Uid=_T("");
+ default:
+ Uid = _T("");
break;
};
}
@@ -240,42 +237,40 @@ void LogSpamToFile(MCONTACT hContact, tstring message)
if (!gbLogToFile) return;
tstring LogStrW, LogTime, LogProtocol, LogContactId, LogContactName;
- tstring filename;
std::fstream file;
TCHAR pszName[MAX_PATH];
if (hStopSpamLogDirH)
FoldersGetCustomPathT(hStopSpamLogDirH, pszName, MAX_PATH, _T(""));
else
- mir_tstrncpy(pszName, VARST( _T("%miranda_logpath%")), _countof(pszName));
+ mir_tstrncpy(pszName, VARST(_T("%miranda_logpath%")), _countof(pszName));
- filename = pszName;
- filename = filename + _T("\\stopspam_mod.log");
-
- file.open(filename.c_str(),std::ios::out |std::ios::app);
+ tstring filename = pszName;
+ filename += _T("\\stopspam_mod.log");
+ file.open(filename.c_str(), std::ios::out | std::ios::app);
// Time Log line
time_t time_now;
- tm *TimeNow;
time(&time_now);
- TimeNow = localtime(&time_now);
- LogTime=_wasctime( TimeNow );
+
+ struct tm *TimeNow = localtime(&time_now);
+ LogTime = _wasctime(TimeNow);
// Time Log line
// Name, UID and Protocol Log line
- LogProtocol=DBGetContactSettingStringPAN(hContact,"Protocol","p",_T(""));
- LogContactName=(TCHAR*)pcli->pfnGetContactDisplayName(hContact, 0);
- LogContactId=(LogProtocol==_T(""))?_T(""):GetContactUid(hContact,LogProtocol);
+ LogProtocol = DBGetContactSettingStringPAN(hContact, "Protocol", "p", _T(""));
+ LogContactName = (TCHAR*)pcli->pfnGetContactDisplayName(hContact, 0);
+ LogContactId = (LogProtocol == _T("")) ? _T("") : GetContactUid(hContact, LogProtocol);
// Name, UID and Protocol Log line
- LogStrW=_T("[")+LogTime.substr(0,LogTime.length()-1)+_T("] ")+
- LogContactId+_T(" - ")+
- LogContactName+_T(" (")+
- LogProtocol+_T("): ")+
- message+_T("\n");
+ LogStrW = _T("[") + LogTime.substr(0, LogTime.length() - 1) + _T("] ") +
+ LogContactId + _T(" - ") +
+ LogContactName + _T(" (") +
+ LogProtocol + _T("): ") +
+ message + _T("\n");
- char * buf=mir_u2a(LogStrW.c_str());
- file.write(buf,LogStrW.length());
+ char *buf = mir_u2a(LogStrW.c_str());
+ file.write(buf, LogStrW.length());
mir_free(buf);
file.close();
@@ -285,24 +280,22 @@ boost::mutex clean_mutex;
void CleanProtocolTmpThread(std::string proto)
{
- while(true)
- {
+ while (true) {
UINT status = CallProtoService(proto.c_str(), PS_GETSTATUS, 0, 0);
- if(status > ID_STATUS_OFFLINE)
+ if (status > ID_STATUS_OFFLINE)
break;
boost::this_thread::sleep(boost::posix_time::seconds(2));
}
std::list<MCONTACT> contacts;
- for(MCONTACT hContact = db_find_first(proto.c_str()); hContact; hContact = db_find_next(hContact, proto.c_str()))
- if(db_get_b(hContact, "CList", "NotOnList", 0)|| (_T("Not In List")== DBGetContactSettingStringPAN(hContact,"CList","Group",_T(""))))
+ for (MCONTACT hContact = db_find_first(proto.c_str()); hContact; hContact = db_find_next(hContact, proto.c_str()))
+ if (db_get_b(hContact, "CList", "NotOnList", 0) || (_T("Not In List") == DBGetContactSettingStringPAN(hContact, "CList", "Group", _T(""))))
contacts.push_back(hContact);
boost::this_thread::sleep(boost::posix_time::seconds(5));
clean_mutex.lock();
std::list<MCONTACT>::iterator end = contacts.end();
- for(std::list<MCONTACT>::iterator i = contacts.begin(); i != end; ++i)
- {
+ for (std::list<MCONTACT>::iterator i = contacts.begin(); i != end; ++i) {
LogSpamToFile(*i, _T("Deleted"));
HistoryLogFunc(*i, "Deleted");
CallService(MS_DB_CONTACT_DELETE, (WPARAM)*i, 0);
@@ -312,24 +305,22 @@ void CleanProtocolTmpThread(std::string proto)
void CleanProtocolExclThread(std::string proto)
{
- while(true)
- {
+ while (true) {
UINT status = CallProtoService(proto.c_str(), PS_GETSTATUS, 0, 0);
- if(status > ID_STATUS_OFFLINE)
+ if (status > ID_STATUS_OFFLINE)
break;
boost::this_thread::sleep(boost::posix_time::seconds(2));
}
std::list<MCONTACT> contacts;
- for(MCONTACT hContact = db_find_first(proto.c_str()); hContact; hContact = db_find_next(hContact, proto.c_str()))
- if(db_get_b(hContact, "CList", "NotOnList", 0) && db_get_b(hContact, pluginName, "Excluded", 0))
+ for (MCONTACT hContact = db_find_first(proto.c_str()); hContact; hContact = db_find_next(hContact, proto.c_str()))
+ if (db_get_b(hContact, "CList", "NotOnList", 0) && db_get_b(hContact, pluginName, "Excluded", 0))
contacts.push_back(hContact);
boost::this_thread::sleep(boost::posix_time::seconds(5));
clean_mutex.lock();
std::list<MCONTACT>::iterator end = contacts.end();
- for(std::list<MCONTACT>::iterator i = contacts.begin(); i != end; ++i)
- {
+ for (std::list<MCONTACT>::iterator i = contacts.begin(); i != end; ++i) {
LogSpamToFile(*i, _T("Deleted"));
HistoryLogFunc(*i, "Deleted");
CallService(MS_DB_CONTACT_DELETE, (WPARAM)*i, 0);
@@ -343,17 +334,15 @@ void CleanThread()
int count = 0;
PROTOACCOUNT **accounts;
Proto_EnumAccounts(&count, &accounts);
- for(int i = 0; i < count; i++)
- {
- if(!strstr(accounts[i]->szModuleName, "MetaContacts") && !strstr(accounts[i]->szModuleName, "Weather")) //not real protocols
+ for (int i = 0; i < count; i++)
+ if (!strstr(accounts[i]->szModuleName, "MetaContacts") && !strstr(accounts[i]->szModuleName, "Weather")) //not real protocols
protocols.push_back(accounts[i]->szModuleName);
- }
+
std::list<std::string>::iterator end = protocols.end();
- for(std::list<std::string>::iterator i = protocols.begin(); i != end; ++i)
- {
- if(gbDelAllTempory)
+ for (std::list<std::string>::iterator i = protocols.begin(); i != end; ++i) {
+ if (gbDelAllTempory)
new boost::thread(boost::bind(&CleanProtocolTmpThread, *i));
- if(gbDelExcluded)
+ if (gbDelExcluded)
new boost::thread(boost::bind(&CleanProtocolExclThread, *i));
}
}
@@ -365,23 +354,22 @@ void HistoryLog(MCONTACT hContact, char *data, int event_type, int flags)
Event.eventType = event_type;
Event.flags = flags | DBEF_UTF;
Event.timestamp = (DWORD)time(NULL);
- Event.cbBlob = (DWORD)mir_strlen(data)+1;
+ Event.cbBlob = (DWORD)mir_strlen(data) + 1;
Event.pBlob = (PBYTE)_strdup(data);
db_event_add(hContact, &Event);
}
void HistoryLogFunc(MCONTACT hContact, std::string message)
{
- if(gbHistoryLog)
- {
- if(hContact == INVALID_CONTACT_ID)
+ if (gbHistoryLog) {
+ if (hContact == INVALID_CONTACT_ID)
return;
std::string msg = message;
msg.append("\n");
msg.append("Protocol: ").append(GetContactProto(hContact)).append(" Contact: ");
msg.append(toUTF8((TCHAR*)pcli->pfnGetContactDisplayName(hContact, 0))).append(" ID: ");
- msg.append(toUTF8(GetContactUid(hContact,toUTF16(GetContactProto(hContact)))));
+ msg.append(toUTF8(GetContactUid(hContact, toUTF16(GetContactProto(hContact)))));
HistoryLog(NULL, (char*)msg.c_str(), EVENTTYPE_MESSAGE, DBEF_READ);
}
}
@@ -389,11 +377,10 @@ void HistoryLogFunc(MCONTACT hContact, std::string message)
std::string toUTF8(std::wstring str)
{
std::string ustr;
- try{
- utf8::utf16to8(str.begin(), str.end(), back_inserter(ustr));
+ try {
+ utf8::utf16to8(str.begin(), str.end(), back_inserter(ustr));
}
- catch(const std::exception&)
- {
+ catch (const std::exception&) {
//TODO: handle utf8cpp exceptions
}
return ustr;
@@ -408,12 +395,11 @@ std::wstring toUTF16(std::string str) //convert as much as possible
{
std::wstring ustr;
std::string tmpstr;
- try{
- utf8::replace_invalid(str.begin(), str.end(), back_inserter(tmpstr));
- utf8::utf8to16(tmpstr.begin(), tmpstr.end(), back_inserter(ustr));
+ try {
+ utf8::replace_invalid(str.begin(), str.end(), back_inserter(tmpstr));
+ utf8::utf8to16(tmpstr.begin(), tmpstr.end(), back_inserter(ustr));
}
- catch(const std::exception &)
- {
+ catch (const std::exception &) {
//TODO: handle utf8cpp exceptions
}
return ustr;
@@ -424,8 +410,8 @@ std::string get_random_num(int length)
std::string chars("123456789");
std::string data;
boost::random_device rng;
- boost::variate_generator<boost::random_device&, boost::uniform_int<>> gen(rng, boost::uniform_int<>(0, (int)chars.length()-1));
- for(int i = 0; i < length; ++i)
+ boost::variate_generator<boost::random_device&, boost::uniform_int<>> gen(rng, boost::uniform_int<>(0, (int)chars.length() - 1));
+ for (int i = 0; i < length; ++i)
data += chars[gen()];
return data;
}