summaryrefslogtreecommitdiff
path: root/parsing.c
blob: 54d6078673ceb511162a8574d81ab966be882f80 (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
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
#include <stdio.h>
#include <string.h>
#include <wctype.h>
#include <wchar.h>
#include <pthread.h>
#include <errno.h>

#include "parsing.h"
#include "channel.h"
#include "user.h"
#include "word.h"
#include "export_xml.h"
#include "config.h"

#define NICK_BUFFER_SIZE 0x100
#define TEXT_BUFFER_SIZE 0x400
#define LINE_BUFFER_SIZE 0x400
#define TIME_BUFFER_SIZE 0xf
#define DATE_BUFFER_SIZE 0x20

#define max(a, b) ((a) > (b) ? (a) : (b))

static pthread_mutex_t file_mutex, user_mutex, word_mutex, channel_mutex, time_mutex;

static struct user_t *last_user = NULL;
static int in_monolog = 0, monolog_len = 0;

static struct tm now_global;

static inline void add_word(struct user_t *user, wchar_t *word, int len) {
	pthread_mutex_lock(&user_mutex);
	user->words++;
	pthread_mutex_unlock(&user_mutex);
	word[len] = '\0';
	char mbword[TEXT_BUFFER_SIZE];
	wcstombs(mbword, word, TEXT_BUFFER_SIZE);
	pthread_mutex_lock(&word_mutex);
	struct word_t *word_s = word_get(mbword);
	word_s->count++;
	pthread_mutex_unlock(&word_mutex);
}

static inline char *parse_getline(char *buffer, int bufsize, FILE *f) {
	char *r;

	pthread_mutex_lock(&file_mutex);
	r = fgets(buffer, bufsize, f);
	pthread_mutex_unlock(&file_mutex);

	return r;
}

static void process_file(FILE *f, struct channel_t *channel, struct regexset_t *rs) {
	char line[LINE_BUFFER_SIZE];
	const char *log_date_format, *day_date_format;
	struct tm now;

	if(rs->log_date_format) {
		log_date_format = rs->log_date_format;
	} else if(ircstats_config.log_date_format) {
		log_date_format = ircstats_config.log_date_format;
	} else {
		log_date_format = NULL;
	}

	if(rs->day_date_format) {
		day_date_format = rs->day_date_format;
	} else if(ircstats_config.day_date_format) {
		day_date_format = ircstats_config.day_date_format;
	} else {
		day_date_format = NULL;
	}

	while(parse_getline(line, LINE_BUFFER_SIZE, f)) {
		int rc;
		int ovector[30];

		rc = pcre_exec(rs->text, rs->text_e, line, strlen(line), 0, 0, ovector, 30);
		if(rc > 0) {
			char nick[NICK_BUFFER_SIZE], text[TEXT_BUFFER_SIZE], hour_s[TIME_BUFFER_SIZE], min_s[TIME_BUFFER_SIZE];
			pcre_copy_named_substring(rs->text, line, ovector, rc, "nick", nick, NICK_BUFFER_SIZE);
			pcre_copy_named_substring(rs->text, line, ovector, rc, "text", text, TEXT_BUFFER_SIZE);
			pcre_copy_named_substring(rs->text, line, ovector, rc, "hour", hour_s, TIME_BUFFER_SIZE);
			pcre_copy_named_substring(rs->text, line, ovector, rc, "minute", min_s, TIME_BUFFER_SIZE);
			pthread_mutex_lock(&user_mutex);
			struct user_t *user = user_get(nick);
			if(user == last_user) {
				monolog_len++;
				if(!in_monolog && monolog_len >= ircstats_config.monolog_min) {
					in_monolog = 1;
					user->monologs++;
					/* Count first lines. */
					user->monolog_lines += monolog_len;
				} else if(in_monolog) {
					user->monolog_lines++;
				}
			} else {
				last_user = user;
				in_monolog = 0;
				monolog_len = 1;
			}
			pthread_mutex_unlock(&user_mutex);

			/* Calculate array index for lines. */
			int hour, min, time_i;
			hour = atoi(hour_s);
			min = atoi(min_s);
			time_i = hour*4 + min / 15;

			/* Count words. */
			wchar_t wtext[TEXT_BUFFER_SIZE];
			mbstowcs(wtext, text, TEXT_BUFFER_SIZE);

			pthread_mutex_lock(&time_mutex);
			now = now_global;
			pthread_mutex_unlock(&time_mutex);

			now.tm_hour = hour;
			now.tm_min = min;
			time_t now_ut = mktime(&now);

			pthread_mutex_lock(&user_mutex);
			user->characters += wcslen(wtext);
			user->lines[time_i]++;

			if(user->seen_first == 0 || now_ut < user->seen_first) {
				user->seen_first = now_ut;
			}
			user->seen_last = max(now_ut, user->seen_last);
			pthread_mutex_unlock(&user_mutex);

			pthread_mutex_lock(&channel_mutex);
			channel->hours[time_i]++;
			pthread_mutex_unlock(&channel_mutex);

			wchar_t word[TEXT_BUFFER_SIZE];
			wchar_t *end = wcschr(wtext, '\0');
			*word = '\0';
			int len = 0;
			for(wchar_t *pos = wtext; pos < end; pos++) {
				if(iswblank(*pos)) {
					if(len >= ircstats_config.wordlen_min) {
						add_word(user, word, len);
					}
					len = 0;
					*word = '\0';
				} else if(iswalpha(*pos)) {
					word[len++] = towlower(*pos);
				} else {
					len = 0;
					*word = '\0';
				}
			}
			if(len >= ircstats_config.wordlen_min) {
				add_word(user, word, len);
			}
			continue;
		}

		rc = pcre_exec(rs->join, rs->join_e, line, strlen(line), 0, 0, ovector, 30);
		if(rc > 0) {
			char nick[NICK_BUFFER_SIZE], hour_s[TIME_BUFFER_SIZE], min_s[TIME_BUFFER_SIZE];
			pcre_copy_named_substring(rs->join, line, ovector, rc, "nick", nick, NICK_BUFFER_SIZE);
			pcre_copy_named_substring(rs->join, line, ovector, rc, "hour", hour_s, TIME_BUFFER_SIZE);
			pcre_copy_named_substring(rs->join, line, ovector, rc, "minute", min_s, TIME_BUFFER_SIZE);

			int hour, min;
			hour = atoi(hour_s);
			min = atoi(min_s);

			pthread_mutex_lock(&time_mutex);
			now = now_global;
			pthread_mutex_unlock(&time_mutex);

			now.tm_hour = hour;
			now.tm_min = min;
			time_t now_ut = mktime(&now);

			pthread_mutex_lock(&user_mutex);
			struct user_t *user = user_get(nick);

			if(user->seen_first == 0 || now_ut < user->seen_first) {
				user->seen_first = now_ut;
			}
			user->seen_last = max(now_ut, user->seen_last);
			pthread_mutex_unlock(&user_mutex);

			continue;
		}

		rc = pcre_exec(rs->part, rs->part_e, line, strlen(line), 0, 0, ovector, 30);
		if(rc > 0) {
			char nick[NICK_BUFFER_SIZE], hour_s[TIME_BUFFER_SIZE], min_s[TIME_BUFFER_SIZE];
			pcre_copy_named_substring(rs->part, line, ovector, rc, "nick", nick, NICK_BUFFER_SIZE);
			pcre_copy_named_substring(rs->part, line, ovector, rc, "hour", hour_s, TIME_BUFFER_SIZE);
			pcre_copy_named_substring(rs->part, line, ovector, rc, "minute", min_s, TIME_BUFFER_SIZE);

			int hour, min;
			hour = atoi(hour_s);
			min = atoi(min_s);

			pthread_mutex_lock(&time_mutex);
			now = now_global;
			pthread_mutex_unlock(&time_mutex);

			now.tm_hour = hour;
			now.tm_min = min;
			time_t now_ut = mktime(&now);

			pthread_mutex_lock(&user_mutex);
			struct user_t *user = user_get(nick);

			if(user->seen_first == 0 || now_ut < user->seen_first) {
				user->seen_first = now_ut;
			}
			user->seen_last = max(now_ut, user->seen_last);
			pthread_mutex_unlock(&user_mutex);

			continue;
		}

		rc = pcre_exec(rs->quit, rs->quit_e, line, strlen(line), 0, 0, ovector, 30);
		if(rc > 0) {
			char nick[NICK_BUFFER_SIZE], hour_s[TIME_BUFFER_SIZE], min_s[TIME_BUFFER_SIZE];
			pcre_copy_named_substring(rs->quit, line, ovector, rc, "nick", nick, NICK_BUFFER_SIZE);
			pcre_copy_named_substring(rs->quit, line, ovector, rc, "hour", hour_s, TIME_BUFFER_SIZE);
			pcre_copy_named_substring(rs->quit, line, ovector, rc, "minute", min_s, TIME_BUFFER_SIZE);

			int hour, min;
			hour = atoi(hour_s);
			min = atoi(min_s);

			pthread_mutex_lock(&time_mutex);
			now = now_global;
			pthread_mutex_unlock(&time_mutex);

			now.tm_hour = hour;
			now.tm_min = min;
			time_t now_ut = mktime(&now);

			pthread_mutex_lock(&user_mutex);
			struct user_t *user = user_get(nick);

			if(user->seen_first == 0 || now_ut < user->seen_first) {
				user->seen_first = now_ut;
			}
			user->seen_last = max(now_ut, user->seen_last);
			pthread_mutex_unlock(&user_mutex);

			continue;
		}

		rc = pcre_exec(rs->nick_changed, rs->nick_changed_e, line, strlen(line), 0, 0, ovector, 30);
		if(rc > 0) {
			char newnick[NICK_BUFFER_SIZE], hour_s[TIME_BUFFER_SIZE], min_s[TIME_BUFFER_SIZE];
			pcre_copy_named_substring(rs->nick_changed, line, ovector, rc, "new_nick", newnick, NICK_BUFFER_SIZE);
			pcre_copy_named_substring(rs->nick_changed, line, ovector, rc, "hour", hour_s, TIME_BUFFER_SIZE);
			pcre_copy_named_substring(rs->nick_changed, line, ovector, rc, "minute", min_s, TIME_BUFFER_SIZE);

			int hour, min;
			hour = atoi(hour_s);
			min = atoi(min_s);

			pthread_mutex_lock(&time_mutex);
			now = now_global;
			pthread_mutex_unlock(&time_mutex);

			now.tm_hour = hour;
			now.tm_min = min;
			time_t now_ut = mktime(&now);

			pthread_mutex_lock(&user_mutex);
			struct user_t *user = user_get(newnick);

			if(user->seen_first == 0 || now_ut < user->seen_first) {
				user->seen_first = now_ut;
			}
			user->seen_last = max(now_ut, user->seen_last);
			pthread_mutex_unlock(&user_mutex);

			continue;
		}

		rc = pcre_exec(rs->log_opened, rs->log_opened_e, line, strlen(line), 0, 0, ovector, 30);
		if(rc > 0) {
			char date[DATE_BUFFER_SIZE];

			if(!log_date_format) {
				continue;
			}

			pcre_copy_named_substring(rs->log_opened, line, ovector, rc, "date", date, DATE_BUFFER_SIZE);

			/* TODO: Find a better way around this.
			 * Locking early to allow setting of correct date before any other threads start setting time
			 * before date is set. */
			pthread_mutex_lock(&time_mutex);
			if(!strptime(date, log_date_format, &now)) {
				fprintf(stderr, "log fail: %s\n", date);
				pthread_mutex_unlock(&time_mutex);
				continue;
			}

			now_global = now;
			pthread_mutex_unlock(&time_mutex);

			continue;
		}

		/* day_changed is optional */
		rc = rs->day_changed ? pcre_exec(rs->day_changed, rs->day_changed_e, line, strlen(line), 0, 0, ovector, 30) : 0;
		if(rc > 0) {
			char date[DATE_BUFFER_SIZE];

			if(!day_date_format) {
				continue;
			}

			pcre_copy_named_substring(rs->day_changed, line, ovector, rc, "date", date, DATE_BUFFER_SIZE);

			/* See comment in log_opened parsing. */
			pthread_mutex_lock(&time_mutex);
			if(!strptime(date, day_date_format, &now)) {
				fprintf(stderr, "day fail: %s\n", date);
				pthread_mutex_unlock(&time_mutex);
				continue;
			}

			now_global = now;
			pthread_mutex_unlock(&time_mutex);

			continue;
		}

		rc = pcre_exec(rs->kick, rs->kick_e, line, strlen(line), 0, 0, ovector, 30);
		if(rc > 0) {
			char nick[NICK_BUFFER_SIZE], victim[NICK_BUFFER_SIZE], hour_s[TIME_BUFFER_SIZE], min_s[TIME_BUFFER_SIZE];
			pcre_copy_named_substring(rs->kick, line, ovector, rc, "hour", hour_s, TIME_BUFFER_SIZE);
			pcre_copy_named_substring(rs->kick, line, ovector, rc, "minute", min_s, TIME_BUFFER_SIZE);
			pcre_copy_named_substring(rs->kick, line, ovector, rc, "nick", nick, NICK_BUFFER_SIZE);
			pcre_copy_named_substring(rs->kick, line, ovector, rc, "victim", victim, NICK_BUFFER_SIZE);

			int hour, min;
			hour = atoi(hour_s);
			min = atoi(min_s);

			pthread_mutex_lock(&time_mutex);
			now = now_global;
			pthread_mutex_unlock(&time_mutex);

			now.tm_hour = hour;
			now.tm_min = min;
			time_t now_ut = mktime(&now);

			pthread_mutex_lock(&user_mutex);
			struct user_t *user = user_get(nick),
				*victim_user = user_get(victim);
			user->kicks++;
			victim_user->kicked++;

			if(user->seen_first == 0 || now_ut < user->seen_first) {
				user->seen_first = now_ut;
			}
			user->seen_last = max(now_ut, user->seen_last);
			pthread_mutex_unlock(&user_mutex);

			continue;
		}
	}
}

struct thread_arg_t {
	FILE *f;
	struct channel_t *channel;
	struct regexset_t *rs;
};

static void *thread_func(void *arg) {
	struct thread_arg_t *ta = arg;
	process_file(ta->f, ta->channel, ta->rs);

	return NULL;
}

void process(int thread_n) {
	pthread_mutex_init(&file_mutex, NULL);
	pthread_mutex_init(&user_mutex, NULL);
	pthread_mutex_init(&word_mutex, NULL);
	pthread_mutex_init(&channel_mutex, NULL);
	pthread_mutex_init(&time_mutex, NULL);
	/* Parsing stuff goes here. */
	for(int chan_i = 0; chan_i < channel_get_count(); chan_i++) {
		user_init();
		word_init();
		struct channel_t *channel = channel_get(chan_i);
		printf("Channel %s\n", channel->name);
		struct channel_file_t *file = channel->files;
		while(file) {
			struct regexset_t *rs = file->rs;
			FILE *f = fopen(file->path, "r");
			if(!f) {
				fprintf(stderr, "\tFailed to open %s\n", file->path);
				file = file->next;
				continue;
			} else
				printf("\tParsing %s\n", file->path);

			last_user = NULL;
			in_monolog = monolog_len = 0;

			pthread_t *threads;
			threads = malloc(sizeof(pthread_t) * thread_n);
			struct thread_arg_t ta;
			ta.f = f;
			ta.channel = channel;
			ta.rs = rs;
			for(int i = 0; i < thread_n; i++) {
				pthread_create(&threads[i], NULL, thread_func, &ta);
			}
			for(int i = 0; i < thread_n; i++) {
				pthread_join(threads[i], NULL);
			}
			free(threads);

			fclose(f);
			file = file->next;
		}

		export_xml(channel, users);
		user_free();
		word_free();
	}
	pthread_mutex_destroy(&file_mutex);
	pthread_mutex_destroy(&time_mutex);
	pthread_mutex_destroy(&user_mutex);
	pthread_mutex_destroy(&word_mutex);
	pthread_mutex_destroy(&channel_mutex);
}