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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
|
/*
kircmessage.cpp - IRC Client
Copyright (c) 2003 by Michel Hermier <[email protected]>
Kopete (c) 2003 by the Kopete engineelopers <[email protected]>
*************************************************************************
* *
* 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. *
* *
*************************************************************************
*/
#include "kircengine.h"
#include "kircmessage.h"
// FIXME: Remove the following dependencies.
#include "kopetemessage.h"
#include "ksparser.h"
#include <kdebug.h>
#include <kextsock.h>
#include <klocale.h>
#include <kmessagebox.h>
using namespace KIRC;
#ifndef _IRC_STRICTNESS_
TQRegExp Message::m_IRCNumericCommand("^\\d{1,3}$");
// TODO: This regexp parsing is no good. It's slower than it needs to be, and
// is not codec-safe since TQString requires a codec. NEed to parse this with
// our own parsing class that operates on the raw TQCStrings
TQRegExp Message::m_IRCCommandType1(
"^(?::([^ ]+) )?([A-Za-z]+|\\d{1,3})((?: [^ :][^ ]*)*) ?(?: :(.*))?$");
// Extra end arg space check -------------------------^
#else // _IRC_STRICTNESS_
TQRegExp Message::m_IRCNumericCommand("^\\d{3,3}$");
TQRegExp Message::m_IRCCommandType1(
"^(?::([^ ]+) )?([A-Za-z]+|\\d{3,3})((?: [^ :][^ ]*){0,13})(?: :(.*))?$");
TQRegExp Message::m_IRCCommandType2(
"^(?::[[^ ]+) )?([A-Za-z]+|\\d{3,3})((?: [^ :][^ ]*){14,14})(?: (.*))?$");
#endif // _IRC_STRICTNESS_
Message::Message()
: m_ctcpMessage(0)
{
}
Message::Message(const Message &obj)
: m_ctcpMessage(0)
{
m_raw = obj.m_raw;
m_prefix = obj.m_prefix;
m_command = obj.m_command;
m_args = obj.m_args;
m_suffix = obj.m_suffix;
m_ctcpRaw = obj.m_ctcpRaw;
if (obj.m_ctcpMessage)
m_ctcpMessage = new Message(obj.m_ctcpMessage);
}
Message::Message(const Message *obj)
: m_ctcpMessage(0)
{
m_raw = obj->m_raw;
m_prefix = obj->m_prefix;
m_command = obj->m_command;
m_args = obj->m_args;
m_suffix = obj->m_suffix;
m_ctcpRaw = obj->m_ctcpRaw;
if (obj->m_ctcpMessage)
m_ctcpMessage = new Message(obj->m_ctcpMessage);
}
Message::~Message()
{
if (m_ctcpMessage)
delete m_ctcpMessage;
}
void Message::writeRawMessage(Engine *engine, const TQTextCodec *codec, const TQString &str)
{
// FIXME: Really handle this
if (!engine->socket())
{
kdDebug(14121) << k_funcinfo << "Not connected while attempting to write:" << str << endl;
return;
}
TQString txt = str + TQString::tqfromLatin1("\r\n");
TQCString s(codec->fromUnicode(txt));
kdDebug(14120) << "Message is " << s.length() << " chars" << endl;
// FIXME: Should check the amount of data really writen.
int wrote = engine->socket()->writeBlock(s.data(), s.length());
kdDebug(14121) << TQString::tqfromLatin1("(%1 bytes) >> %2").tqarg(wrote).tqarg(str) << endl;
}
void Message::writeMessage(Engine *engine, const TQTextCodec *codec, const TQString &message)
{
writeRawMessage(engine, codec, quote(message));
}
void Message::writeMessage(Engine *engine, const TQTextCodec *codec,
const TQString &command, const TQStringList &args, const TQString &suffix)
{
TQString msg = command;
if (!args.isEmpty())
msg += TQChar(' ') + args.join(TQChar(' ')).stripWhiteSpace(); // some extra check should be done here
if (!suffix.isNull())
msg = msg.stripWhiteSpace() + TQString::tqfromLatin1(" :") + suffix;
writeMessage(engine, codec, msg);
}
void Message::writeCtcpMessage(Engine *engine, const TQTextCodec *codec,
const TQString &command, const TQString&to,
const TQString &ctcpMessage)
{
writeMessage(engine, codec, command, to, TQChar(0x01) + ctcpQuote(ctcpMessage) + TQChar(0x01));
}
void Message::writeCtcpMessage(Engine *engine, const TQTextCodec *codec,
const TQString &command, const TQString &to, const TQString &suffix,
const TQString &ctcpCommand, const TQStringList &ctcpArgs, const TQString &ctcpSuffix )
{
TQString ctcpMsg = ctcpCommand;
if (!ctcpArgs.isEmpty())
ctcpMsg += TQChar(' ') + ctcpArgs.join(TQChar(' ')).stripWhiteSpace(); // some extra check should be done here
if (!ctcpSuffix.isNull())
ctcpMsg += TQString::tqfromLatin1(" :") + ctcpSuffix;
writeMessage(engine, codec, command, to, suffix + TQChar(0x01) + ctcpQuote(ctcpMsg) + TQChar(0x01));
}
Message Message::parse(Engine *engine, const TQTextCodec *codec, bool *parseSuccess)
{
if (parseSuccess)
*parseSuccess=false;
if (engine->socket()->canReadLine())
{
TQCString raw(engine->socket()->bytesAvailable()+1);
TQ_LONG length = engine->socket()->readLine(raw.data(), raw.count());
if( length > -1 )
{
raw.resize( length );
// Remove trailing '\r\n' or '\n'.
//
// Some servers send '\n' instead of '\r\n' that the RFCs say they should be sending.
if (length > 1 && raw.at(length-2) == '\n') {
raw.tqat(length-2) = '\0';
}
if (length > 2 && raw.at(length-3) == '\r') {
raw.tqat(length-3) = '\0';
}
kdDebug(14121) << "<< " << raw << endl;
Message msg;
if(matchForIRCRegExp(raw, codec, msg))
{
if(parseSuccess)
*parseSuccess = true;
}
else
{
kdDebug(14120) << k_funcinfo << "Unmatched line: \"" << raw << "\"" << endl;
}
return msg;
}
else
kdWarning(14121) << k_funcinfo << "Failed to read a line while canReadLine returned true!" << endl;
}
return Message();
}
TQString Message::quote(const TQString &str)
{
TQString tmp = str;
TQChar q('\020');
tmp.replace(q, q+TQString(q));
tmp.replace(TQChar('\r'), q+TQString::tqfromLatin1("r"));
tmp.replace(TQChar('\n'), q+TQString::tqfromLatin1("n"));
tmp.replace(TQChar('\0'), q+TQString::tqfromLatin1("0"));
return tmp;
}
// FIXME: The unquote system is buggy.
TQString Message::unquote(const TQString &str)
{
TQString tmp = str;
char b[3] = { 020, 020, '\0' };
const char b2[2] = { 020, '\0' };
tmp.replace( b, b2 );
b[1] = 'r';
tmp.replace( b, "\r");
b[1] = 'n';
tmp.replace( b, "\n");
b[1] = '0';
tmp.replace( b, "\0");
return tmp;
}
TQString Message::ctcpQuote(const TQString &str)
{
TQString tmp = str;
tmp.replace( TQChar('\\'), TQString::tqfromLatin1("\\\\"));
tmp.replace( (char)1, TQString::tqfromLatin1("\\1"));
return tmp;
}
TQString Message::ctcpUnquote(const TQString &str)
{
TQString tmp = str;
tmp.replace("\\\\", "\\");
tmp.replace("\\1", "\1" );
return tmp;
}
bool Message::matchForIRCRegExp(const TQCString &line, const TQTextCodec *codec, Message &message)
{
if(matchForIRCRegExp(m_IRCCommandType1, codec, line, message))
return true;
#ifdef _IRC_STRICTNESS_
if(!matchForIRCRegExp(m_IRCCommandType2, codec, line, message))
return true;
#endif // _IRC_STRICTNESS_
return false;
}
// FIXME: remove the decodeStrings calls or update them.
// FIXME: avoid the recursive call, it make the ctcp command unquoted twice (wich is wrong, but valid in most of the cases)
bool Message::matchForIRCRegExp(TQRegExp ®exp, const TQTextCodec *codec, const TQCString &line, Message &msg )
{
if( regexp.exactMatch( codec->toUnicode(line) ) )
{
msg.m_raw = line;
msg.m_prefix = unquote(regexp.cap(1));
msg.m_command = unquote(regexp.cap(2));
msg.m_args = TQStringList::split(' ', regexp.cap(3));
TQCString suffix = codec->fromUnicode(unquote(regexp.cap(4)));
if (!suffix.isNull() && suffix.length() > 0)
{
TQCString ctcpRaw;
if (extractCtcpCommand(suffix, ctcpRaw))
{
msg.m_ctcpRaw = codec->toUnicode(ctcpRaw);
msg.m_ctcpMessage = new Message();
msg.m_ctcpMessage->m_raw = codec->fromUnicode(ctcpUnquote(msg.m_ctcpRaw));
int space = ctcpRaw.find(' ');
if (!matchForIRCRegExp(msg.m_ctcpMessage->m_raw, codec, *msg.m_ctcpMessage))
{
TQCString command;
if (space > 0)
command = ctcpRaw.mid(0, space).upper();
else
command = ctcpRaw.upper();
msg.m_ctcpMessage->m_command =
Kopete::Message::decodeString( KSParser::parse(command), codec );
}
if (space > 0)
msg.m_ctcpMessage->m_ctcpRaw =
Kopete::Message::decodeString( KSParser::parse(ctcpRaw.mid(space)), codec );
}
msg.m_suffix = Kopete::Message::decodeString( KSParser::parse(suffix), codec );
}
else
msg.m_suffix = TQString();
return true;
}
return false;
}
void Message::decodeAgain( const TQTextCodec *codec )
{
matchForIRCRegExp(m_raw, codec, *this);
}
// FIXME: there are missing parts
TQString Message::toString() const
{
if( !isValid() )
return TQString();
TQString msg = m_command;
for (TQStringList::ConstIterator it = m_args.begin(); it != m_args.end(); ++it)
msg += TQChar(' ') + *it;
if (!m_suffix.isNull())
msg += TQString::tqfromLatin1(" :") + m_suffix;
return msg;
}
bool Message::isNumeric() const
{
return m_IRCNumericCommand.exactMatch(m_command);
}
bool Message::isValid() const
{
// This could/should be more complex but the message validity is tested durring the parsing
// So this is enougth as we don't allow the editing the content.
return !m_command.isEmpty();
}
/* Return true if the given string is a special command string
* (i.e start and finish with the ascii code \001), and the given
* string is splited to get the first part of the message and fill the ctcp command.
* FIXME: The code currently only match for a textual message or a ctcp message not both mixed as it can be (even if very rare).
*/
bool Message::extractCtcpCommand(TQCString &message, TQCString &ctcpline)
{
uint len = message.length();
if( message[0] == 1 && message[len-1] == 1 )
{
ctcpline = message.mid(1,len-2);
message.truncate(0);
return true;
}
return false;
}
void Message::dump() const
{
kdDebug(14120) << "Raw:" << m_raw << endl
<< "Prefix:" << m_prefix << endl
<< "Command:" << m_command << endl
<< "Args:" << m_args << endl
<< "Suffix:" << m_suffix << endl
<< "CtcpRaw:" << m_ctcpRaw << endl;
if(m_ctcpMessage)
{
kdDebug(14120) << "Contains CTCP Message:" << endl;
m_ctcpMessage->dump();
}
}
|