fixed margin moved to class for rtl support
[lhc/web/wiklou.git] / irc / rc2irc.php
1 <?php
2
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;
15
16 #-----------------------------------------------------------------------------
17
18 # Get queue
19
20 $ircPassword = mt_rand(0xffffffff);
21 $hostname = getenv('SERVER_NAME');
22
23 $queue = msg_get_queue($queueId);
24
25 if ( !$queue ) {
26 print "Could not open RC message queue\n";
27 exit;
28 }
29 emptyQueue( $queue );
30
31 # Initialise the IRC connection
32 $sockIRC = fsockopen( $ircSockName, $ircPort );
33 if ( !$sockIRC ) {
34 print "Could not open IRC connection\n";
35 exit;
36 }
37 stream_set_timeout($sockIRC, 0, $ircWriteTimeout);
38
39 fwrite( $sockIRC,
40 "PASS $ircPassword\r\n" .
41 "NICK $ircNick\r\n" .
42 "USER recentchanges $hostname $ircServer Wikipedia RC->IRC bot\r\n"
43 );
44
45 foreach ( $rooms as $room => $v ) {
46 joinRoom( $sockIRC, $room );
47 }
48
49 $readObjs = array( $sockIRC, $queue );
50
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;
59 }
60 if (is_array( $entry )) {
61 $out = getIrcOutput( $sockIRC, $entry );
62 fwrite( $sockIRC, $out );
63 }
64
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 );
71 }
72 }
73 exit();
74
75 #--------------------------------------------------------------
76 function delayMin()
77 {
78 static $lastTime = 0;
79 global $minDelay;
80 if ( !$lastTime ) {
81 $lastTime = getMicroTime();
82 }
83 $curTime = getMicroTime();
84 $timeDifference = $curTime - $lastTime;
85 if ( $timeDifference < $minDelay ) {
86 usleep( ($minDelay - $timeDifference) *1000000 );
87 }
88 $lastTime = $curTime;
89 }
90
91 function getMicroTime()
92 {
93 list($usec, $sec) = explode(" ",microtime());
94 return ((float)$usec + (float)$sec);
95 }
96
97 function getIrcOutput( $socket, $in )
98 {
99 global $rooms;
100
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";
112 }
113
114 if ( !array_key_exists( $rooms, $lang ) ) {
115 return "";
116 }
117 $room = "#{$lang}rc.wikipedia";
118
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";
124 }
125 $spaceTitle = str_replace("_", " ", $title);
126
127 $beep = "";
128 if ( $patterns ) {
129 foreach ( $patterns as $pattern ) {
130 if ( preg_match( $pattern, $comment ) ) {
131 $beep = chr(7);
132 break;
133 }
134 }
135 }
136 if ( $comment !== "" ) {
137 $comment = "($comment)";
138 }
139
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;
144 }
145
146 function joinRoom( $sock, $room )
147 {
148 global $rooms;
149 $rooms[$room] = 1;
150 fwrite( $sock, "JOIN #{$room}rc.wikipedia\r\n" );
151 }
152
153 function partRoom( $sock, $room )
154 {
155 global $rooms;
156 unset( $rooms[$room] );
157 fwrite( $sock, "PART #{$room}rc.wikipedia\r\n" );
158 }
159
160 function processIrcInput( $sock, $line )
161 {
162 global $rooms;
163
164 $die = false;
165 $args = explode( " ", $line );
166
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] );
181 }
182 }
183 }
184 }
185
186 function emptyQueue( $id )
187 {
188 while ( msg_receive($queue, 0, $msgType, $maxMessageSize, $entry, true, MSG_IPC_NOWAIT));
189 }
190
191 ?>
192