Merge branch 'development'
[NeonServV5.git] / QServer / NeonServ_QServer.class.php
1 <?php
2 /* NeonServ_QServer.class.php - NeonServ v5
3  * Copyright (C) 2011-2012  Philipp Kreil (pk910)
4  * 
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  * 
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  * 
15  * You should have received a copy of the GNU General Public License 
16  * along with this program. If not, see <http://www.gnu.org/licenses/>. 
17  */
18
19 class NeonServ_QServer {
20     private $admin, $socket, $numeric;
21     
22     function NeonServ_QServer() {
23         $this->admin['pass']='*'; // QServer Passwort
24         $this->admin['host']='127.0.0.1';
25         $this->admin['port']=7499;
26     }
27     
28     public function disconnect() {
29         fclose($this->socket);
30         $this->socket=false;
31     }
32     
33     public function connect() {
34         if($this->socket) {
35             $this->disconnect();
36         }
37         $this->socket=@fsockopen($this->admin['host'], $this->admin['port'], $errno, $errstr, 3);
38         if ($this->socket) {
39             stream_set_timeout($this->socket, 2);
40             fputs($this->socket, "A ".$this->admin['pass']."\n");
41             $data = $this->parseLine(@fgets($this->socket));
42             if($data[0] == "A")
43                 return true;
44             $this->disconnect(); //not logged in...
45             return false;
46         }
47     }
48     
49     public function connected() {
50         if($this->socket) return true;
51         return false;
52     }
53     
54     private function parseLine($line) {
55         $line = str_replace(array("\r", "\n"), array("", ""), $line);
56         $highExplode = explode(" :", $line, 2);
57         $tokens = explode(" ", $highExplode[0]);
58         if(count($highExplode) > 1)
59             $tokens[] = $highExplode[1];
60         return $tokens;
61     }
62     
63     /* Get IRC User Information
64     * returns array: nick, ident, host, auth, realname
65     */
66     public function getUser($nick) {
67         fputs($this->socket, "U ".$nick."\n");
68         $data = $this->parseLine(@fgets($this->socket));
69         if($data[0] != "U") return NULL;
70         if($data[1] == "0") return NULL; //User not found
71         $user = array();
72         $user['nick'] = $data[2];
73         $user['ident'] = $data[3];
74         $user['host'] = $data[4];
75         $user['auth'] = ($data[5] == "0" ? NULL : $data[5]);
76         $user['realname'] = $data[6];
77         return $user;
78     }
79     
80     /* Get IRC Channel Information
81     * returns array: name, usercount, modes, topic
82     */
83     public function getChannel($name) {
84         fputs($this->socket, "C ".$name."\n");
85         $data = $this->parseLine(@fgets($this->socket));
86         if($data[0] != "C") return NULL;
87         if($data[1] == "0") return NULL; //Channel not found
88         $chan = array();
89         $chan['name'] = $data[2];
90         $chan['usercount'] = $data[3];
91         $chan['modes'] = implode(" ", array_slice($data, 4, (count($data) - 5)));
92         $chan['topic'] = $data[count($data)-1];
93         return $chan;
94     }
95     
96     /* Get All IRC Channels
97     * returns array with subarrays: name, usercount, modes, topic
98     */
99     public function getChannels() {
100         fputs($this->socket, "AC\n");
101         $channels = array();
102         while(true) {
103             $data = $this->parseLine(@fgets($this->socket));
104             if($data[0] == "E") return NULL;
105             if($data[0] == "ACE") break;
106             if($data[0] == "AC") {
107                 $chan = array();
108                 $chan['name'] = $data[1];
109                 $chan['usercount'] = $data[2];
110                 $chan['modes'] = implode(" ", array_slice($data, 3, (count($data) - 4)));
111                 $chan['topic'] = $data[count($data)-1];
112                 $channels[] = $chan;
113             }
114         }
115         return $channels;
116     }
117     
118     /* Get IRC Channel Users
119     * returns array with subarrays: nick, auth, flags
120     */
121     public function getChannelUsers($name, $invisibles = false) {
122         fputs($this->socket, "ACU ".$name." ".($invisibles ? "1" : "0")."\n");
123         $chanusers = array();
124         while(true) {
125             $data = $this->parseLine(@fgets($this->socket));
126             if($data[0] == "E") return NULL;
127             if($data[0] == "ACUE") break;
128             if($data[0] == "ACU") {
129                 $chanuser = array();
130                 $chanuser['nick'] = $data[1];
131                 $chanuser['auth'] = ($data[2] == "0" ? NULL : $data[2]);
132                 $chanuser['flags'] = $data[3];
133                 $chanusers[] = $chanuser;
134             }
135         }
136         return $chanusers;
137     }
138     
139     /* send IRC RAW
140     * returns true / false
141     */
142     public function sendRAW($class, $raw, $classIsNick = false) {
143         fputs($this->socket, "R ".($classIsNick ? "1" : "0")." ".$class." :".$raw."\n");
144         $data = $this->parseLine(@fgets($this->socket));
145         if($data[0] == "R" && $data[1] == "1") return true;
146         return false;
147     }
148 }
149
150 ?>