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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
|
//=============================================================================
//
// File : LogFile.cpp
// Creation date : Thu Apr 14 2011 19:21:59 by Elvio Basello
//
// This file is part of the KVIrc IRC client distribution
// Copyright (C) 2011 Elvio Basello (hellvis69 at gmail dot com)
//
// 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
//
//=============================================================================
#include "LogFile.h"
#include "kvi_settings.h"
#include "KviApplication.h"
#include "KviControlCodes.h"
#include "KviQString.h"
#include "KviCString.h"
#include "KviHtmlGenerator.h"
#include "KviIconManager.h"
#include "KviLocale.h"
#include "KviOptions.h"
#include "KviFileUtils.h"
#include "KviRegExp.h"
#include <QFileInfo>
#include <QDir>
#include <QTextStream>
#include <QLocale>
#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
#include <QTextCodec>
#else
#include <QStringConverter>
#endif
#ifdef COMPILE_ZLIB_SUPPORT
#include <zlib.h>
#endif
LogFile::LogFile(const QString & szName)
{
m_szFilename = szName;
QFileInfo fi(m_szFilename);
QString szTmpName = fi.fileName();
m_bCompressed = (fi.suffix() == "gz");
if(m_bCompressed)
{
//removes trailing dot and extension
szTmpName.chop(3);
}
QString szTypeToken = szTmpName.section('_', 0, 0);
// Ignore non-logs files, this includes '.' and '..'
if(KviQString::equalCI(szTypeToken, "channel") || KviQString::equalCI(szTypeToken, "deadchannel"))
{
m_szType = "channel";
m_eType = Channel;
}
else if(KviQString::equalCI(szTypeToken, "console"))
{
m_szType = "console";
m_eType = Console;
}
else if(KviQString::equalCI(szTypeToken, "query"))
{
m_szType = "query";
m_eType = Query;
}
else if(KviQString::equalCI(szTypeToken, "dccchat"))
{
m_szType = "dccchat";
m_eType = DccChat;
}
else
{
m_szType = "";
m_eType = Other;
}
KviCString szUndecoded = szTmpName.section('.', 0, 0);
szUndecoded.cutToFirst('_');
m_szName = szUndecoded.hexDecode(szUndecoded.ptr()).ptr();
szUndecoded = szTmpName.section('.', 1).section('_', 0, -2);
m_szNetwork = szUndecoded.hexDecode(szUndecoded.ptr()).ptr();
QString szDate = szTmpName.section('_', -1).section('.', 0, -2);
switch(KVI_OPTION_UINT(KviOption_uintOutputDatetimeFormat))
{
case 1:
m_date = QDate::fromString(szDate, Qt::ISODate);
break;
case 2:
m_date = QLocale().toDate(szDate, QLocale::ShortFormat);
if(!m_date.isValid())
{
// some locale date formats use '/' as a separator; we change them to '-'
// when creating log files. Try to reverse that change here
QString szUnescapedDate = szDate;
szUnescapedDate.replace('-', KVI_PATH_SEPARATOR_CHAR);
m_date = QLocale().toDate(szUnescapedDate, QLocale::ShortFormat);
if(m_date.isValid())
{
//qt4 defaults to 1900 for years. So "11" means "1911" instead of "2011".. what a pity
if(m_date.year() < 1990)
m_date = m_date.addYears(100);
}
}
break;
case 0:
default:
m_date = QDate::fromString(szDate, "yyyy.MM.dd");
break;
}
if(!m_date.isValid())
{
// probably the log has been created when the OutputDatetimeFormat option
// was set to a different value. Try to guess its format
m_date = QDate::fromString(szDate, "yyyy.MM.dd");
if(!m_date.isValid())
{
m_date = QDate::fromString(szDate, Qt::ISODate);
if(!m_date.isValid())
{
m_date = QLocale().toDate(szDate, QLocale::ShortFormat);
if(!m_date.isValid())
{
// some locale date formats use '/' as a separator; we change them to '-'
// when creating log files. Try to reverse that change here
QString szUnescapedDate = szDate;
szUnescapedDate.replace('-', KVI_PATH_SEPARATOR_CHAR);
m_date = QLocale().toDate(szUnescapedDate, QLocale::ShortFormat);
if(m_date.isValid())
{
//qt4 defaults to 1900 for years. So "11" means "1911" instead of "2011".. what a pity
if(m_date.year() < 1990)
m_date = m_date.addYears(100);
}
}
// if the date is still not valid, we give up
}
}
}
}
void LogFile::getText(QString & szText) const
{
QString szLogName = fileName();
QFile logFile;
#ifdef COMPILE_ZLIB_SUPPORT
if(m_bCompressed)
{
gzFile file = gzopen(szLogName.toLocal8Bit().data(), "rb");
if(file)
{
char cBuff[1025];
int iLen;
QByteArray data;
//QCString data;
iLen = gzread(file, cBuff, 1024);
while(iLen > 0)
{
cBuff[iLen] = 0;
data.append(cBuff);
iLen = gzread(file, cBuff, 1024);
}
gzclose(file);
szText = QString::fromUtf8(data);
}
else
{
qDebug("Can't open compressed file %s", szLogName.toLocal8Bit().data());
}
}
else
{
#endif
logFile.setFileName(szLogName);
if(!logFile.open(QIODevice::ReadOnly))
return;
QByteArray bytes;
bytes = logFile.readAll();
szText = QString::fromUtf8(bytes.data(), bytes.size());
logFile.close();
#ifdef COMPILE_ZLIB_SUPPORT
}
#endif
}
void LogFile::createLog(ExportType exportType, QString szLog, QString * pszFile) const
{
KviRegExp rx;
QString szLogDir, szInputBuffer, szOutputBuffer, szLine, szTmp;
QString szDate = date().toString("yyyy.MM.dd");
/* Save export directory - this directory path is also used in the HTML export
* and info is used when working with pszFile */
QFileInfo info(szLog);
szLogDir = info.absoluteDir().absolutePath() + QString(KVI_PATH_SEPARATOR_CHAR);
/* Reading in log file - LogFiles are read in as bytes, so '\r' isn't
* sanitised by default */
getText(szInputBuffer);
QStringList lines = szInputBuffer.replace('\r', "").split('\n');
switch(exportType)
{
case LogFile::PlainText:
{
/* Only append extension if it isn't there already (e.g. a specific
* file is to be overwritten) */
if(!szLog.endsWith(".txt"))
szLog += ".txt";
// Scan the file
for(auto & line : lines)
{
szTmp = line;
szLine = KviControlCodes::stripControlBytes(szTmp);
// Remove icons' code
rx.setPattern("^\\d{1,3}\\s");
szLine.replace(rx, "");
// Remove link from a user speaking, deal with (and keep) various ranks
// e.g.: <!ncHelLViS69> --> <HelLViS69>
rx.setPattern("\\s<([+%@&~!]?)!nc");
szLine.replace(rx, " <\\1");
// Remove link from a nick in a mask
// e.g.: !nFoo [~bar@!hfoo.bar] --> Foo [~bar@!hfoo.bar]
rx.setPattern("\\s!n");
szLine.replace(rx, " ");
// Remove link from a host in a mask
// e.g.: Foo [~bar@!hfoo.bar] --> Foo [~bar@foo.bar]
rx.setPattern("@!h");
szLine.replace(rx, "@");
// Remove link from a channel
// e.g.: !c#KVIrc --> #KVIrc
rx.setPattern("!c#");
szLine.replace(rx, "#");
szOutputBuffer += szLine;
szOutputBuffer += "\n";
}
break;
}
case LogFile::HTML:
{
/* Only append extension if it isn't there already (e.g. a specific
* file is to be overwritten) */
if(!szLog.endsWith(".html"))
szLog += ".html";
szTmp = QString("KVIrc %1 %2").arg(KVI_VERSION).arg(KVI_RELEASE_NAME);
QString szNick = "";
bool bFirstLine = true;
QString szTitle;
switch(type())
{
case LogFile::Channel:
szTitle = __tr2qs_ctx("Channel %1 on %2", "log").arg(name(), network());
break;
case LogFile::Console:
szTitle = __tr2qs_ctx("Console on %1", "log").arg(network());
break;
case LogFile::Query:
szTitle = __tr2qs_ctx("Query with: %1 on %2", "log").arg(name(), network());
break;
case LogFile::DccChat:
szTitle = __tr2qs_ctx("DCC Chat with: %1", "log").arg(name());
break;
case LogFile::Other:
szTitle = __tr2qs_ctx("Something on: %1", "log").arg(network());
break;
}
// Prepare HTML document
szOutputBuffer += "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\" \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">\n";
szOutputBuffer += "<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\">\n";
szOutputBuffer += "<head>\n";
szOutputBuffer += "\t<meta http-equiv=\"content-type\" content=\"application/xhtml+xml; charset=utf-8\" />\n";
szOutputBuffer += "\t<meta name=\"author\" content=\"" + szTmp + "\" />\n";
szOutputBuffer += "\t<title>" + szTitle + "</title>\n";
szOutputBuffer += "</head>\n<body>\n";
szOutputBuffer += "<h2>" + szTitle + "</h2>\n<h3>Date: " + szDate + "</h3>\n";
// Scan the file
for(auto & line : lines)
{
szTmp = line;
// Find who has talked
QString szTmpNick = szTmp.section(" ", 2, 2);
if((szTmpNick.left(1) != "<") && (szTmpNick.right(1) != ">"))
szTmpNick = "";
// locate msgtype
QString szNum = szTmp.section(' ', 0, 0);
bool bOk;
int iMsgType = szNum.toInt(&bOk);
// only human text for now...
if(iMsgType != 24 && iMsgType != 25 && iMsgType != 26)
continue;
// remove msgtype tag
szTmp = szTmp.remove(0, szNum.length() + 1);
szTmp = KviHtmlGenerator::convertToHtml(szTmp, true);
// insert msgtype icon at start of the current text line
KviMessageTypeSettings msg(KVI_OPTION_MSGTYPE(iMsgType));
QString szIcon = g_pIconManager->getSmallIconResourceName((KviIconManager::SmallIcon)msg.pixId());
szTmp.prepend("<img src=\"" + szIcon + R"(" alt="" /> )");
/*
* Check if the nick who has talked is the same of the above line.
* If so, we have to put the line as it is, otherwise we have to
* open a new paragraph
*/
if(szTmpNick != szNick)
{
/*
* People is not the same, close the paragraph opened
* before and open a new one
*/
if(!bFirstLine)
szOutputBuffer += "</p>\n";
szTmp.prepend("<p>");
szNick = szTmpNick;
}
else
{
// Break the line
szTmp.prepend("<br />\n");
}
// remove internal tags before nickname
rx.setPattern(">([+%@&~!]?)!nc");
szTmp.replace(rx, ">\\1");
szTmp.replace(">&!nc", ">&");
szOutputBuffer += szTmp;
bFirstLine = false;
}
// Close the last paragraph
szOutputBuffer += "</p>\n";
// regexp to search all embedded icons
rx.setPattern("<img src=\"smallicons:([^\"]+)");
int iIndex = szOutputBuffer.indexOf(rx);
QStringList szImagesList;
// search for icons
while(iIndex >= 0)
{
int iLength = rx.matchedLength();
QString szCap = rx.cap(1);
// if the icon isn't in the images list then add
if(szImagesList.indexOf(szCap) == -1)
szImagesList.append(szCap);
iIndex = szOutputBuffer.indexOf(rx, iIndex + iLength);
}
// get current theme path
QString szCurrentThemePath;
g_pApp->getLocalKvircDirectory(szCurrentThemePath, KviApplication::Themes, KVI_OPTION_STRING(KviOption_stringIconThemeSubdir));
szCurrentThemePath += KVI_PATH_SEPARATOR_CHAR;
// current coresmall path
szCurrentThemePath += "coresmall";
szCurrentThemePath += KVI_PATH_SEPARATOR_CHAR;
// check if coresmall exists in current theme
if(!KviFileUtils::directoryExists(szCurrentThemePath))
{
// get global coresmall path
g_pApp->getGlobalKvircDirectory(szCurrentThemePath, KviApplication::Pics, "coresmall");
KviQString::ensureLastCharIs(szCurrentThemePath, QChar(KVI_PATH_SEPARATOR_CHAR));
}
// copy all icons to the log destination folder
for(int i = 0; i < szImagesList.count(); i++)
{
QString szSourceFile = szCurrentThemePath + szImagesList.at(i);
QString szDestFile = szLogDir + szImagesList.at(i);
KviFileUtils::copyFile(szSourceFile, szDestFile);
}
// remove internal tags
rx.setPattern("<qt>|</qt>|smallicons:");
szOutputBuffer.replace(rx, "");
// Close the document
szOutputBuffer += "</body>\n</html>\n";
break;
}
}
// File overwriting already dealt with when file path was obtained
QFile log(szLog);
if(!log.open(QIODevice::WriteOnly | QIODevice::Text))
return;
if(pszFile)
{
*pszFile = "";
*pszFile = info.filePath();
}
// Ensure we're writing in UTF-8
QTextStream output(&log);
#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
output.setCodec(QTextCodec::codecForMib(106));
#else
output.setEncoding(QStringConverter::Utf8);
#endif
output << szOutputBuffer;
// Close file descriptors
log.close();
}
|