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 this client, and the hostname where the user is, to Citadel.
37 function ctdl_iden() {
40 serv_puts("IDEN 0|8|001|PHP web client|" . $_SERVER['REMOTE_ADDR'] );
47 // login_existing_user() -- attempt to login using a supplied username/password
48 // Returns an array with two variables:
49 // 0. TRUE or FALSE to determine success or failure
50 // 1. String error message (if relevant)
52 function login_existing_user($user, $pass) {
55 serv_puts("USER " . $user);
57 if (substr($resp, 0, 1) != "3") {
58 return array(FALSE, substr($resp, 4));
61 serv_puts("PASS " . $pass);
63 if (substr($resp, 0, 1) != "2") {
64 return array(FALSE, substr($resp, 4));
67 $_SESSION["username"] = $user;
68 $_SESSION["password"] = $pass;
69 become_logged_in(substr($resp, 4));
71 return array(TRUE, "Login successful. Have fun.");
76 // create_new_user() -- attempt to create a new user
77 // using a supplied username/password
78 // Returns an array with two variables:
79 // 0. TRUE or FALSE to determine success or failure
80 // 1. String error message (if relevant)
82 function create_new_user($user, $pass) {
85 serv_puts("NEWU " . $user);
87 if (substr($resp, 0, 1) != "2") {
88 return array(FALSE, substr($resp, 4));
91 serv_puts("SETP " . $pass);
93 if (substr($resp, 0, 1) != "2") {
94 return array(FALSE, substr($resp, 4));
97 $_SESSION["username"] = $user;
98 $_SESSION["password"] = $pass;
99 become_logged_in(substr($resp, 4));
101 return array(TRUE, "Login successful. Have fun.");
106 // Code common to both existing-user and new-user logins
108 function become_logged_in($server_parms) {
109 $_SESSION["logged_in"] = 1;
111 $tok = strtok($server_parms, "|");
112 if ($tok) $thisline["username"] = $tok;
115 if ($tok) $thisline["axlevel"] = $tok;
118 if ($tok) $thisline["calls"] = $tok;
121 if ($tok) $thisline["posts"] = $tok;
124 if ($tok) $thisline["userflags"] = $tok;
127 if ($tok) $thisline["usernum"] = $tok;
130 if ($tok) $thisline["lastcall"] = $tok;
132 ctdl_goto("_BASEROOM_");
138 // Learn all sorts of interesting things about the Citadel server to
139 // which we are connected.
141 function ctdl_get_serv_info() {
144 if (substr($buf, 0, 1) == "1") {
148 if ($i == 1) $_SESSION["serv_nodename"] = $buf;
149 if ($i == 2) $_SESSION["serv_humannode"] = $buf;
150 if ($i == 3) $_SESSION["serv_fqdn"] = $buf;
151 if ($i == 4) $_SESSION["serv_software"] = $buf;
152 if ($i == 6) $_SESSION["serv_city"] = $buf;
153 if ($i == 7) $_SESSION["serv_sysadmin"] = $buf;
155 } while (strcasecmp($buf, "000"));
162 // Display a system banner. (Returns completed HTML.)
163 // (This is probably temporary because it outputs more or less finalized
164 // markup. For now it's just usable.)
166 function ctdl_mesg($msgname) {
167 global $clientsocket;
169 $msgtext = "<DIV ALIGN=CENTER>\n";
171 serv_puts("MESG " . $msgname);
172 $response = serv_gets();
174 if (substr($response, 0, 1) == "1") {
175 while (strcmp($buf = serv_gets(), "000")) {
176 $msgtext .= "<TT>" . htmlspecialchars($buf)
181 $msgtext .= "<B><I>" . substr($response, 4) . "</I></B><BR>\n";
184 $msgtext .= "</DIV>\n";
190 // Fetch the list of users currently logged in.
192 function ctdl_rwho() {
193 global $clientsocket;
196 $response = serv_gets();
198 if (substr($response, 0, 1) != "1") {
199 return array(0, NULL);
202 $all_lines = array();
205 while (strcmp($buf = serv_gets(), "000")) {
209 $tok = strtok($buf, "|");
210 if ($tok) $thisline["session"] = $tok;
213 if ($tok) $thisline["user"] = $tok;
216 if ($tok) $thisline["room"] = $tok;
219 if ($tok) $thisline["host"] = $tok;
222 if ($tok) $thisline["client"] = $tok;
224 // IGnore the rest of the fields for now.
226 $num_lines = array_push($all_lines, $thisline);
229 return array($num_lines, $all_lines);
237 function ctdl_goto($to_where) {
239 serv_puts("GOTO " . $to_where);
240 $response = serv_gets();
242 if (substr($response, 0, 1) == "2") {
243 $_SESSION["room"] = strtok(substr($response, 4), "|");
244 return array(TRUE, substr($response, 0, 3));
248 return array(FALSE, substr($response, 0, 3));
256 // Fetch the list of known rooms.
258 function ctdl_knrooms() {
259 global $clientsocket;
262 $response = serv_gets();
264 if (substr($response, 0, 1) != "1") {
265 return array(0, NULL);
268 $all_lines = array();
271 while (strcmp($buf = serv_gets(), "000")) {
275 $tok = strtok($buf, "|");
276 if ($tok) $thisline["name"] = $tok;
279 if ($tok) $thisline["flags"] = $tok;
282 if ($tok) $thisline["floor"] = $tok;
285 if ($tok) $thisline["order"] = $tok;
288 if ($tok) $thisline["flags2"] = $tok;
291 if ($tok) $thisline["access"] = $tok;
293 if ($thisline["access"] & 8) {
294 $thisline["hasnewmsgs"] = TRUE;
297 $thisline["hasnewmsgs"] = FALSE;
300 $num_lines = array_push($all_lines, $thisline);
303 return array($num_lines, $all_lines);