source: branches/old/test/irc_bot/irc_bot.php

Last change on this file was 192, checked in by george, 17 years ago

Přidáno: IRC bot pro sledování IRC chatu v PHP
Upraveno: Informace o rychlosti internetové linky na 8 Mbit/s
Opraveno: Zobrazování jmén autorů v GM RSS kanálu

File size: 7.1 KB
Line 
1<?php
2
3include('../../global.php');
4$Database->select_db('wow');
5
6$server = "game.zdechov.net";
7$port = "6667";
8$channel = "#wow";
9$nick = "Harvester";
10$jokefile = "jokes.txt";
11$LastMessagesCount = 10;
12
13//Functions:
14function IRCConnect($fp, $nick, $channel, $joinwait)
15{
16 fwrite($fp, "USER USER $nick # # :$nick\n");
17 fwrite($fp, "NICK $nick\n");
18 sleep($joinwait);
19 fwrite($fp, "JOIN $channel\n");
20}
21
22function irc_say($fp, $msg, $nick, $rcpto)
23{
24 //fwrite($fp, ":$nick!~Harvie@r3ax157.net.upc.cz PRIVMSG $rcpto :$msg\n");
25 fwrite($fp, ": PRIVMSG $rcpto :$msg\n");
26}
27
28//Code:
29$jokes = file($jokefile);
30$sfp = fsockopen($server, $port, $errno, $errstr, 5);
31IRCConnect($sfp, $nick, $channel, 0);
32
33$home_channel = $channel;
34
35fwrite($sfp, "JOIN $channel\n");
36
37while(!fwrite($sfp, ""))
38{
39 // ReadLine from IRC Server
40 $line = (fgets($sfp));
41 echo($line);
42
43 // IRC Sever PING/PONG Response
44 $words = (explode(" ", $line));
45 if(count($words) > 0)
46 {
47 if ($words[0] == "PING") {
48 fwrite($sfp, "PONG ".trim($words[1])."\n");
49 echo("-PONG ".trim($words[1])."\n");
50 }
51 }
52
53 if(count($words) > 2)
54 {
55 if((trim($words[1]) == "PART") or (trim($words[1]) == "QUIT"))
56 {
57 $UserNick = substr($words[0], 1, strpos($words[0], '!') - 1);
58 $UserHost = substr($words[0], strpos($words[0], '@') + 1);
59 $Channel = $channel;
60 $DbResult = $Database->select('irc_user_leave', '*', 'nick="'.$UserNick.'" AND host="'.$UserHost.'" AND channel="'.$Channel.'"');
61 if($DbResult->num_rows > 0)
62 $Database->update('irc_user_leave', 'nick="'.$UserNick.'" AND host="'.$UserHost.'" AND channel="'.$Channel.'"', array('time' => 'NOW()'));
63 else $Database->insert('irc_user_leave', array('time' => 'NOW()', 'nick' => $UserNick, 'host' => $UserHost, 'channel' => $Channel));
64 }
65
66 if(trim($words[1]) == "JOIN")
67 {
68 $UserNick = substr($words[0], 1, strpos($words[0], '!') - 1);
69 $UserHost = substr($words[0], strpos($words[0], '@') + 1);
70 $Channel = substr(trim($words[2]), 1);
71 $DbResult = $Database->select('irc_user_leave', 'time', 'nick="'.$UserNick.'" AND host="'.$UserHost.'" AND channel="'.$Channel.'"');
72 if($DbResult->num_rows > 0)
73 {
74 $Row2 = $DbResult->fetch_array();
75 $UserLeave = $DbResult->fetch_array();
76 $DbResult = $Database->select('irc_messages', '*', 'channel="'.$Channel.'" AND time > "'.$Row2['time'].'" ORDER BY time ASC');
77 while($Row = $DbResult->fetch_array())
78 {
79 irc_say($sfp, $Row['time'].' '.$Row['nick'].': '.$Row['message'], $nick, $UserNick);
80 }
81 }
82 }
83 }
84
85 if(count($words) > 3)
86 {
87 // Prijem pozvanky
88 if ( ( trim($words[1]) == "INVITE" ) && ( substr(trim($words[3]), 0, 2) == ":#" ) )
89 {
90 $channel = substr(trim($words[3]), 1);
91 fwrite($sfp, "JOIN $channel\n");
92 irc_say( $sfp, "Hello", $nick, $channel );
93 echo("-JOINED $channel\n");
94 }
95
96 // Logování zpráv
97 if ( ( trim($words[1]) == "PRIVMSG" ) && ( substr(trim($words[3]), 0, 1) == ":" ) )
98 {
99 $Channel = trim($words[2]);
100 $Message = trim(substr($words[3], 1));
101 $UserNick = substr($words[0], 1, strpos($words[0], '!') - 1);
102 $UserHost = substr($words[0], strpos($words[0], '@') + 1);
103 $Database->insert('irc_messages', array('time' => 'NOW()', 'channel' => $Channel, 'nick' => $UserNick, 'host' => $UserHost, 'message' => $Message));
104 }
105 }
106
107 $commands = (explode(":", $line));
108
109 if(count($commands) > 2)
110 {
111 //Harvester - Posle vizitku
112 if ( trim($commands[2]) == "Harvester")
113 {
114 irc_say( $sfp, ("Ahoj lidi, ja jsem Harvieho bot. Random#: ".rand(0,10)), $nick, $channel);
115 echo("-VISITCARD"."\n");
116 }
117
118 //hhelp - vypise tuto napovedu
119 if ( trim($commands[2]) == "hhelp")
120 {
121 irc_say( $sfp, "Ja jsem Harvester", $nick, $channel );
122 /*
123 irc_say( $sfp, "Harvester - Posle vizitku", $nick, $channel );
124 irc_say( $sfp, "hhelp - vypise tuto napovedu", $nick, $channel );
125 irc_say( $sfp, "hsay:Message - Posle zpravu", $nick, $channel );
126 irc_say( $sfp, "hpsay:to:Message - Posle soukromou zpravu kanalu nebo osobe", $nick, $channel );
127 irc_say( $sfp, "hdo:Command - Posle serveru prikaz", $nick, $channel );
128 irc_say( $sfp, "hmove:Channel - Pripoji do kanalu / Zmeni aktivni kanal", $nick, $channel );
129 irc_say( $sfp, "/invite Harvester #channel - Pozve a pripoji bota do kanalu", $nick, $channel );
130 irc_say( $sfp, "hpart:Channel - Odpoji se z kanalu", $nick, $channel );
131 irc_say( $sfp, "hjoke - Posle \"nahodny\" vtip", $nick, $channel );
132 */
133 echo("-HELPED"."\n");
134 }
135
136 //hsay:Message - Posle zpravu
137 if ( trim($commands[2]) == "hsay")
138 {
139 irc_say( $sfp, trim($commands[3]), $nick, $channel );
140 echo("-SAID: ".trim($commands[3])."\n");
141 }
142
143 //hpsay:to:Message - Posle soukromou zpravu kanalu nebo osobe
144 if ( trim($commands[2]) == "hpsay")
145 {
146 irc_say( $sfp, trim($commands[4]), $nick, trim($commands[3]) );
147 echo("-SAID: ".trim($commands[4])." -- To: ".trim($commands[3])."\n");
148 }
149
150 //hcol:to:Message - Posle kolizni zpravu kanalu nebo osobe
151 if ( trim($commands[2]) == "hcol")
152 {
153 irc_say( $sfp, trim($commands[4]), trim($commands[3]), $channel );
154 echo("-COLIDED: ".trim($commands[4])." To: ".trim($commands[3])."\n");
155 }
156
157 //hdo:Command - Posle serveru prikaz
158 if ( trim($commands[2]) == "hdo")
159 {
160 $hdo = explode("hdo:", $line);
161 fwrite( $sfp, trim($hdo[1])."\n" );
162 echo("-DONE: ".trim($hdo[1])."\n");
163 }
164
165 //hpart:Channel - Odpoji se z kanalu
166 if ( trim($commands[2]) == "hpart")
167 {
168 $hdo = explode("hpart:", $line);
169 if ( trim($hdo[1]) != trim($home_channel) )
170 {
171 fwrite( $sfp, "PART :".trim($hdo[1])."\n" );
172 echo("-PARTED: ".trim($hdo[1])."\n");
173 }
174 }
175
176 //hmove:Channel - Zmeni aktivni kanal
177 if ( trim($commands[2]) == "hmove")
178 {
179 $hdo = explode("hmove:", $line);
180 if ( trim($hdo[1]) != trim($home_channel) )
181 {
182 $home_channel = trim($hdo[1]);
183 fwrite($sfp, "JOIN $home_channel\n");
184 irc_say( $sfp, "Hi, im here...", $nick, $home_channel );
185 echo("-ACTIVATED: ".trim($hdo[1])."\n");
186 }
187 }
188
189 //htime - udaje o casu
190 if ( trim($commands[2]) == "htime")
191 {
192 $msg = implode("-", getdate(time()));
193 irc_say( $sfp, trim($msg), $nick, $channel );
194 echo("-TIMED"."\n");
195 }
196
197 //hjoke - Posle nahodny vtip
198 if ( trim($commands[2]) == "hjoke")
199 {
200 $joke = ($jokes[ rand(0, (sizeof($jokes)-1) ) ]);
201 //$ftip = "Potkaji se dve blondyny a jedna rika te druhe: \"How do you do?\" a ta ji odpovi co? kde ses to naucila? To vis vzdycky pretocim radio doprava a tam mluvi jen anglicky tak se to tak ucim. Potkaji se za 2 tydny a jedna rika druhe How do you do a druha ji odpovi chsrchsrsrshchsrshchs";
202 irc_say( $sfp, trim($joke), $nick, $channel );
203 echo("-JOKED"."\n");
204 }
205
206 //Navrat do domaciho kanalu
207 $channel = $home_channel;
208 }
209}
210
211echo("-Connection lost.");
212
213?>
Note: See TracBrowser for help on using the repository browser.