5 // Implements various Citadel server commands.
7 // Copyright (c) 2003 by Art Cancro <ajc@uncensored.citadel.org>
8 // This program is released under the terms of the GNU General Public License.
12 // serv_gets() -- generic function to read one line of text from the server
14 function serv_gets() {
17 $buf = fgets($clientsocket, 4096); // Read line
18 $buf = substr($buf, 0, (strlen($buf)-1) ); // strip trailing LF
24 // serv_puts() -- generic function to write one line of text to the server
26 function serv_puts($buf) {
29 fwrite($clientsocket, $buf . "\n", (strlen($buf)+1) );
35 // Identify ourselves to the Citadel server (do this once after connection)
37 function ctdl_iden() {
40 // Identify client and hostname
41 serv_puts("IDEN 0|8|001|PHP web client|" . $_SERVER['REMOTE_ADDR'] );
44 // Also express our message format preferences
45 serv_puts("MSGP text/html|text/plain");
52 // login_existing_user() -- attempt to login using a supplied username/password
53 // Returns an array with two variables:
54 // 0. TRUE or FALSE to determine success or failure
55 // 1. String error message (if relevant)
57 function login_existing_user($user, $pass) {
60 serv_puts("USER " . $user);
62 if (substr($resp, 0, 1) != "3") {
63 return array(FALSE, substr($resp, 4));
66 serv_puts("PASS " . $pass);
68 if (substr($resp, 0, 1) != "2") {
69 return array(FALSE, substr($resp, 4));
72 $_SESSION["username"] = $user;
73 $_SESSION["password"] = $pass;
74 become_logged_in(substr($resp, 4));
76 return array(TRUE, "Login successful. Have fun.");
81 // create_new_user() -- attempt to create a new user
82 // using a supplied username/password
83 // Returns an array with two variables:
84 // 0. TRUE or FALSE to determine success or failure
85 // 1. String error message (if relevant)
87 function create_new_user($user, $pass) {
90 serv_puts("NEWU " . $user);
92 if (substr($resp, 0, 1) != "2") {
93 return array(FALSE, substr($resp, 4));
96 serv_puts("SETP " . $pass);
98 if (substr($resp, 0, 1) != "2") {
99 return array(FALSE, substr($resp, 4));
102 $_SESSION["username"] = $user;
103 $_SESSION["password"] = $pass;
104 become_logged_in(substr($resp, 4));
106 return array(TRUE, "Login successful. Have fun.");
111 // Code common to both existing-user and new-user logins
113 function become_logged_in($server_parms) {
114 $_SESSION["logged_in"] = 1;
116 $tok = strtok($server_parms, "|");
117 if ($tok) $thisline["username"] = $tok;
120 if ($tok) $thisline["axlevel"] = $tok;
123 if ($tok) $thisline["calls"] = $tok;
126 if ($tok) $thisline["posts"] = $tok;
129 if ($tok) $thisline["userflags"] = $tok;
132 if ($tok) $thisline["usernum"] = $tok;
135 if ($tok) $thisline["lastcall"] = $tok;
137 ctdl_goto("_BASEROOM_");
143 // Learn all sorts of interesting things about the Citadel server to
144 // which we are connected.
146 function ctdl_get_serv_info() {
149 if (substr($buf, 0, 1) == "1") {
153 if ($i == 1) $_SESSION["serv_nodename"] = $buf;
154 if ($i == 2) $_SESSION["serv_humannode"] = $buf;
155 if ($i == 3) $_SESSION["serv_fqdn"] = $buf;
156 if ($i == 4) $_SESSION["serv_software"] = $buf;
157 if ($i == 6) $_SESSION["serv_city"] = $buf;
158 if ($i == 7) $_SESSION["serv_sysadmin"] = $buf;
160 } while (strcasecmp($buf, "000"));
167 // Display a system banner. (Returns completed HTML.)
168 // (This is probably temporary because it outputs more or less finalized
169 // markup. For now it's just usable.)
171 function ctdl_mesg($msgname) {
172 global $clientsocket;
174 $msgtext = "<DIV ALIGN=CENTER>\n";
176 serv_puts("MESG " . $msgname);
177 $response = serv_gets();
179 if (substr($response, 0, 1) == "1") {
180 while (strcmp($buf = serv_gets(), "000")) {
181 $msgtext .= "<TT>" . htmlspecialchars($buf)
186 $msgtext .= "<B><I>" . substr($response, 4) . "</I></B><BR>\n";
189 $msgtext .= "</DIV>\n";
195 // Fetch the list of users currently logged in.
197 function ctdl_rwho() {
198 global $clientsocket;
201 $response = serv_gets();
203 if (substr($response, 0, 1) != "1") {
204 return array(0, NULL);
207 $all_lines = array();
210 while (strcmp($buf = serv_gets(), "000")) {
214 $tok = strtok($buf, "|");
215 if ($tok) $thisline["session"] = $tok;
218 if ($tok) $thisline["user"] = $tok;
221 if ($tok) $thisline["room"] = $tok;
224 if ($tok) $thisline["host"] = $tok;
227 if ($tok) $thisline["client"] = $tok;
229 // IGnore the rest of the fields for now.
231 $num_lines = array_push($all_lines, $thisline);
234 return array($num_lines, $all_lines);
242 function ctdl_goto($to_where) {
244 serv_puts("GOTO " . $to_where);
245 $response = serv_gets();
247 if (substr($response, 0, 1) == "2") {
248 $_SESSION["room"] = strtok(substr($response, 4), "|");
249 return array(TRUE, substr($response, 0, 3));
253 return array(FALSE, substr($response, 0, 3));
261 // Fetch the list of known rooms.
263 function ctdl_knrooms() {
264 global $clientsocket;
267 $response = serv_gets();
269 if (substr($response, 0, 1) != "1") {
270 return array(0, NULL);
273 $all_lines = array();
276 while (strcmp($buf = serv_gets(), "000")) {
280 $tok = strtok($buf, "|");
281 if ($tok) $thisline["name"] = $tok;
284 if ($tok) $thisline["flags"] = $tok;
287 if ($tok) $thisline["floor"] = $tok;
290 if ($tok) $thisline["order"] = $tok;
293 if ($tok) $thisline["flags2"] = $tok;
296 if ($tok) $thisline["access"] = $tok;
298 if ($thisline["access"] & 8) {
299 $thisline["hasnewmsgs"] = TRUE;
302 $thisline["hasnewmsgs"] = FALSE;
305 $num_lines = array_push($all_lines, $thisline);
308 return array($num_lines, $all_lines);
314 // Fetch the list of messages in this room.
315 // Returns: count, response, message array
317 function ctdl_msgs($mode, $count) {
318 global $clientsocket;
320 serv_puts("MSGS " . $mode . "|" . $count);
321 $response = serv_gets();
323 if (substr($response, 0, 1) != "1") {
324 return array(0, substr($response, 4), NULL);
330 while (strcmp($buf = serv_gets(), "000")) {
331 $num_msgs = array_push($msgs, $buf);
334 return array($num_msgs, substr($response, 4), $msgs);
338 // Load a message from the server.
339 function ctdl_fetch_message($msgnum) {
340 global $clientsocket;
342 serv_puts("MSG4 " . $msgnum);
343 $response = serv_gets();
345 if (substr($response, 0, 1) != "1") {
346 return array(FALSE, substr($response, 4), NULL);
350 while (strcmp($buf = serv_gets(), "000")) {
351 if (substr($buf, 0, 4) == "text") {
352 // We're in the text body. New loop here.
353 $fields["text"] = ctdl_msg4_from_server();
354 return array(TRUE, substr($response, 4), $fields);
357 $fields[substr($buf, 0, 4)] = substr($buf, 5);
361 // Message terminated prematurely (no text body)
362 return array(FALSE, substr($response, 4), $fields);
365 // Support function for ctdl_fetch_message(). This handles the text body
366 // portion of the message, converting various formats to HTML as
368 function ctdl_msg4_from_server() {
371 $msgformat = "text/plain";
375 while (strcmp($buf = serv_gets(), "000")) {
376 if ($in_body == FALSE) {
377 if (strlen($buf) == 0) {
381 if (!strncasecmp($buf, "content-type: ", 14)) {
382 $msgformat = substr($buf, 14);
387 if (!strcasecmp($msgformat, "text/html")) {
390 else if (!strcasecmp($msgformat, "text/plain")) {
391 $txt .= "<TT>" . htmlspecialchars($buf) . "</TT><BR>\n" ;
393 else if (!strcasecmp($msgformat, "text/x-citadel-variformat")) {
394 if (substr($previous_line, 0, 1) == " ") {
397 $txt .= htmlspecialchars($buf);
400 $txt .= htmlspecialchars($buf);
402 $previous_line = $buf;