1<?
2/*
3 * licqweb. Copyright 2005, Philip Nelson
4 *
5 * This file is part of licqweb.
6 *
7 * licqweb is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * licqweb is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with licqweb; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20 */
21require_once('config.php');
22require_once('serverpush.php');
23require_once('rms.php');
24
25define('CODE_NOTIFYxSTATUS', '600');
26define('CODE_NOTIFYxMESSAGE', '601');
27define('CODE_LOG', '103');
28session_set_cookie_params(0, substr($_SERVER['REQUEST_URI'], 0, strrchr($_SERVER['REQUEST_URI'], '/')));
29session_start();
30if ($_GET['uin'] || $_GET['password']) {
31	$uin = stripslashes($_GET['uin']);
32	$password = stripslashes($_GET['password']);
33	$_SESSION['uin'] = $uin;
34	$_SESSION['password'] = $password;
35} else {
36	$uin = $_SESSION['uin'];
37	$password = $_SESSION['password'];
38}
39session_write_close();
40if (!$uin || !$password) {
41	header('Content-Type: text/xml');
42	echo "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>";
43	echo "<response><method>loginFailed</method><result>Couldn't log in to rms plugin!</result></response>";
44	exit;
45}
46
47$nick = rmsLogin("$uin\r\n", "$password\r\n");
48if (!$nick) {
49	header('Content-Type: text/xml');
50	echo "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>";
51	echo "<response><method>loginFailed</method><result>Couldn't log in to rms plugin!</result></response>";
52	exit;
53}
54
55$nick = substr($nick, 0, -1);
56$cmd = "NOTIFY\r\n";
57socket_write($sock, $cmd, strlen($cmd));
58socket_read($sock, 512, PHP_NORMAL_READ);
59
60/*
61$cmd = "LOG 15\r\n";
62socket_write($sock, $cmd, strlen($cmd));
63socket_read($sock, 512, PHP_NORMAL_READ);
64*/
65
66http_push_start();
67
68$ownerInfo = rmsGetStatus();
69$ownerxml = "";
70foreach ($ownerInfo as $pp => $info) {
71	$ownerxml .= "<owner><id>" . $info['id'] . "</id><pp>$pp</pp><status>" . $info['status'] . "</status></owner>";
72}
73$message = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n<response><method>setOwnerInfo</method><result>$ownerxml</result></response>";
74push($message);
75
76$message = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n<response><method>setOwnerNick</method><result>" . xmlentities($nick) . "</result></response>";
77push($message);
78
79$users = rmsGetlist($_GET['listtype']);
80if (count($users) > 0) {
81	$userstxt = "<users>";
82	foreach ($users as $user) {
83		$userstxt .= "<user><id>" . xmlentities($user['id']) . "</id><nick>" . str_replace("\n", '', xmlentities($user['nick'])) . "</nick><newmsgs>" . trim(xmlentities($user['newmsgs'])) . "</newmsgs><status>" . str_replace(' ', '', xmlentities($user['status'])) . "</status><pp>" . xmlentities($user['pp']) . "</pp></user>";
84	}
85	$userstxt .= "</users>";
86	$res = "
87		<response>
88			<method>userList</method>
89			<result>$userstxt</result>
90		</response>";
91	$message = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n" . $res;
92	push($message);
93}
94
95while (true) {
96	$packet = socket_read($sock, 512, PHP_NORMAL_READ);
97	if (!$packet) break;
98	$code = substr($packet, 0, 3);
99	switch ($code) {
100		case CODE_NOTIFYxSTATUS:
101			$user = getUserStuff($packet);
102			$method = "updateStatus";
103      $txt = "<newstatus><id>" . xmlentities($user['id'])
104          . "</id><pp>" . xmlentities($user['pp'])
105          . "</pp><nick>" . str_replace("\n", '', xmlentities(kses($user['nick'], array())))
106          . "</nick><nummsgs>" . trim(xmlentities($user['newmsgs']))
107          . "</nummsgs><status>" . str_replace(' ', '', xmlentities($user['status']))
108          . "</status></newstatus>";
109      break;
110		case CODE_NOTIFYxMESSAGE:
111			list($id, $pp, $numMessages) = split(' ', substr($packet, 4), 3);
112			$method = "newMessage";
113			$txt = "<newmessage><id>$id</id><pp>$pp</pp><nummsgs>$numMessages</nummsgs></newmessage>";
114			break;
115		case CODE_LOG:
116			$method = "log";
117			$txt = substr($packet, 4);
118			break;
119		default:
120			continue;
121	}
122	$message = "
123		<response>
124		  <method>$method</method>
125		  <result>$txt</result>
126		</response>
127	";
128	$message = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n" . $message;
129	push($message);
130}
131?>
132