Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 122 lines (107 sloc) 4.849 kb
6afcbc0 @tommyrot add parser support for LimeChat
authored
1 <?php
2
3 /**
4 * Copyright (c) 2012, Jos de Ruijter <jos@dutnie.nl>
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 /**
20 * Parse instructions for the LimeChat logfile format.
21 *
22 * +------------+-------------------------------------------------------+->
23 * | Line | Format | Notes
24 * +------------+-------------------------------------------------------+->
25 * | Normal | NICK: MSG | Skip empty lines.
26 * | Nickchange | NICK is now known as NICK |
27 * | Join | NICK has joined (HOST) |
28 * | Part | NICK has left (MSG) | Part message may be empty due to normalization.
29 * | Quit | NICK has left IRC (MSG) | Quit message may be empty due to normalization. IRC is literal.
30 * | Mode | NICK has changed mode: +o-v NICK NICK | Only check for combinations of ops (+o) and voices (+v).
31 * | Topic | NICK has set topic: MSG | Skip empty topics.
32 * | Kick | NICK has kicked NICK (MSG) | Kick message may be empty due to normalization.
33 * +------------+-------------------------------------------------------+->
34 *
35 * Notes:
36 * - normalize_line() scrubs all lines before passing them on to parse_line().
37 * - LimeChat uses the same syntax for actions as "normal" lines. This makes the two indistinguishable. Pretty dumb.
38 * - Given that nicks can't contain ":" the order of the regular expressions below is irrelevant (current order aims for best performance).
39 */
40 final class parser_limechat extends parser
41 {
42 /**
43 * Variables that shouldn't be tampered with.
44 */
45 private $repeatlock = false;
46
47 /**
48 * Parse a line for various chat data.
49 */
50 protected function parse_line($line)
51 {
52 /**
53 * "Normal" lines.
54 */
55 if (preg_match('/^(?<time>\d{2}:\d{2}(:\d{2})?) (?<nick>\S+): (?<line>.+)$/', $line, $matches)) {
56 $this->set_normal($this->date.' '.$matches['time'], $matches['nick'], $matches['line']);
57
58 /**
59 * "Join" lines.
60 */
61 } elseif (preg_match('/^(?<time>\d{2}:\d{2}(:\d{2})?) (?<nick>\S+) has joined \(\S+\)$/', $line, $matches)) {
62 $this->set_join($this->date.' '.$matches['time'], $matches['nick']);
63
64 /**
65 * "Quit" lines.
66 */
67 } elseif (preg_match('/^(?<time>\d{2}:\d{2}(:\d{2})?) (?<nick>\S+) has left IRC \(.*\)$/', $line, $matches)) {
68 $this->set_quit($this->date.' '.$matches['time'], $matches['nick']);
69
70 /**
71 * "Mode" lines.
72 */
73 } elseif (preg_match('/^(?<time>\d{2}:\d{2}(:\d{2})?) (?<nick_performing>\S+) has changed mode: (?<modes>[-+][ov]+([-+][ov]+)?) (?<nicks_undergoing>\S+( \S+)*)$/', $line, $matches)) {
74 $nicks_undergoing = explode(' ', $matches['nicks_undergoing']);
75 $modenum = 0;
76
77 for ($i = 0, $j = strlen($matches['modes']); $i < $j; $i++) {
78 $mode = substr($matches['modes'], $i, 1);
79
80 if ($mode == '-' || $mode == '+') {
81 $modesign = $mode;
82 } else {
83 $this->set_mode($this->date.' '.$matches['time'], $matches['nick_performing'], $nicks_undergoing[$modenum], $modesign.$mode);
84 $modenum++;
85 }
86 }
87
88 /**
89 * "Nickchange" lines.
90 */
91 } elseif (preg_match('/^(?<time>\d{2}:\d{2}(:\d{2})?) (?<nick_performing>\S+) is now known as (?<nick_undergoing>\S+)$/', $line, $matches)) {
92 $this->set_nickchange($this->date.' '.$matches['time'], $matches['nick_performing'], $matches['nick_undergoing']);
93
94 /**
95 * "Part" lines.
96 */
97 } elseif (preg_match('/^(?<time>\d{2}:\d{2}(:\d{2})?) (?<nick>\S+) has left \(.*\)$/', $line, $matches)) {
98 $this->set_part($this->date.' '.$matches['time'], $matches['nick']);
99
100 /**
101 * "Topic" lines.
102 */
103 } elseif (preg_match('/^(?<time>\d{2}:\d{2}(:\d{2})?) (?<nick>\S+) has set topic: (?<line>.+)$/', $line, $matches)) {
104 $this->set_topic($this->date.' '.$matches['time'], $matches['nick'], $matches['line']);
105
106 /**
107 * "Kick" lines.
108 */
109 } elseif (preg_match('/^(?<time>\d{2}:\d{2}(:\d{2})?) (?<line>(?<nick_performing>\S+) has kicked (?<nick_undergoing>\S+) \(.*\))$/', $line, $matches)) {
110 $this->set_kick($this->date.' '.$matches['time'], $matches['nick_performing'], $matches['nick_undergoing'], $matches['line']);
111
112 /**
113 * Skip everything else.
114 */
115 } elseif ($line != '') {
116 $this->output('debug', 'parse_line(): skipping line '.$this->linenum.': \''.$line.'\'');
117 }
118 }
119 }
120
121 ?>
Something went wrong with that request. Please try again.