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 if ($convert_to_html) {
46 // Strip CR's; we only want the LF's
47 $thetext = trim($thetext, "\r");
49 // Replace hard line breaks with <BR>'s
50 $thetext = str_replace("\n", "<BR>\n", $thetext);
54 // Either mode ... send it to the server now
55 $this_line = strtok($thetext, "\n");
56 while ($this_line !== FALSE) {
57 $this_line = trim($this_line, "\n\r");
58 if ($this_line == "000") $this_line = "-000" ;
59 serv_puts($this_line);
60 $this_line = strtok("\n");
63 serv_puts("000"); // Tell the server we're done...
65 serv_puts("ECHO echo test."); // FIXME
66 echo "Echo test: " . serv_gets() . "<BR>\n" ;
73 // Identify ourselves to the Citadel server (do this once after connection)
75 function ctdl_iden() {
78 // Identify client and hostname
79 serv_puts("IDEN 0|8|001|PHP web client|" . $_SERVER['REMOTE_ADDR'] );
82 // Also express our message format preferences
83 serv_puts("MSGP text/html|text/plain");
90 // login_existing_user() -- attempt to login using a supplied username/password
91 // Returns an array with two variables:
92 // 0. TRUE or FALSE to determine success or failure
93 // 1. String error message (if relevant)
95 function login_existing_user($user, $pass) {
98 serv_puts("USER " . $user);
100 if (substr($resp, 0, 1) != "3") {
101 return array(FALSE, substr($resp, 4));
104 serv_puts("PASS " . $pass);
106 if (substr($resp, 0, 1) != "2") {
107 return array(FALSE, substr($resp, 4));
110 $_SESSION["username"] = $user;
111 $_SESSION["password"] = $pass;
112 become_logged_in(substr($resp, 4));
114 return array(TRUE, "Login successful. Have fun.");
119 // create_new_user() -- attempt to create a new user
120 // using a supplied username/password
121 // Returns an array with two variables:
122 // 0. TRUE or FALSE to determine success or failure
123 // 1. String error message (if relevant)
125 function create_new_user($user, $pass) {
126 global $clientsocket;
128 serv_puts("NEWU " . $user);
130 if (substr($resp, 0, 1) != "2") {
131 return array(FALSE, substr($resp, 4));
134 serv_puts("SETP " . $pass);
136 if (substr($resp, 0, 1) != "2") {
137 return array(FALSE, substr($resp, 4));
140 $_SESSION["username"] = $user;
141 $_SESSION["password"] = $pass;
142 become_logged_in(substr($resp, 4));
144 return array(TRUE, "Login successful. Have fun.");
149 // Code common to both existing-user and new-user logins
151 function become_logged_in($server_parms) {
152 $_SESSION["logged_in"] = 1;
154 $tok = strtok($server_parms, "|");
155 if ($tok) $thisline["username"] = $tok;
158 if ($tok) $thisline["axlevel"] = $tok;
161 if ($tok) $thisline["calls"] = $tok;
164 if ($tok) $thisline["posts"] = $tok;
167 if ($tok) $thisline["userflags"] = $tok;
170 if ($tok) $thisline["usernum"] = $tok;
173 if ($tok) $thisline["lastcall"] = $tok;
175 ctdl_goto("_BASEROOM_");
181 // Learn all sorts of interesting things about the Citadel server to
182 // which we are connected.
184 function ctdl_get_serv_info() {
187 if (substr($buf, 0, 1) == "1") {
191 if ($i == 1) $_SESSION["serv_nodename"] = $buf;
192 if ($i == 2) $_SESSION["serv_humannode"] = $buf;
193 if ($i == 3) $_SESSION["serv_fqdn"] = $buf;
194 if ($i == 4) $_SESSION["serv_software"] = $buf;
195 if ($i == 6) $_SESSION["serv_city"] = $buf;
196 if ($i == 7) $_SESSION["serv_sysadmin"] = $buf;
198 } while (strcasecmp($buf, "000"));
205 // Display a system banner. (Returns completed HTML.)
206 // (This is probably temporary because it outputs more or less finalized
207 // markup. For now it's just usable.)
209 function ctdl_mesg($msgname) {
210 global $clientsocket;
212 $msgtext = "<DIV ALIGN=CENTER>\n";
214 serv_puts("MESG " . $msgname);
215 $response = serv_gets();
217 if (substr($response, 0, 1) == "1") {
218 while (strcmp($buf = serv_gets(), "000")) {
219 $msgtext .= "<TT>" . htmlspecialchars($buf)
224 $msgtext .= "<B><I>" . substr($response, 4) . "</I></B><BR>\n";
227 $msgtext .= "</DIV>\n";
233 // Fetch the list of users currently logged in.
235 function ctdl_rwho() {
236 global $clientsocket;
239 $response = serv_gets();
241 if (substr($response, 0, 1) != "1") {
242 return array(0, NULL);
245 $all_lines = array();
248 while (strcmp($buf = serv_gets(), "000")) {
252 $tok = strtok($buf, "|");
253 if ($tok) $thisline["session"] = $tok;
256 if ($tok) $thisline["user"] = $tok;
259 if ($tok) $thisline["room"] = $tok;
262 if ($tok) $thisline["host"] = $tok;
265 if ($tok) $thisline["client"] = $tok;
267 // IGnore the rest of the fields for now.
269 $num_lines = array_push($all_lines, $thisline);
272 return array($num_lines, $all_lines);
280 function ctdl_goto($to_where) {
282 serv_puts("GOTO " . $to_where);
283 $response = serv_gets();
285 if (substr($response, 0, 1) == "2") {
286 $_SESSION["room"] = strtok(substr($response, 4), "|");
287 return array(TRUE, substr($response, 0, 3));
291 return array(FALSE, substr($response, 0, 3));
299 // Fetch the list of known rooms.
301 function ctdl_knrooms() {
302 global $clientsocket;
305 $response = serv_gets();
307 if (substr($response, 0, 1) != "1") {
308 return array(0, NULL);
311 $all_lines = array();
314 while (strcmp($buf = serv_gets(), "000")) {
318 $tok = strtok($buf, "|");
319 if ($tok) $thisline["name"] = $tok;
322 if ($tok) $thisline["flags"] = $tok;
325 if ($tok) $thisline["floor"] = $tok;
328 if ($tok) $thisline["order"] = $tok;
331 if ($tok) $thisline["flags2"] = $tok;
334 if ($tok) $thisline["access"] = $tok;
336 if ($thisline["access"] & 8) {
337 $thisline["hasnewmsgs"] = TRUE;
340 $thisline["hasnewmsgs"] = FALSE;
343 $num_lines = array_push($all_lines, $thisline);
346 return array($num_lines, $all_lines);
352 // Fetch the list of messages in this room.
353 // Returns: count, response, message array
355 function ctdl_msgs($mode, $count) {
356 global $clientsocket;
358 serv_puts("MSGS " . $mode . "|" . $count);
359 $response = serv_gets();
361 if (substr($response, 0, 1) != "1") {
362 return array(0, substr($response, 4), NULL);
368 while (strcmp($buf = serv_gets(), "000")) {
369 $num_msgs = array_push($msgs, $buf);
372 return array($num_msgs, substr($response, 4), $msgs);
376 // Load a message from the server.
377 function ctdl_fetch_message($msgnum) {
378 global $clientsocket;
380 serv_puts("MSG4 " . $msgnum);
381 $response = serv_gets();
383 if (substr($response, 0, 1) != "1") {
384 return array(FALSE, substr($response, 4), NULL);
388 while (strcmp($buf = serv_gets(), "000")) {
389 if (substr($buf, 0, 4) == "text") {
390 // We're in the text body. New loop here.
391 $fields["text"] = ctdl_msg4_from_server();
392 return array(TRUE, substr($response, 4), $fields);
395 $fields[substr($buf, 0, 4)] = substr($buf, 5);
399 // Message terminated prematurely (no text body)
400 return array(FALSE, substr($response, 4), $fields);
403 // Support function for ctdl_fetch_message(). This handles the text body
404 // portion of the message, converting various formats to HTML as
406 function ctdl_msg4_from_server() {
409 $msgformat = "text/plain";
413 while (strcmp($buf = serv_gets(), "000")) {
414 if ($in_body == FALSE) {
415 if (strlen($buf) == 0) {
419 if (!strncasecmp($buf, "content-type: ", 14)) {
420 $msgformat = substr($buf, 14);
425 if (!strcasecmp($msgformat, "text/html")) {
428 else if (!strcasecmp($msgformat, "text/plain")) {
429 $txt .= "<TT>" . htmlspecialchars($buf) . "</TT><BR>\n" ;
431 else if (!strcasecmp($msgformat, "text/x-citadel-variformat")) {
432 if (substr($previous_line, 0, 1) == " ") {
435 $txt .= htmlspecialchars($buf);
438 $txt .= htmlspecialchars($buf);
440 $previous_line = $buf;