Welcome to mirror list, hosted at ThFree Co, Russian Federation.

IQ.php « StanzaHandlers « lib - github.com/nextcloud/jsxc.nextcloud.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: dd4cd367fef6c0e4f42990dbfdd2ca1a6464c8f3 (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
<?php

namespace OCA\OJSXC\StanzaHandlers;

use OCA\OJSXC\Db\IQRoster;
use OCA\OJSXC\Db\IQNotImplemented;
use OCA\OJSXC\Db\Stanza;
use OCA\OJSXC\Exceptions\TerminateException;
use OCA\OJSXC\IUserProvider;
use OCP\IConfig;
use OCP\IUserManager;

/**
 * Class IQ
 *
 * @package OCA\OJSXC\StanzaHandlers
 */
class IQ extends StanzaHandler
{

	/**
	 * @var IUserManager
	 */
	private $userManager;

	/**
	 * @var IConfig
	 */
	private $config;

	/**
	 * @var IUserProvider
	 */
	private $userProvider;

	/**
	 * IQ constructor.
	 *
	 * @param string $userId
	 * @param string $host
	 * @param IUserManager $userManager
	 * @param IConfig $config
	 * @param IUserProvider $userProvider
	 */
	public function __construct($userId, $host, IUserManager $userManager, IConfig $config, IUserProvider $userProvider)
	{
		parent::__construct($userId, $host);
		$this->userManager = $userManager;
		$this->config = $config;
		$this->userProvider = $userProvider;
	}


	/**
	 * @param array $stanza
	 * @return Stanza|null
	 * @throws TerminateException
	 */
	public function handle(array $stanza)
	{
		$this->to = $this->getAttribute($stanza, 'to');

		// if in debug mode we show the own username in the roster for testing
		$debugMode = $this->config->getSystemValue("debug");

		if ($stanza['value'][0]['name'] === '{http://jabber.org/protocol/disco#items}query' || $stanza['value'][0]['name'] === '{http://jabber.org/protocol/disco#info}query') {
			// the disco queries are currently not implemented but these are the first stanzas send to the server so
			// they are ideal to terminate the connection if a user is excluded from chatting.
			if ($this->userProvider->isUserExcluded($this->userId)) {
				throw new TerminateException();
			}
		} elseif ($stanza['value'][0]['name'] === '{jabber:iq:roster}query') {
			$id = $stanza['attributes']['id'];
			$iqRoster = new IQRoster();
			$iqRoster->setType('result');
			$iqRoster->setTo($this->userId);
			$iqRoster->setQid($id);
			foreach ($this->userProvider->getAllUsers() as $user) {
				if ($debugMode || $user->getUID() !== $this->userId) {
					$iqRoster->addItem($user->getUID() . '@' . $this->host, $user->getFullName());
				}
			}
			return $iqRoster;
		} elseif ($stanza['value'][0]['name'] === '{http://jabber.org/protocol/pubsub}pubsub') {
			$id = $stanza['attributes']['id'];
			$from = \array_key_exists('from', $stanza['attributes']) ? $stanza['attributes']['from'] : $this->userId;

			$iq = new IQNotImplemented();
			$iq->setTo($from);
			$iq->setQid($id);

			return $iq;
		}
	}
}