summaryrefslogtreecommitdiff
path: root/plugins/TabSRMM/src/muchighlight.cpp
blob: 7f35c72039d8956671c20fb11fa40f03a28ee79c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/////////////////////////////////////////////////////////////////////////////////////////
// Miranda NG: the free IM client for Microsoft* Windows*
//
// Copyright (C) 2012-24 Miranda NG team,
// Copyright (c) 2000-09 Miranda ICQ/IM project,
// all portions of this codebase are copyrighted to the people
// listed in contributors.txt.
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// you should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
//
// part of tabSRMM messaging plugin for Miranda.
//
// (C) 2005-2010 by silvercircle _at_ gmail _dot_ com and contributors
//
// highlighter class for multi user chats

#include "stdafx.h"

void CMUCHighlight::cleanup()
{
	mir_free(m_NickPatternString);
	mir_free(m_TextPatternString);
	m_TextPatternString = m_NickPatternString = nullptr;

	mir_free(m_NickPatterns);
	mir_free(m_TextPatterns);
	m_iNickPatterns = m_iTextPatterns = 0;
	m_NickPatterns = m_TextPatterns = nullptr;
}

void CMUCHighlight::init()
{
	DBVARIANT dbv = { 0 };

	if (m_fInitialized)
		cleanup();							// clean up first, if we were already initialized

	m_fInitialized = true;

	if (0 == db_get_ws(0, CHAT_MODULE, "HighlightWords", &dbv)) {
		m_TextPatternString = dbv.pwszVal;
		wcslwr(m_TextPatternString);
	}

	if (0 == db_get_ws(0, CHAT_MODULE, "HighlightNames", &dbv))
		m_NickPatternString = dbv.pwszVal;

	m_iMode = db_get_b(0, CHAT_MODULE, "HighlightEnabled", MATCH_TEXT);
	m_fHighlightMe = (db_get_b(0, CHAT_MODULE, "HighlightMe", 1) ? true : false);

	tokenize(m_TextPatternString, m_TextPatterns, m_iTextPatterns);
	tokenize(m_NickPatternString, m_NickPatterns, m_iNickPatterns);
}

void CMUCHighlight::tokenize(wchar_t *tszString, wchar_t**& patterns, UINT& nr)
{
	if (tszString == nullptr)
		return;

	wchar_t	*p = tszString;

	if (*p == 0)
		return;

	nr = 0;

	if (*p != ' ')
		nr++;

	while (*p) {
		if (*p == ' ') {
			p++;
			while (*p && iswspace(*p))
				p++;
			if (*p)
				nr++;
		}
		p++;
	}
	patterns = (wchar_t **)mir_alloc(nr * sizeof(wchar_t*));

	p = tszString;
	nr = 0;

	if (*p != ' ')
		patterns[nr++] = p;

	while (*p) {
		if (*p == ' ') {
			*p++ = 0;
			while (*p && iswspace(*p))
				p++;
			if (*p)
				patterns[nr++] = p;
		}
		p++;
	}
}

bool CMUCHighlight::match(const GCEVENT *pgce, const SESSION_INFO *psi, uint32_t dwFlags)
{
	int result = 0, nResult = 0;

	if (pgce == nullptr || m_Valid == false)
		return false;

	if ((m_iMode & MATCH_TEXT) && (dwFlags & MATCH_TEXT) && (m_fHighlightMe || m_iTextPatterns > 0) && psi != nullptr) {
		wchar_t *p = NEWWSTR_ALLOCA(pgce->pszText.w);
		if (p == nullptr)
			return false;
		CharLower(p);

		wchar_t *tszMe = (psi && psi->getMe()) ? NEWWSTR_ALLOCA(psi->getMe()->pszNick) : nullptr;
		if (tszMe)
			CharLower(tszMe);

		if (m_fHighlightMe && tszMe) {
			result = wcsstr(p, tszMe) ? MATCH_TEXT : 0;
			if (0 == m_iTextPatterns)
				goto skip_textpatterns;
		}

		while (p && !result) {
			while (*p && (*p == ' ' || *p == ',' || *p == '.' || *p == ':' || *p == ';' || *p == '?' || *p == '!'))
				p++;

			if (*p == 0)
				break;

			wchar_t *p1 = p;
			while (*p1 && *p1 != ' ' && *p1 != ',' && *p1 != '.' && *p1 != ':' && *p1 != ';' && *p1 != '?' && *p1 != '!')
				p1++;

			if (*p1)
				*p1 = 0;
			else
				p1 = nullptr;

			for (UINT i = 0; i < m_iTextPatterns && !result; i++)
				result = wildcmpw(p, m_TextPatterns[i]) ? MATCH_TEXT : 0;

			if (p1) {
				*p1 = ' ';
				p = p1 + 1;
			}
			else p = nullptr;
		}
	}

skip_textpatterns:

	// optionally, match the nickname against the list of nicks to highlight
	if ((m_iMode & MATCH_NICKNAME) && (dwFlags & MATCH_NICKNAME) && pgce->pszNick.w && m_iNickPatterns > 0) {
		DBEVENTINFO dbei = {};
		if (pgce->hEvent)
			db_event_get(pgce->hEvent, &dbei);

		for (UINT i = 0; i < m_iNickPatterns && !nResult; i++) {
			if (pgce->pszNick.w)
				if (wildcmpw(pgce->pszNick.w, m_NickPatterns[i])) {
					nResult = MATCH_NICKNAME;
					break;
				}

			if (dbei.szReplyId)
				if (wildcmpw(_A2T(dbei.szReplyId), m_NickPatterns[i])) {
					nResult = MATCH_NICKNAME;
					break;
				}

			if ((m_iMode & MATCH_UIN) && pgce->pszUserInfo.w)
				if (wildcmpw(pgce->pszUserInfo.w, m_NickPatterns[i])) {
					nResult = MATCH_NICKNAME;
					break;
				}
		}
	}

	return result || nResult;
}