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 define('VIEW_BBS' ,'0'); /* Bulletin board view */
11 define('VIEW_MAILBOX' ,'1'); /* Mailbox summary */
12 define('VIEW_ADDRESSBOOK' ,'2'); /* Address book view */
13 define('VIEW_CALENDAR' ,'3'); /* Calendar view */
14 define('VIEW_TASKS' ,'4'); /* Tasks view */
15 define('VIEW_NOTES' ,'5'); /* Notes view */
16 define("FMT_CITADEL", 0);
17 define("FMT_FIXED", 1);
18 define("FMT_RFC822", 4);
20 function debugLog($string)
25 function dbgprintf_wrapin($string, $html)
27 if (!CITADEL_DEBUG_HTML
){
29 debugLog("<< ".$string."\n");
34 function dbgprintf_wrapout($string, $html)
36 if (!CITADEL_DEBUG_HTML
){
38 debugLog("<< ".$string."\n");
44 //--------------------------------------------------------------------------------
45 // internal functions for server communication
46 //--------------------------------------------------------------------------------
48 // serv_gets() -- generic function to read one line of text from the server
50 function serv_gets($readblock=FALSE) {
53 $buf = fgets($clientsocket, 4096); // Read line
54 $buf = substr($buf, 0, (strlen($buf)-1) ); // strip trailing LF
55 if (CITADEL_DEBUG_CITPROTO
== 1) {
56 if (!$readblock) dbgprintf_wrapin("<div class='ctdldbgRead'>\n", false);
57 dbgprintf_wrapin($buf, true);
58 if (!$readblock) dbgprintf_wrapin ("\n</div>\n", false);
59 else dbgprintf_wrapin ("<br>\n", false);
65 // serv_get_n() -- generic function to read a binary blob from the server
67 function serv_get_n($nBytes) {
70 if (CITADEL_DEBUG_CITPROTO
== 1) {
71 dbgprintf_wrapin ("<div class='ctdldbgRead'>\n", false);
72 dbgprintf_wrapin("reading ".$nBytes." bytes from server\n", true);
73 dbgprintf_wrapin ("</div>\n", false);
75 $buf = fread($clientsocket, $nBytes);
76 if (CITADEL_DEBUG_CITPROTO
== 1) {
77 if (!$buf) dbgprintf_wrapin ("<div class='ctdldbgRead'>\n", false);
78 dbgprintf_wrapin($buf, true);
79 if (!$buf) dbgprintf_wrapin ("</div>\n", false);
80 else dbgprintf_wrapin ("<br>\n", false);
86 // serv_puts() -- generic function to write one line of text to the server
88 function serv_puts($buf) {
91 fwrite($clientsocket, $buf . "\n", (strlen($buf)+
1) );
92 fflush($clientsocket);
93 if (CITADEL_DEBUG_CITPROTO
== 1) {
94 dbgprintf_wrapin("<div class='ctdldbgWrite'>", false);
95 dbgprintf_wrapin($buf, true);
96 dbgprintf_wrapin("</div>\n", false);
101 function read_array() {
103 if (CITADEL_DEBUG_CITPROTO
== 1)
104 dbgprintf_wrapout("<div class='ctdldbgRead'>\n", false);
105 $buf = serv_gets(TRUE);
107 while (strcasecmp($buf, "000")){
108 array_push($ret, $buf);
109 $buf = serv_gets(TRUE);
112 if (CITADEL_DEBUG_CITPROTO
== 1){
113 dbgprintf_wrapout("read ".$nLines." lines from the server.\n", true);
114 dbgprintf_wrapout ("</div>\n", false);
119 function read_binary() {
121 if (CITADEL_DEBUG_CITPROTO
== 1)
122 dbgprintf_wrapout ("<div class='ctdldbgRead'>\n", false);
123 $buf = serv_gets(TRUE);
125 if (CITADEL_DEBUG_CITPROTO
== 1){
126 dbgprintf_wrapout("status line from the server\n", true);
129 $statusline = explode(" ", $buf);
131 if ($statusline[0] == 600)
133 $buf = serv_get_n($statusline[1]);
136 if (CITADEL_DEBUG_CITPROTO
== 1)
137 dbgprintf_wrapout ("</div>\n", false);
138 return array($statusline, $buf);
144 // text_to_server() -- sends a block of text to the server. Assumes that
145 // the server just sent back a SEND_LISTING response code
146 // and is now expecting a 000-terminated series of lines.
147 // Set 'convert_to_html' to TRUE to convert the block of
148 // text to HTML along the way.
150 function text_to_server($thetext, $convert_to_html) {
153 if ($convert_to_html) {
155 // Strip CR's; we only want the LF's
156 $thetext = trim($thetext, "\r");
158 // Replace hard line breaks with <BR>'s
159 $thetext = str_replace("\n", "<BR>\n", $thetext);
163 // Either mode ... send it to the server now
164 $one_line = strtok($thetext, "\n");
165 while ($one_line !== FALSE) {
166 $one_line = trim($one_line, "\n\r");
167 if ($one_line == "000") $one_line = "-000" ;
168 serv_puts($one_line);
169 $one_line = strtok("\n");
172 serv_puts("000"); // Tell the server we're done...
174 serv_puts("ECHO echo test."); // FIXME
175 echo "Echo test: " . serv_gets() . "<BR>\n" ;
179 //--------------------------------------------------------------------------------
181 //--------------------------------------------------------------------------------
185 // Identify ourselves to the Citadel server (do one once after connection)
186 /* http://www.citadel.org/doku.php/documentation:appproto:connection#iden.identify.the.client.software */
188 function ctdl_iden($client_info) {
189 global $clientsocket;
191 if (count($client_info) != 5)
192 die("ctdl_iden takes 5 arguments!");
193 // Identify client and hostname
194 serv_puts("IDEN ".implode('|', $client_info));
198 function ctdl_MessageFormatsPrefered($formatlist){
199 // Also express our message format preferences
200 serv_puts("MSGP ".implode("|", $formatlist));
204 /* http://www.citadel.org/doku.php/documentation:appproto:connection#noop.no.operation */
205 function ctdl_noop(){
206 // Also express our message format preferences
211 /* http://www.citadel.org/doku.php/documentation:appproto:connection#quit.quit */
212 function ctdl_quit(){
213 // Also express our message format preferences
219 /* http://www.citadel.org/doku.php/documentation:appproto:connection#mesg.read.system.message */
220 function ctdl_gtls(){
221 // Also express our message format preferences
228 /* http://www.citadel.org/doku.php/documentation:appproto:connection#qnop.quiet.no.operation */
229 /* this seems to be dangerous. ask IG
230 function ctdl_qnoop(){
231 // Also express our message format preferences
236 /* http://www.citadel.org/doku.php/documentation:appproto:connection#echo.echo.something */
237 function ctdl_doecho($echotext){
238 // Also express our message format preferences
239 serv_puts("ECHO ".$echotext);
244 /* http://www.citadel.org/doku.php/documentation:appproto:connection#time.get.server.local.time */
245 /* TODO: what are the other two params? doku is incomplete here. */
246 function ctdl_time(){
247 // Also express our message format preferences
254 /* http://www.citadel.org/doku.php/documentation:appproto:connection#qdir.query.global.directory */
255 function ctdl_qdir($who){
256 // Also express our message format preferences
257 serv_puts("QDIR ".$who);
259 return array((substr($buf, 0, 1) == "2"), $buf);
263 /* http://www.citadel.org/doku.php/documentation:appproto:connection#auto.autocompletion.of.email.addresses */
264 function ctdl_auto($who){
265 // Also express our message format preferences
266 serv_puts("AUTO ".$who);
268 if (substr($buf, 0, 1) == "1") {
269 $reply = read_array();
270 if (count($reply) == 0)
281 // login_existing_user() -- attempt to login using a supplied username/password
282 // Returns an array with two variables:
283 // 0. TRUE or FALSE to determine success or failure
284 // 1. String error message (if relevant)
285 /* http://www.citadel.org/doku.php/documentation:appproto:connection#user.send.user.name */
286 /* http://www.citadel.org/doku.php/documentation:appproto:connection#pass.send.password */
289 function login_existing_user($user, $pass) {
290 global $clientsocket;
292 serv_puts("USER " . $user);
295 if (substr($resp, 0, 3) == 541) // we're already logged in.
296 return array(TRUE, substr($resp, 4));
297 if (substr($resp, 0, 1) != "3") {
299 return array(FALSE, substr($resp, 4));
302 serv_puts("PASS " . $pass);
304 if (substr($resp, 0, 1) != "2") {
305 return array(FALSE, substr($resp, 4));
308 $_SESSION["username"] = $user;
309 $_SESSION["password"] = $pass;
310 become_logged_in(substr($resp, 4));
312 return array(TRUE, "Login successful. Have fun.");
317 // create_new_user() -- attempt to create a new user
318 // using a supplied username/password
319 // Returns an array with two variables:
320 // 0. TRUE or FALSE to determine success or failure
321 // 1. String error message (if relevant)
323 function create_new_user($user, $pass) {
324 global $clientsocket;
326 serv_puts("NEWU " . $user);
328 if (substr($resp, 0, 1) != "2") {
329 return array(FALSE, substr($resp, 4));
332 serv_puts("SETP " . $pass);
334 if (substr($resp, 0, 1) != "2") {
335 return array(FALSE, substr($resp, 4));
338 $_SESSION["username"] = $user;
339 $_SESSION["password"] = $pass;
340 become_logged_in(substr($resp, 4));
342 return array(TRUE, "Login successful. Have fun.");
347 // Code common to both existing-user and new-user logins
349 function become_logged_in($server_parms) {
350 $_SESSION["logged_in"] = 1;
352 $tokens = explode("|", $server_parms);
354 $oneline["username"] = $tokens[0];
355 $oneline["axlevel"] = $tokens[1];
356 $oneline["calls"] = $tokens[2];
357 $oneline["posts"] = $tokens[3];
358 $oneline["userflags"] = $tokens[4];
359 $oneline["usernum"] = $tokens[5];
360 $oneline["lastcall"] = $tokens[6];
362 ctdl_goto("_BASEROOM_");
368 // Learn all sorts of interesting things about the Citadel server to
369 // which we are connected.
370 /* http://www.citadel.org/doku.php/documentation:appproto:connection#info.get.server.info */
372 function ctdl_get_serv_info() {
374 $reply = read_array();
375 if ((count($reply) == 23) &&
376 substr($reply[0], 0, 1) == "1") {
377 $server_info=array();
378 $server_info["serv_nodename"] = $reply[1];
379 $server_info["serv_humannode"] = $reply[2];
380 $server_info["serv_fqdn"] = $reply[3];
381 $server_info["serv_software"] = $reply[4];
382 $server_info["serv_city"] = $reply[6];
383 $server_info["serv_sysadmin"] = $reply[7];
384 if (CITADEL_DEBUG_CITPROTO
== 1)
386 dbgprintf_wrapout("<pre>", false);
387 dbgprintf_wrapout(print_r($server_info, true), true);
388 dbgprintf_wrapout("</pre>", false);
394 dbgprintf_wrapin ("didn't understand the reply to the INFO command".
395 print_r($reply, TRUE), false);
397 die ("CTDLPHP: didn't understand the reply to the INFO command");
402 // Learn all sorts of interesting things about the Citadel server to
403 // which we are connected.
404 /* http://www.citadel.org/doku.php/documentation:appproto:connection#info.get.server.info */
406 function ctdl_get_registration_info() {
408 $reply = read_array();
409 dbgprintf_wrapout(print_r($reply, true), true);
410 // die ("didn't understand the reply to the INFO command");
416 // Display a system banner. (Returns completed HTML.)
417 // (One is probably temporary because it outputs more or less finalized
418 // markup. For now it's just usable.)
420 /* http://www.citadel.org/doku.php/documentation:appproto:connection#mesg.read.system.message */
421 function ctdl_mesg($msgname) {
422 global $clientsocket;
424 $msgtext = "<DIV ALIGN=CENTER>\n";
426 serv_puts("MESG " . $msgname);
427 $response = read_array();
429 if (substr($response[0], 0, 1) == "1") {
430 array_shift($response); // throw away the status code.
431 $msgtext .= "<TT>" . implode( "</TT><BR>\n" ,$response);
434 $msgtext .= "<B><I>" . substr($response[0], 4) . "</I></B><BR>\n";
437 $msgtext .= "</DIV>\n";
443 // http://www.citadel.org/doku.php/documentation:appproto:room_indexes_and_messages#dele.delete.a.message
445 function ctdl_dele($msgname) {
446 global $clientsocket;
448 $msgtext = "<DIV ALIGN=CENTER>\n";
450 serv_puts("DELE " . $msgname);
451 $response = serv_gets();
453 if (substr($response[0], 0, 1) == "1") {
461 /* http://www.citadel.org/doku.php/documentation:appproto:connection#mesg.read.system.message */
462 //// TODO: is this still supported?
463 function ctdl_mrtg($what) {
464 global $clientsocket;
466 serv_puts("MRTG ".$what);
467 $response = serv_gets();
469 if (substr($response, 0, 1) != "1") {
470 return array(0, NULL);
473 $responses = read_array();
477 // Fetch the list of users currently logged in.
478 /* http://www.citadel.org/doku.php/documentation:appproto:connection#rwho.read.who.s.online */
480 function ctdl_rwho() {
481 global $clientsocket;
484 $response = serv_gets();
486 if (substr($response, 0, 1) != "1") {
487 return array(0, NULL);
490 $all_lines = array();
493 $responses = read_array();
494 foreach ($responses as $response) {
495 $tokens = explode("|", $response);
498 $oneline["session"] = $tokens[0];
499 $oneline["user"] = $tokens[1];
500 $oneline["room"] = $tokens[2];
501 $oneline["host"] = $tokens[3];
502 $oneline["client"] = $tokens[4];
503 $oneline["idlesince"] = $tokens[5];
504 $oneline["lastcmd"] = $tokens[6];
505 $oneline["flags"] = $tokens[7];
506 $oneline["realname"] = $tokens[8];
507 $oneline["realroom"] = $tokens[9];
508 $oneline["realhostname"] = $tokens[10];
509 $oneline["registered"] = $tokens[11];
511 // IGnore the rest of the fields for now.
512 if (CITADEL_DEBUG_CITPROTO
== 1)
514 dbgprintf_wrapout("<pre>", false);
515 dbgprintf_wrapout(print_r($oneline, true), true);
516 dbgprintf_wrapout("</pre>", false);;
521 $num_lines = array_push($all_lines, $oneline);
524 return array($num_lines, $all_lines);
532 function ctdl_goto($to_where) {
534 serv_puts("GOTO " . $to_where);
535 $response = serv_gets();
537 $results = explode ("|", $response);
538 $status_room = array_shift($results);
539 $status = substr($status_room, 0, 3);
540 if (substr($status, 0, 1) == "2") {
541 $room = substr($status_room, 4);
542 array_unshift($results, $room);
545 "statereply" => $status,
546 "roomname" => $results[ 0],
547 "nunreadmsg" => $results[ 1],
548 "nmessages" => $results[ 2],
549 "rinfopresent" => $results[ 3],
550 "flags" => $results[ 4],
551 "msgidmax" => $results[ 5],
552 "msgidreadmax" => $results[ 6],
553 "ismailroom" => $results[ 7],
554 "isroomaide" => $results[ 8],
555 "nnewmessages" => $results[ 9],
556 "floorid" => $results[10],
557 "viewselected" => $results[11],
558 "defaultview" => $results[12],
559 "istrashcan" => $results[13]);
561 $_SESSION["room"] = $room;
562 if (CITADEL_DEBUG_CITPROTO
== 1)
564 dbgprintf_wrapout("<pre>", false);
565 dbgprintf_wrapout(print_r($room_state, true), true);
566 dbgprintf_wrapout("</pre>", false);
574 return array("state" => FALSE, "statereply" => $status);
582 // Fetch the list of known rooms.
584 function ctdl_knrooms() {
585 global $clientsocket;
588 $response = serv_gets();
589 if (substr($response, 0, 1) != "1") {
590 return array(0, NULL);
592 $results = read_array();
593 $all_lines = array();
596 foreach ($results as $result){
598 $tokens = explode("|",$result);
600 $oneline["name"] = $tokens[0];
601 $oneline["flags"] = $tokens[1];
602 $oneline["floor"] = $tokens[2];
603 $oneline["order"] = $tokens[3];
604 $oneline["flags2"] = $tokens[4];
605 $oneline["access"] = $tokens[5];
607 if ($oneline["access"] & 8) {
608 $oneline["hasnewmsgs"] = TRUE;
611 $oneline["hasnewmsgs"] = FALSE;
614 if (CITADEL_DEBUG_CITPROTO
== 1)
616 dbgprintf_wrapout("<pre>", false);
617 dbgprintf_wrapout(print_r($oneline, true), true);
618 dbgprintf_wrapout("</pre>", false);
621 $num_lines = array_push($all_lines, $oneline);
624 return array($num_lines, $all_lines);
629 // Fetch the list of known floors.
631 /* http://www.citadel.org/doku.php/documentation:appproto:rooms#lflr.list.all.known.floors */
632 function ctdl_knfloors() {
633 global $clientsocket;
636 $response = serv_gets();
637 if (substr($response, 0, 1) != "1") {
638 return array(0, NULL);
641 $results = read_array();
642 $all_lines = array();
645 foreach ($results as $result){
647 $tokens = explode("|",$result);
649 $oneline["id"] = $tokens[0];
650 $oneline["name"] = $tokens[1];
651 $oneline["nref"] = $tokens[2];
653 if (CITADEL_DEBUG_CITPROTO
== 1)
655 dbgprintf_wrapout("<pre>", false);
656 dbgprintf_wrapout(print_r($oneline, true), true);
657 dbgprintf_wrapout("</pre>", false);
659 $num_lines = array_push($all_lines, $oneline);
662 return array($num_lines, $all_lines);
666 /* http://www.citadel.org/doku.php/documentation:appproto:rooms#cflr.create.a.new.floor */
669 // Fetch the list of messages in one room.
670 // Returns: count, response, message array
672 function ctdl_msgs($mode, $count) {
673 global $clientsocket;
675 serv_puts("MSGS " . $mode . "|" . $count);
676 $responses = read_array();
677 dbgprintf_wrapout(print_r($responses, true), false);
679 $response = array_shift($responses);
681 $num_msgs = count($responses);
682 if (substr($response, 0, 1) != "1") {
683 return array(0, substr($response, 4), NULL);
686 if (CITADEL_DEBUG_CITPROTO
== 1)
688 dbgprintf_wrapout("found ".$num_msgs." messages.", true);
690 return array($num_msgs, $response, $responses);
694 // Load a message from the server.
695 function ctdl_fetch_message($msgnum) {
696 global $clientsocket;
698 serv_puts("MSG4 " . $msgnum);
700 if (CITADEL_DEBUG_CITPROTO
== 1)
701 dbgprintf_wrapout("<div class='ctdldbgRead'>", false);
702 $response = serv_gets(TRUE);
704 if (substr($response, 0, 1) != "1") {
705 return array(FALSE, substr($response, 4), NULL);
709 while (strcmp($buf = serv_gets(TRUE), "000")) {
710 if (substr($buf, 0, 4) == "text") {
711 if (CITADEL_DEBUG_CITPROTO
== 1)
712 dbgprintf_wrapout("</div>\n<h3>Message Body Follows</h3><div class='ctdldbgRead'>", false);
713 // We're in the text body. New loop here.
714 $texts = ctdl_msg4_from_server();
715 $fields["text"] = $texts[0];
716 $fields["formated_text"]=$texts[1];
717 if (CITADEL_DEBUG_CITPROTO
== 1)
718 dbgprintf_wrapout ("</div>", false);
719 return array(TRUE, substr($response, 4), $fields);
722 $fields[substr($buf, 0, 4)] = substr($buf, 5);
726 // Message terminated prematurely (no text body)
727 return array(FALSE, substr($response, 4), $fields);
730 // Load a message from the server.
731 function ctdl_fetch_message_rfc822($msgnum) {
732 global $clientsocket;
734 serv_puts("MSG2 " . $msgnum);
736 if (CITADEL_DEBUG_CITPROTO
== 1)
737 dbgprintf_wrapout("<div class='ctdldbgRead'>", false);
738 $response = serv_gets(TRUE);
740 if (substr($response, 0, 1) != "1") {
741 return array(FALSE, NULL);
745 while ($buf = serv_gets(TRUE)) {
746 // dbgprintf_wrapout($buf, true);
752 $message = $message . "\n" . $buf;
756 // dbgprintf_wrapout($message, true);
757 // Message terminated prematurely (no text body)
758 return array(TRUE, $message);
761 // Support function for ctdl_fetch_message(). This handles the text body
762 // portion of the message, converting various formats to HTML as
764 function ctdl_msg4_from_server() {
768 $msgformat = "text/plain";
772 while (strcmp($buf = serv_gets(TRUE), "000")) {
773 if ($in_body == FALSE) {
774 if (strlen($buf) == 0) {
778 if (!strncasecmp($buf, "content-type: ", 14)) {
779 $msgformat = substr($buf, 14);
784 if (!strcasecmp($msgformat, "text/html")) {
787 else if (!strcasecmp($msgformat, "text/plain")) {
789 $modified_txt .= "<TT>" . htmlspecialchars($buf) . "</TT><BR>\n" ;
792 else if (!strcasecmp($msgformat, "text/x-citadel-variformat")) {
793 if (substr($previous_line, 0, 1) == " ") {
796 $txt .= htmlspecialchars($buf);
799 $txt .= htmlspecialchars($buf);
801 $previous_line = $buf;
805 return(array($txt, $modified_txt));
810 function download_attachment($msgnum, $attindex)
812 $command = "DLAT ".$msgnum."|".$attindex;
814 $reply = read_binary();
820 function enter_message_0($msgHdr, $contentType, $data)
824 if (isset($msgHdr['newusermail']))
825 array_unshift($send, $msgHdr['newusermail']);
827 array_unshift($send, "");
829 if (isset($msgHdr['supplied_euid']))
830 array_unshift($send, $msgHdr['supplied_euid']);
832 array_unshift($send, "");
834 if (isset($msgHdr['bcc']))
835 array_unshift($send, $msgHdr['bcc']);
837 array_unshift($send, "");
839 if (isset($msgHdr['cc']))
840 array_unshift($send, $msgHdr['cc']);
842 array_unshift($send, "");
844 if (isset($msgHdr['do_confirm']))
845 array_unshift($send, $msgHdr['do_confirm']);
847 array_unshift($send, "");
849 if (isset($msgHdr['newusername']))
850 array_unshift($send, $msgHdr['newusername']);
852 array_unshift($send, "");
854 if (isset($msgHdr['subject']))
855 array_unshift($send, $msgHdr['subject']);
857 array_unshift($send, "");
859 if (isset($msgHdr['format_type']))
860 array_unshift($send, $msgHdr['format_type']);
862 array_unshift($send, "");
864 if (isset($msgHdr['anon_flag']))
865 array_unshift($send, $msgHdr['anon_flag']);
867 array_unshift($send, "");
869 if (isset($msgHdr['recp']))
870 array_unshift($send, $msgHdr['recp']);
872 array_unshift($send, "");
874 if (isset($msgHdr['post']))
875 array_unshift($send, $msgHdr['post']);
877 array_unshift($send, "");
879 $params = implode('|', $send);
880 serv_puts("ENT0 ".$params);
883 if (substr($reply, 0, 1) != 4)
884 return array(false, array(), array());
885 serv_puts("Content-type: ".$contentType);
887 serv_puts($data."\r\n");