+function dfrn_dispatch_public($postdata)
+{
+ $msg = Diaspora::decodeRaw([], $postdata, true);
+ if (!$msg) {
+ // We have to fail silently to be able to hand it over to the salmon parser
+ return false;
+ }
+
+ // Fetch the corresponding public contact
+ $contact = Contact::getDetailsByAddr($msg['author'], 0);
+ if (!$contact) {
+ Logger::log('Contact not found for address ' . $msg['author']);
+ System::xmlExit(3, 'Contact ' . $msg['author'] . ' not found');
+ }
+
+ $importer = DFRN::getImporter($contact['id']);
+
+ // This should never fail
+ if (empty($importer)) {
+ Logger::log('Contact not found for address ' . $msg['author']);
+ System::xmlExit(3, 'Contact ' . $msg['author'] . ' not found');
+ }
+
+ Logger::log('Importing post from ' . $msg['author'] . ' with the public envelope.', Logger::DEBUG);
+
+ // Now we should be able to import it
+ $ret = DFRN::import($msg['message'], $importer);
+ System::xmlExit($ret, 'Done');
+}
+
+function dfrn_dispatch_private($user, $postdata)
+{
+ $msg = Diaspora::decodeRaw($user, $postdata);
+ if (!$msg) {
+ System::xmlExit(4, 'Unable to parse message');
+ }
+
+ // Check if the user has got this contact
+ $cid = Contact::getIdForURL($msg['author'], $user['uid']);
+ if (!$cid) {
+ // Otherwise there should be a public contact
+ $cid = Contact::getIdForURL($msg['author']);
+ if (!$cid) {
+ Logger::log('Contact not found for address ' . $msg['author']);
+ System::xmlExit(3, 'Contact ' . $msg['author'] . ' not found');
+ }
+ }
+
+ $importer = DFRN::getImporter($cid, $user['uid']);
+
+ // This should never fail
+ if (empty($importer)) {
+ Logger::log('Contact not found for address ' . $msg['author']);
+ System::xmlExit(3, 'Contact ' . $msg['author'] . ' not found');
+ }
+
+ Logger::log('Importing post from ' . $msg['author'] . ' to ' . $user['nickname'] . ' with the private envelope.', Logger::DEBUG);
+
+ // Now we should be able to import it
+ $ret = DFRN::import($msg['message'], $importer);
+ System::xmlExit($ret, 'Done');
+}