Main Page | Namespace List | Class Hierarchy | Class List | File List | Class Members | File Members | Related Pages

rc2irc.php

Go to the documentation of this file.
00001 <?php 00002 00003 $ircNick = "wikipedia_rc"; 00004 $rooms = array("en" => 1, "fr" => 1, "de" => 1); 00005 $ircServer = "irc.freenode.net"; 00006 $ircSockName = "tcp://$ircServer"; 00007 $ircPort = 6667; 00008 $minDelay = 0.5; 00009 $ircReadTimeout = 200000; # us 00010 $ircWriteTimeout = 30; # s 00011 $fmB = chr(2); 00012 $fmU = chr(31); 00013 $queueId = 337055475; 00014 $maxMessageSize = 16384; 00015 00016 #----------------------------------------------------------------------------- 00017 00018 # Get queue 00019 00020 $ircPassword = mt_rand(0xffffffff); 00021 $hostname = getenv('SERVER_NAME'); 00022 00023 $queue = msg_get_queue($queueId); 00024 00025 if ( !$queue ) { 00026 print "Could not open RC message queue\n"; 00027 exit; 00028 } 00029 emptyQueue( $queue ); 00030 00031 # Initialise the IRC connection 00032 $sockIRC = fsockopen( $ircSockName, $ircPort ); 00033 if ( !$sockIRC ) { 00034 print "Could not open IRC connection\n"; 00035 exit; 00036 } 00037 stream_set_timeout($sockIRC, 0, $ircWriteTimeout); 00038 00039 fwrite( $sockIRC, 00040 "PASS $ircPassword\r\n" . 00041 "NICK $ircNick\r\n" . 00042 "USER recentchanges $hostname $ircServer Wikipedia RC->IRC bot\r\n" 00043 ); 00044 00045 foreach ( $rooms as $room => $v ) { 00046 joinRoom( $sockIRC, $room ); 00047 } 00048 00049 $readObjs = array( $sockIRC, $queue ); 00050 00051 # Main input loop 00052 $die = false; 00053 while ( !$die ) { 00054 # RC input 00055 $msgType = 0; 00056 $entry = false; 00057 if (!msg_receive($queue, 0, $msgType, $maxMessageSize, $entry, true, MSG_IPC_NOWAIT)) { 00058 $entry = false; 00059 } 00060 if (is_array( $entry )) { 00061 $out = getIrcOutput( $sockIRC, $entry ); 00062 fwrite( $sockIRC, $out ); 00063 } 00064 00065 # IRC input 00066 stream_set_timeout($sockIRC, 0, $ircReadTimeout); 00067 $line = rtrim(fgets( $sockIRC )); 00068 stream_set_timeout($sockIRC, 0, $ircWriteTimeout); 00069 if ( $line ) { 00070 $die = processIrcInput( $sockIRC, $line ); 00071 } 00072 } 00073 exit(); 00074 00075 #-------------------------------------------------------------- 00076 function delayMin() 00077 { 00078 static $lastTime = 0; 00079 global $minDelay; 00080 if ( !$lastTime ) { 00081 $lastTime = getMicroTime(); 00082 } 00083 $curTime = getMicroTime(); 00084 $timeDifference = $curTime - $lastTime; 00085 if ( $timeDifference < $minDelay ) { 00086 usleep( ($minDelay - $timeDifference) *1000000 ); 00087 } 00088 $lastTime = $curTime; 00089 } 00090 00091 function getMicroTime() 00092 { 00093 list($usec, $sec) = explode(" ",microtime()); 00094 return ((float)$usec + (float)$sec); 00095 } 00096 00097 function getIrcOutput( $socket, $in ) 00098 { 00099 global $rooms; 00100 00101 delayMin(); 00102 $bad = array("\n", "\r"); 00103 $empty = array("", ""); 00104 $comment = $in['comment']; 00105 $title = $in['prefixedDBkey']; 00106 $user = $in['userText']; 00107 $lastid = IntVal($in['lastOldid']); 00108 $flag = ($in['minor'] ? "M" : "") . ($in['new'] ? "N" : ""); 00109 $lang = $in['lang']; 00110 if ( $lang == "w" ) { 00111 $lang = "en"; 00112 } 00113 00114 if ( !array_key_exists( $rooms, $lang ) ) { 00115 return ""; 00116 } 00117 $room = "#{$lang}rc.wikipedia"; 00118 00119 if ( $in['new'] ) { 00120 $url = "http://$lang.wikipedia.org/wiki/" . urlencode($title); 00121 } else { 00122 $url = "http://$lang.wikipedia.org/w/wiki.phtml?title=" . urlencode($title) . 00123 "&diff=0&oldid=$lastid"; 00124 } 00125 $spaceTitle = str_replace("_", " ", $title); 00126 00127 $beep = ""; 00128 if ( $patterns ) { 00129 foreach ( $patterns as $pattern ) { 00130 if ( preg_match( $pattern, $comment ) ) { 00131 $beep = chr(7); 00132 break; 00133 } 00134 } 00135 } 00136 if ( $comment !== "" ) { 00137 $comment = "($comment)"; 00138 } 00139 00140 $fullString = str_replace($bad, $empty, 00141 "$beep$fmB$spaceTitle$fmB $flag $url $user $comment"); 00142 $fullString = "PRIVMSG $room :$fullString\r\n"; 00143 return $fullString; 00144 } 00145 00146 function joinRoom( $sock, $room ) 00147 { 00148 global $rooms; 00149 $rooms[$room] = 1; 00150 fwrite( $sock, "JOIN #{$room}rc.wikipedia\r\n" ); 00151 } 00152 00153 function partRoom( $sock, $room ) 00154 { 00155 global $rooms; 00156 unset( $rooms[$room] ); 00157 fwrite( $sock, "PART #{$room}rc.wikipedia\r\n" ); 00158 } 00159 00160 function processIrcInput( $sock, $line ) 00161 { 00162 global $rooms; 00163 00164 $die = false; 00165 $args = explode( " ", $line ); 00166 00167 if ( $args[0] == "PING" ) { 00168 fwrite( $sock, "PONG {$args[1]}\r\n" ); 00169 } elseif ( $args[0]{0} == ":" ) { 00170 $name = array_shift( $args ); 00171 $name = substr($name, 1); 00172 $cmd = array_shift( $args ); 00173 if ( $cmd == "PRIVMSG" ) { 00174 $msgRoom = array_shift( $args ); 00175 if ( $args[0] == "die" ) { 00176 $die = true; 00177 } elseif ( $args[0] == "join" ) { 00178 joinRoom( $args[1] ); 00179 } elseif ( $args[0] == "part" ) { 00180 partRoom( $args[1] ); 00181 } 00182 } 00183 } 00184 } 00185 00186 function emptyQueue( $id ) 00187 { 00188 while ( msg_receive($queue, 0, $msgType, $maxMessageSize, $entry, true, MSG_IPC_NOWAIT)); 00189 } 00190 00191 ?> 00192

Generated on Tue Jun 29 23:40:06 2004 for Mediawiki by doxygen 1.3.7