Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 124 lines (109 sloc) 5.192 kb
203d81e @tommyrot add parser support for HexChat
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 HexChat 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 (HOST) has joined CHAN |
28 * | Part | * NICK (HOST) has left CHAN (MSG) | Part message may be absent, or empty due to normalization.
29 * | Quit | * NICK has quit (MSG) | Quit message may be empty due to normalization.
30 * | Mode | * NICK gives channel operator status to NICK NICK | Only check for ops (channel operator status) and voices.
2d3308a @tommyrot make things clearer
authored
31 * | Mode | * NICK removes voice from NICK NICK | "
203d81e @tommyrot add parser support for HexChat
authored
32 * | Topic | * NICK has changed the topic to: MSG | Skip empty topics.
33 * | Kick | * NICK has kicked NICK from CHAN (MSG) | Kick message may be empty due to normalization.
34 * +------------+-------------------------------------------------------+->
35 *
36 * Notes:
37 * - normalize_line() scrubs all lines before passing them on to parse_line().
38 * - The way HexChat logs actions is pretty dumb, we can spoof nearly all other line types with our actions. Even non-chat messages are logged with the same
39 * syntax. For this reason we won't parse for actions.
2d3308a @tommyrot make things clearer
authored
40 * - The order of the regular expressions below is irrelevant (current order aims for best performance).
203d81e @tommyrot add parser support for HexChat
authored
41 * - The most common channel prefixes are "#&!+".
42 */
43 final class parser_hexchat extends parser
44 {
45 /**
46 * Parse a line for various chat data.
47 */
48 protected function parse_line($line)
49 {
50 /**
51 * "Normal" lines.
52 */
53 if (preg_match('/^\S{3} \d{2} (?<time>\d{2}:\d{2}(:\d{2})?) <(?<nick>\S+)> (?<line>.+)$/', $line, $matches)) {
54 $this->set_normal($this->date.' '.$matches['time'], $matches['nick'], $matches['line']);
55
56 /**
57 * "Join" lines.
58 */
59 } elseif (preg_match('/^\S{3} \d{2} (?<time>\d{2}:\d{2}(:\d{2})?) \* (?<nick>\S+) \(\S+\) has joined [#&!+]\S+$/', $line, $matches)) {
60 $this->set_join($this->date.' '.$matches['time'], $matches['nick']);
61
62 /**
63 * "Quit" lines.
64 */
65 } elseif (preg_match('/^\S{3} \d{2} (?<time>\d{2}:\d{2}(:\d{2})?) \* (?<nick>\S+) has quit \(.*\)$/', $line, $matches)) {
66 $this->set_quit($this->date.' '.$matches['time'], $matches['nick']);
67
68 /**
69 * "Mode" lines.
70 */
71 } elseif (preg_match('/^\S{3} \d{2} (?<time>\d{2}:\d{2}(:\d{2})?) \* (?<nick_performing>\S+) (?<modesign>gives|removes) (?<mode>channel operator status|voice) (to|from) (?<nicks_undergoing>\S+( \S+)*)$/', $line, $matches)) {
72 $nicks_undergoing = explode(' ', $matches['nicks_undergoing']);
73
74 if ($matches['modesign'] == 'gives') {
75 $modesign = '+';
76 } else {
77 $modesign = '-';
78 }
79
80 if ($matches['mode'] == 'channel operator status') {
81 $mode = 'o';
82 } else {
83 $mode = 'v';
84 }
85
86 foreach ($nicks_undergoing as $nick_undergoing) {
87 $this->set_mode($this->date.' '.$matches['time'], $matches['nick_performing'], $nick_undergoing, $modesign.$mode);
88 }
89
90 /**
91 * "Nickchange" lines.
92 */
93 } elseif (preg_match('/^\S{3} \d{2} (?<time>\d{2}:\d{2}(:\d{2})?) \* (?<nick_performing>\S+) is now known as (?<nick_undergoing>\S+)$/', $line, $matches)) {
94 $this->set_nickchange($this->date.' '.$matches['time'], $matches['nick_performing'], $matches['nick_undergoing']);
95
96 /**
97 * "Part" lines.
98 */
99 } elseif (preg_match('/^\S{3} \d{2} (?<time>\d{2}:\d{2}(:\d{2})?) \* (?<nick>\S+) \(\S+\) has left [#&!+]\S+( \(.*\))?$/', $line, $matches)) {
100 $this->set_part($this->date.' '.$matches['time'], $matches['nick']);
101
102 /**
103 * "Topic" lines.
104 */
105 } elseif (preg_match('/^\S{3} \d{2} (?<time>\d{2}:\d{2}(:\d{2})?) \* (?<nick>\S+) has changed the topic to: (?<line>.+)$/', $line, $matches)) {
106 $this->set_topic($this->date.' '.$matches['time'], $matches['nick'], $matches['line']);
107
108 /**
109 * "Kick" lines.
110 */
111 } elseif (preg_match('/^\S{3} \d{2} (?<time>\d{2}:\d{2}(:\d{2})?) \* (?<line>(?<nick_performing>\S+) has kicked (?<nick_undergoing>\S+) from [#&!+]\S+ \(.*\))$/', $line, $matches)) {
112 $this->set_kick($this->date.' '.$matches['time'], $matches['nick_performing'], $matches['nick_undergoing'], $matches['line']);
113
114 /**
115 * Skip everything else.
116 */
117 } elseif ($line != '') {
118 $this->output('debug', 'parse_line(): skipping line '.$this->linenum.': \''.$line.'\'');
119 }
120 }
121 }
122
123 ?>
Something went wrong with that request. Please try again.