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) );
30 fflush($clientsocket);
36 // Identify ourselves to the Citadel server (do this once after connection)
38 function ctdl_iden() {
41 // Identify client and hostname
42 serv_puts("IDEN 0|8|001|PHP web client|" . $_SERVER['REMOTE_ADDR'] );
45 // Also express our message format preferences
46 serv_puts("MSGP text/html|text/plain");
53 // login_existing_user() -- attempt to login using a supplied username/password
54 // Returns an array with two variables:
55 // 0. TRUE or FALSE to determine success or failure
56 // 1. String error message (if relevant)
58 function login_existing_user($user, $pass) {
61 serv_puts("USER " . $user);
63 if (substr($resp, 0, 1) != "3") {
64 return array(FALSE, substr($resp, 4));
67 serv_puts("PASS " . $pass);
69 if (substr($resp, 0, 1) != "2") {
70 return array(FALSE, substr($resp, 4));
73 $_SESSION["username"] = $user;
74 $_SESSION["password"] = $pass;
75 become_logged_in(substr($resp, 4));
77 return array(TRUE, "Login successful. Have fun.");
82 // create_new_user() -- attempt to create a new user
83 // using a supplied username/password
84 // Returns an array with two variables:
85 // 0. TRUE or FALSE to determine success or failure
86 // 1. String error message (if relevant)
88 function create_new_user($user, $pass) {
91 serv_puts("NEWU " . $user);
93 if (substr($resp, 0, 1) != "2") {
94 return array(FALSE, substr($resp, 4));
97 serv_puts("SETP " . $pass);
99 if (substr($resp, 0, 1) != "2") {
100 return array(FALSE, substr($resp, 4));
103 $_SESSION["username"] = $user;
104 $_SESSION["password"] = $pass;
105 become_logged_in(substr($resp, 4));
107 return array(TRUE, "Login successful. Have fun.");
112 // Code common to both existing-user and new-user logins
114 function become_logged_in($server_parms) {
115 $_SESSION["logged_in"] = 1;
117 $tok = strtok($server_parms, "|");
118 if ($tok) $thisline["username"] = $tok;
121 if ($tok) $thisline["axlevel"] = $tok;
124 if ($tok) $thisline["calls"] = $tok;
127 if ($tok) $thisline["posts"] = $tok;
130 if ($tok) $thisline["userflags"] = $tok;
133 if ($tok) $thisline["usernum"] = $tok;
136 if ($tok) $thisline["lastcall"] = $tok;
138 ctdl_goto("_BASEROOM_");
144 // Learn all sorts of interesting things about the Citadel server to
145 // which we are connected.
147 function ctdl_get_serv_info() {
150 if (substr($buf, 0, 1) == "1") {
154 if ($i == 1) $_SESSION["serv_nodename"] = $buf;
155 if ($i == 2) $_SESSION["serv_humannode"] = $buf;
156 if ($i == 3) $_SESSION["serv_fqdn"] = $buf;
157 if ($i == 4) $_SESSION["serv_software"] = $buf;
158 if ($i == 6) $_SESSION["serv_city"] = $buf;
159 if ($i == 7) $_SESSION["serv_sysadmin"] = $buf;
161 } while (strcasecmp($buf, "000"));
168 // Display a system banner. (Returns completed HTML.)
169 // (This is probably temporary because it outputs more or less finalized
170 // markup. For now it's just usable.)
172 function ctdl_mesg($msgname) {
173 global $clientsocket;
175 $msgtext = "<DIV ALIGN=CENTER>\n";
177 serv_puts("MESG " . $msgname);
178 $response = serv_gets();
180 if (substr($response, 0, 1) == "1") {
181 while (strcmp($buf = serv_gets(), "000")) {
182 $msgtext .= "<TT>" . htmlspecialchars($buf)
187 $msgtext .= "<B><I>" . substr($response, 4) . "</I></B><BR>\n";
190 $msgtext .= "</DIV>\n";
196 // Fetch the list of users currently logged in.
198 function ctdl_rwho() {
199 global $clientsocket;
202 $response = serv_gets();
204 if (substr($response, 0, 1) != "1") {
205 return array(0, NULL);
208 $all_lines = array();
211 while (strcmp($buf = serv_gets(), "000")) {
215 $tok = strtok($buf, "|");
216 if ($tok) $thisline["session"] = $tok;
219 if ($tok) $thisline["user"] = $tok;
222 if ($tok) $thisline["room"] = $tok;
225 if ($tok) $thisline["host"] = $tok;
228 if ($tok) $thisline["client"] = $tok;
230 // IGnore the rest of the fields for now.
232 $num_lines = array_push($all_lines, $thisline);
235 return array($num_lines, $all_lines);
243 function ctdl_goto($to_where) {
245 serv_puts("GOTO " . $to_where);
246 $response = serv_gets();
248 if (substr($response, 0, 1) == "2") {
249 $_SESSION["room"] = strtok(substr($response, 4), "|");
250 return array(TRUE, substr($response, 0, 3));
254 return array(FALSE, substr($response, 0, 3));
262 // Fetch the list of known rooms.
264 function ctdl_knrooms() {
265 global $clientsocket;
268 $response = serv_gets();
270 if (substr($response, 0, 1) != "1") {
271 return array(0, NULL);
274 $all_lines = array();
277 while (strcmp($buf = serv_gets(), "000")) {
281 $tok = strtok($buf, "|");
282 if ($tok) $thisline["name"] = $tok;
285 if ($tok) $thisline["flags"] = $tok;
288 if ($tok) $thisline["floor"] = $tok;
291 if ($tok) $thisline["order"] = $tok;
294 if ($tok) $thisline["flags2"] = $tok;
297 if ($tok) $thisline["access"] = $tok;
299 if ($thisline["access"] & 8) {
300 $thisline["hasnewmsgs"] = TRUE;
303 $thisline["hasnewmsgs"] = FALSE;
306 $num_lines = array_push($all_lines, $thisline);
309 return array($num_lines, $all_lines);
315 // Fetch the list of messages in this room.
316 // Returns: count, response, message array
318 function ctdl_msgs($mode, $count) {
319 global $clientsocket;
321 serv_puts("MSGS " . $mode . "|" . $count);
322 $response = serv_gets();
324 if (substr($response, 0, 1) != "1") {
325 return array(0, substr($response, 4), NULL);
331 while (strcmp($buf = serv_gets(), "000")) {
332 $num_msgs = array_push($msgs, $buf);
335 return array($num_msgs, substr($response, 4), $msgs);
339 // Load a message from the server.
340 function ctdl_fetch_message($msgnum) {
341 global $clientsocket;
343 serv_puts("MSG4 " . $msgnum);
344 $response = serv_gets();
346 if (substr($response, 0, 1) != "1") {
347 return array(FALSE, substr($response, 4), NULL);
351 while (strcmp($buf = serv_gets(), "000")) {
352 if (substr($buf, 0, 4) == "text") {
353 // We're in the text body. New loop here.
354 $fields["text"] = ctdl_msg4_from_server();
355 return array(TRUE, substr($response, 4), $fields);
358 $fields[substr($buf, 0, 4)] = substr($buf, 5);
362 // Message terminated prematurely (no text body)
363 return array(FALSE, substr($response, 4), $fields);
366 // Support function for ctdl_fetch_message(). This handles the text body
367 // portion of the message, converting various formats to HTML as
369 function ctdl_msg4_from_server() {
372 $msgformat = "text/plain";
376 while (strcmp($buf = serv_gets(), "000")) {
377 if ($in_body == FALSE) {
378 if (strlen($buf) == 0) {
382 if (!strncasecmp($buf, "content-type: ", 14)) {
383 $msgformat = substr($buf, 14);
388 if (!strcasecmp($msgformat, "text/html")) {
391 else if (!strcasecmp($msgformat, "text/plain")) {
392 $txt .= "<TT>" . htmlspecialchars($buf) . "</TT><BR>\n" ;
394 else if (!strcasecmp($msgformat, "text/x-citadel-variformat")) {
395 if (substr($previous_line, 0, 1) == " ") {
398 $txt .= htmlspecialchars($buf);
401 $txt .= htmlspecialchars($buf);
403 $previous_line = $buf;