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'>\n");
25 if (!$readblock) printf ("\n</div>\n");
26 else printf ("<br>\n");
32 // serv_get_n() -- generic function to read a binary blob from the server
34 function serv_get_n($nBytes) {
37 if (CITADEL_DEBUG_CITPROTO == 1) {
38 printf ("<div class='ctdldbgRead'>\n");
39 printf("reading ".$nBytes." bytes from server\n");
42 $buf = fread($clientsocket, $nBytes);
43 if (CITADEL_DEBUG_CITPROTO == 1) {
44 if (!$buf) printf ("<div class='ctdldbgRead'>\n");
46 if (!$buf) printf ("</div>\n");
47 else printf ("<br>\n");
53 // serv_puts() -- generic function to write one line of text to the server
55 function serv_puts($buf) {
58 fwrite($clientsocket, $buf . "\n", (strlen($buf)+1) );
59 fflush($clientsocket);
60 if (CITADEL_DEBUG_CITPROTO == 1)
61 printf ("<div class='ctdldbgWrite'>".$buf."</div>\n");
65 function read_array() {
67 if (CITADEL_DEBUG_CITPROTO == 1)
68 printf ("<div class='ctdldbgRead'>\n");
69 $buf = serv_gets(TRUE);
71 while (strcasecmp($buf, "000")){
72 array_push($ret, $buf);
73 $buf = serv_gets(TRUE);
76 if (CITADEL_DEBUG_CITPROTO == 1){
77 echo "read ".$nLines." lines from the server.\n";
83 function read_binary() {
85 if (CITADEL_DEBUG_CITPROTO == 1)
86 printf ("<div class='ctdldbgRead'>\n");
87 $buf = serv_gets(TRUE);
89 if (CITADEL_DEBUG_CITPROTO == 1){
90 echo "status line from the server\n";
93 $statusline = explode(" ", $buf);
95 if ($statusline[0] == 600)
97 $buf = serv_get_n($statusline[1]);
100 if (CITADEL_DEBUG_CITPROTO == 1)
102 return array($statusline, $buf);
108 // text_to_server() -- sends a block of text to the server. Assumes that
109 // the server just sent back a SEND_LISTING response code
110 // and is now expecting a 000-terminated series of lines.
111 // Set 'convert_to_html' to TRUE to convert the block of
112 // text to HTML along the way.
114 function text_to_server($thetext, $convert_to_html) {
117 if ($convert_to_html) {
119 // Strip CR's; we only want the LF's
120 $thetext = trim($thetext, "\r");
122 // Replace hard line breaks with <BR>'s
123 $thetext = str_replace("\n", "<BR>\n", $thetext);
127 // Either mode ... send it to the server now
128 $one_line = strtok($thetext, "\n");
129 while ($one_line !== FALSE) {
130 $one_line = trim($one_line, "\n\r");
131 if ($one_line == "000") $one_line = "-000" ;
132 serv_puts($one_line);
133 $one_line = strtok("\n");
136 serv_puts("000"); // Tell the server we're done...
138 serv_puts("ECHO echo test."); // FIXME
139 echo "Echo test: " . serv_gets() . "<BR>\n" ;
143 //--------------------------------------------------------------------------------
145 //--------------------------------------------------------------------------------
149 // Identify ourselves to the Citadel server (do one once after connection)
150 /* http://www.citadel.org/doku.php/documentation:appproto:connection#iden.identify.the.client.software */
152 function ctdl_iden($client_info) {
153 global $clientsocket;
155 if (count($client_info) != 5)
156 die("ctdl_iden takes 5 arguments!");
157 // Identify client and hostname
158 serv_puts("IDEN ".implode('|', $client_info));
162 function ctdl_MessageFormatsPrefered($formatlist){
163 // Also express our message format preferences
164 serv_puts("MSGP ".implode("|", $formatlist));
168 /* http://www.citadel.org/doku.php/documentation:appproto:connection#noop.no.operation */
169 function ctdl_noop(){
170 // Also express our message format preferences
175 /* http://www.citadel.org/doku.php/documentation:appproto:connection#quit.quit */
176 function ctdl_quit(){
177 // Also express our message format preferences
183 /* http://www.citadel.org/doku.php/documentation:appproto:connection#mesg.read.system.message */
184 function ctdl_gtls(){
185 // Also express our message format preferences
192 /* http://www.citadel.org/doku.php/documentation:appproto:connection#qnop.quiet.no.operation */
193 /* this seems to be dangerous. ask IG
194 function ctdl_qnoop(){
195 // Also express our message format preferences
200 /* http://www.citadel.org/doku.php/documentation:appproto:connection#echo.echo.something */
201 function ctdl_doecho($echotext){
202 // Also express our message format preferences
203 serv_puts("ECHO ".$echotext);
208 /* http://www.citadel.org/doku.php/documentation:appproto:connection#time.get.server.local.time */
209 /* TODO: what are the other two params? doku is incomplete here. */
210 function ctdl_time(){
211 // Also express our message format preferences
218 /* http://www.citadel.org/doku.php/documentation:appproto:connection#qdir.query.global.directory */
219 function ctdl_qdir($who){
220 // Also express our message format preferences
221 serv_puts("QDIR ".$who);
223 return array((substr($buf, 0, 1) == "2"), $buf);
227 /* http://www.citadel.org/doku.php/documentation:appproto:connection#auto.autocompletion.of.email.addresses */
228 function ctdl_auto($who){
229 // Also express our message format preferences
230 serv_puts("AUTO ".$who);
232 if (substr($buf, 0, 1) == "1") {
233 $reply = read_array();
234 if (count($reply) == 0)
245 // login_existing_user() -- attempt to login using a supplied username/password
246 // Returns an array with two variables:
247 // 0. TRUE or FALSE to determine success or failure
248 // 1. String error message (if relevant)
249 /* http://www.citadel.org/doku.php/documentation:appproto:connection#user.send.user.name */
250 /* http://www.citadel.org/doku.php/documentation:appproto:connection#pass.send.password */
253 function login_existing_user($user, $pass) {
254 global $clientsocket;
256 serv_puts("USER " . $user);
258 if (substr($resp, 0, 1) != "3") {
259 return array(FALSE, substr($resp, 4));
262 serv_puts("PASS " . $pass);
264 if (substr($resp, 0, 1) != "2") {
265 return array(FALSE, substr($resp, 4));
268 $_SESSION["username"] = $user;
269 $_SESSION["password"] = $pass;
270 become_logged_in(substr($resp, 4));
272 return array(TRUE, "Login successful. Have fun.");
277 // create_new_user() -- attempt to create a new user
278 // using a supplied username/password
279 // Returns an array with two variables:
280 // 0. TRUE or FALSE to determine success or failure
281 // 1. String error message (if relevant)
283 function create_new_user($user, $pass) {
284 global $clientsocket;
286 serv_puts("NEWU " . $user);
288 if (substr($resp, 0, 1) != "2") {
289 return array(FALSE, substr($resp, 4));
292 serv_puts("SETP " . $pass);
294 if (substr($resp, 0, 1) != "2") {
295 return array(FALSE, substr($resp, 4));
298 $_SESSION["username"] = $user;
299 $_SESSION["password"] = $pass;
300 become_logged_in(substr($resp, 4));
302 return array(TRUE, "Login successful. Have fun.");
307 // Code common to both existing-user and new-user logins
309 function become_logged_in($server_parms) {
310 $_SESSION["logged_in"] = 1;
312 $tokens = explode("|", $server_parms);
314 $oneline["username"] = $tokens[0];
315 $oneline["axlevel"] = $tokens[1];
316 $oneline["calls"] = $tokens[2];
317 $oneline["posts"] = $tokens[3];
318 $oneline["userflags"] = $tokens[4];
319 $oneline["usernum"] = $tokens[5];
320 $oneline["lastcall"] = $tokens[6];
322 ctdl_goto("_BASEROOM_");
328 // Learn all sorts of interesting things about the Citadel server to
329 // which we are connected.
330 /* http://www.citadel.org/doku.php/documentation:appproto:connection#info.get.server.info */
332 function ctdl_get_serv_info() {
334 $reply = read_array();
335 if ((count($reply) == 18) &&
336 substr($reply[0], 0, 1) == "1") {
337 $server_info=array();
338 $server_info["serv_nodename"] = $reply[1];
339 $server_info["serv_humannode"] = $reply[2];
340 $server_info["serv_fqdn"] = $reply[3];
341 $server_info["serv_software"] = $reply[4];
342 $server_info["serv_city"] = $reply[6];
343 $server_info["serv_sysadmin"] = $reply[7];
344 if (CITADEL_DEBUG_CITPROTO == 1)
347 print_r($server_info);
353 die ("didn't understand the reply to the INFO command");
358 // Learn all sorts of interesting things about the Citadel server to
359 // which we are connected.
360 /* http://www.citadel.org/doku.php/documentation:appproto:connection#info.get.server.info */
362 function ctdl_get_registration_info() {
364 $reply = read_array();
366 // die ("didn't understand the reply to the INFO command");
372 // Display a system banner. (Returns completed HTML.)
373 // (One is probably temporary because it outputs more or less finalized
374 // markup. For now it's just usable.)
376 /* http://www.citadel.org/doku.php/documentation:appproto:connection#mesg.read.system.message */
377 function ctdl_mesg($msgname) {
378 global $clientsocket;
380 $msgtext = "<DIV ALIGN=CENTER>\n";
382 serv_puts("MESG " . $msgname);
383 $response = read_array();
385 if (substr($response[0], 0, 1) == "1") {
386 array_shift($response); // throw away the status code.
387 $msgtext .= "<TT>" . implode( "</TT><BR>\n" ,$response);
390 $msgtext .= "<B><I>" . substr($response[0], 4) . "</I></B><BR>\n";
393 $msgtext .= "</DIV>\n";
397 /* http://www.citadel.org/doku.php/documentation:appproto:connection#mesg.read.system.message */
398 //// TODO: is this still supported?
399 function ctdl_mrtg($what) {
400 global $clientsocket;
402 serv_puts("MRTG ".$what);
403 $response = serv_gets();
405 if (substr($response, 0, 1) != "1") {
406 return array(0, NULL);
409 $responses = read_array();
413 // Fetch the list of users currently logged in.
414 /* http://www.citadel.org/doku.php/documentation:appproto:connection#rwho.read.who.s.online */
416 function ctdl_rwho() {
417 global $clientsocket;
420 $response = serv_gets();
422 if (substr($response, 0, 1) != "1") {
423 return array(0, NULL);
426 $all_lines = array();
429 $responses = read_array();
430 foreach ($responses as $response) {
431 $tokens = explode("|", $response);
434 $oneline["session"] = $tokens[0];
435 $oneline["user"] = $tokens[1];
436 $oneline["room"] = $tokens[2];
437 $oneline["host"] = $tokens[3];
438 $oneline["client"] = $tokens[4];
439 $oneline["idlesince"] = $tokens[5];
440 $oneline["lastcmd"] = $tokens[6];
441 $oneline["flags"] = $tokens[7];
442 $oneline["realname"] = $tokens[8];
443 $oneline["realroom"] = $tokens[9];
444 $oneline["realhostname"] = $tokens[10];
445 $oneline["registered"] = $tokens[11];
447 // IGnore the rest of the fields for now.
448 if (CITADEL_DEBUG_CITPROTO == 1)
457 $num_lines = array_push($all_lines, $oneline);
460 return array($num_lines, $all_lines);
468 function ctdl_goto($to_where) {
470 serv_puts("GOTO " . $to_where);
471 $response = serv_gets();
473 $results = explode ("|", $response);
474 $status_room = array_shift($results);
475 $status = substr($status_room, 0, 3);
476 if (substr($status, 0, 1) == "2") {
477 $room = substr($status_room, 4);
478 array_unshift($results, $room);
481 "statereply" => $status,
482 "roomname" => $results[ 0],
483 "nunreadmsg" => $results[ 1],
484 "nmessages" => $results[ 2],
485 "rinfopresent" => $results[ 3],
486 "flags" => $results[ 4],
487 "msgidmax" => $results[ 5],
488 "msgidreadmax" => $results[ 6],
489 "ismailroom" => $results[ 7],
490 "isroomaide" => $results[ 8],
491 "nnewmessages" => $results[ 9],
492 "floorid" => $results[10],
493 "viewselected" => $results[11],
494 "defaultview" => $results[12],
495 "istrashcan" => $results[13]);
497 $_SESSION["room"] = $room;
498 if (CITADEL_DEBUG_CITPROTO == 1)
501 print_r($room_state);
510 return array("state" => FALSE, "statereply" => $status);
518 // Fetch the list of known rooms.
520 function ctdl_knrooms() {
521 global $clientsocket;
524 $response = serv_gets();
525 if (substr($response, 0, 1) != "1") {
526 return array(0, NULL);
528 $results = read_array();
529 $all_lines = array();
532 foreach ($results as $result){
534 $tokens = explode("|",$result);
536 $oneline["name"] = $tokens[0];
537 $oneline["flags"] = $tokens[1];
538 $oneline["floor"] = $tokens[2];
539 $oneline["order"] = $tokens[3];
540 $oneline["flags2"] = $tokens[4];
541 $oneline["access"] = $tokens[5];
543 if ($oneline["access"] & 8) {
544 $oneline["hasnewmsgs"] = TRUE;
547 $oneline["hasnewmsgs"] = FALSE;
550 if (CITADEL_DEBUG_CITPROTO == 1)
557 $num_lines = array_push($all_lines, $oneline);
560 return array($num_lines, $all_lines);
565 // Fetch the list of known floors.
567 /* http://www.citadel.org/doku.php/documentation:appproto:rooms#lflr.list.all.known.floors */
568 function ctdl_knfloors() {
569 global $clientsocket;
572 $response = serv_gets();
573 if (substr($response, 0, 1) != "1") {
574 return array(0, NULL);
577 $results = read_array();
578 $all_lines = array();
581 foreach ($results as $result){
583 $tokens = explode("|",$result);
585 $oneline["id"] = $tokens[0];
586 $oneline["name"] = $tokens[1];
587 $oneline["nref"] = $tokens[2];
589 if (CITADEL_DEBUG_CITPROTO == 1)
596 $num_lines = array_push($all_lines, $oneline);
599 return array($num_lines, $all_lines);
603 /* http://www.citadel.org/doku.php/documentation:appproto:rooms#cflr.create.a.new.floor */
606 // Fetch the list of messages in one room.
607 // Returns: count, response, message array
609 function ctdl_msgs($mode, $count) {
610 global $clientsocket;
612 serv_puts("MSGS " . $mode . "|" . $count);
613 $responses = read_array();
616 $response = array_shift($responses);
618 $num_msgs = count($responses);
619 if (substr($response, 0, 1) != "1") {
620 return array(0, substr($response, 4), NULL);
623 if (CITADEL_DEBUG_CITPROTO == 1)
625 printf("found ".$num_msgs." messages.");
627 return array($num_msgs, $response, $responses);
631 // Load a message from the server.
632 function ctdl_fetch_message($msgnum) {
633 global $clientsocket;
635 serv_puts("MSG4 " . $msgnum);
637 if (CITADEL_DEBUG_CITPROTO == 1)
638 printf ("<div class='ctdldbgRead'>");
639 $response = serv_gets(TRUE);
641 if (substr($response, 0, 1) != "1") {
642 return array(FALSE, substr($response, 4), NULL);
646 while (strcmp($buf = serv_gets(TRUE), "000")) {
647 if (substr($buf, 0, 4) == "text") {
648 if (CITADEL_DEBUG_CITPROTO == 1)
649 printf ("</div>\n<h3>Message Body Follows</h3><div class='ctdldbgRead'>");
650 // We're in the text body. New loop here.
651 $fields["text"] = ctdl_msg4_from_server();
652 if (CITADEL_DEBUG_CITPROTO == 1)
654 return array(TRUE, substr($response, 4), $fields);
657 $fields[substr($buf, 0, 4)] = substr($buf, 5);
661 // Message terminated prematurely (no text body)
662 return array(FALSE, substr($response, 4), $fields);
665 // Support function for ctdl_fetch_message(). This handles the text body
666 // portion of the message, converting various formats to HTML as
668 function ctdl_msg4_from_server() {
671 $msgformat = "text/plain";
675 while (strcmp($buf = serv_gets(TRUE), "000")) {
676 if ($in_body == FALSE) {
677 if (strlen($buf) == 0) {
681 if (!strncasecmp($buf, "content-type: ", 14)) {
682 $msgformat = substr($buf, 14);
687 if (!strcasecmp($msgformat, "text/html")) {
690 else if (!strcasecmp($msgformat, "text/plain")) {
691 $txt .= "<TT>" . htmlspecialchars($buf) . "</TT><BR>\n" ;
693 else if (!strcasecmp($msgformat, "text/x-citadel-variformat")) {
694 if (substr($previous_line, 0, 1) == " ") {
697 $txt .= htmlspecialchars($buf);
700 $txt .= htmlspecialchars($buf);
702 $previous_line = $buf;
711 function download_attachment($msgnum, $attindex)
713 $command = "DLAT ".$msgnum."|".$attindex;
715 $reply = read_binary();