aboutsummaryrefslogtreecommitdiff
path: root/mod/beechat/lib/beechat.php
blob: bf002d6487f065715f796b41c7e88d261555b8b4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
<?
/*
	allow_change_subj
        allow_private_messages
        allow_query_users
        allow_user_invites
        anonymous
        logging
        max_users
        members_by_default
        members_only
        moderated
        password
        password_protected
        persistent
        public
        public_list
        title

muc_room_set_affiliation struct[{name, String}, {service, String},
                                {jid, String}, {affiliation, Affiliation}]  Integer


*/
	//$request = xmlrpc_encode_request('muc_online_rooms', "global", (array('encoding' => 'utf-8')));


function ejabberd_xmlrpc_send($request)
{
	$context = stream_context_create(array('http' => array(
	    'method' => "POST",
	    'header' => "User-Agent: XMLRPC::Client mod_xmlrpc\r\n" .
			"Content-Type: text/xml\r\n" .
			"Content-Length: ".strlen($request),
	    'content' => $request
	)));

	$file = file_get_contents("http://".elgg_get_plugin_setting("xmlrpcip", "beechat").":4560/RPC2", false, $context);

	$response = xmlrpc_decode($file);
	if (is_array($response) && xmlrpc_is_fault($response)) {
	    trigger_error("xmlrpc: $response[faultString] ($response[faultCode])");
	} else {
	//    print_r($response);
	}
}

function ejabberd_xmlrpc_command($command, $params)
{
	//error_log("send xmlrpc: ".$command);
	$request = xmlrpc_encode_request($command, $params, (array('encoding' => 'utf-8')));
        return ejabberd_xmlrpc_send($request);
}

function xmpp_escape($name) {
	// http://xmpp.org/extensions/xep-0106.html#escaping
	$name = str_replace(' ', '\\20', $name);
	$name = str_replace('"', '\\22', $name);
	$name = str_replace('&', '\\26', $name);
	$name = str_replace("'", '\\27', $name);
	$name = str_replace('/', '\\2f', $name);
	$name = str_replace(';', '\\3a', $name);
	$name = str_replace('<', '\\3c', $name);
	$name = str_replace('>', '\\3e', $name);
	$name = str_replace('@', '\\40', $name);
	$name = str_replace('\\', '\\5c', $name);
	return $name;
}

class EjabberdMucRoom {
	function __construct($group) {
		$this->group = $group;
	}
	function setOption($name, $value) {
		$group = $this->group;
		$param=array("name"=>elgg_get_friendly_title($group->name),
			"service"=>elgg_get_plugin_setting("groupdomain", "beechat"),
			"option"=>$name,
			"value"=>$value);
		ejabberd_xmlrpc_command('muc_room_change_option', $param);
	}
	function addMember($member) {
		//"outcast" | "none" | "member" | "admin" | "owner"
		$group = $this->group;
		if ($member->guid === $group->owner_guid)
			$affiliation = "owner";
		elseif ($group->canEdit($member->guid))
			$affiliation = "admin";
		else
			$affiliation = "member";
		$this->setAffiliation($member, $affiliation);
	}

	function setAffiliation($member, $affiliation) {
		$group = $this->group;
		$param = array("name" => elgg_get_friendly_title($group->name),
			"service" => elgg_get_plugin_setting("groupdomain", "beechat"),
			"jid" => xmpp_escape($member->username) . '@' . elgg_get_plugin_setting("domain", "beechat"),
			"affiliation" => $affiliation);
		ejabberd_xmlrpc_command('muc_room_set_affiliation', $param);
		//echo "set affiliation ".$member->username."<br/>";
	}
}

function ejabberd_create_group($group)
{
	//echo "creating " . $group->name . "<br/>";
	// create room
	$param=array("name"=>elgg_get_friendly_title($group->name),
			"service"=>elgg_get_plugin_setting("groupdomain", "beechat"),
			"server"=>elgg_get_plugin_setting("domain", "beechat"));
	ejabberd_xmlrpc_command('create_muc_room', $param);

	// persistency

	$room = new EjabberdMucRoom($group);
	$room->setOption("persistent", true);
	$room->setOption("title", $group->name);
	// open to public?
	if ($group->isPublicMembership()) {
		$room->setOption("members_only", false);
	}
	else
		$room->setOption("members_only", true);

	if ($group->access_id === ACCESS_PUBLIC) {
		$room->setOption("public_list", true);
		$room->setOption("public", true);
	}
	else {
		$room->setOption("public_list", false);
		$room->setOption("public", false);
	}
	$members = $group->getMembers(0);
	foreach($members as $member) {
		$room->addMember($member);
	}
	$room->addMember(get_entity($group->owner_guid));
}

function ejabberd_destroy_group($group)
{
	$param=array("name"=>elgg_get_friendly_title($group->name),
			"service"=>elgg_get_plugin_setting("groupdomain", "beechat"),
			"server"=>elgg_get_plugin_setting("domain", "beechat"));
	ejabberd_xmlrpc_command('delete_muc_room', $param);
}

function ejabberd_getjid($user, $do_external=false)
{
	if ($user->foreign || ($do_external && $user->alias && get_plugin_usersetting("usealias", $user->guid,"openid_client"))) {
		if ($user->foreign)
			$webid = $user->webid;
		else
			$webid = $user->alias;
		if (strpos($webid, 'http') === 0) {
			// http or https addresses
			$hostparts = parse_url($webid);
                        $urlparts = explode('/', $webid);
			$host = $hostparts['host'];
			$username = $urlparts[count($urlparts)-1];
		} else {
			if (strpos($webid, ':') > 0) {
				$webidparts = explode(':', $webid);
				$hostparts = explode('@',$webidparts[1]);
			} else {
				$hostparts = explode('@',$webid);
			}
			$username = $hostparts[0];
			$host = $hostparts[1];
		}
	}
	else {
		$username = $user->username;
		$host = elgg_get_plugin_setting("domain", "beechat");
	}
	return xmpp_escape($username) . '@' . $host;
}

function ejabberd_friend_command($user, $friend, $command, $is_out) // $user adds $friend
{
	error_log(" * ".$friend->username."->".ejabberd_getjid($user)." ".$command);
	if ($friend->foreign) {
         	error_log(" * beechat: friend is foreign!");       
		return;
	}
	$param = array("user" => elgg_get_friendly_title($friend->username),
			"server" => elgg_get_plugin_setting("domain", "beechat"),
	 		"from" => ejabberd_getjid($user),
			"subs" => $command);
	if ($is_out) {
		error_log("out");
		ejabberd_xmlrpc_command('send_roster_request_out', $param);
	}
	else {
		$param['reason'] = 'unknown';
		ejabberd_xmlrpc_command('send_roster_request_in', $param);
	}
}


function ejabberd_friend_request($user, $friend) // $user adds $friend
{
	error_log('ejabberd_friend_request');
	ejabberd_friend_command($friend, $user, 'subscribe', true); // out:$user : $friend
	error_log('ejabberd_friend_requested');
}
function ejabberd_friend_accept($user, $friend) // $user adds $friend
{
	error_log('ejabberd_friend_accept');
	ejabberd_friend_command($friend, $user, 'subscribed', true);
	// following might be needed to have symmetry (and important for remote)
        if ($friend->foreign) {
            // following is needed for xmpp nodes
	    ejabberd_friend_command($friend, $user, 'subscribe', true);
	}
	// ejabberd_friend_command($friend, $user, 'subscribed', false);
	// following can't be faked
        if (!$friend->foreign)
	    ejabberd_friend_command($user, $friend, 'subscribed', true);
	error_log('ejabberd_friend_accepted');
}
function ejabberd_friend_deny($user, $friend) // $user adds $friend
{
	error_log('ejabberd_friedeny');
	ejabberd_friend_command($friend, $user, 'unsubscribed', true);
}
function ejabberd_friend_remove($user, $friend) // $user adds $friend
{
	error_log('ejabberd_friend_remove');
        if ($friend->foreign) {
		ejabberd_friend_command($friend, $user, 'unsubscribed', true);
		ejabberd_friend_command($friend, $user, 'unsubscribed', false);
	}
	else
		ejabberd_friend_command($friend, $user, 'unsubscribed', false);
        if (!$friend->foreign)
		ejabberd_friend_command($user, $friend, 'unsubscribed', false);
	error_log('ejabberd_friend_removed');
}

/*function ejabberd_send_chat($from, $to, $body) { // $user adds $friend
	$param = array("body"=>$body,
			"from"=>$from,
			"to"=>$to);
	ejabberd_xmlrpc_command('send_chat_message', $param);
}*/


?>