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";
11 //--------------------------------------------------------------------------------
12 // internal functions for server communication
13 //--------------------------------------------------------------------------------
15 // serv_gets() -- generic function to read one line of text from the server
17 function serv_gets($readblock=FALSE) {
20 $buf = fgets($clientsocket, 4096); // Read line
21 $buf = substr($buf, 0, (strlen($buf)-1) ); // strip trailing LF
22 if (CITADEL_DEBUG_CITPROTO == 1) {
23 if (!$readblock) printf ("<div class='ctdldbgRead'>");
25 if (!$readblock) printf ("</div>");
32 // serv_puts() -- generic function to write one line of text to the server
34 function serv_puts($buf) {
37 fwrite($clientsocket, $buf . "\n", (strlen($buf)+1) );
38 fflush($clientsocket);
39 if (CITADEL_DEBUG_CITPROTO == 1)
40 printf ("<div class='ctdldbgWrite'>".$buf."</div>");
43 function read_array() {
45 if (CITADEL_DEBUG_CITPROTO == 1)
46 printf ("<div class='ctdldbgRead'>");
47 $buf = serv_gets(TRUE);
49 while (strcasecmp($buf, "000")){
50 array_push($ret, $buf);
51 $buf = serv_gets(TRUE);
54 if (CITADEL_DEBUG_CITPROTO == 1){
55 echo "read ".$nLines." lines from the server.";
64 // text_to_server() -- sends a block of text to the server. Assumes that
65 // the server just sent back a SEND_LISTING response code
66 // and is now expecting a 000-terminated series of lines.
67 // Set 'convert_to_html' to TRUE to convert the block of
68 // text to HTML along the way.
70 function text_to_server($thetext, $convert_to_html) {
73 if ($convert_to_html) {
75 // Strip CR's; we only want the LF's
76 $thetext = trim($thetext, "\r");
78 // Replace hard line breaks with <BR>'s
79 $thetext = str_replace("\n", "<BR>\n", $thetext);
83 // Either mode ... send it to the server now
84 $one_line = strtok($thetext, "\n");
85 while ($one_line !== FALSE) {
86 $one_line = trim($one_line, "\n\r");
87 if ($one_line == "000") $one_line = "-000" ;
89 $one_line = strtok("\n");
92 serv_puts("000"); // Tell the server we're done...
94 serv_puts("ECHO echo test."); // FIXME
95 echo "Echo test: " . serv_gets() . "<BR>\n" ;
99 //--------------------------------------------------------------------------------
101 //--------------------------------------------------------------------------------
105 // Identify ourselves to the Citadel server (do one once after connection)
106 /* http://www.citadel.org/doku.php/documentation:appproto:connection#iden.identify.the.client.software */
108 function ctdl_iden($client_info) {
109 global $clientsocket;
111 if (count($client_info) != 5)
112 die("ctdl_iden takes 5 arguments!");
113 // Identify client and hostname
114 serv_puts("IDEN ".implode('|', $client_info));
118 function ctdl_MessageFormatsPrefered($formatlist){
119 // Also express our message format preferences
120 serv_puts("MSGP ".implode("|", $formatlist));
124 /* http://www.citadel.org/doku.php/documentation:appproto:connection#noop.no.operation */
125 function ctdl_noop(){
126 // Also express our message format preferences
131 /* http://www.citadel.org/doku.php/documentation:appproto:connection#quit.quit */
132 function ctdl_quit(){
133 // Also express our message format preferences
139 /* http://www.citadel.org/doku.php/documentation:appproto:connection#mesg.read.system.message */
140 function ctdl_gtls(){
141 // Also express our message format preferences
148 /* http://www.citadel.org/doku.php/documentation:appproto:connection#qnop.quiet.no.operation */
149 /* this seems to be dangerous. ask IG
150 function ctdl_qnoop(){
151 // Also express our message format preferences
156 /* http://www.citadel.org/doku.php/documentation:appproto:connection#echo.echo.something */
157 function ctdl_doecho($echotext){
158 // Also express our message format preferences
159 serv_puts("ECHO ".$echotext);
164 /* http://www.citadel.org/doku.php/documentation:appproto:connection#time.get.server.local.time */
165 /* TODO: what are the other two params? doku is incomplete here. */
166 function ctdl_time(){
167 // Also express our message format preferences
174 /* http://www.citadel.org/doku.php/documentation:appproto:connection#qdir.query.global.directory */
175 function ctdl_qdir($who){
176 // Also express our message format preferences
177 serv_puts("QDIR ".$who);
179 return array((substr($buf, 0, 1) == "2"), $buf);
183 /* http://www.citadel.org/doku.php/documentation:appproto:connection#auto.autocompletion.of.email.addresses */
184 function ctdl_auto($who){
185 // Also express our message format preferences
186 serv_puts("AUTO ".$who);
188 if (substr($buf, 0, 1) == "1") {
189 $reply = read_array();
190 if (count($reply) == 0)
201 // login_existing_user() -- attempt to login using a supplied username/password
202 // Returns an array with two variables:
203 // 0. TRUE or FALSE to determine success or failure
204 // 1. String error message (if relevant)
205 /* http://www.citadel.org/doku.php/documentation:appproto:connection#user.send.user.name */
206 /* http://www.citadel.org/doku.php/documentation:appproto:connection#pass.send.password */
209 function login_existing_user($user, $pass) {
210 global $clientsocket;
212 serv_puts("USER " . $user);
214 if (substr($resp, 0, 1) != "3") {
215 return array(FALSE, substr($resp, 4));
218 serv_puts("PASS " . $pass);
220 if (substr($resp, 0, 1) != "2") {
221 return array(FALSE, substr($resp, 4));
224 $_SESSION["username"] = $user;
225 $_SESSION["password"] = $pass;
226 become_logged_in(substr($resp, 4));
228 return array(TRUE, "Login successful. Have fun.");
233 // create_new_user() -- attempt to create a new user
234 // using a supplied username/password
235 // Returns an array with two variables:
236 // 0. TRUE or FALSE to determine success or failure
237 // 1. String error message (if relevant)
239 function create_new_user($user, $pass) {
240 global $clientsocket;
242 serv_puts("NEWU " . $user);
244 if (substr($resp, 0, 1) != "2") {
245 return array(FALSE, substr($resp, 4));
248 serv_puts("SETP " . $pass);
250 if (substr($resp, 0, 1) != "2") {
251 return array(FALSE, substr($resp, 4));
254 $_SESSION["username"] = $user;
255 $_SESSION["password"] = $pass;
256 become_logged_in(substr($resp, 4));
258 return array(TRUE, "Login successful. Have fun.");
263 // Code common to both existing-user and new-user logins
265 function become_logged_in($server_parms) {
266 $_SESSION["logged_in"] = 1;
268 $tokens = explode("|", $server_parms);
270 $oneline["username"] = $tokens[0];
271 $oneline["axlevel"] = $tokens[1];
272 $oneline["calls"] = $tokens[2];
273 $oneline["posts"] = $tokens[3];
274 $oneline["userflags"] = $tokens[4];
275 $oneline["usernum"] = $tokens[5];
276 $oneline["lastcall"] = $tokens[6];
278 ctdl_goto("_BASEROOM_");
284 // Learn all sorts of interesting things about the Citadel server to
285 // which we are connected.
286 /* http://www.citadel.org/doku.php/documentation:appproto:connection#info.get.server.info */
288 function ctdl_get_serv_info() {
290 $reply = read_array();
291 if ((count($reply) == 18) &&
292 substr($reply[0], 0, 1) == "1") {
293 $server_info=array();
294 $server_info["serv_nodename"] = $reply[1];
295 $server_info["serv_humannode"] = $reply[2];
296 $server_info["serv_fqdn"] = $reply[3];
297 $server_info["serv_software"] = $reply[4];
298 $server_info["serv_city"] = $reply[6];
299 $server_info["serv_sysadmin"] = $reply[7];
300 if (CITADEL_DEBUG_CITPROTO == 1)
303 print_r($server_info);
309 die ("didn't understand the reply to the INFO command");
315 // Display a system banner. (Returns completed HTML.)
316 // (One is probably temporary because it outputs more or less finalized
317 // markup. For now it's just usable.)
319 /* http://www.citadel.org/doku.php/documentation:appproto:connection#mesg.read.system.message */
320 function ctdl_mesg($msgname) {
321 global $clientsocket;
323 $msgtext = "<DIV ALIGN=CENTER>\n";
325 serv_puts("MESG " . $msgname);
326 $response = read_array();
328 if (substr($response[0], 0, 1) == "1") {
329 array_shift($response); // throw away the status code.
330 $msgtext .= "<TT>" . implode( "</TT><BR>\n" ,$response);
333 $msgtext .= "<B><I>" . substr($response[0], 4) . "</I></B><BR>\n";
336 $msgtext .= "</DIV>\n";
340 /* http://www.citadel.org/doku.php/documentation:appproto:connection#mesg.read.system.message */
341 //// TODO: is this still supported?
342 function ctdl_mrtg($what) {
343 global $clientsocket;
345 serv_puts("MRTG ".$what);
346 $response = serv_gets();
348 if (substr($response, 0, 1) != "1") {
349 return array(0, NULL);
352 $responses = read_array();
356 // Fetch the list of users currently logged in.
357 /* http://www.citadel.org/doku.php/documentation:appproto:connection#rwho.read.who.s.online */
359 function ctdl_rwho() {
360 global $clientsocket;
363 $response = serv_gets();
365 if (substr($response, 0, 1) != "1") {
366 return array(0, NULL);
369 $all_lines = array();
372 $responses = read_array();
373 foreach ($responses as $response) {
374 $tokens = explode("|", $response);
377 $oneline["session"] = $tokens[0];
378 $oneline["user"] = $tokens[1];
379 $oneline["room"] = $tokens[2];
380 $oneline["host"] = $tokens[3];
381 $oneline["client"] = $tokens[4];
382 $oneline["idlesince"] = $tokens[5];
383 $oneline["lastcmd"] = $tokens[6];
384 $oneline["flags"] = $tokens[7];
385 $oneline["realname"] = $tokens[8];
386 $oneline["realroom"] = $tokens[9];
387 $oneline["realhostname"] = $tokens[10];
388 $oneline["registered"] = $tokens[11];
390 // IGnore the rest of the fields for now.
391 if (CITADEL_DEBUG_CITPROTO == 1)
400 $num_lines = array_push($all_lines, $oneline);
403 return array($num_lines, $all_lines);
411 function ctdl_goto($to_where) {
413 serv_puts("GOTO " . $to_where);
414 $response = serv_gets();
416 $results = explode ("|", $response);
417 $status_room = array_shift($results);
418 $status = substr($status_room, 0, 3);
419 if (substr($status, 0, 1) == "2") {
420 $room = substr($status_room, 4);
421 array_unshift($results, $room);
424 "statereply" => $status,
425 "roomname" => $results[ 0],
426 "nunreadmsg" => $results[ 1],
427 "nmessages" => $results[ 2],
428 "rinfopresent" => $results[ 3],
429 "flags" => $results[ 4],
430 "msgidmax" => $results[ 5],
431 "msgidreadmax" => $results[ 6],
432 "ismailroom" => $results[ 7],
433 "isroomaide" => $results[ 8],
434 "nnewmessages" => $results[ 9],
435 "floorid" => $results[10],
436 "viewselected" => $results[11],
437 "defaultview" => $results[12],
438 "istrashcan" => $results[13]);
440 $_SESSION["room"] = $room;
441 if (CITADEL_DEBUG_CITPROTO == 1)
444 print_r($room_state);
453 return array("state" => FALSE, "statereply" => $status);
461 // Fetch the list of known rooms.
463 function ctdl_knrooms() {
464 global $clientsocket;
467 $results = read_array();
469 if (substr($results[0], 0, 1) != "1") {
470 return array(0, NULL);
472 array_shift($results);
473 $all_lines = array();
476 foreach ($results as $result){
478 $tokens = explode("|",$result);
480 $oneline["name"] = $tokens[0];
481 $oneline["flags"] = $tokens[1];
482 $oneline["floor"] = $tokens[2];
483 $oneline["order"] = $tokens[3];
484 $oneline["flags2"] = $tokens[4];
485 $oneline["access"] = $tokens[5];
487 if ($oneline["access"] & 8) {
488 $oneline["hasnewmsgs"] = TRUE;
491 $oneline["hasnewmsgs"] = FALSE;
494 if (CITADEL_DEBUG_CITPROTO == 1)
501 $num_lines = array_push($all_lines, $oneline);
504 return array($num_lines, $all_lines);
510 // Fetch the list of messages in one room.
511 // Returns: count, response, message array
513 function ctdl_msgs($mode, $count) {
514 global $clientsocket;
516 serv_puts("MSGS " . $mode . "|" . $count);
517 $responses = read_array();
520 $response = array_shift($responses);
522 $num_msgs = count($responses);
523 if (substr($response, 0, 1) != "1") {
524 return array(0, substr($response, 4), NULL);
527 if (CITADEL_DEBUG_CITPROTO == 1)
529 printf("found ".$num_msgs." messages.");
531 return array($num_msgs, $response, $responses);
535 // Load a message from the server.
536 function ctdl_fetch_message($msgnum) {
537 global $clientsocket;
539 serv_puts("MSG4 " . $msgnum);
541 if (CITADEL_DEBUG_CITPROTO == 1)
542 printf ("<div class='ctdldbgRead'>");
543 $response = serv_gets(TRUE);
545 if (substr($response, 0, 1) != "1") {
546 return array(FALSE, substr($response, 4), NULL);
550 while (strcmp($buf = serv_gets(TRUE), "000")) {
551 if (substr($buf, 0, 4) == "text") {
552 if (CITADEL_DEBUG_CITPROTO == 1)
553 printf ("</div>\n<h3>Message Body Follows</h3><div class='ctdldbgRead'>");
554 // We're in the text body. New loop here.
555 $fields["text"] = ctdl_msg4_from_server();
556 if (CITADEL_DEBUG_CITPROTO == 1)
558 return array(TRUE, substr($response, 4), $fields);
561 $fields[substr($buf, 0, 4)] = substr($buf, 5);
565 // Message terminated prematurely (no text body)
566 return array(FALSE, substr($response, 4), $fields);
569 // Support function for ctdl_fetch_message(). This handles the text body
570 // portion of the message, converting various formats to HTML as
572 function ctdl_msg4_from_server() {
575 $msgformat = "text/plain";
579 while (strcmp($buf = serv_gets(TRUE), "000")) {
580 if ($in_body == FALSE) {
581 if (strlen($buf) == 0) {
585 if (!strncasecmp($buf, "content-type: ", 14)) {
586 $msgformat = substr($buf, 14);
591 if (!strcasecmp($msgformat, "text/html")) {
594 else if (!strcasecmp($msgformat, "text/plain")) {
595 $txt .= "<TT>" . htmlspecialchars($buf) . "</TT><BR>\n" ;
597 else if (!strcasecmp($msgformat, "text/x-citadel-variformat")) {
598 if (substr($previous_line, 0, 1) == " ") {
601 $txt .= htmlspecialchars($buf);
604 $txt .= htmlspecialchars($buf);
606 $previous_line = $buf;