* parse function reply properly
[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
11 //--------------------------------------------------------------------------------
12 //   internal functions for server communication
13 //--------------------------------------------------------------------------------
14 //
15 // serv_gets() -- generic function to read one line of text from the server
16 //
17 function serv_gets($readblock=FALSE) {
18         global $clientsocket;
19
20         $buf = fgets($clientsocket, 4096);              // Read line
21         $buf = substr($buf, 0, (strlen($buf)-1) );      // strip trailing LF
22         if (CITADEL_DEBUG_CITPROTO == 1) {
23                 if (!$readblock) printf ("<div class='ctdldbgRead'>\n");
24                 printf($buf);
25                 if (!$readblock) printf ("\n</div>\n");
26                 else printf ("<br>\n");
27         }
28         return $buf;
29 }
30
31 //
32 // serv_get_n() -- generic function to read a binary blob from the server
33 //
34 function serv_get_n($nBytes) {
35         global $clientsocket;
36
37         if (CITADEL_DEBUG_CITPROTO == 1) {
38                 printf ("<div class='ctdldbgRead'>\n");
39                 printf("reading ".$nBytes." bytes from server\n");
40                 printf ("</div>\n");
41         }
42         $buf = fread($clientsocket, $nBytes);
43         if (CITADEL_DEBUG_CITPROTO == 1) {
44                 if (!$buf) printf ("<div class='ctdldbgRead'>\n");
45                 printf($buf);
46                 if (!$buf) printf ("</div>\n");
47                 else printf ("<br>\n");
48         }
49         return $buf;
50 }
51
52 //
53 // serv_puts() -- generic function to write one line of text to the server
54 //
55 function serv_puts($buf) {
56         global $clientsocket;
57         
58         fwrite($clientsocket, $buf . "\n", (strlen($buf)+1) );
59         fflush($clientsocket);
60         if (CITADEL_DEBUG_CITPROTO == 1)
61                 printf ("<div class='ctdldbgWrite'>".$buf."</div>\n");
62 }
63
64
65 function read_array() {
66         $nLines = 0;
67         if (CITADEL_DEBUG_CITPROTO == 1)
68             printf ("<div class='ctdldbgRead'>\n");
69         $buf = serv_gets(TRUE);
70         $ret = array();
71         while (strcasecmp($buf, "000")){
72                 array_push($ret, $buf);
73                 $buf = serv_gets(TRUE);
74                 $nLines++;
75         }
76         if (CITADEL_DEBUG_CITPROTO == 1){
77                 echo "read ".$nLines." lines from the server.\n";
78                 printf ("</div>\n");
79         }
80         return $ret;
81 }
82
83 function read_binary() {
84         $nLines = 0;
85         if (CITADEL_DEBUG_CITPROTO == 1)
86             printf ("<div class='ctdldbgRead'>\n");
87         $buf = serv_gets(TRUE);
88         
89         if (CITADEL_DEBUG_CITPROTO == 1){
90                 echo "status line from the server\n";
91         }
92
93         $statusline = explode(" ", $buf);
94         
95         if ($statusline[0] == 600)
96         {
97                 $buf = serv_get_n($statusline[1]);
98                 
99         }
100         if (CITADEL_DEBUG_CITPROTO == 1)
101             printf ("</div>\n");
102         return array($statusline, $buf);
103 }
104
105
106
107 // 
108 // text_to_server() -- sends a block of text to the server.  Assumes that
109 //                     the server just sent back a SEND_LISTING response code
110 //                     and is now expecting a 000-terminated series of lines.
111 //                     Set 'convert_to_html' to TRUE to convert the block of
112 //                     text to HTML along the way.
113 //
114 function text_to_server($thetext, $convert_to_html) {
115
116         // HTML mode
117         if ($convert_to_html) {
118
119                 // Strip CR's; we only want the LF's
120                 $thetext = trim($thetext, "\r");
121
122                 // Replace hard line breaks with <BR>'s
123                 $thetext = str_replace("\n", "<BR>\n", $thetext);
124
125         }
126
127         // Either mode ... send it to the server now
128         $one_line = strtok($thetext, "\n");
129         while ($one_line !== FALSE) {
130                 $one_line = trim($one_line, "\n\r");
131                 if ($one_line == "000") $one_line = "-000" ;
132                 serv_puts($one_line);
133                 $one_line = strtok("\n");
134         }
135
136         serv_puts("000");       // Tell the server we're done...
137
138         serv_puts("ECHO echo test.");           // FIXME
139         echo "Echo test: " . serv_gets() . "<BR>\n" ;
140
141 }
142
143 //--------------------------------------------------------------------------------
144 //   protocol commands
145 //--------------------------------------------------------------------------------
146
147
148 //
149 // Identify ourselves to the Citadel server (do one once after connection)
150 /* http://www.citadel.org/doku.php/documentation:appproto:connection#iden.identify.the.client.software */
151 //
152 function ctdl_iden($client_info) {
153         global $clientsocket;
154
155         if (count($client_info) != 5)
156                 die("ctdl_iden takes 5 arguments!");
157         // Identify client and hostname
158         serv_puts("IDEN ".implode('|', $client_info));
159         $buf = serv_gets();
160 }
161
162 function ctdl_MessageFormatsPrefered($formatlist){
163         // Also express our message format preferences
164         serv_puts("MSGP ".implode("|", $formatlist));
165         $buf = serv_gets();
166 }
167
168 /* http://www.citadel.org/doku.php/documentation:appproto:connection#noop.no.operation */
169 function ctdl_noop(){
170         // Also express our message format preferences
171         serv_puts("NOOP ");
172         $buf = serv_gets();
173 }
174
175 /* http://www.citadel.org/doku.php/documentation:appproto:connection#quit.quit */
176 function ctdl_quit(){
177         // Also express our message format preferences
178         serv_puts("QUIT ");
179         $buf = serv_gets();
180 }
181
182
183 /* http://www.citadel.org/doku.php/documentation:appproto:connection#mesg.read.system.message */
184 function ctdl_gtls(){
185         // Also express our message format preferences
186         serv_puts("GTLS ");
187         $buf = serv_gets();
188         return $buf;
189 }
190
191
192 /* http://www.citadel.org/doku.php/documentation:appproto:connection#qnop.quiet.no.operation */
193 /* this seems to be dangerous. ask IG
194 function ctdl_qnoop(){
195         // Also express our message format preferences
196         serv_puts("QNOP ");
197 }
198 */
199
200 /* http://www.citadel.org/doku.php/documentation:appproto:connection#echo.echo.something */
201 function ctdl_doecho($echotext){
202         // Also express our message format preferences
203         serv_puts("ECHO ".$echotext);
204         $buf = serv_gets();
205
206 }
207
208 /* http://www.citadel.org/doku.php/documentation:appproto:connection#time.get.server.local.time */
209 /* TODO: what are the other two params? doku is incomplete here. */
210 function ctdl_time(){
211         // Also express our message format preferences
212         serv_puts("TIME");
213         $buf = serv_gets();
214
215 }
216
217
218 /* http://www.citadel.org/doku.php/documentation:appproto:connection#qdir.query.global.directory */
219 function ctdl_qdir($who){
220         // Also express our message format preferences
221         serv_puts("QDIR ".$who);
222         $buf = serv_gets();
223         return array((substr($buf, 0, 1) == "2"), $buf);
224 }
225
226
227 /* http://www.citadel.org/doku.php/documentation:appproto:connection#auto.autocompletion.of.email.addresses */
228 function ctdl_auto($who){
229         // Also express our message format preferences
230         serv_puts("AUTO ".$who);
231         $buf = serv_gets();
232         if (substr($buf, 0, 1) == "1") {
233                 $reply = read_array();
234                 if (count($reply) == 0)
235                         return false;
236                 return $reply;
237         }
238         else
239                 return false;
240 }
241
242
243
244 //
245 // login_existing_user() -- attempt to login using a supplied username/password
246 // Returns an array with two variables:
247 // 0. TRUE or FALSE to determine success or failure
248 // 1. String error message (if relevant)
249 /* http://www.citadel.org/doku.php/documentation:appproto:connection#user.send.user.name */
250 /* http://www.citadel.org/doku.php/documentation:appproto:connection#pass.send.password */
251
252 //
253 function login_existing_user($user, $pass) {
254         global $clientsocket;
255
256         serv_puts("USER " . $user);
257         $resp = serv_gets();
258         if (substr($resp, 0, 1) != "3") {
259                 return array(FALSE, substr($resp, 4));
260         }
261
262         serv_puts("PASS " . $pass);
263         $resp = serv_gets();
264         if (substr($resp, 0, 1) != "2") {
265                 return array(FALSE, substr($resp, 4));
266         }
267
268         $_SESSION["username"] = $user;
269         $_SESSION["password"] = $pass;
270         become_logged_in(substr($resp, 4));
271
272         return array(TRUE, "Login successful.  Have fun.");
273 }
274
275
276 //
277 // create_new_user() -- attempt to create a new user 
278 //                      using a supplied username/password
279 // Returns an array with two variables:
280 // 0. TRUE or FALSE to determine success or failure
281 // 1. String error message (if relevant)
282 //
283 function create_new_user($user, $pass) {
284         global $clientsocket;
285
286         serv_puts("NEWU " . $user);
287         $resp = serv_gets();
288         if (substr($resp, 0, 1) != "2") {
289                 return array(FALSE, substr($resp, 4));
290         }
291
292         serv_puts("SETP " . $pass);
293         $resp = serv_gets();
294         if (substr($resp, 0, 1) != "2") {
295                 return array(FALSE, substr($resp, 4));
296         }
297
298         $_SESSION["username"] = $user;
299         $_SESSION["password"] = $pass;
300         become_logged_in(substr($resp, 4));
301
302         return array(TRUE, "Login successful.  Have fun.");
303 }
304
305
306 //
307 // Code common to both existing-user and new-user logins
308 //
309 function become_logged_in($server_parms) {
310         $_SESSION["logged_in"] = 1;
311
312         $tokens = explode("|", $server_parms);
313         
314         $oneline["username"]   = $tokens[0];
315         $oneline["axlevel"]    = $tokens[1];
316         $oneline["calls"]      = $tokens[2];
317         $oneline["posts"]      = $tokens[3];
318         $oneline["userflags"]  = $tokens[4];
319         $oneline["usernum"]    = $tokens[5];
320         $oneline["lastcall"]   = $tokens[6];
321                 
322         ctdl_goto("_BASEROOM_");
323 }
324
325
326
327 //
328 // Learn all sorts of interesting things about the Citadel server to
329 // which we are connected.
330 /* http://www.citadel.org/doku.php/documentation:appproto:connection#info.get.server.info */
331 //
332 function ctdl_get_serv_info() {
333         serv_puts("INFO");
334         $reply = read_array();
335         if ((count($reply) == 18) &&
336             substr($reply[0], 0, 1) == "1") {
337                 $server_info=array();
338                 $server_info["serv_nodename"]  = $reply[1];
339                 $server_info["serv_humannode"] = $reply[2];
340                 $server_info["serv_fqdn"]      = $reply[3];
341                 $server_info["serv_software"]  = $reply[4];
342                 $server_info["serv_city"]      = $reply[6];
343                 $server_info["serv_sysadmin"]  = $reply[7];
344                 if (CITADEL_DEBUG_CITPROTO == 1)
345                 {
346                         echo "<pre>";
347                         print_r($server_info);
348                         echo "</pre>";
349                 }
350                 return $server_info;
351         }
352         else 
353                 die ("didn't understand the reply to the INFO command");
354
355 }
356
357 //
358 // Learn all sorts of interesting things about the Citadel server to
359 // which we are connected.
360 /* http://www.citadel.org/doku.php/documentation:appproto:connection#info.get.server.info */
361 //
362 function ctdl_get_registration_info() {
363         serv_puts("GREG");
364         $reply = read_array();
365         print_r($reply);
366 //              die ("didn't understand the reply to the INFO command");
367
368 }
369
370
371 //
372 // Display a system banner.  (Returns completed HTML.)
373 // (One is probably temporary because it outputs more or less finalized
374 // markup.  For now it's just usable.)
375 //
376 /* http://www.citadel.org/doku.php/documentation:appproto:connection#mesg.read.system.message */
377 function ctdl_mesg($msgname) {
378         global $clientsocket;
379
380         $msgtext = "<DIV ALIGN=CENTER>\n";
381
382         serv_puts("MESG " . $msgname);
383         $response = read_array();
384
385         if (substr($response[0], 0, 1) == "1") {
386                 array_shift($response); // throw away the status code.
387                 $msgtext .= "<TT>" . implode( "</TT><BR>\n" ,$response);
388         }
389         else {
390                 $msgtext .= "<B><I>" . substr($response[0], 4) . "</I></B><BR>\n";
391         }
392
393         $msgtext .= "</DIV>\n";
394         return($msgtext);
395 }
396
397 /* http://www.citadel.org/doku.php/documentation:appproto:connection#mesg.read.system.message */
398 //// TODO: is this still supported?
399 function ctdl_mrtg($what) {
400         global $clientsocket;
401
402         serv_puts("MRTG ".$what);
403         $response = serv_gets();
404
405         if (substr($response, 0, 1) != "1") {
406                 return array(0, NULL);
407         }
408                 
409         $responses = read_array();
410         return $responses;
411 }
412 //
413 // Fetch the list of users currently logged in.
414 /* http://www.citadel.org/doku.php/documentation:appproto:connection#rwho.read.who.s.online */
415 //
416 function ctdl_rwho() {
417         global $clientsocket;
418
419         serv_puts("RWHO");
420         $response = serv_gets();
421
422         if (substr($response, 0, 1) != "1") {
423                 return array(0, NULL);
424         }
425         
426         $all_lines = array();
427         $num_lines = 0;
428         
429         $responses = read_array();
430         foreach ($responses as $response) {
431                 $tokens = explode("|", $response);
432                 $oneline = array();
433
434                 $oneline["session"]      = $tokens[0];          
435                 $oneline["user"]         = $tokens[1];          
436                 $oneline["room"]         = $tokens[2];          
437                 $oneline["host"]         = $tokens[3];          
438                 $oneline["client"]       = $tokens[4];
439                 $oneline["idlesince"]    = $tokens[5];
440                 $oneline["lastcmd"]      = $tokens[6];
441                 $oneline["flags"]        = $tokens[7];
442                 $oneline["realname"]     = $tokens[8];
443                 $oneline["realroom"]     = $tokens[9];
444                 $oneline["realhostname"] = $tokens[10];
445                 $oneline["registered"]   = $tokens[11];
446
447                 // IGnore the rest of the fields for now.
448                 if (CITADEL_DEBUG_CITPROTO == 1)
449                 {
450                         echo "<pre>";
451                         print_r($oneline);
452                         echo "</pre>";
453
454                 }
455
456
457                 $num_lines = array_push($all_lines, $oneline);
458         }
459
460         return array($num_lines, $all_lines);
461
462 }
463
464
465 //
466 // Goto a room.
467 //
468 function ctdl_goto($to_where) {
469         
470         serv_puts("GOTO " . $to_where);
471         $response = serv_gets();
472
473         $results = explode ("|", $response);
474         $status_room = array_shift($results);
475         $status = substr($status_room, 0, 3);
476         if (substr($status, 0, 1) == "2") {
477                 $room = substr($status_room, 4);
478                 array_unshift($results, $room);
479                 $room_state=array(
480                         "state"          => TRUE,
481                         "statereply"     => $status,
482                         "roomname"       => $results[ 0],
483                         "nunreadmsg"     => $results[ 1],
484                         "nmessages"      => $results[ 2],
485                         "rinfopresent"   => $results[ 3],
486                         "flags"          => $results[ 4],
487                         "msgidmax"       => $results[ 5],
488                         "msgidreadmax"   => $results[ 6],
489                         "ismailroom"     => $results[ 7],
490                         "isroomaide"     => $results[ 8],
491                         "nnewmessages"   => $results[ 9],
492                         "floorid"        => $results[10],
493                         "viewselected"   => $results[11],
494                         "defaultview"    => $results[12],
495                         "istrashcan"     => $results[13]);
496                         
497                 $_SESSION["room"] = $room;
498                 if (CITADEL_DEBUG_CITPROTO == 1)
499                 {
500                         echo "<pre>";
501                         print_r($room_state);
502                         echo "</pre>";
503
504                 }
505
506                 return $room_state;
507         }
508
509         else {
510                 return array("state" => FALSE, "statereply" => $status);
511         }
512
513 }
514
515
516
517 //
518 // Fetch the list of known rooms.
519 //
520 function ctdl_knrooms() {
521         global $clientsocket;
522
523         serv_puts("LKRA");
524         $response = serv_gets();
525         if (substr($response, 0, 1) != "1") {
526                 return array(0, NULL);
527         }
528         $results = read_array();
529         $all_lines = array();
530         $num_lines = 0;
531
532         foreach ($results as $result){
533                 $oneline = array();
534                 $tokens = explode("|",$result);
535
536                 $oneline["name"]   = $tokens[0];                
537                 $oneline["flags"]  = $tokens[1];                
538                 $oneline["floor"]  = $tokens[2];                
539                 $oneline["order"]  = $tokens[3];                
540                 $oneline["flags2"] = $tokens[4];                
541                 $oneline["access"] = $tokens[5];
542
543                 if ($oneline["access"] & 8) {
544                         $oneline["hasnewmsgs"] = TRUE;
545                 }
546                 else {
547                         $oneline["hasnewmsgs"] = FALSE;
548                 }
549
550                 if (CITADEL_DEBUG_CITPROTO == 1)
551                 {
552                         echo "<pre>";
553                         print_r($oneline);
554                         echo "</pre>";
555
556                 }
557                 $num_lines = array_push($all_lines, $oneline);
558         }
559
560         return array($num_lines, $all_lines);
561
562 }
563
564 //
565 // Fetch the list of known floors.
566 //
567 /* http://www.citadel.org/doku.php/documentation:appproto:rooms#lflr.list.all.known.floors */
568 function ctdl_knfloors() {
569         global $clientsocket;
570
571         serv_puts("LFLR");
572         $response = serv_gets();
573         if (substr($response, 0, 1) != "1") {
574                 return array(0, NULL);
575         }
576
577         $results = read_array();
578         $all_lines = array();
579         $num_lines = 0;
580
581         foreach ($results as $result){
582                 $oneline = array();
583                 $tokens = explode("|",$result);
584
585                 $oneline["id"] = $tokens[0];            
586                 $oneline["name"]   = $tokens[1];                
587                 $oneline["nref"] = $tokens[2];
588
589                 if (CITADEL_DEBUG_CITPROTO == 1)
590                 {
591                         echo "<pre>";
592                         print_r($oneline);
593                         echo "</pre>";
594
595                 }
596                 $num_lines = array_push($all_lines, $oneline);
597         }
598
599         return array($num_lines, $all_lines);
600
601 }
602
603 /* http://www.citadel.org/doku.php/documentation:appproto:rooms#cflr.create.a.new.floor */
604
605 //
606 // Fetch the list of messages in one room.
607 // Returns: count, response, message array
608 //
609 function ctdl_msgs($mode, $count) {
610         global $clientsocket;
611
612         serv_puts("MSGS " . $mode . "|" . $count);
613         $responses = read_array();
614         print_r($responses);
615
616         $response = array_shift($responses);
617
618         $num_msgs = count($responses);
619         if (substr($response, 0, 1) != "1") {
620                 return array(0, substr($response, 4), NULL);
621         }
622         
623         if (CITADEL_DEBUG_CITPROTO == 1)
624         {
625                 printf("found ".$num_msgs." messages.");
626         }
627         return array($num_msgs, $response, $responses);
628 }
629
630
631 // Load a message from the server.
632 function ctdl_fetch_message($msgnum) {
633         global $clientsocket;
634
635         serv_puts("MSG4 " . $msgnum);
636
637         if (CITADEL_DEBUG_CITPROTO == 1)
638             printf ("<div class='ctdldbgRead'>");
639         $response = serv_gets(TRUE);
640
641         if (substr($response, 0, 1) != "1") {
642                 return array(FALSE, substr($response, 4), NULL);
643         }
644
645         $fields = array();
646         while (strcmp($buf = serv_gets(TRUE), "000")) {
647                 if (substr($buf, 0, 4) == "text") {
648                         if (CITADEL_DEBUG_CITPROTO == 1)
649                                 printf ("</div>\n<h3>Message Body Follows</h3><div class='ctdldbgRead'>");
650                         // We're in the text body.  New loop here.
651                         $fields["text"] = ctdl_msg4_from_server();
652                         if (CITADEL_DEBUG_CITPROTO == 1)
653                                 printf ("</div>");
654                         return array(TRUE, substr($response, 4), $fields);
655                 }
656                 else {
657                         $fields[substr($buf, 0, 4)] = substr($buf, 5);
658                 }
659         }
660
661         // Message terminated prematurely (no text body)
662         return array(FALSE, substr($response, 4), $fields);
663 }
664
665 // Support function for ctdl_fetch_message(). This handles the text body
666 // portion of the message, converting various formats to HTML as
667 // appropriate.
668 function ctdl_msg4_from_server() {
669
670         $txt = "";
671         $msgformat = "text/plain";
672         $in_body = FALSE;
673
674         $previous_line = "";
675         while (strcmp($buf = serv_gets(TRUE), "000")) {
676                 if ($in_body == FALSE) {
677                         if (strlen($buf) == 0) {
678                                 $in_body = TRUE;
679                         }
680                         else {
681                                 if (!strncasecmp($buf, "content-type: ", 14)) {
682                                         $msgformat = substr($buf, 14);
683                                 }
684                         }
685                 }
686                 else {
687                         if (!strcasecmp($msgformat, "text/html")) {
688                                 $txt .= $buf;
689                         }
690                         else if (!strcasecmp($msgformat, "text/plain")) {
691                                 $txt .= "<TT>" . htmlspecialchars($buf) . "</TT><BR>\n" ;
692                         }
693                         else if (!strcasecmp($msgformat, "text/x-citadel-variformat")) {
694                                 if (substr($previous_line, 0, 1) == " ") {
695                                         $txt .= "<BR>\n" ;
696                                 }
697                                 $txt .= htmlspecialchars($buf);
698                         }
699                         else {
700                                 $txt .= htmlspecialchars($buf);
701                         }
702                         $previous_line = $buf;
703                 }
704         }
705
706         return($txt);
707 }
708
709
710
711 function download_attachment($msgnum, $attindex)
712 {
713         $command = "DLAT ".$msgnum."|".$attindex;
714         serv_puts($command);
715         $reply = read_binary();
716         return $reply;
717
718 }
719
720
721
722 ?>