summaryrefslogtreecommitdiffstats
path: root/kopete/protocols/irc/ksparser.cpp
blob: 514421ff8e04452fdbf1ee19846e8162ce271a2a (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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/* This file is part of ksirc
   Copyright (c) 2001 Malte Starostik <malte@kde.org>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License version 2 as published by the Free Software Foundation.

   This library 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
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/

/*
Color parser code courtesy of ksirc <http://www.kde.org>
Modified by Jason Keirstead <jason@keirstead.org>
*/

#include <knotifyclient.h>
#include <kdebug.h>
#include <tqbuffer.h>
#include <tqdatastream.h>
#include <tqstylesheet.h>
#include "ksparser.h"
#include <stdlib.h>

KSParser KSParser::m_parser;

const TQColor KSParser::IRC_Colors[17]=
{
	TQt::white,
	TQt::black,
	TQt::darkBlue,
	TQt::darkGreen,
	TQt::red,
	TQt::darkRed,
	TQt::darkMagenta,
	TQt::darkYellow,
	TQt::yellow,
	TQt::green,
	TQt::darkCyan,
	TQt::cyan,
	TQt::blue,
	TQt::magenta,
	TQt::darkGray,
	TQt::gray,
	TQColor() // default invalid color, must be the last
};

const TQRegExp KSParser::sm_colorsModeRegexp("(\\d{1,2})(?:,(\\d{1,2}))?");

template <typename _TYPE_>
	inline void swap(_TYPE_ &o1, _TYPE_ &o2)
{
	_TYPE_ tmp = o1;
	o1 = o2;
	o2 = tmp;
}

KSParser::KSParser()
{
	kdDebug(14120) << k_funcinfo << endl;
}

KSParser::~KSParser()
{
	kdDebug(14120) << k_funcinfo << endl;
}

/* NOTE: If thread corruption are seen simply ad a qlock here */
TQCString KSParser::parse(const TQCString &message)
{
	return m_parser._parse(message);
}

TQCString KSParser::_parse(const TQCString &message)
{
	TQCString data( message.size() * 2 );
	TQBuffer buff( data );
	buff.open( IO_WriteOnly );

	m_tags.clear();
	m_attributes.clear();

	TQRegExp colorsModeRegexp(sm_colorsModeRegexp);

	// should be set to the current default colors ....
	TQColor fgColor; /*KopeteMesage::fg().name()*/
	TQColor bgColor; /*KopeteMesage::bg().name()*/

	uint chars = 0;
	for(uint i = 0; i < message.length(); ++i)
	{
		const TQChar &cur = message[i];
		TQString toAppend;

		switch (cur)
		{
			case 0x02:	//Bold: ^B
				toAppend= toggleTag("b");
				break;
			case 0x03:	//Color code: ^C
				if (colorsModeRegexp.search(message, i+1) == (int)i+1)
				{
					i += colorsModeRegexp.matchedLength(); // + 1 will be added by ++
					TQString tagStyle;

					fgColor = ircColor(colorsModeRegexp.cap(1));
					bgColor = ircColor(colorsModeRegexp.cap(2));

					toAppend = pushColorTag(fgColor, bgColor);
				}
				else
				{
					toAppend = popTag(TQString::fromLatin1("span"));
				}
				break;
			case 0x07:	//System bell: ^G
				KNotifyClient::beep( TQString::fromLatin1("IRC beep event received in a message") );
				break;
			case '\t':	// 0x09
				toAppend = TQString::fromLatin1("&nbsp;&nbsp;&nbsp;&nbsp;");
				break;
			case '\n':	// 0x0D
				toAppend= TQString::fromLatin1("<br/>");
				break;
			case 0x0D:	// Italics: ^N
				toAppend = toggleTag("i");
				break;
			case 0x0F:	//Plain Text, close all tags: ^O
				toAppend = popAll();
				break;
	//		case 0x12:	// Reverse original text colors: ^R
	//			break;
			case 0x16:	//Invert Colors: ^V
				swap(fgColor, bgColor);
				toAppend = pushColorTag(fgColor, bgColor);
				break;
			case 0x1F:	//Underline
				toAppend = toggleTag("u");
				break;
			case '<':
				toAppend = TQString::fromLatin1("&lt;");
				break;
			case '>':
				toAppend = TQString::fromLatin1("&gt;");
				break;
			default:
				if (cur < TQChar(' ')) // search for control characters
					toAppend = TQString::fromLatin1("&lt;%1&gt;").arg(cur, 2, 16).upper();
				else
					toAppend = TQStyleSheet::escape(cur);
		}

		chars += toAppend.length();
		buff.writeBlock( toAppend.latin1(), toAppend.length() );
	}

	TQString toAppend = popAll();
	chars += toAppend.length();
	buff.writeBlock( toAppend.latin1(), toAppend.length() );

	// Make sure we have enough room for NULL character.
	if (data.size() < chars+1)
		data.resize(chars+1);

	data[chars] = '\0';

	return data;
}

TQString KSParser::pushTag(const TQString &tag, const TQString &attributes)
{
	TQString res;
	m_tags.push(tag);
	if(!m_attributes.contains(tag))
		m_attributes.insert(tag, attributes);
	else if(!attributes.isEmpty())
		m_attributes.replace(tag, attributes);
	res.append("<" + tag);
	if(!m_attributes[tag].isEmpty())
		res.append(" " + m_attributes[tag]);
	return res + ">";
}

TQString KSParser::pushColorTag(const TQColor &fgColor, const TQColor &bgColor)
{
	TQString tagStyle;

	if (fgColor.isValid())
		tagStyle += TQString::fromLatin1("color:%1;").arg(fgColor.name());
	if (bgColor.isValid())
		tagStyle += TQString::fromLatin1("background-color:%1;").arg(bgColor.name());

	if(!tagStyle.isEmpty())
		tagStyle = TQString::fromLatin1("style=\"%1\"").arg(tagStyle);

	return pushTag(TQString::fromLatin1("span"), tagStyle);;
}

TQString KSParser::popTag(const TQString &tag)
{
	if (!m_tags.contains(tag))
		return TQString();

	TQString res;
	TQValueStack<TQString> savedTags;
	while(m_tags.top() != tag)
	{
		savedTags.push(m_tags.pop());
		res.append("</" + savedTags.top() + ">");
	}
	res.append("</" + m_tags.pop() + ">");
	m_attributes.remove(tag);
	while(!savedTags.isEmpty())
		res.append(pushTag(savedTags.pop()));
	return res;
}

TQString KSParser::toggleTag(const TQString &tag)
{
	return m_attributes.contains(tag)?popTag(tag):pushTag(tag);
}

TQString KSParser::popAll()
{
	TQString res;
	while(!m_tags.isEmpty())
		res.append("</" + m_tags.pop() + ">");
	m_attributes.clear();
	return res;
}

TQColor KSParser::ircColor(const TQString &color)
{
	bool success;
	unsigned int intColor = color.toUInt(&success);

	if (success)
		return ircColor(intColor);
	else
		return TQColor();
}

TQColor KSParser::ircColor(unsigned int color)
{
	unsigned int maxcolor = sizeof(IRC_Colors)/sizeof(TQColor);
	return color<=maxcolor?IRC_Colors[color]:IRC_Colors[maxcolor];
}

int KSParser::colorForHTML(const TQString &html)
{
	TQColor color(html);
	for(uint i=0; i<sizeof(IRC_Colors)/sizeof(TQColor); i++)
	{
		if(IRC_Colors[i] == color)
			return i;
	}
	return -1;
}