1:
#!/usr/bin/php
2:
3:
<?php
4:
5:
if(!defined("STDOUT")) die(); // Not running from a terminal
6:
7:
class Bot
8:
{
9:
private $server = "irc.quakenet.org"; // IRC Server
10:
private $port = 6667; // Port
11:
private $channel = "#phpportalen"; // Channel to join
12:
private $nick = "PHPBotIRC"; // Desired nickname
13:
14:
private $socket;
15:
16:
public function main($args)
17:
{
18:
unset($args[0]); // First arg is filename, unset it
19:
20:
// Parse input and assign it
21:
for($i = 1; $i <= count($args); $i++)
22:
{
23:
if(($args[$i] == "-s" || strtolower($args[$i]) == "--server") && $i < count($args))
24:
{
25:
$this->server = $args[$i + 1];
26:
}
27:
if(($args[$i] == "-p" || strtolower($args[$i]) == "--port") && $i < count($args))
28:
{
29:
if(is_numeric($args[$i + 1]))
30:
{
31:
$this->port = (int)$args[$i + 1];
32:
}
33:
}
34:
if(($args[$i] == "-c" || strtolower($args[$i] == "--channel")) && $i < count($args))
35:
{
36:
$hash = $args[$i + 1][0] == "#";
37:
$this->channel = $hash ? $args[$i + 1] : "#" . $args[$i + 1];
38:
}
39:
if(($args[$i] == "-n" || strtolower($args[$i] == "--nick")) && $i < count($args))
40:
{
41:
$this->nick = $args[$i + 1];
42:
}
43:
}
44:
45:
echo "Do you wish to connect with the following settings?\n".
46:
"Server:\t $this->server\n".
47:
"Port:\t $this->port\n".
48:
"Channel: $this->channel\n".
49:
"Nick:\t $this->nick\n\n[y/N]: ";
50:
51:
if(!in_array(trim(strtolower(fgets(fopen("php://stdin", 'r')))), array("y", "yes", "j", "ja")))
52:
{
53:
die();
54:
}
55:
echo "\n";
56:
}
57:
58:
public function connect()
59:
{
60:
$this->socket = fsockopen($this->server, $this->port) or die("Connection failed"); // Opens socket stream
61:
$this->send("USER $this->nick 0 * :$this->nick\n");
62:
$this->send("NICK $this->nick");
63:
$this->communicator();
64:
}
65:
66:
private function send($data)
67:
{
68:
fputs($this->socket, $data . "\n");
69:
flush();
70:
}
71:
72:
private function sendText($text, $color = "black")
73:
{
74:
75:
$colors = array(
76:
1 =>
77:
"black", // Black
78:
"navy", // Navy Blue
79:
"green", // Green
80:
"red", // Red
81:
"brown", // Brown
82:
"purple", // Purple
83:
"olive", // Olive Green
84:
"yellow", // Yellow
85:
"lime", // Lime Green
86:
"teal", // Teal
87:
"aqua", // Aqua Light
88:
"royal", // Royal Blue
89:
"pink", // Hot Pink
90:
"dgray", // Dark Gray
91:
"lgray", // Light Gray
92:
"white" // White
93:
);
94:
95:
$colorNumber = 1; // Default black
96:
97:
if(in_array($color, $colors))
98:
{
99:
$colors = array_flip($colors);
100:
$colorNumber = $colors[$color];
101:
}
102:
103:
$out = "PRIVMSG $this->channel :" . chr(3) . $colorNumber . $text; // Raw output to send to the server
104:
$this->send($out);
105:
echo "Bot says: $out\n";
106:
}
107:
108:
private function communicator()
109:
{
110:
while(true)
111:
{
112:
while($raw = fgets($this->socket))
113:
{
114:
echo "Server says: " . $raw; // Echoes raw input from server
115:
flush();
116:
117:
$in = explode(" ", $raw); // Parse out the data
118:
$input = array_map("trim", $in); // Trim all input
119:
120:
if($input[1] == "001") // If connected
121:
{
122:
$this->send("JOIN $this->channel");
123:
}
124:
125:
if($input[0] == "PING")
126:
{
127:
$this->send("PONG " . $input[1]);
128:
}
129:
130:
////////// EDIT HERE //////////
131:
132:
// Example 1
133:
if(end($input) == ":!hello")
134:
{
135:
$this->sendText("Hello! My name is $this->nick!", "royal");
136:
}
137:
138:
// Example 2
139:
if($input[count($input) - 2] == ":!sqrt")
140:
{
141:
if(is_numeric(end($input)))
142:
$this->sendText("The square root of " . end($input) . " is " . sqrt(end($input)));
143:
else
144:
$this->sendText(end($input) . " is not a number!", "red");
145:
}
146:
147:
///////////////////////////////
148:
149:
}
150:
}
151:
}
152:
}
153:
154:
if($argc > 1 && ($argv[1] == "-h" || $argv[1] == "--help")) // help
155:
{
156:
echo "Args:\n";
157:
echo "-s or --server\t\tServer\n";
158:
echo "-p or --port\t\tPort\n";
159:
echo "-c or --channel\t\tChannel\n";
160:
echo "-n or --nick\t\tNickname\n";
161:
echo "-h or --help\t\tThis list\n\n";
162:
}
163:
else
164:
{
165:
$BOT = new Bot();
166:
$BOT->main($argv);
167:
$BOT->connect();
168:
}
169:
170:
?>