3 $ircNick = "wikipedia_rc";
4 $rooms = array("en" => 1, "fr" => 1, "de" => 1);
5 $ircServer = "irc.freenode.net"
6 $ircSockName = "tcp://$ircServer";
9 $ircReadTimeout = 200000; # us
10 $ircWriteTimeout = 30; # s
14 $maxMessageSize = 16384;
16 #-----------------------------------------------------------------------------
20 $ircPassword = mt_rand(0xffffffff);
21 $hostname = getenv('SERVER_NAME');
23 $queue = msg_get_queue($queueId);
26 print "Could not open RC message queue\n";
31 # Initialise the IRC connection
32 $sockIRC = fsockopen( $ircSockName, $ircPort );
34 print "Could not open IRC connection\n";
37 stream_set_timeout($sockIRC, 0, $ircWriteTimeout);
40 "PASS $ircPassword\r\n" .
42 "USER recentchanges $hostname $ircServer Wikipedia RC->IRC bot\r\n"
45 foreach ( $rooms as $room => $v ) {
46 joinRoom( $sockIRC, $room );
49 $readObjs = array( $sockIRC, $queue );
57 if (!msg_receive($queue, 0, $msgType, $maxMessageSize, $entry, true, MSG_IPC_NOWAIT
)) {
60 if (is_array( $entry )) {
61 $out = getIrcOutput( $sockIRC, $entry );
62 fwrite( $sockIRC, $out );
66 stream_set_timeout($sockIRC, 0, $ircReadTimeout);
67 $line = rtrim(fgets( $sockIRC ));
68 stream_set_timeout($sockIRC, 0, $ircWriteTimeout);
70 $die = processIrcInput( $sockIRC, $line );
75 #--------------------------------------------------------------
81 $lastTime = getMicroTime();
83 $curTime = getMicroTime();
84 $timeDifference = $curTime - $lastTime;
85 if ( $timeDifference < $minDelay ) {
86 usleep( ($minDelay - $timeDifference) *1000000 );
91 function getMicroTime()
93 list($usec, $sec) = explode(" ",microtime());
94 return ((float)$usec +
(float)$sec);
97 function getIrcOutput( $socket, $in )
102 $bad = array("\n", "\r");
103 $empty = array("", "");
104 $comment = $in['comment'];
105 $title = $in['prefixedDBkey'];
106 $user = $in['userText'];
107 $lastid = IntVal($in['lastOldid']);
108 $flag = ($in['minor'] ?
"M" : "") . ($in['new'] ?
"N" : "");
110 if ( $lang == "w" ) {
114 if ( !array_key_exists( $rooms, $lang ) ) {
117 $room = "#{$lang}rc.wikipedia";
120 $url = "http://$lang.wikipedia.org/wiki/" . urlencode($title);
122 $url = "http://$lang.wikipedia.org/w/wiki.phtml?title=" . urlencode($title) .
123 "&diff=0&oldid=$lastid";
125 $spaceTitle = str_replace("_", " ", $title);
129 foreach ( $patterns as $pattern ) {
130 if ( preg_match( $pattern, $comment ) ) {
136 if ( $comment !== "" ) {
137 $comment = "($comment)";
140 $fullString = str_replace($bad, $empty,
141 "$beep$fmB$spaceTitle$fmB $flag $url $user $comment");
142 $fullString = "PRIVMSG $room :$fullString\r\n";
146 function joinRoom( $sock, $room )
150 fwrite( $sock, "JOIN #{$room}rc.wikipedia\r\n" );
153 function partRoom( $sock, $room )
156 unset( $rooms[$room] );
157 fwrite( $sock, "PART #{$room}rc.wikipedia\r\n" );
160 function processIrcInput( $sock, $line )
165 $args = explode( " ", $line );
167 if ( $args[0] == "PING" ) {
168 fwrite( $sock, "PONG {$args[1]}\r\n" );
169 } elseif ( $args[0]{0} == ":" ) {
170 $name = array_shift( $args );
171 $name = substr($name, 1);
172 $cmd = array_shift( $args );
173 if ( $cmd == "PRIVMSG" ) {
174 $msgRoom = array_shift( $args );
175 if ( $args[0] == "die" ) {
177 } elseif ( $args[0] == "join" ) {
178 joinRoom( $args[1] );
179 } elseif ( $args[0] == "part" ) {
180 partRoom( $args[1] );
186 function emptyQueue( $id )
188 while ( msg_receive($queue, 0, $msgType, $maxMessageSize, $entry, true, MSG_IPC_NOWAIT
));