]> code.citadel.org Git - citadel.git/blobdiff - ctdlphp/ctdlprotocol.php
* Replaced the two-second sleep (and associated race condition) for the
[citadel.git] / ctdlphp / ctdlprotocol.php
index 0463ad5125e8accb647755c79e0ba2b855889936..7770364112ffe252bf9ff5c583a8be43e992a79e 100644 (file)
@@ -6,7 +6,6 @@
 //
 // Copyright (c) 2003 by Art Cancro <ajc@uncensored.citadel.org>
 // This program is released under the terms of the GNU General Public License.
-//
 
 
 //
@@ -33,13 +32,18 @@ function serv_puts($buf) {
 
 
 //
-// Identify this client, and the hostname where the user is, to Citadel.
+// Identify ourselves to the Citadel server (do this once after connection)
 //
 function ctdl_iden() {
        global $clientsocket;
 
+       // Identify client and hostname
        serv_puts("IDEN 0|8|001|PHP web client|" . $_SERVER['REMOTE_ADDR'] );
        $buf = serv_gets();
+
+       // Also express our message format preferences
+       serv_puts("MSGP text/html|text/plain");
+       $buf = serv_gets();
 }
 
 
@@ -108,6 +112,28 @@ function create_new_user($user, $pass) {
 //
 function become_logged_in($server_parms) {
        $_SESSION["logged_in"] = 1;
+
+       $tok = strtok($server_parms, "|");
+       if ($tok) $thisline["username"] = $tok;
+
+       $tok = strtok("|");
+       if ($tok) $thisline["axlevel"] = $tok;
+               
+       $tok = strtok("|");
+       if ($tok) $thisline["calls"] = $tok;
+               
+       $tok = strtok("|");
+       if ($tok) $thisline["posts"] = $tok;
+               
+       $tok = strtok("|");
+       if ($tok) $thisline["userflags"] = $tok;
+               
+       $tok = strtok("|");
+       if ($tok) $thisline["usernum"] = $tok;
+               
+       $tok = strtok("|");
+       if ($tok) $thisline["lastcall"] = $tok;
+               
        ctdl_goto("_BASEROOM_");
 }
 
@@ -269,6 +295,13 @@ function ctdl_knrooms() {
                $tok = strtok("|");
                if ($tok) $thisline["access"] = $tok;
 
+               if ($thisline["access"] & 8) {
+                       $thisline["hasnewmsgs"] = TRUE;
+               }
+               else {
+                       $thisline["hasnewmsgs"] = FALSE;
+               }
+
                $num_lines = array_push($all_lines, $thisline);
        }
 
@@ -277,5 +310,101 @@ function ctdl_knrooms() {
 }
 
 
+//
+// Fetch the list of messages in this room.
+// Returns: count, response, message array
+//
+function ctdl_msgs($mode, $count) {
+       global $clientsocket;
+
+       serv_puts("MSGS " . $mode . "|" . $count);
+       $response = serv_gets();
+
+       if (substr($response, 0, 1) != "1") {
+               return array(0, substr($response, 4), NULL);
+       }
+       
+       $msgs = array();
+       $num_msgs = 0;
+
+       while (strcmp($buf = serv_gets(), "000")) {
+               $num_msgs = array_push($msgs, $buf);
+       }
+
+       return array($num_msgs, substr($response, 4), $msgs);
+}
+
+
+// Load a message from the server.
+function ctdl_fetch_message($msgnum) {
+       global $clientsocket;
+
+       serv_puts("MSG4 " . $msgnum);
+       $response = serv_gets();
+
+       if (substr($response, 0, 1) != "1") {
+               return array(FALSE, substr($response, 4), NULL);
+       }
+
+       $fields = array();
+       while (strcmp($buf = serv_gets(), "000")) {
+               if (substr($buf, 0, 4) == "text") {
+                       // We're in the text body.  New loop here.
+                       $fields["text"] = ctdl_msg4_from_server();
+                       return array(TRUE, substr($response, 4), $fields);
+               }
+               else {
+                       $fields[substr($buf, 0, 4)] = substr($buf, 5);
+               }
+       }
+
+       // Message terminated prematurely (no text body)
+       return array(FALSE, substr($response, 4), $fields);
+}
+
+// Support function for ctdl_fetch_message().  This handles the text body
+// portion of the message, converting various formats to HTML as
+// appropriate.
+function ctdl_msg4_from_server() {
+
+       $txt = "";
+       $msgformat = "text/plain";
+       $in_body = FALSE;
+
+       $previous_line = "";
+       while (strcmp($buf = serv_gets(), "000")) {
+               if ($in_body == FALSE) {
+                       if (strlen($buf) == 0) {
+                               $in_body = TRUE;
+                       }
+                       else {
+                               if (!strncasecmp($buf, "content-type: ", 14)) {
+                                       $msgformat = substr($buf, 14);
+                               }
+                       }
+               }
+               else {
+                       if (!strcasecmp($msgformat, "text/html")) {
+                               $txt .= $buf;
+                       }
+                       else if (!strcasecmp($msgformat, "text/plain")) {
+                               $txt .= "<TT>" . htmlspecialchars($buf) . "</TT><BR>\n" ;
+                       }
+                       else if (!strcasecmp($msgformat, "text/x-citadel-variformat")) {
+                               if (substr($previous_line, 0, 1) == " ") {
+                                       $txt .= "<BR>\n" ;
+                               }
+                               $txt .= htmlspecialchars($buf);
+                       }
+                       else {
+                               $txt .= htmlspecialchars($buf);
+                       }
+                       $previous_line = $buf;
+               }
+       }
+
+       return($txt);
+}
+
 
 ?>