b08ed6a3ad09843c378166d7eaf2c9b8beb6a624
[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);
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);
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) == 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)
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                 die ("didn't understand the reply to the INFO command");
394
395 }
396
397 //
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 */
401 //
402 function ctdl_get_registration_info() {
403         serv_puts("GREG");
404         $reply = read_array();
405         dbgprintf_wrapout(print_r($reply, true), true);
406 //              die ("didn't understand the reply to the INFO command");
407
408 }
409
410
411 //
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.)
415 //
416 /* http://www.citadel.org/doku.php/documentation:appproto:connection#mesg.read.system.message */
417 function ctdl_mesg($msgname) {
418         global $clientsocket;
419
420         $msgtext = "<DIV ALIGN=CENTER>\n";
421
422         serv_puts("MESG " . $msgname);
423         $response = read_array();
424
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);
428         }
429         else {
430                 $msgtext .= "<B><I>" . substr($response[0], 4) . "</I></B><BR>\n";
431         }
432
433         $msgtext .= "</DIV>\n";
434         return($msgtext);
435 }
436
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;
441
442         serv_puts("MRTG ".$what);
443         $response = serv_gets();
444
445         if (substr($response, 0, 1) != "1") {
446                 return array(0, NULL);
447         }
448                 
449         $responses = read_array();
450         return $responses;
451 }
452 //
453 // Fetch the list of users currently logged in.
454 /* http://www.citadel.org/doku.php/documentation:appproto:connection#rwho.read.who.s.online */
455 //
456 function ctdl_rwho() {
457         global $clientsocket;
458
459         serv_puts("RWHO");
460         $response = serv_gets();
461
462         if (substr($response, 0, 1) != "1") {
463                 return array(0, NULL);
464         }
465         
466         $all_lines = array();
467         $num_lines = 0;
468         
469         $responses = read_array();
470         foreach ($responses as $response) {
471                 $tokens = explode("|", $response);
472                 $oneline = array();
473
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];
486
487                 // IGnore the rest of the fields for now.
488                 if (CITADEL_DEBUG_CITPROTO == 1)
489                 {
490                         dbgprintf_wrapout("<pre>", false);
491                         dbgprintf_wrapout(print_r($oneline, true), true);
492                         dbgprintf_wrapout("</pre>", false);;
493
494                 }
495
496
497                 $num_lines = array_push($all_lines, $oneline);
498         }
499
500         return array($num_lines, $all_lines);
501
502 }
503
504
505 //
506 // Goto a room.
507 //
508 function ctdl_goto($to_where) {
509         
510         serv_puts("GOTO " . $to_where);
511         $response = serv_gets();
512
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);
519                 $room_state=array(
520                         "state"          => TRUE,
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]);
536                         
537                 $_SESSION["room"] = $room;
538                 if (CITADEL_DEBUG_CITPROTO == 1)
539                 {
540                         dbgprintf_wrapout("<pre>", false);
541                         dbgprintf_wrapout(print_r($room_state, true), true);
542                         dbgprintf_wrapout("</pre>", false);
543
544                 }
545
546                 return $room_state;
547         }
548
549         else {
550                 return array("state" => FALSE, "statereply" => $status);
551         }
552
553 }
554
555
556
557 //
558 // Fetch the list of known rooms.
559 //
560 function ctdl_knrooms() {
561         global $clientsocket;
562
563         serv_puts("LKRA");
564         $response = serv_gets();
565         if (substr($response, 0, 1) != "1") {
566                 return array(0, NULL);
567         }
568         $results = read_array();
569         $all_lines = array();
570         $num_lines = 0;
571
572         foreach ($results as $result){
573                 $oneline = array();
574                 $tokens = explode("|",$result);
575
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];
582
583                 if ($oneline["access"] & 8) {
584                         $oneline["hasnewmsgs"] = TRUE;
585                 }
586                 else {
587                         $oneline["hasnewmsgs"] = FALSE;
588                 }
589
590                 if (CITADEL_DEBUG_CITPROTO == 1)
591                 {
592                         dbgprintf_wrapout("<pre>", false);
593                         dbgprintf_wrapout(print_r($oneline, true), true);
594                         dbgprintf_wrapout("</pre>", false);
595
596                 }
597                 $num_lines = array_push($all_lines, $oneline);
598         }
599
600         return array($num_lines, $all_lines);
601
602 }
603
604 //
605 // Fetch the list of known floors.
606 //
607 /* http://www.citadel.org/doku.php/documentation:appproto:rooms#lflr.list.all.known.floors */
608 function ctdl_knfloors() {
609         global $clientsocket;
610
611         serv_puts("LFLR");
612         $response = serv_gets();
613         if (substr($response, 0, 1) != "1") {
614                 return array(0, NULL);
615         }
616
617         $results = read_array();
618         $all_lines = array();
619         $num_lines = 0;
620
621         foreach ($results as $result){
622                 $oneline = array();
623                 $tokens = explode("|",$result);
624
625                 $oneline["id"] = $tokens[0];            
626                 $oneline["name"]   = $tokens[1];                
627                 $oneline["nref"] = $tokens[2];
628
629                 if (CITADEL_DEBUG_CITPROTO == 1)
630                 {
631                         dbgprintf_wrapout("<pre>", false);
632                         dbgprintf_wrapout(print_r($oneline, true), true);
633                         dbgprintf_wrapout("</pre>", false);
634                 }
635                 $num_lines = array_push($all_lines, $oneline);
636         }
637
638         return array($num_lines, $all_lines);
639
640 }
641
642 /* http://www.citadel.org/doku.php/documentation:appproto:rooms#cflr.create.a.new.floor */
643
644 //
645 // Fetch the list of messages in one room.
646 // Returns: count, response, message array
647 //
648 function ctdl_msgs($mode, $count) {
649         global $clientsocket;
650
651         serv_puts("MSGS " . $mode . "|" . $count);
652         $responses = read_array();
653         dbgprintf_wrapout(print_r($responses, true), false);
654
655         $response = array_shift($responses);
656
657         $num_msgs = count($responses);
658         if (substr($response, 0, 1) != "1") {
659                 return array(0, substr($response, 4), NULL);
660         }
661         
662         if (CITADEL_DEBUG_CITPROTO == 1)
663         {
664                 dbgprintf_wrapout("found ".$num_msgs." messages.", true);
665         }
666         return array($num_msgs, $response, $responses);
667 }
668
669
670 // Load a message from the server.
671 function ctdl_fetch_message($msgnum) {
672         global $clientsocket;
673
674         serv_puts("MSG4 " . $msgnum);
675
676         if (CITADEL_DEBUG_CITPROTO == 1)
677                 dbgprintf_wrapout("<div class='ctdldbgRead'>", false);
678         $response = serv_gets(TRUE);
679
680         if (substr($response, 0, 1) != "1") {
681                 return array(FALSE, substr($response, 4), NULL);
682         }
683
684         $fields = array();
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);
694                 }
695                 else {
696                         $fields[substr($buf, 0, 4)] = substr($buf, 5);
697                 }
698         }
699
700         // Message terminated prematurely (no text body)
701         return array(FALSE, substr($response, 4), $fields);
702 }
703
704 // Load a message from the server.
705 function ctdl_fetch_message_rfc822($msgnum) {
706         global $clientsocket;
707
708         serv_puts("MSG2 " . $msgnum);
709
710         if (CITADEL_DEBUG_CITPROTO == 1)
711                 dbgprintf_wrapout("<div class='ctdldbgRead'>", false);
712         $response = serv_gets(TRUE);
713
714         if (substr($response, 0, 1) != "1") {
715                 return array(FALSE, NULL);
716         }
717         $message = "";
718         $buf="";
719         while ($buf = serv_gets(TRUE)) {
720 //              dbgprintf_wrapout($buf, true);
721                 if ($buf=="000")
722                         break;
723                 $message = $message . "\n" . $buf;
724                 $buf = "";
725         }
726
727 //      dbgprintf_wrapout($message, true);
728         // Message terminated prematurely (no text body)
729         return array(TRUE, $message);
730 }
731
732 // Support function for ctdl_fetch_message(). This handles the text body
733 // portion of the message, converting various formats to HTML as
734 // appropriate.
735 function ctdl_msg4_from_server() {
736
737         $txt = "";
738         $msgformat = "text/plain";
739         $in_body = FALSE;
740
741         $previous_line = "";
742         while (strcmp($buf = serv_gets(TRUE), "000")) {
743                 if ($in_body == FALSE) {
744                         if (strlen($buf) == 0) {
745                                 $in_body = TRUE;
746                         }
747                         else {
748                                 if (!strncasecmp($buf, "content-type: ", 14)) {
749                                         $msgformat = substr($buf, 14);
750                                 }
751                         }
752                 }
753                 else {
754                         if (!strcasecmp($msgformat, "text/html")) {
755                                 $txt .= $buf;
756                         }
757                         else if (!strcasecmp($msgformat, "text/plain")) {
758                                 $txt .= "<TT>" . htmlspecialchars($buf) . "</TT><BR>\n" ;
759                         }
760                         else if (!strcasecmp($msgformat, "text/x-citadel-variformat")) {
761                                 if (substr($previous_line, 0, 1) == " ") {
762                                         $txt .= "<BR>\n" ;
763                                 }
764                                 $txt .= htmlspecialchars($buf);
765                         }
766                         else {
767                                 $txt .= htmlspecialchars($buf);
768                         }
769                         $previous_line = $buf;
770                 }
771         }
772
773         return($txt);
774 }
775
776
777
778 function download_attachment($msgnum, $attindex)
779 {
780         $command = "DLAT ".$msgnum."|".$attindex;
781         serv_puts($command);
782         $reply = read_binary();
783         return $reply;
784
785 }
786
787
788 function enter_message_0($msgHdr, $contentType, $data)
789 {
790         $send = array();
791
792         if (isset($msgHdr['newusermail']))
793                 array_unshift($send, $msgHdr['newusermail']);
794         else
795                 array_unshift($send, "");
796
797         if (isset($msgHdr['supplied_euid']))
798                 array_unshift($send, $msgHdr['supplied_euid']);
799         else
800                 array_unshift($send, "");
801
802         if (isset($msgHdr['bcc']))
803                 array_unshift($send, $msgHdr['bcc']);
804         else
805                 array_unshift($send, "");
806
807         if (isset($msgHdr['cc']))
808                 array_unshift($send, $msgHdr['cc']);
809         else
810                 array_unshift($send, "");
811
812         if (isset($msgHdr['do_confirm']))
813                 array_unshift($send, $msgHdr['do_confirm']);
814         else
815                 array_unshift($send, "");
816
817         if (isset($msgHdr['newusername']))
818                 array_unshift($send, $msgHdr['newusername']);
819         else
820                 array_unshift($send, "");
821
822         if (isset($msgHdr['subject']))
823                 array_unshift($send, $msgHdr['subject']);
824         else
825                 array_unshift($send, "");
826
827         if (isset($msgHdr['format_type']))
828                 array_unshift($send, $msgHdr['format_type']);
829         else
830                 array_unshift($send, "");
831
832         if (isset($msgHdr['anon_flag']))
833                 array_unshift($send, $msgHdr['anon_flag']);
834         else
835                 array_unshift($send, "");
836
837         if (isset($msgHdr['recp']))
838                 array_unshift($send, $msgHdr['recp']);
839         else
840                 array_unshift($send, "");
841
842         if (isset($msgHdr['post']))
843                 array_unshift($send, $msgHdr['post']);
844         else
845                 array_unshift($send, "");
846
847         $params = implode('|', $send);
848         serv_puts("ENT0 ".$params);
849
850         $reply=serv_gets();
851         if (substr($reply, 0, 1) != 4)
852                 return array(false, array(), array());
853         serv_puts("Content-type: ".$contentType);
854         serv_puts("");
855         serv_puts($data."\r\n");
856         serv_puts("000");
857 }
858
859
860 ?>