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