Added a comma after each msgnum exported. The parser was globbing them all together...
[citadel.git] / ctdlphp / ctdlprotocol.php
1 <?PHP
2 // $Id$
3 // 
4 // Implements various Citadel server commands.
5 //
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";
9
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);
19
20 function debugLog($string)
21 {
22         print ($string);
23 }
24
25 function dbgprintf_wrapin($string, $html)
26 {
27         if (!CITADEL_DEBUG_HTML){
28                 if ($html)
29                         debugLog("<< ".$string."\n");
30         }
31         else 
32                 printf($string);
33 }
34 function dbgprintf_wrapout($string, $html)
35 {
36         if (!CITADEL_DEBUG_HTML){
37                 if ($html)
38                         debugLog("<< ".$string."\n");
39         }
40         else
41                 printf($string);
42
43 }
44 //--------------------------------------------------------------------------------
45 //   internal functions for server communication
46 //--------------------------------------------------------------------------------
47 //
48 // serv_gets() -- generic function to read one line of text from the server
49 //
50 function serv_gets($readblock=FALSE) {
51         global $clientsocket;
52
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);
60         }
61         return $buf;
62 }
63
64 //
65 // serv_get_n() -- generic function to read a binary blob from the server
66 //
67 function serv_get_n($nBytes) {
68         global $clientsocket;
69
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);
74         }
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);
81         }
82         return $buf;
83 }
84
85 //
86 // serv_puts() -- generic function to write one line of text to the server
87 //
88 function serv_puts($buf) {
89         global $clientsocket;
90         
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);
97         }
98 }
99
100
101 function read_array() {
102         $nLines = 0;
103         if (CITADEL_DEBUG_CITPROTO == 1)
104                 dbgprintf_wrapout("<div class='ctdldbgRead'>\n", false);
105         $buf = serv_gets(TRUE);
106         $ret = array();
107         while (strcasecmp($buf, "000")){
108                 array_push($ret, $buf);
109                 $buf = serv_gets(TRUE);
110                 $nLines++;
111         }
112         if (CITADEL_DEBUG_CITPROTO == 1){
113                 dbgprintf_wrapout("read ".$nLines." lines from the server.\n", true);
114                 dbgprintf_wrapout ("</div>\n", false);
115         }
116         return $ret;
117 }
118
119 function read_binary() {
120         $nLines = 0;
121         if (CITADEL_DEBUG_CITPROTO == 1)
122                 dbgprintf_wrapout ("<div class='ctdldbgRead'>\n", false);
123         $buf = serv_gets(TRUE);
124         
125         if (CITADEL_DEBUG_CITPROTO == 1){
126                 dbgprintf_wrapout("status line from the server\n", true);
127         }
128
129         $statusline = explode(" ", $buf);
130         
131         if ($statusline[0] == 600)
132         {
133                 $buf = serv_get_n($statusline[1]);
134                 
135         }
136         if (CITADEL_DEBUG_CITPROTO == 1)
137                 dbgprintf_wrapout ("</div>\n", false);
138         return array($statusline, $buf);
139 }
140
141
142
143 // 
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.
149 //
150 function text_to_server($thetext, $convert_to_html) {
151
152         // HTML mode
153         if ($convert_to_html) {
154
155                 // Strip CR's; we only want the LF's
156                 $thetext = trim($thetext, "\r");
157
158                 // Replace hard line breaks with <BR>'s
159                 $thetext = str_replace("\n", "<BR>\n", $thetext);
160
161         }
162
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");
170         }
171
172         serv_puts("000");       // Tell the server we're done...
173
174         serv_puts("ECHO echo test.");           // FIXME
175         echo "Echo test: " . serv_gets() . "<BR>\n" ;
176
177 }
178
179 //--------------------------------------------------------------------------------
180 //   protocol commands
181 //--------------------------------------------------------------------------------
182
183
184 //
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 */
187 //
188 function ctdl_iden($client_info) {
189         global $clientsocket;
190
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));
195         $buf = serv_gets();
196 }
197
198 function ctdl_MessageFormatsPrefered($formatlist){
199         // Also express our message format preferences
200         serv_puts("MSGP ".implode("|", $formatlist));
201         $buf = serv_gets();
202 }
203
204 /* http://www.citadel.org/doku.php/documentation:appproto:connection#noop.no.operation */
205 function ctdl_noop(){
206         // Also express our message format preferences
207         serv_puts("NOOP ");
208         $buf = serv_gets();
209 }
210
211 /* http://www.citadel.org/doku.php/documentation:appproto:connection#quit.quit */
212 function ctdl_quit(){
213         // Also express our message format preferences
214         serv_puts("QUIT ");
215         $buf = serv_gets();
216 }
217
218
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
222         serv_puts("GTLS ");
223         $buf = serv_gets();
224         return $buf;
225 }
226
227
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
232         serv_puts("QNOP ");
233 }
234 */
235
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);
240         $buf = serv_gets();
241
242 }
243
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
248         serv_puts("TIME");
249         $buf = serv_gets();
250
251 }
252
253
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);
258         $buf = serv_gets();
259         return array((substr($buf, 0, 1) == "2"), $buf);
260 }
261
262
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);
267         $buf = serv_gets();
268         if (substr($buf, 0, 1) == "1") {
269                 $reply = read_array();
270                 if (count($reply) == 0)
271                         return false;
272                 return $reply;
273         }
274         else
275                 return false;
276 }
277
278
279
280 //
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 */
287
288 //
289 function login_existing_user($user, $pass) {
290         global $clientsocket;
291
292         serv_puts("USER " . $user);
293         $resp = serv_gets();
294         
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") {
298                 
299                 return array(FALSE, substr($resp, 4));
300         }
301
302         serv_puts("PASS " . $pass);
303         $resp = serv_gets();
304         if (substr($resp, 0, 1) != "2") {
305                 return array(FALSE, substr($resp, 4));
306         }
307
308         $_SESSION["username"] = $user;
309         $_SESSION["password"] = $pass;
310         become_logged_in(substr($resp, 4));
311
312         return array(TRUE, "Login successful.  Have fun.");
313 }
314
315
316 //
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)
322 //
323 function create_new_user($user, $pass) {
324         global $clientsocket;
325
326         serv_puts("NEWU " . $user);
327         $resp = serv_gets();
328         if (substr($resp, 0, 1) != "2") {
329                 return array(FALSE, substr($resp, 4));
330         }
331
332         serv_puts("SETP " . $pass);
333         $resp = serv_gets();
334         if (substr($resp, 0, 1) != "2") {
335                 return array(FALSE, substr($resp, 4));
336         }
337
338         $_SESSION["username"] = $user;
339         $_SESSION["password"] = $pass;
340         become_logged_in(substr($resp, 4));
341
342         return array(TRUE, "Login successful.  Have fun.");
343 }
344
345
346 //
347 // Code common to both existing-user and new-user logins
348 //
349 function become_logged_in($server_parms) {
350         $_SESSION["logged_in"] = 1;
351
352         $tokens = explode("|", $server_parms);
353         
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];
361                 
362         ctdl_goto("_BASEROOM_");
363 }
364
365
366
367 //
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 */
371 //
372 function ctdl_get_serv_info() {
373         serv_puts("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)
385                 {
386                         dbgprintf_wrapout("<pre>", false);
387                         dbgprintf_wrapout(print_r($server_info, true), true);
388                         dbgprintf_wrapout("</pre>", false);
389                 }
390                 return $server_info;
391         }
392         else 
393         {
394                 dbgprintf_wrapin ("didn't understand the reply to the INFO command".
395                                   print_r($reply, TRUE), false);
396                 
397                 die ("CTDLPHP: didn't understand the reply to the INFO command");
398         }
399 }
400
401 //
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 */
405 //
406 function ctdl_get_registration_info() {
407         serv_puts("GREG");
408         $reply = read_array();
409         dbgprintf_wrapout(print_r($reply, true), true);
410 //              die ("didn't understand the reply to the INFO command");
411
412 }
413
414
415 //
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.)
419 //
420 /* http://www.citadel.org/doku.php/documentation:appproto:connection#mesg.read.system.message */
421 function ctdl_mesg($msgname) {
422         global $clientsocket;
423
424         $msgtext = "<DIV ALIGN=CENTER>\n";
425
426         serv_puts("MESG " . $msgname);
427         $response = read_array();
428
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);
432         }
433         else {
434                 $msgtext .= "<B><I>" . substr($response[0], 4) . "</I></B><BR>\n";
435         }
436
437         $msgtext .= "</DIV>\n";
438         return($msgtext);
439 }
440
441 //
442 // Delete a Message.
443 // http://www.citadel.org/doku.php/documentation:appproto:room_indexes_and_messages#dele.delete.a.message
444
445 function ctdl_dele($msgname) {
446         global $clientsocket;
447
448         $msgtext = "<DIV ALIGN=CENTER>\n";
449
450         serv_puts("DELE " . $msgname);
451         $response = serv_gets();
452
453         if (substr($response[0], 0, 1) == "1") {
454                 return TRUE;
455         }
456         else {
457                 return FALSE;
458         }
459 }
460
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;
465
466         serv_puts("MRTG ".$what);
467         $response = serv_gets();
468
469         if (substr($response, 0, 1) != "1") {
470                 return array(0, NULL);
471         }
472                 
473         $responses = read_array();
474         return $responses;
475 }
476 //
477 // Fetch the list of users currently logged in.
478 /* http://www.citadel.org/doku.php/documentation:appproto:connection#rwho.read.who.s.online */
479 //
480 function ctdl_rwho() {
481         global $clientsocket;
482
483         serv_puts("RWHO");
484         $response = serv_gets();
485
486         if (substr($response, 0, 1) != "1") {
487                 return array(0, NULL);
488         }
489         
490         $all_lines = array();
491         $num_lines = 0;
492         
493         $responses = read_array();
494         foreach ($responses as $response) {
495                 $tokens = explode("|", $response);
496                 $oneline = array();
497
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];
510
511                 // IGnore the rest of the fields for now.
512                 if (CITADEL_DEBUG_CITPROTO == 1)
513                 {
514                         dbgprintf_wrapout("<pre>", false);
515                         dbgprintf_wrapout(print_r($oneline, true), true);
516                         dbgprintf_wrapout("</pre>", false);;
517
518                 }
519
520
521                 $num_lines = array_push($all_lines, $oneline);
522         }
523
524         return array($num_lines, $all_lines);
525
526 }
527
528
529 //
530 // Goto a room.
531 //
532 function ctdl_goto($to_where) {
533         
534         serv_puts("GOTO " . $to_where);
535         $response = serv_gets();
536
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);
543                 $room_state=array(
544                         "state"          => TRUE,
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]);
560                         
561                 $_SESSION["room"] = $room;
562                 if (CITADEL_DEBUG_CITPROTO == 1)
563                 {
564                         dbgprintf_wrapout("<pre>", false);
565                         dbgprintf_wrapout(print_r($room_state, true), true);
566                         dbgprintf_wrapout("</pre>", false);
567
568                 }
569
570                 return $room_state;
571         }
572
573         else {
574                 return array("state" => FALSE, "statereply" => $status);
575         }
576
577 }
578
579
580
581 //
582 // Fetch the list of known rooms.
583 //
584 function ctdl_knrooms() {
585         global $clientsocket;
586
587         serv_puts("LKRA");
588         $response = serv_gets();
589         if (substr($response, 0, 1) != "1") {
590                 return array(0, NULL);
591         }
592         $results = read_array();
593         $all_lines = array();
594         $num_lines = 0;
595
596         foreach ($results as $result){
597                 $oneline = array();
598                 $tokens = explode("|",$result);
599
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];
606
607                 if ($oneline["access"] & 8) {
608                         $oneline["hasnewmsgs"] = TRUE;
609                 }
610                 else {
611                         $oneline["hasnewmsgs"] = FALSE;
612                 }
613
614                 if (CITADEL_DEBUG_CITPROTO == 1)
615                 {
616                         dbgprintf_wrapout("<pre>", false);
617                         dbgprintf_wrapout(print_r($oneline, true), true);
618                         dbgprintf_wrapout("</pre>", false);
619
620                 }
621                 $num_lines = array_push($all_lines, $oneline);
622         }
623
624         return array($num_lines, $all_lines);
625
626 }
627
628 //
629 // Fetch the list of known floors.
630 //
631 /* http://www.citadel.org/doku.php/documentation:appproto:rooms#lflr.list.all.known.floors */
632 function ctdl_knfloors() {
633         global $clientsocket;
634
635         serv_puts("LFLR");
636         $response = serv_gets();
637         if (substr($response, 0, 1) != "1") {
638                 return array(0, NULL);
639         }
640
641         $results = read_array();
642         $all_lines = array();
643         $num_lines = 0;
644
645         foreach ($results as $result){
646                 $oneline = array();
647                 $tokens = explode("|",$result);
648
649                 $oneline["id"] = $tokens[0];            
650                 $oneline["name"]   = $tokens[1];                
651                 $oneline["nref"] = $tokens[2];
652
653                 if (CITADEL_DEBUG_CITPROTO == 1)
654                 {
655                         dbgprintf_wrapout("<pre>", false);
656                         dbgprintf_wrapout(print_r($oneline, true), true);
657                         dbgprintf_wrapout("</pre>", false);
658                 }
659                 $num_lines = array_push($all_lines, $oneline);
660         }
661
662         return array($num_lines, $all_lines);
663
664 }
665
666 /* http://www.citadel.org/doku.php/documentation:appproto:rooms#cflr.create.a.new.floor */
667
668 //
669 // Fetch the list of messages in one room.
670 // Returns: count, response, message array
671 //
672 function ctdl_msgs($mode, $count) {
673         global $clientsocket;
674
675         serv_puts("MSGS " . $mode . "|" . $count);
676         $responses = read_array();
677         dbgprintf_wrapout(print_r($responses, true), false);
678
679         $response = array_shift($responses);
680
681         $num_msgs = count($responses);
682         if (substr($response, 0, 1) != "1") {
683                 return array(0, substr($response, 4), NULL);
684         }
685         
686         if (CITADEL_DEBUG_CITPROTO == 1)
687         {
688                 dbgprintf_wrapout("found ".$num_msgs." messages.", true);
689         }
690         return array($num_msgs, $response, $responses);
691 }
692
693
694 // Load a message from the server.
695 function ctdl_fetch_message($msgnum) {
696         global $clientsocket;
697
698         serv_puts("MSG4 " . $msgnum);
699
700         if (CITADEL_DEBUG_CITPROTO == 1)
701                 dbgprintf_wrapout("<div class='ctdldbgRead'>", false);
702         $response = serv_gets(TRUE);
703
704         if (substr($response, 0, 1) != "1") {
705                 return array(FALSE, substr($response, 4), NULL);
706         }
707
708         $fields = array();
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);
720                 }
721                 else {
722                         $fields[substr($buf, 0, 4)] = substr($buf, 5);
723                 }
724         }
725
726         // Message terminated prematurely (no text body)
727         return array(FALSE, substr($response, 4), $fields);
728 }
729
730 // Load a message from the server.
731 function ctdl_fetch_message_rfc822($msgnum) {
732         global $clientsocket;
733
734         serv_puts("MSG2 " . $msgnum);
735
736         if (CITADEL_DEBUG_CITPROTO == 1)
737                 dbgprintf_wrapout("<div class='ctdldbgRead'>", false);
738         $response = serv_gets(TRUE);
739
740         if (substr($response, 0, 1) != "1") {
741                 return array(FALSE, NULL);
742         }
743         $message = "";
744         $buf="";
745         while ($buf = serv_gets(TRUE)) {
746 //              dbgprintf_wrapout($buf, true);
747                 if ($buf=="000")
748                 {
749                         $message .= "\n.\n";
750                         break;
751                 }
752                 $message = $message . "\n" . $buf;
753                 $buf = "";
754         }
755
756 //      dbgprintf_wrapout($message, true);
757         // Message terminated prematurely (no text body)
758         return array(TRUE, $message);
759 }
760
761 // Support function for ctdl_fetch_message(). This handles the text body
762 // portion of the message, converting various formats to HTML as
763 // appropriate.
764 function ctdl_msg4_from_server() {
765
766         $txt = "";
767         $modified_txt = "";
768         $msgformat = "text/plain";
769         $in_body = FALSE;
770
771         $previous_line = "";
772         while (strcmp($buf = serv_gets(TRUE), "000")) {
773                 if ($in_body == FALSE) {
774                         if (strlen($buf) == 0) {
775                                 $in_body = TRUE;
776                         }
777                         else {
778                                 if (!strncasecmp($buf, "content-type: ", 14)) {
779                                         $msgformat = substr($buf, 14);
780                                 }
781                         }
782                 }
783                 else {
784                         if (!strcasecmp($msgformat, "text/html")) {
785                                 $txt .= $buf;
786                         }
787                         else if (!strcasecmp($msgformat, "text/plain")) {
788                                 $txt .= "\r\n".$buf;
789                                 $modified_txt .= "<TT>" . htmlspecialchars($buf) . "</TT><BR>\n" ;
790
791                         }
792                         else if (!strcasecmp($msgformat, "text/x-citadel-variformat")) {
793                                 if (substr($previous_line, 0, 1) == " ") {
794                                         $txt .= "<BR>\n" ;
795                                 }
796                                 $txt .= htmlspecialchars($buf);
797                         }
798                         else {
799                                 $txt .= htmlspecialchars($buf);
800                         }
801                         $previous_line = $buf;
802                 }
803         }
804
805         return(array($txt, $modified_txt));
806 }
807
808
809
810 function download_attachment($msgnum, $attindex)
811 {
812         $command = "DLAT ".$msgnum."|".$attindex;
813         serv_puts($command);
814         $reply = read_binary();
815         return $reply;
816
817 }
818
819
820 function enter_message_0($msgHdr, $contentType, $data)
821 {
822         $send = array();
823
824         if (isset($msgHdr['newusermail']))
825                 array_unshift($send, $msgHdr['newusermail']);
826         else
827                 array_unshift($send, "");
828
829         if (isset($msgHdr['supplied_euid']))
830                 array_unshift($send, $msgHdr['supplied_euid']);
831         else
832                 array_unshift($send, "");
833
834         if (isset($msgHdr['bcc']))
835                 array_unshift($send, $msgHdr['bcc']);
836         else
837                 array_unshift($send, "");
838
839         if (isset($msgHdr['cc']))
840                 array_unshift($send, $msgHdr['cc']);
841         else
842                 array_unshift($send, "");
843
844         if (isset($msgHdr['do_confirm']))
845                 array_unshift($send, $msgHdr['do_confirm']);
846         else
847                 array_unshift($send, "");
848
849         if (isset($msgHdr['newusername']))
850                 array_unshift($send, $msgHdr['newusername']);
851         else
852                 array_unshift($send, "");
853
854         if (isset($msgHdr['subject']))
855                 array_unshift($send, $msgHdr['subject']);
856         else
857                 array_unshift($send, "");
858
859         if (isset($msgHdr['format_type']))
860                 array_unshift($send, $msgHdr['format_type']);
861         else
862                 array_unshift($send, "");
863
864         if (isset($msgHdr['anon_flag']))
865                 array_unshift($send, $msgHdr['anon_flag']);
866         else
867                 array_unshift($send, "");
868
869         if (isset($msgHdr['recp']))
870                 array_unshift($send, $msgHdr['recp']);
871         else
872                 array_unshift($send, "");
873
874         if (isset($msgHdr['post']))
875                 array_unshift($send, $msgHdr['post']);
876         else
877                 array_unshift($send, "");
878
879         $params = implode('|', $send);
880         serv_puts("ENT0 ".$params);
881
882         $reply=serv_gets();
883         if (substr($reply, 0, 1) != 4)
884                 return array(false, array(), array());
885         serv_puts("Content-type: ".$contentType);
886         serv_puts("");
887         serv_puts($data."\r\n");
888         serv_puts("000");
889 }
890
891
892 ?>