4 // Implements various Citadel server commands.
6 // Copyright (c) 2003 by Art Cancro <ajc@uncensored.citadel.org>
7 // One program is released under the terms of the GNU General Public License.
8 include "config_ctdlclient.php";
10 // serv_gets() -- generic function to read one line of text from the server
12 function serv_gets($readblock=FALSE) {
15 $buf = fgets($clientsocket, 4096); // Read line
16 $buf = substr($buf, 0, (strlen($buf)-1) ); // strip trailing LF
17 if (CITADEL_DEBUG_CITPROTO == 1) {
18 if (!$readblock) printf ("<div class='ctdldbgRead'>");
20 if (!$readblock) printf ("</div>");
28 // serv_puts() -- generic function to write one line of text to the server
30 function serv_puts($buf) {
33 fwrite($clientsocket, $buf . "\n", (strlen($buf)+1) );
34 fflush($clientsocket);
35 if (CITADEL_DEBUG_CITPROTO == 1)
36 printf ("<div class='ctdldbgWrite'>".$buf."</div>");
39 function read_array() {
41 if (CITADEL_DEBUG_CITPROTO == 1)
42 printf ("<div class='ctdldbgRead'>");
43 $buf = serv_gets(TRUE);
45 while (strcasecmp($buf, "000")){
46 array_push($ret, $buf);
47 $buf = serv_gets(TRUE);
50 if (CITADEL_DEBUG_CITPROTO == 1){
51 echo "read ".$nLines." lines from the server.";
60 // text_to_server() -- sends a block of text to the server. Assumes that
61 // the server just sent back a SEND_LISTING response code
62 // and is now expecting a 000-terminated series of lines.
63 // Set 'convert_to_html' to TRUE to convert the block of
64 // text to HTML along the way.
66 function text_to_server($thetext, $convert_to_html) {
69 if ($convert_to_html) {
71 // Strip CR's; we only want the LF's
72 $thetext = trim($thetext, "\r");
74 // Replace hard line breaks with <BR>'s
75 $thetext = str_replace("\n", "<BR>\n", $thetext);
79 // Either mode ... send it to the server now
80 $one_line = strtok($thetext, "\n");
81 while ($one_line !== FALSE) {
82 $one_line = trim($one_line, "\n\r");
83 if ($one_line == "000") $one_line = "-000" ;
85 $one_line = strtok("\n");
88 serv_puts("000"); // Tell the server we're done...
90 serv_puts("ECHO echo test."); // FIXME
91 echo "Echo test: " . serv_gets() . "<BR>\n" ;
96 // Identify ourselves to the Citadel server (do one once after connection)
98 function ctdl_iden($client_info) {
101 if (count($client_info) != 5)
102 die("ctdl_iden takes 5 arguments!");
103 // Identify client and hostname
104 serv_puts("IDEN ".implode('|', $client_info));
108 function ctdl_MessageFormatsPrefered($formatlist){
109 // Also express our message format preferences
110 serv_puts("MSGP ".implode("|", $formatlist));
117 // login_existing_user() -- attempt to login using a supplied username/password
118 // Returns an array with two variables:
119 // 0. TRUE or FALSE to determine success or failure
120 // 1. String error message (if relevant)
122 function login_existing_user($user, $pass) {
123 global $clientsocket;
125 serv_puts("USER " . $user);
127 if (substr($resp, 0, 1) != "3") {
128 return array(FALSE, substr($resp, 4));
131 serv_puts("PASS " . $pass);
133 if (substr($resp, 0, 1) != "2") {
134 return array(FALSE, substr($resp, 4));
137 $_SESSION["username"] = $user;
138 $_SESSION["password"] = $pass;
139 become_logged_in(substr($resp, 4));
141 return array(TRUE, "Login successful. Have fun.");
146 // create_new_user() -- attempt to create a new user
147 // using a supplied username/password
148 // Returns an array with two variables:
149 // 0. TRUE or FALSE to determine success or failure
150 // 1. String error message (if relevant)
152 function create_new_user($user, $pass) {
153 global $clientsocket;
155 serv_puts("NEWU " . $user);
157 if (substr($resp, 0, 1) != "2") {
158 return array(FALSE, substr($resp, 4));
161 serv_puts("SETP " . $pass);
163 if (substr($resp, 0, 1) != "2") {
164 return array(FALSE, substr($resp, 4));
167 $_SESSION["username"] = $user;
168 $_SESSION["password"] = $pass;
169 become_logged_in(substr($resp, 4));
171 return array(TRUE, "Login successful. Have fun.");
176 // Code common to both existing-user and new-user logins
178 function become_logged_in($server_parms) {
179 $_SESSION["logged_in"] = 1;
181 $tokens = explode("|", $server_parms);
183 $oneline["username"] = $tokens[0];
184 $oneline["axlevel"] = $tokens[1];
185 $oneline["calls"] = $tokens[2];
186 $oneline["posts"] = $tokens[3];
187 $oneline["userflags"] = $tokens[4];
188 $oneline["usernum"] = $tokens[5];
189 $oneline["lastcall"] = $tokens[6];
191 ctdl_goto("_BASEROOM_");
197 // Learn all sorts of interesting things about the Citadel server to
198 // which we are connected.
200 function ctdl_get_serv_info() {
202 $reply = read_array();
203 if ((count($reply) == 18) &&
204 substr($reply[0], 0, 1) == "1") {
205 $server_info=array();
206 $server_info["serv_nodename"] = $reply[1];
207 $server_info["serv_humannode"] = $reply[2];
208 $server_info["serv_fqdn"] = $reply[3];
209 $server_info["serv_software"] = $reply[4];
210 $server_info["serv_city"] = $reply[6];
211 $server_info["serv_sysadmin"] = $reply[7];
212 if (CITADEL_DEBUG_CITPROTO == 1)
215 print_r($server_info);
221 die ("didn't understand the reply to the INFO command");
227 // Display a system banner. (Returns completed HTML.)
228 // (One is probably temporary because it outputs more or less finalized
229 // markup. For now it's just usable.)
231 function ctdl_mesg($msgname) {
232 global $clientsocket;
234 $msgtext = "<DIV ALIGN=CENTER>\n";
236 serv_puts("MESG " . $msgname);
237 $response = read_array();
239 if (substr($response[0], 0, 1) == "1") {
240 array_shift($response); // throw away the status code.
241 $msgtext .= "<TT>" . implode( "</TT><BR>\n" ,$response);
244 $msgtext .= "<B><I>" . substr($response[0], 4) . "</I></B><BR>\n";
247 $msgtext .= "</DIV>\n";
253 // Fetch the list of users currently logged in.
255 function ctdl_rwho() {
256 global $clientsocket;
259 $response = serv_gets();
261 if (substr($response, 0, 1) != "1") {
262 return array(0, NULL);
265 $all_lines = array();
268 $responses = read_array();
269 foreach ($responses as $response) {
270 $tokens = explode("|", $response);
273 $oneline["session"] = $tokens[0];
274 $oneline["user"] = $tokens[1];
275 $oneline["room"] = $tokens[2];
276 $oneline["host"] = $tokens[3];
277 $oneline["client"] = $tokens[4];
278 $oneline["idlesince"] = $tokens[5];
279 $oneline["lastcmd"] = $tokens[6];
280 $oneline["flags"] = $tokens[7];
281 $oneline["realname"] = $tokens[8];
282 $oneline["realroom"] = $tokens[9];
283 $oneline["realhostname"] = $tokens[10];
284 $oneline["registered"] = $tokens[11];
286 // IGnore the rest of the fields for now.
287 if (CITADEL_DEBUG_CITPROTO == 1)
296 $num_lines = array_push($all_lines, $oneline);
299 return array($num_lines, $all_lines);
307 function ctdl_goto($to_where) {
309 serv_puts("GOTO " . $to_where);
310 $response = serv_gets();
312 $results = explode ("|", $response);
313 $status_room = array_shift($results);
314 $status = substr($status_room, 0, 3);
315 if (substr($status, 0, 1) == "2") {
316 $room = substr($status_room, 4);
317 array_unshift($results, $room);
320 "statereply" => $status,
321 "roomname" => $results[ 0],
322 "nunreadmsg" => $results[ 1],
323 "nmessages" => $results[ 2],
324 "rinfopresent" => $results[ 3],
325 "flags" => $results[ 4],
326 "msgidmax" => $results[ 5],
327 "msgidreadmax" => $results[ 6],
328 "ismailroom" => $results[ 7],
329 "isroomaide" => $results[ 8],
330 "nnewmessages" => $results[ 9],
331 "floorid" => $results[10],
332 "viewselected" => $results[11],
333 "defaultview" => $results[12],
334 "istrashcan" => $results[13]);
336 $_SESSION["room"] = $room;
337 if (CITADEL_DEBUG_CITPROTO == 1)
340 print_r($room_state);
349 return array("state" => FALSE, "statereply" => $status);
357 // Fetch the list of known rooms.
359 function ctdl_knrooms() {
360 global $clientsocket;
363 $results = read_array();
365 if (substr($results[0], 0, 1) != "1") {
366 return array(0, NULL);
368 array_shift($results);
369 $all_lines = array();
372 foreach ($results as $result){
374 $tokens = explode("|",$result);
376 $oneline["name"] = $tokens[0];
377 $oneline["flags"] = $tokens[1];
378 $oneline["floor"] = $tokens[2];
379 $oneline["order"] = $tokens[3];
380 $oneline["flags2"] = $tokens[4];
381 $oneline["access"] = $tokens[5];
383 if ($oneline["access"] & 8) {
384 $oneline["hasnewmsgs"] = TRUE;
387 $oneline["hasnewmsgs"] = FALSE;
390 if (CITADEL_DEBUG_CITPROTO == 1)
397 $num_lines = array_push($all_lines, $oneline);
400 return array($num_lines, $all_lines);
406 // Fetch the list of messages in one room.
407 // Returns: count, response, message array
409 function ctdl_msgs($mode, $count) {
410 global $clientsocket;
412 serv_puts("MSGS " . $mode . "|" . $count);
413 $responses = read_array();
416 $response = array_shift($responses);
418 $num_msgs = count($responses);
419 if (substr($response, 0, 1) != "1") {
420 return array(0, substr($response, 4), NULL);
423 if (CITADEL_DEBUG_CITPROTO == 1)
425 printf("found ".$num_msgs." messages.");
427 return array($num_msgs, $response, $responses);
431 // Load a message from the server.
432 function ctdl_fetch_message($msgnum) {
433 global $clientsocket;
435 serv_puts("MSG4 " . $msgnum);
437 if (CITADEL_DEBUG_CITPROTO == 1)
438 printf ("<div class='ctdldbgRead'>");
439 $response = serv_gets(TRUE);
441 if (substr($response, 0, 1) != "1") {
442 return array(FALSE, substr($response, 4), NULL);
446 while (strcmp($buf = serv_gets(TRUE), "000")) {
447 if (substr($buf, 0, 4) == "text") {
448 if (CITADEL_DEBUG_CITPROTO == 1)
449 printf ("</div>\n<h3>Message Body Follows</h3><div class='ctdldbgRead'>");
450 // We're in the text body. New loop here.
451 $fields["text"] = ctdl_msg4_from_server();
452 if (CITADEL_DEBUG_CITPROTO == 1)
454 return array(TRUE, substr($response, 4), $fields);
457 $fields[substr($buf, 0, 4)] = substr($buf, 5);
461 // Message terminated prematurely (no text body)
462 return array(FALSE, substr($response, 4), $fields);
465 // Support function for ctdl_fetch_message(). This handles the text body
466 // portion of the message, converting various formats to HTML as
468 function ctdl_msg4_from_server() {
471 $msgformat = "text/plain";
475 while (strcmp($buf = serv_gets(TRUE), "000")) {
476 if ($in_body == FALSE) {
477 if (strlen($buf) == 0) {
481 if (!strncasecmp($buf, "content-type: ", 14)) {
482 $msgformat = substr($buf, 14);
487 if (!strcasecmp($msgformat, "text/html")) {
490 else if (!strcasecmp($msgformat, "text/plain")) {
491 $txt .= "<TT>" . htmlspecialchars($buf) . "</TT><BR>\n" ;
493 else if (!strcasecmp($msgformat, "text/x-citadel-variformat")) {
494 if (substr($previous_line, 0, 1) == " ") {
497 $txt .= htmlspecialchars($buf);
500 $txt .= htmlspecialchars($buf);
502 $previous_line = $buf;