[go: up one dir, main page]

Menu

[r456]: / tags / miau-0-6-1 / src / qlog.c  Maximize  Restore  History

Download this file

367 lines (302 with data), 7.7 kB

  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
/* $Id$
* -------------------------------------------------------
* Copyright (C) 2003-2005 Tommi Saviranta <wnd@iki.fi>
* -------------------------------------------------------
* 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.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif /* ifdef HAVE_CONFIG_H */
#ifdef QUICKLOG
#include "qlog.h"
#include "client.h"
#include "irc.h"
#include "list.h"
#include "llist.h"
#include "miau.h"
#include "tools.h"
#include "commands.h"
#include "error.h"
#include "common.h"
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
static list_type *qlog = NULL;
#ifdef QLOGSTAMP
static const char *qlog_add_timestamp(qlogentry *entry, char *buf,
size_t size);
#endif /* QLOGSTAMP */
static channel_type *qlog_get_channel(const char *msg);
/*
* Walk thru qlog to see if any of active channels has some log to be replayed.
* Basically this could be done while writing qlog, but this would be waste of
* CPU-time as qlog can be checked just as well before replaying.
*/
void
qlog_check(void)
{
llist_node *iterl;
list_type *iter;
/* First set each channel not to have qlog. */
for (iterl = active_channels.head; iterl != NULL; iterl = iterl->next) {
channel_type *chan;
chan = (channel_type *) iterl->data;
chan->hasqlog = 0;
}
for (iterl = old_channels.head; iterl != NULL; iterl = iterl->next) {
channel_type *chan;
chan = (channel_type *) iterl->data;
chan->hasqlog = 0;
}
for (iter = qlog; iter != NULL; iter = iter->next) {
qlogentry *entry;
entry = (qlogentry *) iter->data;
/* Don't waste time looking for channel if line's privmsg. */
#ifdef INBOX
if (entry->privmsg == 0)
#endif /* ifdef else INBOX */
{
channel_type *chan;
chan = qlog_get_channel(entry->text);
if (chan != NULL) {
chan->hasqlog = 1;
}
}
}
} /* void qlog_check(void) */
/*
* (Replay and) clean quicklog data.
*/
void
qlog_replay(connection_type *client, int keep)
{
list_type *iter;
list_type *next;
#ifdef QLOGSTAMP
char qlogbuf[IRC_MSGLEN];
#endif /* QLOGSTAMP */
qlog_drop_old();
/* Walk thru quicklog. */
for (iter = qlog; iter != NULL; ) { /* handle next at the end */
qlogentry *entry;
next = iter->next;
entry = (qlogentry *) iter->data;
if (client != NULL) {
#ifdef QLOGSTAMP
if (cfg.timestamp != TS_NONE) {
const char *out;
out = qlog_add_timestamp(entry, qlogbuf,
IRC_MSGLEN);
irc_write(client, "%s", out);
} else {
irc_write(client, "%s", entry->text);
}
#else /* ifdef QLOGSTAMP */
irc_write(client, "%s", entry->text);
#endif /* ifdef else QLOGSTAMP */
}
if (keep == 0) {
xfree(entry->text);
xfree(entry);
qlog = list_delete(qlog, iter);
}
iter = next;
}
} /* void qlog_replay(connection_type *client, int keep) */
#ifdef QLOGSTAMP
/*
* Add timestamp in qlogentry.
*/
static const char *
qlog_add_timestamp(qlogentry *entry, char *buf, size_t size)
{
int cmd, i;
char *p;
/* TSLEN: "[HH:MM:SS]\0" == 11 */
#define TSLEN 11
char stamp[TSLEN];
/* attach tag only if we know what we're doing */
p = nextword(entry->text);
if (p == NULL) {
return entry->text;
}
/* Next find out what command it was. */
i = pos(p, ' ');
if (i != -1 && i < TSLEN) {
char tmp[18];
strncpy(tmp, p, i);
tmp[i] = '\0';
cmd = command_find(tmp);
} else {
return entry->text;
}
/* Is this something we can handle? */
if (cmd != CMD_PRIVMSG && cmd != CMD_NOTICE && cmd != CMD_QUIT &&
cmd != CMD_PART && cmd != CMD_KICK && cmd != CMD_KILL) {
return entry->text;
}
switch (cfg.timestamp) {
case TS_BEGINNING:
{
char rep;
p = strchr(entry->text + 1, (int) ':');
if (p != NULL) {
if (p[1] == '\0') {
p = NULL;
} else if (p[1] == '\1') {
p = strchr(p, (int) ' ');
}
}
/*
* Confused? Don't break already broken things any
* further.
*/
if (p == NULL) {
return entry->text;
}
rep = *p;
*p = '\0'; /* ugly, but makes life easier */
strftime(stamp, TSLEN, "[%H:%M:%S]",
localtime(&entry->timestamp));
snprintf(buf, size, "%s%c%s %s",
entry->text, rep, stamp, p + 1);
buf[size - 1] = '\0';
*p = rep;
return buf;
}
case TS_END:
{
int add_one;
int len;
len = strlen(entry->text);
if (entry->text[len - 1] == '\1') {
entry->text[len - 1] = '\0';
add_one = 1;
} else {
add_one = 0;
}
strftime(stamp, TSLEN, "[%H:%M:%S]",
localtime(&entry->timestamp));
snprintf(buf, size, "%s %s%s", entry->text,
stamp, add_one == 1 ? "\1" : "");
buf[size - 1] = '\0';
return buf;
}
default:
return entry->text;
}
} /* static const char *qlog_add_timestamp(qlogentry *entry, char *buf,
size_t size) */
#endif /* ifdef QLOGSTAMP */
/*
* Remove old lines from quicklog.
*/
void
qlog_drop_old(void)
{
time_t oldest;
if (qlog == NULL) {
return;
}
oldest = time(NULL) - (cfg.qloglength * 60);
while (qlog != NULL) { /* secondary exit condition */
qlogentry *entry;
entry = (qlogentry *) qlog->data;
/* primary exit condition */
if (entry->timestamp > oldest) {
break;
}
#ifdef INBOX
if (entry->privmsg == 1) {
char *message;
/* Get sender (split it) and beginning of payload. */
message = strchr(entry->text + 1, (int) ':');
if (message == NULL) {
#ifdef ENDUSERDEBUG
enduserdebug("converting invalid qlog-line?");
enduserdebug("%s", entry->text);
#endif /* ifdef ENDUSERDEBUG */
goto drop_free;
}
strtok(entry->text, " ");
if (entry->text[0] == '\0' || message[0] == '\0') {
#ifdef ENDUSERDEBUG
enduserdebug("invalid stuff in qlog");
#endif /* ifdef else ENDUSERDEBUG */
goto drop_free;
}
/* termination and validity guaranteed */
fprintf(inbox, "%s <%s> %s\n",
get_timestamp(&entry->timestamp,
TIMESTAMP_SHORT),
entry->text + 1, message + 1);
fflush(inbox);
}
#endif /* INBOX */
drop_free:
xfree(entry->text);
xfree(entry);
qlog = list_delete(qlog, qlog);
}
} /* void qlog_drop_old(void) */
/*
* Write lines to quick log.
*/
void
qlog_write(const int privmsg, char *format, ...)
{
qlogentry *line;
va_list va;
char buf[BUFFERSIZE];
/* First remove possible outdated lines. */
qlog_drop_old();
va_start(va, format);
vsnprintf(buf, IRC_MSGLEN - 2, format, va);
va_end(va);
buf[IRC_MSGLEN - 3] = '\0';
/* Create new line of quicklog. */
line = (qlogentry *) xmalloc(sizeof(qlogentry));
time(&line->timestamp);
line->text = xstrdup(buf);
#ifdef INBOX
line->privmsg = privmsg;
#endif /* ifdef INBOX */
qlog = list_add_tail(qlog, line);
} /* void qlog_write(const int privmsg, char *format, ...) */
static channel_type *
qlog_get_channel(const char *msg)
{
channel_type *chan;
char *b;
chan = NULL;
b = strchr(msg, (int) ' ');
if (b != NULL) {
b = strchr(b + 1, (int) ' ');
if (b != NULL) {
int l;
l = pos(b + 1, ' ');
if (l != -1) {
char *t;
t = (char *) xmalloc(l + 1);
memcpy(t, b + 1, l);
t[l] = '\0';
/* Check active/old_channels. */
chan = channel_find(t, LIST_ACTIVE);
if (chan == NULL) {
chan = channel_find(t, LIST_OLD);
}
xfree(t);
}
}
}
return chan;
} /* static channel_type *qlog_get_channel(const char *msg) */
#endif /* ifdef QUICKLOG */