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);
34 function dbgprintf_wrapout($string, $html)
36 if (!CITADEL_DEBUG_HTML){
38 debugLog("<< ".$string);
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) == 18) &&
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);
393 die ("didn't understand the reply to the INFO command");
398 // Learn all sorts of interesting things about the Citadel server to
399 // which we are connected.
400 /* http://www.citadel.org/doku.php/documentation:appproto:connection#info.get.server.info */
402 function ctdl_get_registration_info() {
404 $reply = read_array();
405 dbgprintf_wrapout(print_r($reply, true), true);
406 // die ("didn't understand the reply to the INFO command");
412 // Display a system banner. (Returns completed HTML.)
413 // (One is probably temporary because it outputs more or less finalized
414 // markup. For now it's just usable.)
416 /* http://www.citadel.org/doku.php/documentation:appproto:connection#mesg.read.system.message */
417 function ctdl_mesg($msgname) {
418 global $clientsocket;
420 $msgtext = "<DIV ALIGN=CENTER>\n";
422 serv_puts("MESG " . $msgname);
423 $response = read_array();
425 if (substr($response[0], 0, 1) == "1") {
426 array_shift($response); // throw away the status code.
427 $msgtext .= "<TT>" . implode( "</TT><BR>\n" ,$response);
430 $msgtext .= "<B><I>" . substr($response[0], 4) . "</I></B><BR>\n";
433 $msgtext .= "</DIV>\n";
437 /* http://www.citadel.org/doku.php/documentation:appproto:connection#mesg.read.system.message */
438 //// TODO: is this still supported?
439 function ctdl_mrtg($what) {
440 global $clientsocket;
442 serv_puts("MRTG ".$what);
443 $response = serv_gets();
445 if (substr($response, 0, 1) != "1") {
446 return array(0, NULL);
449 $responses = read_array();
453 // Fetch the list of users currently logged in.
454 /* http://www.citadel.org/doku.php/documentation:appproto:connection#rwho.read.who.s.online */
456 function ctdl_rwho() {
457 global $clientsocket;
460 $response = serv_gets();
462 if (substr($response, 0, 1) != "1") {
463 return array(0, NULL);
466 $all_lines = array();
469 $responses = read_array();
470 foreach ($responses as $response) {
471 $tokens = explode("|", $response);
474 $oneline["session"] = $tokens[0];
475 $oneline["user"] = $tokens[1];
476 $oneline["room"] = $tokens[2];
477 $oneline["host"] = $tokens[3];
478 $oneline["client"] = $tokens[4];
479 $oneline["idlesince"] = $tokens[5];
480 $oneline["lastcmd"] = $tokens[6];
481 $oneline["flags"] = $tokens[7];
482 $oneline["realname"] = $tokens[8];
483 $oneline["realroom"] = $tokens[9];
484 $oneline["realhostname"] = $tokens[10];
485 $oneline["registered"] = $tokens[11];
487 // IGnore the rest of the fields for now.
488 if (CITADEL_DEBUG_CITPROTO == 1)
490 dbgprintf_wrapout("<pre>", false);
491 dbgprintf_wrapout(print_r($oneline, true), true);
492 dbgprintf_wrapout("</pre>", false);;
497 $num_lines = array_push($all_lines, $oneline);
500 return array($num_lines, $all_lines);
508 function ctdl_goto($to_where) {
510 serv_puts("GOTO " . $to_where);
511 $response = serv_gets();
513 $results = explode ("|", $response);
514 $status_room = array_shift($results);
515 $status = substr($status_room, 0, 3);
516 if (substr($status, 0, 1) == "2") {
517 $room = substr($status_room, 4);
518 array_unshift($results, $room);
521 "statereply" => $status,
522 "roomname" => $results[ 0],
523 "nunreadmsg" => $results[ 1],
524 "nmessages" => $results[ 2],
525 "rinfopresent" => $results[ 3],
526 "flags" => $results[ 4],
527 "msgidmax" => $results[ 5],
528 "msgidreadmax" => $results[ 6],
529 "ismailroom" => $results[ 7],
530 "isroomaide" => $results[ 8],
531 "nnewmessages" => $results[ 9],
532 "floorid" => $results[10],
533 "viewselected" => $results[11],
534 "defaultview" => $results[12],
535 "istrashcan" => $results[13]);
537 $_SESSION["room"] = $room;
538 if (CITADEL_DEBUG_CITPROTO == 1)
540 dbgprintf_wrapout("<pre>", false);
541 dbgprintf_wrapout(print_r($room_state, true), true);
542 dbgprintf_wrapout("</pre>", false);
550 return array("state" => FALSE, "statereply" => $status);
558 // Fetch the list of known rooms.
560 function ctdl_knrooms() {
561 global $clientsocket;
564 $response = serv_gets();
565 if (substr($response, 0, 1) != "1") {
566 return array(0, NULL);
568 $results = read_array();
569 $all_lines = array();
572 foreach ($results as $result){
574 $tokens = explode("|",$result);
576 $oneline["name"] = $tokens[0];
577 $oneline["flags"] = $tokens[1];
578 $oneline["floor"] = $tokens[2];
579 $oneline["order"] = $tokens[3];
580 $oneline["flags2"] = $tokens[4];
581 $oneline["access"] = $tokens[5];
583 if ($oneline["access"] & 8) {
584 $oneline["hasnewmsgs"] = TRUE;
587 $oneline["hasnewmsgs"] = FALSE;
590 if (CITADEL_DEBUG_CITPROTO == 1)
592 dbgprintf_wrapout("<pre>", false);
593 dbgprintf_wrapout(print_r($oneline, true), true);
594 dbgprintf_wrapout("</pre>", false);
597 $num_lines = array_push($all_lines, $oneline);
600 return array($num_lines, $all_lines);
605 // Fetch the list of known floors.
607 /* http://www.citadel.org/doku.php/documentation:appproto:rooms#lflr.list.all.known.floors */
608 function ctdl_knfloors() {
609 global $clientsocket;
612 $response = serv_gets();
613 if (substr($response, 0, 1) != "1") {
614 return array(0, NULL);
617 $results = read_array();
618 $all_lines = array();
621 foreach ($results as $result){
623 $tokens = explode("|",$result);
625 $oneline["id"] = $tokens[0];
626 $oneline["name"] = $tokens[1];
627 $oneline["nref"] = $tokens[2];
629 if (CITADEL_DEBUG_CITPROTO == 1)
631 dbgprintf_wrapout("<pre>", false);
632 dbgprintf_wrapout(print_r($oneline, true), true);
633 dbgprintf_wrapout("</pre>", false);
635 $num_lines = array_push($all_lines, $oneline);
638 return array($num_lines, $all_lines);
642 /* http://www.citadel.org/doku.php/documentation:appproto:rooms#cflr.create.a.new.floor */
645 // Fetch the list of messages in one room.
646 // Returns: count, response, message array
648 function ctdl_msgs($mode, $count) {
649 global $clientsocket;
651 serv_puts("MSGS " . $mode . "|" . $count);
652 $responses = read_array();
653 dbgprintf_wrapout(print_r($responses, true), false);
655 $response = array_shift($responses);
657 $num_msgs = count($responses);
658 if (substr($response, 0, 1) != "1") {
659 return array(0, substr($response, 4), NULL);
662 if (CITADEL_DEBUG_CITPROTO == 1)
664 dbgprintf_wrapout("found ".$num_msgs." messages.", true);
666 return array($num_msgs, $response, $responses);
670 // Load a message from the server.
671 function ctdl_fetch_message($msgnum) {
672 global $clientsocket;
674 serv_puts("MSG4 " . $msgnum);
676 if (CITADEL_DEBUG_CITPROTO == 1)
677 dbgprintf_wrapout("<div class='ctdldbgRead'>", false);
678 $response = serv_gets(TRUE);
680 if (substr($response, 0, 1) != "1") {
681 return array(FALSE, substr($response, 4), NULL);
685 while (strcmp($buf = serv_gets(TRUE), "000")) {
686 if (substr($buf, 0, 4) == "text") {
687 if (CITADEL_DEBUG_CITPROTO == 1)
688 dbgprintf_wrapout("</div>\n<h3>Message Body Follows</h3><div class='ctdldbgRead'>", false);
689 // We're in the text body. New loop here.
690 $fields["text"] = ctdl_msg4_from_server();
691 if (CITADEL_DEBUG_CITPROTO == 1)
692 dbgprintf_wrapout ("</div>", false);
693 return array(TRUE, substr($response, 4), $fields);
696 $fields[substr($buf, 0, 4)] = substr($buf, 5);
700 // Message terminated prematurely (no text body)
701 return array(FALSE, substr($response, 4), $fields);
704 // Load a message from the server.
705 function ctdl_fetch_message_rfc822($msgnum) {
706 global $clientsocket;
708 serv_puts("MSG2 " . $msgnum);
710 if (CITADEL_DEBUG_CITPROTO == 1)
711 dbgprintf_wrapout("<div class='ctdldbgRead'>", false);
712 $response = serv_gets(TRUE);
714 if (substr($response, 0, 1) != "1") {
715 return array(FALSE, NULL);
719 while ($buf = serv_gets(TRUE)) {
720 // dbgprintf_wrapout($buf, true);
723 $message = $message . "\n" . $buf;
727 // dbgprintf_wrapout($message, true);
728 // Message terminated prematurely (no text body)
729 return array(TRUE, $message);
732 // Support function for ctdl_fetch_message(). This handles the text body
733 // portion of the message, converting various formats to HTML as
735 function ctdl_msg4_from_server() {
738 $msgformat = "text/plain";
742 while (strcmp($buf = serv_gets(TRUE), "000")) {
743 if ($in_body == FALSE) {
744 if (strlen($buf) == 0) {
748 if (!strncasecmp($buf, "content-type: ", 14)) {
749 $msgformat = substr($buf, 14);
754 if (!strcasecmp($msgformat, "text/html")) {
757 else if (!strcasecmp($msgformat, "text/plain")) {
758 $txt .= "<TT>" . htmlspecialchars($buf) . "</TT><BR>\n" ;
760 else if (!strcasecmp($msgformat, "text/x-citadel-variformat")) {
761 if (substr($previous_line, 0, 1) == " ") {
764 $txt .= htmlspecialchars($buf);
767 $txt .= htmlspecialchars($buf);
769 $previous_line = $buf;
778 function download_attachment($msgnum, $attindex)
780 $command = "DLAT ".$msgnum."|".$attindex;
782 $reply = read_binary();
788 function enter_message_0($msgHdr, $contentType, $data)
792 if (isset($msgHdr['newusermail']))
793 array_unshift($send, $msgHdr['newusermail']);
795 array_unshift($send, "");
797 if (isset($msgHdr['supplied_euid']))
798 array_unshift($send, $msgHdr['supplied_euid']);
800 array_unshift($send, "");
802 if (isset($msgHdr['bcc']))
803 array_unshift($send, $msgHdr['bcc']);
805 array_unshift($send, "");
807 if (isset($msgHdr['cc']))
808 array_unshift($send, $msgHdr['cc']);
810 array_unshift($send, "");
812 if (isset($msgHdr['do_confirm']))
813 array_unshift($send, $msgHdr['do_confirm']);
815 array_unshift($send, "");
817 if (isset($msgHdr['newusername']))
818 array_unshift($send, $msgHdr['newusername']);
820 array_unshift($send, "");
822 if (isset($msgHdr['subject']))
823 array_unshift($send, $msgHdr['subject']);
825 array_unshift($send, "");
827 if (isset($msgHdr['format_type']))
828 array_unshift($send, $msgHdr['format_type']);
830 array_unshift($send, "");
832 if (isset($msgHdr['anon_flag']))
833 array_unshift($send, $msgHdr['anon_flag']);
835 array_unshift($send, "");
837 if (isset($msgHdr['recp']))
838 array_unshift($send, $msgHdr['recp']);
840 array_unshift($send, "");
842 if (isset($msgHdr['post']))
843 array_unshift($send, $msgHdr['post']);
845 array_unshift($send, "");
847 $params = implode('|', $send);
848 serv_puts("ENT0 ".$params);
851 if (substr($reply, 0, 1) != 4)
852 return array(false, array(), array());
853 serv_puts("Content-type: ".$contentType);
855 serv_puts($data."\r\n");