source: trunk/Modules/Chat/irc_bot.php

Last change on this file was 929, checked in by chronos, 3 years ago
  • Modified: Removed commended out print_r and echo commands used only for debugging.
File size: 6.0 KB
Line 
1<?php
2
3include_once('../../Common/Global.php');
4
5class IRCBot
6{
7 public string $OwnerName = 'Chronosův';
8 public string $Server = "game.zdechov.net";
9 public int $Port = 6667;
10 public string $Channel = "#zdechov";
11 public string $Nick = "History";
12 public string $JokeFileName = "jokes.txt";
13 public $File;
14
15 function __construct()
16 {
17 $this->Jokes = file($this->JokeFileName);
18 $this->File = fsockopen($this->Server, $this->Port, $ErrNumber, $ErrString, 5);
19 $this->Connect($this->File, $this->Nick, $this->Channel, 0);
20 }
21
22 function Connect()
23 {
24 $this->Command('USER USER '.$this->Nick." # # :".$this->Nick);
25 $this->Command('NICK '.$this->Nick);
26 //sleep($JoinWaitTime);
27 $this->Command('JOIN '.$this->Channel);
28 }
29
30 function Say($Message, $Recipient = '')
31 {
32 if ($Recipient == '') $Recipient = $this->Channel;
33 $this->Command(': PRIVMSG '.$Recipient.' :'.$Message);
34 }
35
36 function Command($Text)
37 {
38 fwrite($this->File, $Text."\n");
39 }
40
41 function Run(): void
42 {
43 global $Database;
44
45 while (!fwrite($this->File, ''))
46 {
47 $this->Command('JOIN '.$this->Channel);
48
49 $Line = fgets($this->File);
50 echo($Line);
51
52 $LineParts = explode(' ', $Line);
53 if (count($LineParts) > 0)
54 {
55 if ($LineParts[0] == 'PING')
56 {
57 $this->Command('PONG '.trim($LineParts[1])."\n");
58 echo('-PONG '.trim($LineParts[1])."\n");
59 }
60
61 if (count($LineParts) > 1)
62 {
63 if ((trim($LineParts[1]) == 'INVITE') && (substr(trim($LineParts[3]), 0, 2) == ':#'))
64 {
65 $CurrentChannel = substr(trim($LineParts[3]), 1);
66 fwrite($this->File, 'JOIN '.$this->Channel."\n");
67 $this->Say('Hello');
68 echo('-JOINED '.$this->Channel."\n");
69 }
70 }
71 }
72
73 $Commands = array(substr($Line, 0, strpos($Line, ':') + 1));
74 $Line = substr($Line, strlen($Commands[0]));
75 $Commands[1] = substr($Line, 0, strpos($Line, ':') + 1);
76 $Commands[2] = substr($Line, strlen($Commands[1]));
77
78 // Log messages to database
79 if (strpos($Line, 'PRIVMSG') !== false)
80 {
81 $Text = addslashes($Commands[2]);
82 $LineParts2 = explode(' ', $Commands[1]);
83 $LineParts3 = explode('@', $LineParts2[0]);
84 $Host = addslashes($LineParts3[1]);
85 $LineParts4 = explode('!', $LineParts3[0]);
86 $Nick = addslashes($LineParts4[0]);
87 $Database->insert('ChatHistory', array('Nick' => $Nick, 'Text' => $Text, 'Host' => $Host, 'Time' => 'NOW()', 'RoomName' => 'Všichni'));
88 }
89
90
91 explode(':', $Line);
92 foreach ($Commands as $Index => $Item)
93 $Commands[$Index] = trim($Item);
94
95 if (count($Commands) >= 2)
96 {
97 $Command = $Commands[2];
98
99 // Jméno - Pošle vizitku
100 if ($Command == $this->Nick)
101 {
102 $this->Say('Ahoj lidi, ja jsem '.$this->OwnerName.' bot. Random#: '.rand(0, 10));
103 echo('-VISITCARD'."\n");
104 }
105
106 // hhelp - vypise tuto napovedu
107 if ($Command == 'hhelp')
108 {
109 $this->Say('Ja jsem Harvester.');
110 /*
111 irc_say( $sfp, "Bot - Posle vizitku", $nick, $channel );
112 irc_say( $sfp, "hhelp - vypise tuto napovedu", $nick, $channel );
113 irc_say( $sfp, "hsay:Message - Posle zpravu", $nick, $channel );
114 irc_say( $sfp, "hpsay:to:Message - Posle soukromou zpravu kanalu nebo osobe", $nick, $channel );
115 irc_say( $sfp, "hdo:Command - Posle serveru prikaz", $nick, $channel );
116 irc_say( $sfp, "hmove:Channel - Pripoji do kanalu / Zmeni aktivni kanal", $nick, $channel );
117 irc_say( $sfp, "/invite Harvester #channel - Pozve a pripoji bota do kanalu", $nick, $channel );
118 irc_say( $sfp, "hpart:Channel - Odpoji se z kanalu", $nick, $channel );
119 irc_say( $sfp, "hjoke - Posle \"nahodny\" vtip", $nick, $channel );
120 */
121 echo("-HELPED"."\n");
122 }
123
124 // hsay:Message - Posle zpravu
125 if ($Command == 'hsay')
126 {
127 $this->Say($Commands[3]);
128 echo("-SAID: ".$Commands[3]."\n");
129 }
130
131 // hpsay:to:Message - Posle soukromou zpravu kanalu nebo osobe
132 if ($Command == 'hpsay')
133 {
134 $this->Say($Commands[4], $Commands[3]);
135 echo("-SAID: ".$Commands[4]." -- To: ".$Commands[3]."\n");
136 }
137
138 // hcol:to:Message - Posle kolizni zpravu kanalu nebo osobe
139 if ($Command == 'hcol')
140 {
141 $this->Say($Commands[4], $Commands[3]);
142 echo("-COLIDED: ".$Commands[4]." To: ".$Commands[3]."\n");
143 }
144
145 // hdo:Command - Posle serveru prikaz
146 if ($Command == 'hdo')
147 {
148 $hdo = explode('hdo:', $Line);
149 $this->Command(trim($hdo[1]));
150 echo("-DONE: ".trim($hdo[1])."\n");
151 }
152
153 // hpart:Channel - Odpoji se z kanalu
154 if ($Command == 'hpart')
155 {
156 $hdo = explode('hpart:', $Line);
157 if (trim($hdo[1]) != trim($this->Channel))
158 {
159 $this->Command('PART :'.trim($hdo[1])."\n");
160 echo("-PARTED: ".trim($hdo[1])."\n");
161 }
162 }
163
164 // hmove:Channel - Zmeni aktivni kanal
165 if ($Command == 'hmove')
166 {
167 $hdo = explode("hmove:", $Line);
168 if (trim($hdo[1]) != trim($$this->Cannel))
169 {
170 $this->Channel = trim($hdo[1]);
171 $this->Command('JOIN '.$this->Channel."\n");
172 $this->Say('Hi, im here...');
173 echo("-ACTIVATED: ".trim($hdo[1])."\n");
174 }
175 }
176
177 // htime - udaje o casu
178 if ($Command == 'htime')
179 {
180 $Date = implode("-", getdate(time()));
181 $this->Say(trim($Date));
182 echo("-TIMED"."\n");
183 }
184
185 // hjoke - Posle nahodny vtip
186 if ($Command == 'hjoke')
187 {
188 $Joke = ($this->Jokes[rand(0, (sizeof($this->Jokes) - 1))]);
189 $this->Say(trim($Joke));
190 echo("-JOKED"."\n");
191 }
192 }
193 }
194 echo("-Connection lost.\n");
195 }
196}
197
198$IRCBot = new IRCBot();
199$IRCBot->Run();
Note: See TracBrowser for help on using the repository browser.