aboutsummaryrefslogtreecommitdiffstats
path: root/Zotlabs/Zot6/Zot6Handler.php
blob: d95009784d8984eede12205b087c074ec084b5cd (plain) (blame)
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
251
252
253
254
255
256
257
258
<?php

namespace Zotlabs\Zot6;

use Zotlabs\Lib\Libzot;
use Zotlabs\Lib\Queue;

class Zot6Handler implements IHandler {

	function Notify($data,$hub) {
		return self::reply_notify($data,$hub);
	}

	function Request($data,$hub) {
		return self::reply_message_request($data,$hub);
	}

	function Rekey($sender,$data,$hub) {
		return self::reply_rekey_request($sender,$data,$hub);
	}

	function Refresh($sender,$recipients,$hub) {
		return self::reply_refresh($sender,$recipients,$hub);
	}

	function Purge($sender,$recipients,$hub) {
		return self::reply_purge($sender,$recipients,$hub);
	}


	// Implementation of specific methods follows;
	// These generally do a small amout of validation and call Libzot
	// to do any heavy lifting

	static function reply_notify($data,$hub) {

		$ret = [ 'success' => false ];

		logger('notify received from ' . $hub['hubloc_url']);

		$x = Libzot::fetch($data);
		$ret['delivery_report'] = $x;


		$ret['success'] = true;
		return $ret;
	}



	/**
	 * @brief Remote channel info (such as permissions or photo or something)
	 * has been updated. Grab a fresh copy and sync it.
	 *
	 * The difference between refresh and force_refresh is that force_refresh
	 * unconditionally creates a directory update record, even if no changes were
	 * detected upon processing.
	 *
	 * @param array $sender
	 * @param array $recipients
	 * @param array $hub
	 *
	 * @return array
	 */
	static function reply_refresh($sender, $recipients, $hub) {
		$ret = array('success' => false);

		if($recipients) {

			// This would be a permissions update, typically for one connection

			foreach ($recipients as $recip) {
				$r = q("select channel.*,xchan.* from channel
					left join xchan on channel_hash = xchan_hash
					where xchan_hash ='%s' limit 1",
					dbesc($recip)
				);
				/// @FIXME $msgtype is undefined
				$x = Libzot::refresh( [ 'hubloc_id_url' => $hub['hubloc_id_url'] ], $r[0], (($msgtype === 'force_refresh') ? true : false));
			}
		}
		else {
			// system wide refresh
			/// @FIXME $msgtype is undefined
			$x = Libzot::refresh( [ 'hubloc_id_url' => $hub['hubloc_id_url'] ], null, (($msgtype === 'force_refresh') ? true : false));
		}

		$ret['success'] = true;
		return $ret;
	}



	/**
	 * @brief Process a message request.
	 *
	 * If a site receives a comment to a post but finds they have no parent to attach it with, they
	 * may send a 'request' packet containing the message_id of the missing parent. This is the handler
	 * for that packet. We will create a message_list array of the entire conversation starting with
	 * the missing parent and invoke delivery to the sender of the packet.
	 *
	 * Zotlabs/Daemon/Deliver.php (for local delivery) and
	 * mod/post.php???? @fixme (for web delivery) detect the existence of
	 * this 'message_list' at the destination and split it into individual messages which are
	 * processed/delivered in order.
	 *
	 * @param array $data
	 * @param array $hub
	 * @return array
	 */
	static function reply_message_request($data, $hub) {
		$ret = [ 'success' => false ];

		$message_id = EMPTY_STR;

		if(array_key_exists('data',$data))
		$ptr = $data['data'];
		if(is_array($ptr) && array_key_exists(0,$ptr)) {
			$ptr = $ptr[0];
		}
		if(is_string($ptr)) {
			$message_id = $ptr;
		}
		if(is_array($ptr) && array_key_exists('id',$ptr)) {
			$message_id = $ptr['id'];
		}

		if (! $message_id) {
			$ret['message'] = 'no message_id';
			logger('no message_id');
			return $ret;
		}

		$sender = $hub['hubloc_hash'];

		/*
		 * Find the local channel in charge of this post (the first and only recipient of the request packet)
		 */

		$arr = $data['recipients'][0];

		$c = q("select * from channel left join xchan on channel_hash = xchan_hash where channel_hash = '%s' limit 1",
			dbesc($arr['portable_id'])
		);
		if (! $c) {
			logger('recipient channel not found.');
			$ret['message'] .= 'recipient not found.' . EOL;
			return $ret;
		}

		/*
		 * fetch the requested conversation
		 */
		$messages = zot_feed($c[0]['channel_id'], $sender, [ 'message_id' => $data['message_id'], 'encoding' => 'activitystreams' ]);

		return (($messages) ? : [] );
	}

	static function rekey_request($sender,$data,$hub) {

		$ret = array('success' => false);

		//	newsig is newkey signed with oldkey

		// The original xchan will remain. In Zot/Receiver we will have imported the new xchan and hubloc to verify
		// the packet authenticity. What we will do now is verify that the keychange operation was signed by the
		// oldkey, and if so change all the abook, abconfig, group, and permission elements which reference the
		// old xchan_hash.

		if((! $data['old_key']) && (! $data['new_key']) && (! $data['new_sig']))
			return $ret;


		$old = null;

		if(Libzot::verify($data['old_guid'],$data['old_guid_sig'],$data['old_key'])) {
			$oldhash = Libzot::make_xchan_hash($data['old_guid'],$data['old_key']);
			$old = q("select * from xchan where xchan_hash = '%s' limit 1",
				dbesc($oldhash)
			);
		}
		else
			return $ret;


		if(! $old) {
			return $ret;
		}

		$xchan = $old[0];

		if(! Libzot::verify($data['new_key'],$data['new_sig'],$xchan['xchan_pubkey'])) {
			return $ret;
		}

		$r = q("select * from xchan where xchan_hash = '%s' limit 1",
			dbesc($sender)
		);

		$newxchan = $r[0];

		// @todo
		// if ! $update create a linked identity


		xchan_change_key($xchan,$newxchan,$data);

		$ret['success'] = true;
		return $ret;
	}


	/**
	 * @brief
	 *
	 * @param array $sender
	 * @param array $recipients
	 * @param array $hub
	 *
	 * @return array
	 */
	static function reply_purge($sender, $recipients, $hub) {

		$ret = array('success' => false);

		if ($recipients) {
			// basically this means "unfriend"
			foreach ($recipients as $recip) {
				$channel = q("select channel.*,xchan.* from channel
					left join xchan on channel_hash = xchan_hash
					where channel_hash = '%s' limit 1",
					dbesc($recip)
				);
				if ($channel) {
					$abook = q("select abook_id from abook where abook_channel = %d and abook_xchan = '%s' limit 1",
						intval($channel[0]['channel_id']),
						dbesc($sender)
					);
					if ($abook) {
						contact_remove($channel[0]['channel_id'],$abook[0]['abook_id']);
					}
				}
			}
			$ret['success'] = true;
		}
		else {

			// Unfriend everybody - basically this means the channel has committed suicide

			remove_all_xchan_resources($sender);

			$ret['success'] = true;
		}

		return $ret;
	}

}