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 define("FMT_CITADEL", 0);
12 define("FMT_FIXED", 1);
13 define("FMT_RFC822", 4);
15 //--------------------------------------------------------------------------------
16 // internal functions for server communication
17 //--------------------------------------------------------------------------------
19 // serv_gets() -- generic function to read one line of text from the server
21 function serv_gets($readblock=FALSE) {
24 $buf = fgets($clientsocket, 4096); // Read line
25 $buf = substr($buf, 0, (strlen($buf)-1) ); // strip trailing LF
26 if (CITADEL_DEBUG_CITPROTO == 1) {
27 if (!$readblock) printf ("<div class='ctdldbgRead'>\n");
29 if (!$readblock) printf ("\n</div>\n");
30 else printf ("<br>\n");
36 // serv_get_n() -- generic function to read a binary blob from the server
38 function serv_get_n($nBytes) {
41 if (CITADEL_DEBUG_CITPROTO == 1) {
42 printf ("<div class='ctdldbgRead'>\n");
43 printf("reading ".$nBytes." bytes from server\n");
46 $buf = fread($clientsocket, $nBytes);
47 if (CITADEL_DEBUG_CITPROTO == 1) {
48 if (!$buf) printf ("<div class='ctdldbgRead'>\n");
50 if (!$buf) printf ("</div>\n");
51 else printf ("<br>\n");
57 // serv_puts() -- generic function to write one line of text to the server
59 function serv_puts($buf) {
62 fwrite($clientsocket, $buf . "\n", (strlen($buf)+1) );
63 fflush($clientsocket);
64 if (CITADEL_DEBUG_CITPROTO == 1)
65 printf ("<div class='ctdldbgWrite'>".$buf."</div>\n");
69 function read_array() {
71 if (CITADEL_DEBUG_CITPROTO == 1)
72 printf ("<div class='ctdldbgRead'>\n");
73 $buf = serv_gets(TRUE);
75 while (strcasecmp($buf, "000")){
76 array_push($ret, $buf);
77 $buf = serv_gets(TRUE);
80 if (CITADEL_DEBUG_CITPROTO == 1){
81 echo "read ".$nLines." lines from the server.\n";
87 function read_binary() {
89 if (CITADEL_DEBUG_CITPROTO == 1)
90 printf ("<div class='ctdldbgRead'>\n");
91 $buf = serv_gets(TRUE);
93 if (CITADEL_DEBUG_CITPROTO == 1){
94 echo "status line from the server\n";
97 $statusline = explode(" ", $buf);
99 if ($statusline[0] == 600)
101 $buf = serv_get_n($statusline[1]);
104 if (CITADEL_DEBUG_CITPROTO == 1)
106 return array($statusline, $buf);
112 // text_to_server() -- sends a block of text to the server. Assumes that
113 // the server just sent back a SEND_LISTING response code
114 // and is now expecting a 000-terminated series of lines.
115 // Set 'convert_to_html' to TRUE to convert the block of
116 // text to HTML along the way.
118 function text_to_server($thetext, $convert_to_html) {
121 if ($convert_to_html) {
123 // Strip CR's; we only want the LF's
124 $thetext = trim($thetext, "\r");
126 // Replace hard line breaks with <BR>'s
127 $thetext = str_replace("\n", "<BR>\n", $thetext);
131 // Either mode ... send it to the server now
132 $one_line = strtok($thetext, "\n");
133 while ($one_line !== FALSE) {
134 $one_line = trim($one_line, "\n\r");
135 if ($one_line == "000") $one_line = "-000" ;
136 serv_puts($one_line);
137 $one_line = strtok("\n");
140 serv_puts("000"); // Tell the server we're done...
142 serv_puts("ECHO echo test."); // FIXME
143 echo "Echo test: " . serv_gets() . "<BR>\n" ;
147 //--------------------------------------------------------------------------------
149 //--------------------------------------------------------------------------------
153 // Identify ourselves to the Citadel server (do one once after connection)
154 /* http://www.citadel.org/doku.php/documentation:appproto:connection#iden.identify.the.client.software */
156 function ctdl_iden($client_info) {
157 global $clientsocket;
159 if (count($client_info) != 5)
160 die("ctdl_iden takes 5 arguments!");
161 // Identify client and hostname
162 serv_puts("IDEN ".implode('|', $client_info));
166 function ctdl_MessageFormatsPrefered($formatlist){
167 // Also express our message format preferences
168 serv_puts("MSGP ".implode("|", $formatlist));
172 /* http://www.citadel.org/doku.php/documentation:appproto:connection#noop.no.operation */
173 function ctdl_noop(){
174 // Also express our message format preferences
179 /* http://www.citadel.org/doku.php/documentation:appproto:connection#quit.quit */
180 function ctdl_quit(){
181 // Also express our message format preferences
187 /* http://www.citadel.org/doku.php/documentation:appproto:connection#mesg.read.system.message */
188 function ctdl_gtls(){
189 // Also express our message format preferences
196 /* http://www.citadel.org/doku.php/documentation:appproto:connection#qnop.quiet.no.operation */
197 /* this seems to be dangerous. ask IG
198 function ctdl_qnoop(){
199 // Also express our message format preferences
204 /* http://www.citadel.org/doku.php/documentation:appproto:connection#echo.echo.something */
205 function ctdl_doecho($echotext){
206 // Also express our message format preferences
207 serv_puts("ECHO ".$echotext);
212 /* http://www.citadel.org/doku.php/documentation:appproto:connection#time.get.server.local.time */
213 /* TODO: what are the other two params? doku is incomplete here. */
214 function ctdl_time(){
215 // Also express our message format preferences
222 /* http://www.citadel.org/doku.php/documentation:appproto:connection#qdir.query.global.directory */
223 function ctdl_qdir($who){
224 // Also express our message format preferences
225 serv_puts("QDIR ".$who);
227 return array((substr($buf, 0, 1) == "2"), $buf);
231 /* http://www.citadel.org/doku.php/documentation:appproto:connection#auto.autocompletion.of.email.addresses */
232 function ctdl_auto($who){
233 // Also express our message format preferences
234 serv_puts("AUTO ".$who);
236 if (substr($buf, 0, 1) == "1") {
237 $reply = read_array();
238 if (count($reply) == 0)
249 // login_existing_user() -- attempt to login using a supplied username/password
250 // Returns an array with two variables:
251 // 0. TRUE or FALSE to determine success or failure
252 // 1. String error message (if relevant)
253 /* http://www.citadel.org/doku.php/documentation:appproto:connection#user.send.user.name */
254 /* http://www.citadel.org/doku.php/documentation:appproto:connection#pass.send.password */
257 function login_existing_user($user, $pass) {
258 global $clientsocket;
260 serv_puts("USER " . $user);
262 if (substr($resp, 0, 1) != "3") {
263 return array(FALSE, substr($resp, 4));
266 serv_puts("PASS " . $pass);
268 if (substr($resp, 0, 1) != "2") {
269 return array(FALSE, substr($resp, 4));
272 $_SESSION["username"] = $user;
273 $_SESSION["password"] = $pass;
274 become_logged_in(substr($resp, 4));
276 return array(TRUE, "Login successful. Have fun.");
281 // create_new_user() -- attempt to create a new user
282 // using a supplied username/password
283 // Returns an array with two variables:
284 // 0. TRUE or FALSE to determine success or failure
285 // 1. String error message (if relevant)
287 function create_new_user($user, $pass) {
288 global $clientsocket;
290 serv_puts("NEWU " . $user);
292 if (substr($resp, 0, 1) != "2") {
293 return array(FALSE, substr($resp, 4));
296 serv_puts("SETP " . $pass);
298 if (substr($resp, 0, 1) != "2") {
299 return array(FALSE, substr($resp, 4));
302 $_SESSION["username"] = $user;
303 $_SESSION["password"] = $pass;
304 become_logged_in(substr($resp, 4));
306 return array(TRUE, "Login successful. Have fun.");
311 // Code common to both existing-user and new-user logins
313 function become_logged_in($server_parms) {
314 $_SESSION["logged_in"] = 1;
316 $tokens = explode("|", $server_parms);
318 $oneline["username"] = $tokens[0];
319 $oneline["axlevel"] = $tokens[1];
320 $oneline["calls"] = $tokens[2];
321 $oneline["posts"] = $tokens[3];
322 $oneline["userflags"] = $tokens[4];
323 $oneline["usernum"] = $tokens[5];
324 $oneline["lastcall"] = $tokens[6];
326 ctdl_goto("_BASEROOM_");
332 // Learn all sorts of interesting things about the Citadel server to
333 // which we are connected.
334 /* http://www.citadel.org/doku.php/documentation:appproto:connection#info.get.server.info */
336 function ctdl_get_serv_info() {
338 $reply = read_array();
339 if ((count($reply) == 18) &&
340 substr($reply[0], 0, 1) == "1") {
341 $server_info=array();
342 $server_info["serv_nodename"] = $reply[1];
343 $server_info["serv_humannode"] = $reply[2];
344 $server_info["serv_fqdn"] = $reply[3];
345 $server_info["serv_software"] = $reply[4];
346 $server_info["serv_city"] = $reply[6];
347 $server_info["serv_sysadmin"] = $reply[7];
348 if (CITADEL_DEBUG_CITPROTO == 1)
351 print_r($server_info);
357 die ("didn't understand the reply to the INFO command");
362 // Learn all sorts of interesting things about the Citadel server to
363 // which we are connected.
364 /* http://www.citadel.org/doku.php/documentation:appproto:connection#info.get.server.info */
366 function ctdl_get_registration_info() {
368 $reply = read_array();
370 // die ("didn't understand the reply to the INFO command");
376 // Display a system banner. (Returns completed HTML.)
377 // (One is probably temporary because it outputs more or less finalized
378 // markup. For now it's just usable.)
380 /* http://www.citadel.org/doku.php/documentation:appproto:connection#mesg.read.system.message */
381 function ctdl_mesg($msgname) {
382 global $clientsocket;
384 $msgtext = "<DIV ALIGN=CENTER>\n";
386 serv_puts("MESG " . $msgname);
387 $response = read_array();
389 if (substr($response[0], 0, 1) == "1") {
390 array_shift($response); // throw away the status code.
391 $msgtext .= "<TT>" . implode( "</TT><BR>\n" ,$response);
394 $msgtext .= "<B><I>" . substr($response[0], 4) . "</I></B><BR>\n";
397 $msgtext .= "</DIV>\n";
401 /* http://www.citadel.org/doku.php/documentation:appproto:connection#mesg.read.system.message */
402 //// TODO: is this still supported?
403 function ctdl_mrtg($what) {
404 global $clientsocket;
406 serv_puts("MRTG ".$what);
407 $response = serv_gets();
409 if (substr($response, 0, 1) != "1") {
410 return array(0, NULL);
413 $responses = read_array();
417 // Fetch the list of users currently logged in.
418 /* http://www.citadel.org/doku.php/documentation:appproto:connection#rwho.read.who.s.online */
420 function ctdl_rwho() {
421 global $clientsocket;
424 $response = serv_gets();
426 if (substr($response, 0, 1) != "1") {
427 return array(0, NULL);
430 $all_lines = array();
433 $responses = read_array();
434 foreach ($responses as $response) {
435 $tokens = explode("|", $response);
438 $oneline["session"] = $tokens[0];
439 $oneline["user"] = $tokens[1];
440 $oneline["room"] = $tokens[2];
441 $oneline["host"] = $tokens[3];
442 $oneline["client"] = $tokens[4];
443 $oneline["idlesince"] = $tokens[5];
444 $oneline["lastcmd"] = $tokens[6];
445 $oneline["flags"] = $tokens[7];
446 $oneline["realname"] = $tokens[8];
447 $oneline["realroom"] = $tokens[9];
448 $oneline["realhostname"] = $tokens[10];
449 $oneline["registered"] = $tokens[11];
451 // IGnore the rest of the fields for now.
452 if (CITADEL_DEBUG_CITPROTO == 1)
461 $num_lines = array_push($all_lines, $oneline);
464 return array($num_lines, $all_lines);
472 function ctdl_goto($to_where) {
474 serv_puts("GOTO " . $to_where);
475 $response = serv_gets();
477 $results = explode ("|", $response);
478 $status_room = array_shift($results);
479 $status = substr($status_room, 0, 3);
480 if (substr($status, 0, 1) == "2") {
481 $room = substr($status_room, 4);
482 array_unshift($results, $room);
485 "statereply" => $status,
486 "roomname" => $results[ 0],
487 "nunreadmsg" => $results[ 1],
488 "nmessages" => $results[ 2],
489 "rinfopresent" => $results[ 3],
490 "flags" => $results[ 4],
491 "msgidmax" => $results[ 5],
492 "msgidreadmax" => $results[ 6],
493 "ismailroom" => $results[ 7],
494 "isroomaide" => $results[ 8],
495 "nnewmessages" => $results[ 9],
496 "floorid" => $results[10],
497 "viewselected" => $results[11],
498 "defaultview" => $results[12],
499 "istrashcan" => $results[13]);
501 $_SESSION["room"] = $room;
502 if (CITADEL_DEBUG_CITPROTO == 1)
505 print_r($room_state);
514 return array("state" => FALSE, "statereply" => $status);
522 // Fetch the list of known rooms.
524 function ctdl_knrooms() {
525 global $clientsocket;
528 $response = serv_gets();
529 if (substr($response, 0, 1) != "1") {
530 return array(0, NULL);
532 $results = read_array();
533 $all_lines = array();
536 foreach ($results as $result){
538 $tokens = explode("|",$result);
540 $oneline["name"] = $tokens[0];
541 $oneline["flags"] = $tokens[1];
542 $oneline["floor"] = $tokens[2];
543 $oneline["order"] = $tokens[3];
544 $oneline["flags2"] = $tokens[4];
545 $oneline["access"] = $tokens[5];
547 if ($oneline["access"] & 8) {
548 $oneline["hasnewmsgs"] = TRUE;
551 $oneline["hasnewmsgs"] = FALSE;
554 if (CITADEL_DEBUG_CITPROTO == 1)
561 $num_lines = array_push($all_lines, $oneline);
564 return array($num_lines, $all_lines);
569 // Fetch the list of known floors.
571 /* http://www.citadel.org/doku.php/documentation:appproto:rooms#lflr.list.all.known.floors */
572 function ctdl_knfloors() {
573 global $clientsocket;
576 $response = serv_gets();
577 if (substr($response, 0, 1) != "1") {
578 return array(0, NULL);
581 $results = read_array();
582 $all_lines = array();
585 foreach ($results as $result){
587 $tokens = explode("|",$result);
589 $oneline["id"] = $tokens[0];
590 $oneline["name"] = $tokens[1];
591 $oneline["nref"] = $tokens[2];
593 if (CITADEL_DEBUG_CITPROTO == 1)
600 $num_lines = array_push($all_lines, $oneline);
603 return array($num_lines, $all_lines);
607 /* http://www.citadel.org/doku.php/documentation:appproto:rooms#cflr.create.a.new.floor */
610 // Fetch the list of messages in one room.
611 // Returns: count, response, message array
613 function ctdl_msgs($mode, $count) {
614 global $clientsocket;
616 serv_puts("MSGS " . $mode . "|" . $count);
617 $responses = read_array();
620 $response = array_shift($responses);
622 $num_msgs = count($responses);
623 if (substr($response, 0, 1) != "1") {
624 return array(0, substr($response, 4), NULL);
627 if (CITADEL_DEBUG_CITPROTO == 1)
629 printf("found ".$num_msgs." messages.");
631 return array($num_msgs, $response, $responses);
635 // Load a message from the server.
636 function ctdl_fetch_message($msgnum) {
637 global $clientsocket;
639 serv_puts("MSG4 " . $msgnum);
641 if (CITADEL_DEBUG_CITPROTO == 1)
642 printf ("<div class='ctdldbgRead'>");
643 $response = serv_gets(TRUE);
645 if (substr($response, 0, 1) != "1") {
646 return array(FALSE, substr($response, 4), NULL);
650 while (strcmp($buf = serv_gets(TRUE), "000")) {
651 if (substr($buf, 0, 4) == "text") {
652 if (CITADEL_DEBUG_CITPROTO == 1)
653 printf ("</div>\n<h3>Message Body Follows</h3><div class='ctdldbgRead'>");
654 // We're in the text body. New loop here.
655 $fields["text"] = ctdl_msg4_from_server();
656 if (CITADEL_DEBUG_CITPROTO == 1)
658 return array(TRUE, substr($response, 4), $fields);
661 $fields[substr($buf, 0, 4)] = substr($buf, 5);
665 // Message terminated prematurely (no text body)
666 return array(FALSE, substr($response, 4), $fields);
669 // Support function for ctdl_fetch_message(). This handles the text body
670 // portion of the message, converting various formats to HTML as
672 function ctdl_msg4_from_server() {
675 $msgformat = "text/plain";
679 while (strcmp($buf = serv_gets(TRUE), "000")) {
680 if ($in_body == FALSE) {
681 if (strlen($buf) == 0) {
685 if (!strncasecmp($buf, "content-type: ", 14)) {
686 $msgformat = substr($buf, 14);
691 if (!strcasecmp($msgformat, "text/html")) {
694 else if (!strcasecmp($msgformat, "text/plain")) {
695 $txt .= "<TT>" . htmlspecialchars($buf) . "</TT><BR>\n" ;
697 else if (!strcasecmp($msgformat, "text/x-citadel-variformat")) {
698 if (substr($previous_line, 0, 1) == " ") {
701 $txt .= htmlspecialchars($buf);
704 $txt .= htmlspecialchars($buf);
706 $previous_line = $buf;
715 function download_attachment($msgnum, $attindex)
717 $command = "DLAT ".$msgnum."|".$attindex;
719 $reply = read_binary();
725 function enter_message_0($msgHdr, $contentType, $data)
729 if (isset($msgHdr['newusermail']))
730 array_unshift($send, $msgHdr['newusermail']);
732 array_unshift($send, "");
734 if (isset($msgHdr['supplied_euid']))
735 array_unshift($send, $msgHdr['supplied_euid']);
737 array_unshift($send, "");
739 if (isset($msgHdr['bcc']))
740 array_unshift($send, $msgHdr['bcc']);
742 array_unshift($send, "");
744 if (isset($msgHdr['cc']))
745 array_unshift($send, $msgHdr['cc']);
747 array_unshift($send, "");
749 if (isset($msgHdr['do_confirm']))
750 array_unshift($send, $msgHdr['do_confirm']);
752 array_unshift($send, "");
754 if (isset($msgHdr['newusername']))
755 array_unshift($send, $msgHdr['newusername']);
757 array_unshift($send, "");
759 if (isset($msgHdr['subject']))
760 array_unshift($send, $msgHdr['subject']);
762 array_unshift($send, "");
764 if (isset($msgHdr['format_type']))
765 array_unshift($send, $msgHdr['format_type']);
767 array_unshift($send, "");
769 if (isset($msgHdr['anon_flag']))
770 array_unshift($send, $msgHdr['anon_flag']);
772 array_unshift($send, "");
774 if (isset($msgHdr['recp']))
775 array_unshift($send, $msgHdr['recp']);
777 array_unshift($send, "");
779 if (isset($msgHdr['post']))
780 array_unshift($send, $msgHdr['post']);
782 array_unshift($send, "");
784 $params = implode('|', $send);
785 serv_puts("ENT0 ".$params);
788 if (substr($reply, 0, 1) != 4)
789 return array(false, array(), array());
790 serv_puts("Content-type: ".$contentType);
792 serv_puts($data."\r\n");