-
Notifications
You must be signed in to change notification settings - Fork 1
/
feed_functions.php
161 lines (150 loc) · 6.25 KB
/
feed_functions.php
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
<?php
namespace CluebotNG;
/*
* Copyright (C) 2015 Jacobi Carter and Chris Breneman
*
* This file is part of ClueBot NG.
*
* ClueBot NG 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.
*
* ClueBot NG 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 ClueBot NG. If not, see <http://www.gnu.org/licenses/>.
*/
class Feed
{
public static $host = 'irc.wikimedia.org';
public static $port = 6667;
public static $channel = '#en.wikipedia';
private static $fd;
public static $wlTimer;
public static function connectLoop()
{
self::$fd = fsockopen(self::$host, self::$port, $feederrno, $feederrstr, 30);
if (!self::$fd) {
return;
}
$nick = str_replace(' ', '_', Config::$user);
self::send('USER ' . $nick . ' "1" "1" :ClueBot Wikipedia Bot 2.0.');
self::send('NICK ' . $nick);
while (!feof(self::$fd)) {
$rawline = fgets(self::$fd, 1024);
$line = str_replace(array("\n", "\r"), '', $rawline);
self::loop($line);
}
}
public static function send($line)
{
fwrite(self::$fd, $line . "\n");
}
private static function loop($line)
{
global $logger;
$d = IRC::split($line);
if ($d === null) {
return;
}
if ($d['type'] == 'direct') {
switch ($d['command']) {
case 'ping':
self::send('PONG :' . $d['pieces'][0]);
break;
}
} else {
switch ($d['command']) {
case '376':
case '422':
self::send('JOIN ' . self::$channel);
break;
case 'privmsg':
if (strtolower($d['target']) == self::$channel) {
$rawmessage = $d['pieces'][0];
$message = str_replace("\002", '', $rawmessage);
$message = preg_replace('/\003(\d\d?(,\d\d?)?)?/', '', $message);
$data = parseFeed($message);
if ($data === false) {
return;
}
$data['line'] = $message;
$data['rawline'] = $rawmessage;
if (stripos('N', $data['flags']) !== false) {
self::bail($data, 'New article');
return;
}
$stalkchannel = array();
foreach (Globals::$stalk as $key => $value) {
if (myfnmatch(str_replace('_', ' ', $key), str_replace('_', ' ', $data['user']))) {
$stalkchannel = array_merge($stalkchannel, explode(',', $value));
}
}
foreach (Globals::$edit as $key => $value) {
if (
myfnmatch(
str_replace('_', ' ', $key),
str_replace(
'_',
' ',
($data['namespace'] == 'Main:' ? '' : $data['namespace']) .
$data['title']
)
)
) {
$stalkchannel = array_merge($stalkchannel, explode(',', $value));
}
}
switch ($data['namespace'] . $data['title']) {
case 'User:' . Config::$user . '/Run':
Globals::$run = Api::$q->getpage('User:' . Config::$user . '/Run');
break;
case 'User:' . Config::$user . '/Optin':
Globals::$optin = Api::$q->getpage('User:' . Config::$user . '/Optin');
break;
case 'User:' . Config::$user . '/AngryOptin':
Globals::$aoptin = Api::$q->getpage('User:' . Config::$user . '/AngryOptin');
break;
}
if (
$data['namespace'] != 'Main:' and
!preg_match(
'/\* \[\[(' . preg_quote($data['namespace'] . $data['title'], '/') .
')\]\] \- .*/i',
Globals::$optin
)
) {
self::bail($data, 'Outside of valid namespaces');
return;
}
$logger->addInfo('Processing: ' . $message);
Process::processEdit($data);
}
break;
}
}
if (!Feed::$wlTimer || Feed::$wlTimer + 3600 <= time()) {
$logger->addInfo('Reloading huggle whitelist on timer');
Feed::$wlTimer = time();
loadHuggleWhitelist();
}
}
public static function bail($change, $why = '', $score = 'N/A', $reverted = false)
{
global $logger;
$rchange = $change;
$rchange['edit_reason'] = $why;
$rchange['edit_score'] = $score;
if (!in_array('raw_line', $change)) {
return;
}
$logger->addInfo($change['rawline'] . " # " . $score .
' # ' . $why . ' # ' . ($reverted ? 'Reverted' : 'Not reverted'));
IRC::spam($change['rawline'] . "\003 # " . $score . ' # ' . $why .
' # ' . ($reverted ? 'Reverted' : 'Not reverted'));
}
}