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");
45 // while ($nRead < $nBytes)
47 $buf = fread($clientsocket, $nBytes);
48 // $buf.=fgetc($clientsocket) | die ("fgetc failed");
49 // $buf .= serv_gets(TRUE);
50 // $tbuf = fgets($clientsocket, $nBytes - $nRead);
51 if (CITADEL_DEBUG_CITPROTO == 1) {
52 if (!$buf) printf ("<div class='ctdldbgRead'>\n");
54 if (!$buf) printf ("</div>\n");
55 else printf ("<br>\n");
58 // $nRead = strlen ($buf);
62 //$buf = fread($clientsocket, $nBytes) | die ("fread failed"); // Read line
63 if (CITADEL_DEBUG_CITPROTO == 1) {
64 printf ("<div class='ctdldbgRead'>\n");
73 // serv_puts() -- generic function to write one line of text to the server
75 function serv_puts($buf) {
78 fwrite($clientsocket, $buf . "\n", (strlen($buf)+1) );
79 fflush($clientsocket);
80 if (CITADEL_DEBUG_CITPROTO == 1)
81 printf ("<div class='ctdldbgWrite'>".$buf."</div>\n");
85 function read_array() {
87 if (CITADEL_DEBUG_CITPROTO == 1)
88 printf ("<div class='ctdldbgRead'>\n");
89 $buf = serv_gets(TRUE);
91 while (strcasecmp($buf, "000")){
92 array_push($ret, $buf);
93 $buf = serv_gets(TRUE);
96 if (CITADEL_DEBUG_CITPROTO == 1){
97 echo "read ".$nLines." lines from the server.\n";
103 function read_binary() {
105 if (CITADEL_DEBUG_CITPROTO == 1)
106 printf ("<div class='ctdldbgRead'>\n");
107 $buf = serv_gets(TRUE);
109 if (CITADEL_DEBUG_CITPROTO == 1){
110 echo "status line from the server\n";
113 $statusline = explode(" ", $buf);
115 if ($statusline[0] == 600)
117 $buf = serv_get_n($statusline[1]);
121 if (CITADEL_DEBUG_CITPROTO == 1){
122 echo "read ".$statusline[1]." bytes from the server.\n";
125 return array($statusline, $buf);
131 // text_to_server() -- sends a block of text to the server. Assumes that
132 // the server just sent back a SEND_LISTING response code
133 // and is now expecting a 000-terminated series of lines.
134 // Set 'convert_to_html' to TRUE to convert the block of
135 // text to HTML along the way.
137 function text_to_server($thetext, $convert_to_html) {
140 if ($convert_to_html) {
142 // Strip CR's; we only want the LF's
143 $thetext = trim($thetext, "\r");
145 // Replace hard line breaks with <BR>'s
146 $thetext = str_replace("\n", "<BR>\n", $thetext);
150 // Either mode ... send it to the server now
151 $one_line = strtok($thetext, "\n");
152 while ($one_line !== FALSE) {
153 $one_line = trim($one_line, "\n\r");
154 if ($one_line == "000") $one_line = "-000" ;
155 serv_puts($one_line);
156 $one_line = strtok("\n");
159 serv_puts("000"); // Tell the server we're done...
161 serv_puts("ECHO echo test."); // FIXME
162 echo "Echo test: " . serv_gets() . "<BR>\n" ;
166 //--------------------------------------------------------------------------------
168 //--------------------------------------------------------------------------------
172 // Identify ourselves to the Citadel server (do one once after connection)
173 /* http://www.citadel.org/doku.php/documentation:appproto:connection#iden.identify.the.client.software */
175 function ctdl_iden($client_info) {
176 global $clientsocket;
178 if (count($client_info) != 5)
179 die("ctdl_iden takes 5 arguments!");
180 // Identify client and hostname
181 serv_puts("IDEN ".implode('|', $client_info));
185 function ctdl_MessageFormatsPrefered($formatlist){
186 // Also express our message format preferences
187 serv_puts("MSGP ".implode("|", $formatlist));
191 /* http://www.citadel.org/doku.php/documentation:appproto:connection#noop.no.operation */
192 function ctdl_noop(){
193 // Also express our message format preferences
198 /* http://www.citadel.org/doku.php/documentation:appproto:connection#quit.quit */
199 function ctdl_quit(){
200 // Also express our message format preferences
206 /* http://www.citadel.org/doku.php/documentation:appproto:connection#mesg.read.system.message */
207 function ctdl_gtls(){
208 // Also express our message format preferences
215 /* http://www.citadel.org/doku.php/documentation:appproto:connection#qnop.quiet.no.operation */
216 /* this seems to be dangerous. ask IG
217 function ctdl_qnoop(){
218 // Also express our message format preferences
223 /* http://www.citadel.org/doku.php/documentation:appproto:connection#echo.echo.something */
224 function ctdl_doecho($echotext){
225 // Also express our message format preferences
226 serv_puts("ECHO ".$echotext);
231 /* http://www.citadel.org/doku.php/documentation:appproto:connection#time.get.server.local.time */
232 /* TODO: what are the other two params? doku is incomplete here. */
233 function ctdl_time(){
234 // Also express our message format preferences
241 /* http://www.citadel.org/doku.php/documentation:appproto:connection#qdir.query.global.directory */
242 function ctdl_qdir($who){
243 // Also express our message format preferences
244 serv_puts("QDIR ".$who);
246 return array((substr($buf, 0, 1) == "2"), $buf);
250 /* http://www.citadel.org/doku.php/documentation:appproto:connection#auto.autocompletion.of.email.addresses */
251 function ctdl_auto($who){
252 // Also express our message format preferences
253 serv_puts("AUTO ".$who);
255 if (substr($buf, 0, 1) == "1") {
256 $reply = read_array();
257 if (count($reply) == 0)
268 // login_existing_user() -- attempt to login using a supplied username/password
269 // Returns an array with two variables:
270 // 0. TRUE or FALSE to determine success or failure
271 // 1. String error message (if relevant)
272 /* http://www.citadel.org/doku.php/documentation:appproto:connection#user.send.user.name */
273 /* http://www.citadel.org/doku.php/documentation:appproto:connection#pass.send.password */
276 function login_existing_user($user, $pass) {
277 global $clientsocket;
279 serv_puts("USER " . $user);
281 if (substr($resp, 0, 1) != "3") {
282 return array(FALSE, substr($resp, 4));
285 serv_puts("PASS " . $pass);
287 if (substr($resp, 0, 1) != "2") {
288 return array(FALSE, substr($resp, 4));
291 $_SESSION["username"] = $user;
292 $_SESSION["password"] = $pass;
293 become_logged_in(substr($resp, 4));
295 return array(TRUE, "Login successful. Have fun.");
300 // create_new_user() -- attempt to create a new user
301 // using a supplied username/password
302 // Returns an array with two variables:
303 // 0. TRUE or FALSE to determine success or failure
304 // 1. String error message (if relevant)
306 function create_new_user($user, $pass) {
307 global $clientsocket;
309 serv_puts("NEWU " . $user);
311 if (substr($resp, 0, 1) != "2") {
312 return array(FALSE, substr($resp, 4));
315 serv_puts("SETP " . $pass);
317 if (substr($resp, 0, 1) != "2") {
318 return array(FALSE, substr($resp, 4));
321 $_SESSION["username"] = $user;
322 $_SESSION["password"] = $pass;
323 become_logged_in(substr($resp, 4));
325 return array(TRUE, "Login successful. Have fun.");
330 // Code common to both existing-user and new-user logins
332 function become_logged_in($server_parms) {
333 $_SESSION["logged_in"] = 1;
335 $tokens = explode("|", $server_parms);
337 $oneline["username"] = $tokens[0];
338 $oneline["axlevel"] = $tokens[1];
339 $oneline["calls"] = $tokens[2];
340 $oneline["posts"] = $tokens[3];
341 $oneline["userflags"] = $tokens[4];
342 $oneline["usernum"] = $tokens[5];
343 $oneline["lastcall"] = $tokens[6];
345 ctdl_goto("_BASEROOM_");
351 // Learn all sorts of interesting things about the Citadel server to
352 // which we are connected.
353 /* http://www.citadel.org/doku.php/documentation:appproto:connection#info.get.server.info */
355 function ctdl_get_serv_info() {
357 $reply = read_array();
358 if ((count($reply) == 18) &&
359 substr($reply[0], 0, 1) == "1") {
360 $server_info=array();
361 $server_info["serv_nodename"] = $reply[1];
362 $server_info["serv_humannode"] = $reply[2];
363 $server_info["serv_fqdn"] = $reply[3];
364 $server_info["serv_software"] = $reply[4];
365 $server_info["serv_city"] = $reply[6];
366 $server_info["serv_sysadmin"] = $reply[7];
367 if (CITADEL_DEBUG_CITPROTO == 1)
370 print_r($server_info);
376 die ("didn't understand the reply to the INFO command");
381 // Learn all sorts of interesting things about the Citadel server to
382 // which we are connected.
383 /* http://www.citadel.org/doku.php/documentation:appproto:connection#info.get.server.info */
385 function ctdl_get_registration_info() {
387 $reply = read_array();
389 // die ("didn't understand the reply to the INFO command");
395 // Display a system banner. (Returns completed HTML.)
396 // (One is probably temporary because it outputs more or less finalized
397 // markup. For now it's just usable.)
399 /* http://www.citadel.org/doku.php/documentation:appproto:connection#mesg.read.system.message */
400 function ctdl_mesg($msgname) {
401 global $clientsocket;
403 $msgtext = "<DIV ALIGN=CENTER>\n";
405 serv_puts("MESG " . $msgname);
406 $response = read_array();
408 if (substr($response[0], 0, 1) == "1") {
409 array_shift($response); // throw away the status code.
410 $msgtext .= "<TT>" . implode( "</TT><BR>\n" ,$response);
413 $msgtext .= "<B><I>" . substr($response[0], 4) . "</I></B><BR>\n";
416 $msgtext .= "</DIV>\n";
420 /* http://www.citadel.org/doku.php/documentation:appproto:connection#mesg.read.system.message */
421 //// TODO: is this still supported?
422 function ctdl_mrtg($what) {
423 global $clientsocket;
425 serv_puts("MRTG ".$what);
426 $response = serv_gets();
428 if (substr($response, 0, 1) != "1") {
429 return array(0, NULL);
432 $responses = read_array();
436 // Fetch the list of users currently logged in.
437 /* http://www.citadel.org/doku.php/documentation:appproto:connection#rwho.read.who.s.online */
439 function ctdl_rwho() {
440 global $clientsocket;
443 $response = serv_gets();
445 if (substr($response, 0, 1) != "1") {
446 return array(0, NULL);
449 $all_lines = array();
452 $responses = read_array();
453 foreach ($responses as $response) {
454 $tokens = explode("|", $response);
457 $oneline["session"] = $tokens[0];
458 $oneline["user"] = $tokens[1];
459 $oneline["room"] = $tokens[2];
460 $oneline["host"] = $tokens[3];
461 $oneline["client"] = $tokens[4];
462 $oneline["idlesince"] = $tokens[5];
463 $oneline["lastcmd"] = $tokens[6];
464 $oneline["flags"] = $tokens[7];
465 $oneline["realname"] = $tokens[8];
466 $oneline["realroom"] = $tokens[9];
467 $oneline["realhostname"] = $tokens[10];
468 $oneline["registered"] = $tokens[11];
470 // IGnore the rest of the fields for now.
471 if (CITADEL_DEBUG_CITPROTO == 1)
480 $num_lines = array_push($all_lines, $oneline);
483 return array($num_lines, $all_lines);
491 function ctdl_goto($to_where) {
493 serv_puts("GOTO " . $to_where);
494 $response = serv_gets();
496 $results = explode ("|", $response);
497 $status_room = array_shift($results);
498 $status = substr($status_room, 0, 3);
499 if (substr($status, 0, 1) == "2") {
500 $room = substr($status_room, 4);
501 array_unshift($results, $room);
504 "statereply" => $status,
505 "roomname" => $results[ 0],
506 "nunreadmsg" => $results[ 1],
507 "nmessages" => $results[ 2],
508 "rinfopresent" => $results[ 3],
509 "flags" => $results[ 4],
510 "msgidmax" => $results[ 5],
511 "msgidreadmax" => $results[ 6],
512 "ismailroom" => $results[ 7],
513 "isroomaide" => $results[ 8],
514 "nnewmessages" => $results[ 9],
515 "floorid" => $results[10],
516 "viewselected" => $results[11],
517 "defaultview" => $results[12],
518 "istrashcan" => $results[13]);
520 $_SESSION["room"] = $room;
521 if (CITADEL_DEBUG_CITPROTO == 1)
524 print_r($room_state);
533 return array("state" => FALSE, "statereply" => $status);
541 // Fetch the list of known rooms.
543 function ctdl_knrooms() {
544 global $clientsocket;
547 $response = serv_gets();
548 if (substr($response, 0, 1) != "1") {
549 return array(0, NULL);
551 $results = read_array();
552 $all_lines = array();
555 foreach ($results as $result){
557 $tokens = explode("|",$result);
559 $oneline["name"] = $tokens[0];
560 $oneline["flags"] = $tokens[1];
561 $oneline["floor"] = $tokens[2];
562 $oneline["order"] = $tokens[3];
563 $oneline["flags2"] = $tokens[4];
564 $oneline["access"] = $tokens[5];
566 if ($oneline["access"] & 8) {
567 $oneline["hasnewmsgs"] = TRUE;
570 $oneline["hasnewmsgs"] = FALSE;
573 if (CITADEL_DEBUG_CITPROTO == 1)
580 $num_lines = array_push($all_lines, $oneline);
583 return array($num_lines, $all_lines);
588 // Fetch the list of known floors.
590 /* http://www.citadel.org/doku.php/documentation:appproto:rooms#lflr.list.all.known.floors */
591 function ctdl_knfloors() {
592 global $clientsocket;
595 $response = serv_gets();
596 if (substr($response, 0, 1) != "1") {
597 return array(0, NULL);
600 $results = read_array();
601 $all_lines = array();
604 foreach ($results as $result){
606 $tokens = explode("|",$result);
608 $oneline["id"] = $tokens[0];
609 $oneline["name"] = $tokens[1];
610 $oneline["nref"] = $tokens[2];
612 if (CITADEL_DEBUG_CITPROTO == 1)
619 $num_lines = array_push($all_lines, $oneline);
622 return array($num_lines, $all_lines);
626 /* http://www.citadel.org/doku.php/documentation:appproto:rooms#cflr.create.a.new.floor */
629 // Fetch the list of messages in one room.
630 // Returns: count, response, message array
632 function ctdl_msgs($mode, $count) {
633 global $clientsocket;
635 serv_puts("MSGS " . $mode . "|" . $count);
636 $responses = read_array();
639 $response = array_shift($responses);
641 $num_msgs = count($responses);
642 if (substr($response, 0, 1) != "1") {
643 return array(0, substr($response, 4), NULL);
646 if (CITADEL_DEBUG_CITPROTO == 1)
648 printf("found ".$num_msgs." messages.");
650 return array($num_msgs, $response, $responses);
654 // Load a message from the server.
655 function ctdl_fetch_message($msgnum) {
656 global $clientsocket;
658 serv_puts("MSG4 " . $msgnum);
660 if (CITADEL_DEBUG_CITPROTO == 1)
661 printf ("<div class='ctdldbgRead'>");
662 $response = serv_gets(TRUE);
664 if (substr($response, 0, 1) != "1") {
665 return array(FALSE, substr($response, 4), NULL);
669 while (strcmp($buf = serv_gets(TRUE), "000")) {
670 if (substr($buf, 0, 4) == "text") {
671 if (CITADEL_DEBUG_CITPROTO == 1)
672 printf ("</div>\n<h3>Message Body Follows</h3><div class='ctdldbgRead'>");
673 // We're in the text body. New loop here.
674 $fields["text"] = ctdl_msg4_from_server();
675 if (CITADEL_DEBUG_CITPROTO == 1)
677 return array(TRUE, substr($response, 4), $fields);
680 $fields[substr($buf, 0, 4)] = substr($buf, 5);
684 // Message terminated prematurely (no text body)
685 return array(FALSE, substr($response, 4), $fields);
688 // Support function for ctdl_fetch_message(). This handles the text body
689 // portion of the message, converting various formats to HTML as
691 function ctdl_msg4_from_server() {
694 $msgformat = "text/plain";
698 while (strcmp($buf = serv_gets(TRUE), "000")) {
699 if ($in_body == FALSE) {
700 if (strlen($buf) == 0) {
704 if (!strncasecmp($buf, "content-type: ", 14)) {
705 $msgformat = substr($buf, 14);
710 if (!strcasecmp($msgformat, "text/html")) {
713 else if (!strcasecmp($msgformat, "text/plain")) {
714 $txt .= "<TT>" . htmlspecialchars($buf) . "</TT><BR>\n" ;
716 else if (!strcasecmp($msgformat, "text/x-citadel-variformat")) {
717 if (substr($previous_line, 0, 1) == " ") {
720 $txt .= htmlspecialchars($buf);
723 $txt .= htmlspecialchars($buf);
725 $previous_line = $buf;
734 function downoad_attachment($msgnum, $attindex)
736 $command = "DLAT ".$msgnum."|".$attindex;
738 $reply = read_binary();