profiling correction
[mediawiki.git] / irc / rc2irc.php
blob2422687f1b5ac2216380139ed957ed27d453034a
1 <?php
3 $ircNick = "wikipedia_rc";
4 $rooms = array("en" => 1, "fr" => 1, "de" => 1);
5 $ircServer = "irc.freenode.net";
6 $ircSockName = "tcp://$ircServer";
7 $ircPort = 6667;
8 $minDelay = 0.5;
9 $ircReadTimeout = 200000; # us
10 $ircWriteTimeout = 30; # s
11 $fmB = chr(2);
12 $fmU = chr(31);
13 $queueId = 337055475;
14 $maxMessageSize = 16384;
16 #-----------------------------------------------------------------------------
18 # Get queue
20 $ircPassword = mt_rand(0xffffffff);
21 $hostname = getenv('SERVER_NAME');
23 $queue = msg_get_queue($queueId);
25 if ( !$queue ) {
26 print "Could not open RC message queue\n";
27 exit;
29 emptyQueue( $queue );
31 # Initialise the IRC connection
32 $sockIRC = fsockopen( $ircSockName, $ircPort );
33 if ( !$sockIRC ) {
34 print "Could not open IRC connection\n";
35 exit;
37 stream_set_timeout($sockIRC, 0, $ircWriteTimeout);
39 fwrite( $sockIRC,
40 "PASS $ircPassword\r\n" .
41 "NICK $ircNick\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 );
51 # Main input loop
52 $die = false;
53 while ( !$die ) {
54 # RC input
55 $msgType = 0;
56 $entry = false;
57 if (!msg_receive($queue, 0, $msgType, $maxMessageSize, $entry, true, MSG_IPC_NOWAIT)) {
58 $entry = false;
60 if (is_array( $entry )) {
61 $out = getIrcOutput( $sockIRC, $entry );
62 fwrite( $sockIRC, $out );
65 # IRC input
66 stream_set_timeout($sockIRC, 0, $ircReadTimeout);
67 $line = rtrim(fgets( $sockIRC ));
68 stream_set_timeout($sockIRC, 0, $ircWriteTimeout);
69 if ( $line ) {
70 $die = processIrcInput( $sockIRC, $line );
73 exit();
75 #--------------------------------------------------------------
76 function delayMin()
78 static $lastTime = 0;
79 global $minDelay;
80 if ( !$lastTime ) {
81 $lastTime = getMicroTime();
83 $curTime = getMicroTime();
84 $timeDifference = $curTime - $lastTime;
85 if ( $timeDifference < $minDelay ) {
86 usleep( ($minDelay - $timeDifference) *1000000 );
88 $lastTime = $curTime;
91 function getMicroTime()
93 list($usec, $sec) = explode(" ",microtime());
94 return ((float)$usec + (float)$sec);
97 function getIrcOutput( $socket, $in )
99 global $rooms;
101 delayMin();
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" : "");
109 $lang = $in['lang'];
110 if ( $lang == "w" ) {
111 $lang = "en";
114 if ( !array_key_exists( $rooms, $lang ) ) {
115 return "";
117 $room = "#{$lang}rc.wikipedia";
119 if ( $in['new'] ) {
120 $url = "http://$lang.wikipedia.org/wiki/" . urlencode($title);
121 } else {
122 $url = "http://$lang.wikipedia.org/w/wiki.phtml?title=" . urlencode($title) .
123 "&diff=0&oldid=$lastid";
125 $spaceTitle = str_replace("_", " ", $title);
127 $beep = "";
128 if ( $patterns ) {
129 foreach ( $patterns as $pattern ) {
130 if ( preg_match( $pattern, $comment ) ) {
131 $beep = chr(7);
132 break;
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";
143 return $fullString;
146 function joinRoom( $sock, $room )
148 global $rooms;
149 $rooms[$room] = 1;
150 fwrite( $sock, "JOIN #{$room}rc.wikipedia\r\n" );
153 function partRoom( $sock, $room )
155 global $rooms;
156 unset( $rooms[$room] );
157 fwrite( $sock, "PART #{$room}rc.wikipedia\r\n" );
160 function processIrcInput( $sock, $line )
162 global $rooms;
164 $die = false;
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" ) {
176 $die = true;
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));