summaryrefslogtreecommitdiff
path: root/Client.php
blob: f5cb7f2d1db7a32f6d16f83fdf39783b81fed4e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
<?php

/**
 * A headless, slim IRC Client, that act's as the core of an IRC BOT.
 */
class Client
{
    protected $connected = false;
    protected $socket = null;
    protected $config = null;
    protected $blatech = null;

    /**
     * Constructor to load up a config file, and create a socket connection
     * and register the client to the IRC server. Then join then the channel
     *
     * @todo Use a config object and create a isConfigValid function
     */
    public function __construct($config)
    {
        // Create the client from the given config
        $this->config = parse_ini_file($config);
        if ($error = $this->isSocket() === true) {
            $this->userCommand();
            $ping = $this->nickCommand();
            $this->pongCommand($ping);
            $this->connected = true;
            $this->identifyCommand();
            $this->joinCommand();
        } else {
            var_dump($error);
            die();
        }

    }

    /**
     * Checks to see if a socket has already been created
     * If not, it creates the new socket based on the config
     * and then returns true if the socket is created
     *
     * @todo gracefully return false if the socket didn't get created
     */
    protected function isSocket()
    {
        if (!$this->socket) {
            // Try and create socket
            if (($this->socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) {
                 echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
                 die();
            }
            if (!socket_set_option($this->socket, SOL_SOCKET, SO_REUSEADDR, 1)) {
                echo socket_strerror(socket_last_error($this->socket));
            }
            if (socket_connect($this->socket, $this->config['server'], $this->config['port']) === false) {
                echo "socket_connect() failed: reason: " . socket_strerror(socket_last_error($this->socket)) . "\n";
            }
        }
        $this->getResponse();
        return true;
    }

    /**
     * Send the USER command to the IRC server using the config to get the
     * required details.
     *
     * @todo Create sendCommand function to handle generic message sending
     */
    protected function userCommand()
    {
        socket_write($this->socket, "USER ". $this->config["username"] . ' ' . $this->config['hostname'] . ' ' . $this->config['servername'] . " :" . $this->config['realname'] . "\r\n");
    }

    /**
     * Send the NICK command to the IRC server using the config to get the
     * required details.
     */
    protected function nickCommand()
    {
        // Set the nick
        socket_write($this->socket, "NICK ". $this->config["username"] . "\r\n");
        $response = $this->getResponse();
        var_dump('NICK COMMAND RESPONSE: ');
        var_dump($response);
        return $response; // save response to use in pong response
    }

    /**
     * Send the PONG command to the IRC server using the config to get the
     * required details. If the ping param is set, then send that too.
     */
    protected function pongCommand($ping = null)
    {
        // Pong the server
        if (!$ping) {
            socket_write($this->socket, "PONG");
        } else {
            socket_write($this->socket, "PONG $ping\r\n");
        }
        if (!$this->connected) {
            var_dump('PONG COMMAND RESPONSE: ');
            $this->getResponse();
        }
    }

    /**
     * Send a mesage to the IRC server, messaging the nick server to identify
     * the client with the config details.
     */
    protected function identifyCommand()
    {
        // Identify the user
        socket_write($this->socket, "PRIVMSG ". "nickserv identify " . $this->config["pass"] . "\r\n");
        var_dump('IDENTIFY COMMAND RESPONSE: ');
        $this->getResponse();
    }

    /**
     * Send a JOIN command to the IRC server to join a channel based on the
     * config details
     */
    protected function joinCommand()
    {
        // Join the channel
        socket_write($this->socket, "JOIN ". $this->config["channel"] . "\r\n");
        var_dump('JOIN COMMAND RESPONSE: ');
        $this->getResponse();
    }

    /**
     * Read the socket, waiting for an incoming message from the IRC
     * server. Display memory usage here. Dump and return the response.
     */
    protected function getResponse()
    {
        // var_dump("memory: " . memory_get_usage()/1000 . 'KB');
        $response = "";
        while(true) {
            sleep(1);
            $response .= socket_read($this->socket, 4096);
            if ($response == 0) {
                echo($response) . "\n";
                return $response;
            }
        }
    }

    /**
     * Send a PRIVMSG command to the IRC server to message a channel based on
     * the config details.
     */
    public function msgCommand($msg)
    {
        socket_write($this->socket, "PRIVMSG ". $this->config["channel"] . ' ' . $msg . "\r\n");
    }

    /**
     * The main loop of the Client. Constantly listen for incoming messages
     * from the IRC server; respond to pings, and look out for bot commands.
     */
    public function loop()
    {
        while (true) {
            $message = explode(" ", $this->getResponse());
            if ($message[0] == 'PING') {
                $this->pongCommand($message[1]);
            } else {
                if (strpos($message[3], ":@") === 0) {
                    $args = '';
                    if(($count = count($message)) > 4) {
                        for ($i = 4; $i < $count; $i++ ) {
                            $args .= rtrim($message[$i]) . ' ';
                        }
                        $args = rtrim($args);
                    }
                    $this->msgCommand($this->blatech->runCommand($message, $args));
                }
            }
            $message = null;
        }
    }

    /**
     * Check for an existing Blatech object, if it doesn't exist, create it
     * with the config path that is passed.
     */
    public function loadBlatech($config)
    {
        if (null == $this->blatech) {
            include_once "Blatech.php";
            $this->blatech = new Blatech($config);
        }
        return $this->blatech;
    }
}

// Create a new client object
$client = new Client('Client.ini');
// Load up the blatech Object
$client->loadBlatech('Blatech.ini');
// Send a hello command to the channel
$client->msgCommand('In The Rear, With The Gear!');
// Start the main loop
$client->loop();