* Added a basic read loop ... currently only fetches message numbers
[citadel.git] / ctdlphp / ctdlprotocol.php
1 <?PHP
2
3 // $Id$
4 // 
5 // Implements various Citadel server commands.
6 //
7 // Copyright (c) 2003 by Art Cancro <ajc@uncensored.citadel.org>
8 // This program is released under the terms of the GNU General Public License.
9
10
11 //
12 // serv_gets() -- generic function to read one line of text from the server
13 //
14 function serv_gets() {
15         global $clientsocket;
16
17         $buf = fgets($clientsocket, 4096);              // Read line
18         $buf = substr($buf, 0, (strlen($buf)-1) );      // strip trailing LF
19         return $buf;
20 }
21
22
23 //
24 // serv_puts() -- generic function to write one line of text to the server
25 //
26 function serv_puts($buf) {
27         global $clientsocket;
28         
29         fwrite($clientsocket, $buf . "\n", (strlen($buf)+1) );
30 }
31
32
33
34 //
35 // Identify this client, and the hostname where the user is, to Citadel.
36 //
37 function ctdl_iden() {
38         global $clientsocket;
39
40         serv_puts("IDEN 0|8|001|PHP web client|" . $_SERVER['REMOTE_ADDR'] );
41         $buf = serv_gets();
42 }
43
44
45
46 //
47 // login_existing_user() -- attempt to login using a supplied username/password
48 // Returns an array with two variables:
49 // 0. TRUE or FALSE to determine success or failure
50 // 1. String error message (if relevant)
51 //
52 function login_existing_user($user, $pass) {
53         global $clientsocket;
54
55         serv_puts("USER " . $user);
56         $resp = serv_gets();
57         if (substr($resp, 0, 1) != "3") {
58                 return array(FALSE, substr($resp, 4));
59         }
60
61         serv_puts("PASS " . $pass);
62         $resp = serv_gets();
63         if (substr($resp, 0, 1) != "2") {
64                 return array(FALSE, substr($resp, 4));
65         }
66
67         $_SESSION["username"] = $user;
68         $_SESSION["password"] = $pass;
69         become_logged_in(substr($resp, 4));
70
71         return array(TRUE, "Login successful.  Have fun.");
72 }
73
74
75 //
76 // create_new_user() -- attempt to create a new user 
77 //                      using a supplied username/password
78 // Returns an array with two variables:
79 // 0. TRUE or FALSE to determine success or failure
80 // 1. String error message (if relevant)
81 //
82 function create_new_user($user, $pass) {
83         global $clientsocket;
84
85         serv_puts("NEWU " . $user);
86         $resp = serv_gets();
87         if (substr($resp, 0, 1) != "2") {
88                 return array(FALSE, substr($resp, 4));
89         }
90
91         serv_puts("SETP " . $pass);
92         $resp = serv_gets();
93         if (substr($resp, 0, 1) != "2") {
94                 return array(FALSE, substr($resp, 4));
95         }
96
97         $_SESSION["username"] = $user;
98         $_SESSION["password"] = $pass;
99         become_logged_in(substr($resp, 4));
100
101         return array(TRUE, "Login successful.  Have fun.");
102 }
103
104
105 //
106 // Code common to both existing-user and new-user logins
107 //
108 function become_logged_in($server_parms) {
109         $_SESSION["logged_in"] = 1;
110
111         $tok = strtok($server_parms, "|");
112         if ($tok) $thisline["username"] = $tok;
113
114         $tok = strtok("|");
115         if ($tok) $thisline["axlevel"] = $tok;
116                 
117         $tok = strtok("|");
118         if ($tok) $thisline["calls"] = $tok;
119                 
120         $tok = strtok("|");
121         if ($tok) $thisline["posts"] = $tok;
122                 
123         $tok = strtok("|");
124         if ($tok) $thisline["userflags"] = $tok;
125                 
126         $tok = strtok("|");
127         if ($tok) $thisline["usernum"] = $tok;
128                 
129         $tok = strtok("|");
130         if ($tok) $thisline["lastcall"] = $tok;
131                 
132         ctdl_goto("_BASEROOM_");
133 }
134
135
136
137 //
138 // Learn all sorts of interesting things about the Citadel server to
139 // which we are connected.
140 //
141 function ctdl_get_serv_info() {
142         serv_puts("INFO");
143         $buf = serv_gets();
144         if (substr($buf, 0, 1) == "1") {
145                 $i = 0;
146                 do {
147                         $buf = serv_gets();
148                         if ($i == 1) $_SESSION["serv_nodename"] = $buf;
149                         if ($i == 2) $_SESSION["serv_humannode"] = $buf;
150                         if ($i == 3) $_SESSION["serv_fqdn"] = $buf;
151                         if ($i == 4) $_SESSION["serv_software"] = $buf;
152                         if ($i == 6) $_SESSION["serv_city"] = $buf;
153                         if ($i == 7) $_SESSION["serv_sysadmin"] = $buf;
154                         $i = $i + 1;
155                 } while (strcasecmp($buf, "000"));
156         }
157
158 }
159
160
161 //
162 // Display a system banner.  (Returns completed HTML.)
163 // (This is probably temporary because it outputs more or less finalized
164 // markup.  For now it's just usable.)
165 //
166 function ctdl_mesg($msgname) {
167         global $clientsocket;
168
169         $msgtext = "<DIV ALIGN=CENTER>\n";
170
171         serv_puts("MESG " . $msgname);
172         $response = serv_gets();
173
174         if (substr($response, 0, 1) == "1") {
175                 while (strcmp($buf = serv_gets(), "000")) {
176                         $msgtext .= "<TT>" . htmlspecialchars($buf)
177                                 . "</TT><BR>\n" ;
178                 }
179         }
180         else {
181                 $msgtext .= "<B><I>" . substr($response, 4) . "</I></B><BR>\n";
182         }
183
184         $msgtext .= "</DIV>\n";
185         return($msgtext);
186 }
187
188
189 //
190 // Fetch the list of users currently logged in.
191 //
192 function ctdl_rwho() {
193         global $clientsocket;
194
195         serv_puts("RWHO");
196         $response = serv_gets();
197
198         if (substr($response, 0, 1) != "1") {
199                 return array(0, NULL);
200         }
201         
202         $all_lines = array();
203         $num_lines = 0;
204
205         while (strcmp($buf = serv_gets(), "000")) {
206
207                 $thisline = array();
208
209                 $tok = strtok($buf, "|");
210                 if ($tok) $thisline["session"] = $tok;
211
212                 $tok = strtok("|");
213                 if ($tok) $thisline["user"] = $tok;
214                 
215                 $tok = strtok("|");
216                 if ($tok) $thisline["room"] = $tok;
217                 
218                 $tok = strtok("|");
219                 if ($tok) $thisline["host"] = $tok;
220                 
221                 $tok = strtok("|");
222                 if ($tok) $thisline["client"] = $tok;
223
224                 // IGnore the rest of the fields for now.
225
226                 $num_lines = array_push($all_lines, $thisline);
227         }
228
229         return array($num_lines, $all_lines);
230
231 }
232
233
234 //
235 // Goto a room.
236 //
237 function ctdl_goto($to_where) {
238         
239         serv_puts("GOTO " . $to_where);
240         $response = serv_gets();
241
242         if (substr($response, 0, 1) == "2") {
243                 $_SESSION["room"] = strtok(substr($response, 4), "|");
244                 return array(TRUE, substr($response, 0, 3));
245         }
246
247         else {
248                 return array(FALSE, substr($response, 0, 3));
249         }
250
251 }
252
253
254
255 //
256 // Fetch the list of known rooms.
257 //
258 function ctdl_knrooms() {
259         global $clientsocket;
260
261         serv_puts("LKRA");
262         $response = serv_gets();
263
264         if (substr($response, 0, 1) != "1") {
265                 return array(0, NULL);
266         }
267         
268         $all_lines = array();
269         $num_lines = 0;
270
271         while (strcmp($buf = serv_gets(), "000")) {
272
273                 $thisline = array();
274
275                 $tok = strtok($buf, "|");
276                 if ($tok) $thisline["name"] = $tok;
277
278                 $tok = strtok("|");
279                 if ($tok) $thisline["flags"] = $tok;
280                 
281                 $tok = strtok("|");
282                 if ($tok) $thisline["floor"] = $tok;
283                 
284                 $tok = strtok("|");
285                 if ($tok) $thisline["order"] = $tok;
286                 
287                 $tok = strtok("|");
288                 if ($tok) $thisline["flags2"] = $tok;
289
290                 $tok = strtok("|");
291                 if ($tok) $thisline["access"] = $tok;
292
293                 if ($thisline["access"] & 8) {
294                         $thisline["hasnewmsgs"] = TRUE;
295                 }
296                 else {
297                         $thisline["hasnewmsgs"] = FALSE;
298                 }
299
300                 $num_lines = array_push($all_lines, $thisline);
301         }
302
303         return array($num_lines, $all_lines);
304
305 }
306
307
308 //
309 // Fetch the list of messages in this room.
310 // Returns: count, response, message array
311 //
312 function ctdl_msgs($mode, $count) {
313         global $clientsocket;
314
315         serv_puts("MSGS " . $mode . "|" . $count);
316         $response = serv_gets();
317
318         if (substr($response, 0, 1) != "1") {
319                 return array(0, substr($response, 4), NULL);
320         }
321         
322         $msgs = array();
323         $num_msgs = 0;
324
325         while (strcmp($buf = serv_gets(), "000")) {
326                 $num_msgs = array_push($msgs, $buf);
327         }
328
329         return array($num_msgs, substr($response, 4), $msgs);
330 }
331
332
333 ?>