5 // Implements various Citadel server commands.
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.
12 // serv_gets() -- generic function to read one line of text from the server
14 function serv_gets() {
17 $buf = fgets($clientsocket, 4096); // Read line
18 $buf = substr($buf, 0, (strlen($buf)-1) ); // strip trailing LF
24 // serv_puts() -- generic function to write one line of text to the server
26 function serv_puts($buf) {
29 fwrite($clientsocket, $buf . "\n", (strlen($buf)+1) );
30 fflush($clientsocket);
35 // text_to_server() -- sends a block of text to the server. Assumes that
36 // the server just sent back a SEND_LISTING response code
37 // and is now expecting a 000-terminated series of lines.
38 // Set 'convert_to_html' to TRUE to convert the block of
39 // text to HTML along the way.
41 function text_to_server($thetext, $convert_to_html) {
44 $this_line = strtok($thetext, "\n");
47 $this_line = trim($this_line, "\n\r");
48 if ($this_line == "000") $this_line = "-000" ;
49 serv_puts($this_line);
50 $this_line = strtok("\n");
54 serv_puts("ECHO echo test."); // FIXME
55 echo "Echo test: " . serv_gets() . "<BR>\n" ;
62 // Identify ourselves to the Citadel server (do this once after connection)
64 function ctdl_iden() {
67 // Identify client and hostname
68 serv_puts("IDEN 0|8|001|PHP web client|" . $_SERVER['REMOTE_ADDR'] );
71 // Also express our message format preferences
72 serv_puts("MSGP text/html|text/plain");
79 // login_existing_user() -- attempt to login using a supplied username/password
80 // Returns an array with two variables:
81 // 0. TRUE or FALSE to determine success or failure
82 // 1. String error message (if relevant)
84 function login_existing_user($user, $pass) {
87 serv_puts("USER " . $user);
89 if (substr($resp, 0, 1) != "3") {
90 return array(FALSE, substr($resp, 4));
93 serv_puts("PASS " . $pass);
95 if (substr($resp, 0, 1) != "2") {
96 return array(FALSE, substr($resp, 4));
99 $_SESSION["username"] = $user;
100 $_SESSION["password"] = $pass;
101 become_logged_in(substr($resp, 4));
103 return array(TRUE, "Login successful. Have fun.");
108 // create_new_user() -- attempt to create a new user
109 // using a supplied username/password
110 // Returns an array with two variables:
111 // 0. TRUE or FALSE to determine success or failure
112 // 1. String error message (if relevant)
114 function create_new_user($user, $pass) {
115 global $clientsocket;
117 serv_puts("NEWU " . $user);
119 if (substr($resp, 0, 1) != "2") {
120 return array(FALSE, substr($resp, 4));
123 serv_puts("SETP " . $pass);
125 if (substr($resp, 0, 1) != "2") {
126 return array(FALSE, substr($resp, 4));
129 $_SESSION["username"] = $user;
130 $_SESSION["password"] = $pass;
131 become_logged_in(substr($resp, 4));
133 return array(TRUE, "Login successful. Have fun.");
138 // Code common to both existing-user and new-user logins
140 function become_logged_in($server_parms) {
141 $_SESSION["logged_in"] = 1;
143 $tok = strtok($server_parms, "|");
144 if ($tok) $thisline["username"] = $tok;
147 if ($tok) $thisline["axlevel"] = $tok;
150 if ($tok) $thisline["calls"] = $tok;
153 if ($tok) $thisline["posts"] = $tok;
156 if ($tok) $thisline["userflags"] = $tok;
159 if ($tok) $thisline["usernum"] = $tok;
162 if ($tok) $thisline["lastcall"] = $tok;
164 ctdl_goto("_BASEROOM_");
170 // Learn all sorts of interesting things about the Citadel server to
171 // which we are connected.
173 function ctdl_get_serv_info() {
176 if (substr($buf, 0, 1) == "1") {
180 if ($i == 1) $_SESSION["serv_nodename"] = $buf;
181 if ($i == 2) $_SESSION["serv_humannode"] = $buf;
182 if ($i == 3) $_SESSION["serv_fqdn"] = $buf;
183 if ($i == 4) $_SESSION["serv_software"] = $buf;
184 if ($i == 6) $_SESSION["serv_city"] = $buf;
185 if ($i == 7) $_SESSION["serv_sysadmin"] = $buf;
187 } while (strcasecmp($buf, "000"));
194 // Display a system banner. (Returns completed HTML.)
195 // (This is probably temporary because it outputs more or less finalized
196 // markup. For now it's just usable.)
198 function ctdl_mesg($msgname) {
199 global $clientsocket;
201 $msgtext = "<DIV ALIGN=CENTER>\n";
203 serv_puts("MESG " . $msgname);
204 $response = serv_gets();
206 if (substr($response, 0, 1) == "1") {
207 while (strcmp($buf = serv_gets(), "000")) {
208 $msgtext .= "<TT>" . htmlspecialchars($buf)
213 $msgtext .= "<B><I>" . substr($response, 4) . "</I></B><BR>\n";
216 $msgtext .= "</DIV>\n";
222 // Fetch the list of users currently logged in.
224 function ctdl_rwho() {
225 global $clientsocket;
228 $response = serv_gets();
230 if (substr($response, 0, 1) != "1") {
231 return array(0, NULL);
234 $all_lines = array();
237 while (strcmp($buf = serv_gets(), "000")) {
241 $tok = strtok($buf, "|");
242 if ($tok) $thisline["session"] = $tok;
245 if ($tok) $thisline["user"] = $tok;
248 if ($tok) $thisline["room"] = $tok;
251 if ($tok) $thisline["host"] = $tok;
254 if ($tok) $thisline["client"] = $tok;
256 // IGnore the rest of the fields for now.
258 $num_lines = array_push($all_lines, $thisline);
261 return array($num_lines, $all_lines);
269 function ctdl_goto($to_where) {
271 serv_puts("GOTO " . $to_where);
272 $response = serv_gets();
274 if (substr($response, 0, 1) == "2") {
275 $_SESSION["room"] = strtok(substr($response, 4), "|");
276 return array(TRUE, substr($response, 0, 3));
280 return array(FALSE, substr($response, 0, 3));
288 // Fetch the list of known rooms.
290 function ctdl_knrooms() {
291 global $clientsocket;
294 $response = serv_gets();
296 if (substr($response, 0, 1) != "1") {
297 return array(0, NULL);
300 $all_lines = array();
303 while (strcmp($buf = serv_gets(), "000")) {
307 $tok = strtok($buf, "|");
308 if ($tok) $thisline["name"] = $tok;
311 if ($tok) $thisline["flags"] = $tok;
314 if ($tok) $thisline["floor"] = $tok;
317 if ($tok) $thisline["order"] = $tok;
320 if ($tok) $thisline["flags2"] = $tok;
323 if ($tok) $thisline["access"] = $tok;
325 if ($thisline["access"] & 8) {
326 $thisline["hasnewmsgs"] = TRUE;
329 $thisline["hasnewmsgs"] = FALSE;
332 $num_lines = array_push($all_lines, $thisline);
335 return array($num_lines, $all_lines);
341 // Fetch the list of messages in this room.
342 // Returns: count, response, message array
344 function ctdl_msgs($mode, $count) {
345 global $clientsocket;
347 serv_puts("MSGS " . $mode . "|" . $count);
348 $response = serv_gets();
350 if (substr($response, 0, 1) != "1") {
351 return array(0, substr($response, 4), NULL);
357 while (strcmp($buf = serv_gets(), "000")) {
358 $num_msgs = array_push($msgs, $buf);
361 return array($num_msgs, substr($response, 4), $msgs);
365 // Load a message from the server.
366 function ctdl_fetch_message($msgnum) {
367 global $clientsocket;
369 serv_puts("MSG4 " . $msgnum);
370 $response = serv_gets();
372 if (substr($response, 0, 1) != "1") {
373 return array(FALSE, substr($response, 4), NULL);
377 while (strcmp($buf = serv_gets(), "000")) {
378 if (substr($buf, 0, 4) == "text") {
379 // We're in the text body. New loop here.
380 $fields["text"] = ctdl_msg4_from_server();
381 return array(TRUE, substr($response, 4), $fields);
384 $fields[substr($buf, 0, 4)] = substr($buf, 5);
388 // Message terminated prematurely (no text body)
389 return array(FALSE, substr($response, 4), $fields);
392 // Support function for ctdl_fetch_message(). This handles the text body
393 // portion of the message, converting various formats to HTML as
395 function ctdl_msg4_from_server() {
398 $msgformat = "text/plain";
402 while (strcmp($buf = serv_gets(), "000")) {
403 if ($in_body == FALSE) {
404 if (strlen($buf) == 0) {
408 if (!strncasecmp($buf, "content-type: ", 14)) {
409 $msgformat = substr($buf, 14);
414 if (!strcasecmp($msgformat, "text/html")) {
417 else if (!strcasecmp($msgformat, "text/plain")) {
418 $txt .= "<TT>" . htmlspecialchars($buf) . "</TT><BR>\n" ;
420 else if (!strcasecmp($msgformat, "text/x-citadel-variformat")) {
421 if (substr($previous_line, 0, 1) == " ") {
424 $txt .= htmlspecialchars($buf);
427 $txt .= htmlspecialchars($buf);
429 $previous_line = $buf;