extern "C" __declspec(dllexport) int Load() { mir_getLP(&pluginInfo); srand((unsigned)time(0)); bayesdb = NULL; if (_getOptB("BayesEnabled", defaultBayesEnabled)) { if (CheckBayes()) { OpenBayes(); if (_getOptB("BayesAutolearnNotApproved", defaultBayesAutolearnNotApproved)) { dequeue_messages(); last_queue_check = time(NULL); } } } hModulesLoaded = HookEvent(ME_SYSTEM_MODULESLOADED, OnModulesLoaded); hDBContactAdded = HookEvent(ME_DB_CONTACT_ADDED, OnDBContactAdded); hDBEventAdded = HookEvent(ME_DB_EVENT_ADDED, OnDBEventAdded); hDBEventFilterAdd = HookEvent(ME_DB_EVENT_FILTER_ADD, OnDBEventFilterAdd); return 0; }
void queue_message(MCONTACT hContact, DWORD msgtime, TCHAR *message) { char *tmp; sqlite3_stmt *stmt; if (!_getOptB("BayesAutolearnApproved", defaultBayesAutolearnApproved) && !_getOptB("BayesAutolearnNotApproved", defaultBayesAutolearnNotApproved)) return; if (_getOptB("BayesEnabled", defaultBayesEnabled) == 0) return; if (bayesdb == NULL) OpenBayes(); sqlite3_prepare_v2(bayesdb, "INSERT INTO queue VALUES(?,?,?)", -1, &stmt, NULL); sqlite3_bind_int(stmt, 1, (DWORD)hContact); sqlite3_bind_int(stmt, 2, msgtime); tmp = mir_u2a(message); sqlite3_bind_text(stmt, 3, tmp, (int)mir_strlen(tmp), NULL); sqlite3_step(stmt); mir_free(tmp); sqlite3_finalize(stmt); }
/* Learn one message as either SPAM or HAM as specified in type parameter */ void learn(int type, TCHAR *msg) { char *tok, *message; BYTE digest[16]; char sql_select[200], sql_update[200], sql_insert[200], sql_counter[200]; sqlite3_stmt *stmt; #ifdef _DEBUG sqlite3_stmt *stmtdbg; #endif if (_getOptB("BayesEnabled", defaultBayesEnabled) == 0) return; if (bayesdb == NULL) OpenBayes(); message = mir_u2a(msg); tok = strtok(message, DELIMS); mir_snprintf(sql_counter, "UPDATE stats SET value=value+1 WHERE key='%s'", type == SPAM ? "spam_msgcount" : "ham_msgcount"); mir_snprintf(sql_select, "SELECT 1 FROM %s WHERE token=?", type == SPAM ? "spam" : "ham"); mir_snprintf(sql_update, "UPDATE %s SET num=num+1 WHERE token=?", type ? "spam" : "ham"); mir_snprintf(sql_insert, "INSERT INTO %s VALUES(?, 1)", type ? "spam" : "ham"); #ifdef _DEBUG sqlite3_exec(bayesdbg, "BEGIN", NULL, NULL, NULL); #endif sqlite3_exec(bayesdb, "BEGIN", NULL, NULL, NULL); while (tok) { if (!is_token_valid(tok)) { tok = strtok(NULL, DELIMS); continue; } tokenhash(tok, digest); sqlite3_prepare_v2(bayesdb, sql_select, -1, &stmt, NULL); sqlite3_bind_blob(stmt, 1, digest, 16, SQLITE_STATIC); if (SQLITE_ROW == sqlite3_step(stmt)) { sqlite3_finalize(stmt); sqlite3_prepare_v2(bayesdb, sql_update, -1, &stmt, NULL); } else { sqlite3_finalize(stmt); sqlite3_prepare_v2(bayesdb, sql_insert, -1, &stmt, NULL); } sqlite3_bind_blob(stmt, 1, digest, 16, SQLITE_STATIC); sqlite3_step(stmt); sqlite3_finalize(stmt); #ifdef _DEBUG sqlite3_prepare_v2(bayesdbg, sql_select, -1, &stmtdbg, NULL); sqlite3_bind_text(stmtdbg, 1, tok, (int)mir_strlen(tok), NULL); if (SQLITE_ROW == sqlite3_step(stmtdbg)) { sqlite3_finalize(stmtdbg); sqlite3_prepare_v2(bayesdbg, sql_update, -1, &stmtdbg, NULL); } else { sqlite3_finalize(stmtdbg); sqlite3_prepare_v2(bayesdbg, sql_insert, -1, &stmtdbg, NULL); } sqlite3_bind_text(stmtdbg, 1, tok, (int)mir_strlen(tok), SQLITE_STATIC); sqlite3_step(stmtdbg); sqlite3_finalize(stmtdbg); #endif tok = strtok(NULL, DELIMS); } sqlite3_exec(bayesdb, sql_counter, NULL, NULL, NULL); sqlite3_exec(bayesdb, "COMMIT", NULL, NULL, NULL); #ifdef _DEBUG sqlite3_exec(bayesdbg, "COMMIT", NULL, NULL, NULL); #endif mir_free(message); }