Polling functions are split into several functions

This commit is contained in:
Michael 2019-06-27 20:38:09 +00:00
parent 7e7acf5763
commit d552db5c55

View file

@ -11,7 +11,6 @@ use Friendica\Core\Logger;
use Friendica\Core\PConfig;
use Friendica\Core\Protocol;
use Friendica\Database\DBA;
use Friendica\Model\APContact;
use Friendica\Model\Contact;
use Friendica\Model\Item;
use Friendica\Model\User;
@ -133,7 +132,117 @@ class OnePoll
Logger::log("poll: ({$protocol}-{$contact['id']}) IMPORTER: {$importer['name']}, CONTACT: {$contact['name']}");
$xml = '';
if ($protocol === Protocol::DFRN) {
$xml = self::pollDFRN($contact, $updated);
} elseif (($protocol === Protocol::OSTATUS)
|| ($protocol === Protocol::DIASPORA)
|| ($protocol === Protocol::FEED)) {
$xml = self::pollFeed($contact, $protocol, $updated);
} elseif ($protocol === Protocol::MAIL) {
self::pollMail($contact, $importer_uid, $updated);
}
if (!empty($xml)) {
Logger::log('received xml : ' . $xml, Logger::DATA);
if (!strstr($xml, '<')) {
Logger::log('post_handshake: response from ' . $url . ' did not contain XML.');
$fields = ['last-update' => $updated, 'failure_update' => $updated];
self::updateContact($contact, $fields);
Contact::markForArchival($contact);
return;
}
Logger::log("Consume feed of contact ".$contact['id']);
consume_feed($xml, $importer, $contact, $hub);
// do it a second time for DFRN so that any children find their parents.
if ($protocol === Protocol::DFRN) {
consume_feed($xml, $importer, $contact, $hub);
}
$hubmode = 'subscribe';
if ($protocol === Protocol::DFRN || $contact['blocked']) {
$hubmode = 'unsubscribe';
}
if (($protocol === Protocol::OSTATUS || $protocol == Protocol::FEED) && (! $contact['hub-verify'])) {
$hub_update = true;
}
if ($force) {
$hub_update = true;
}
Logger::log("Contact ".$contact['id']." returned hub: ".$hub." Network: ".$protocol." Relation: ".$contact['rel']." Update: ".$hub_update);
if (strlen($hub) && $hub_update && (($contact['rel'] != Contact::FOLLOWER) || $protocol == Protocol::FEED)) {
Logger::log('hub ' . $hubmode . ' : ' . $hub . ' contact name : ' . $contact['name'] . ' local user : ' . $importer['name']);
$hubs = explode(',', $hub);
if (count($hubs)) {
foreach ($hubs as $h) {
$h = trim($h);
if (!strlen($h)) {
continue;
}
subscribe_to_hub($h, $importer, $contact, $hubmode);
}
}
}
self::updateContact($contact, ['last-update' => $updated, 'success_update' => $updated]);
DBA::update('gcontact', ['last_contact' => $updated], ['nurl' => $contact['nurl']]);
Contact::unmarkForArchival($contact);
} elseif (in_array($contact["network"], [Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS, Protocol::FEED])) {
self::updateContact($contact, ['last-update' => $updated, 'failure_update' => $updated]);
DBA::update('gcontact', ['last_failure' => $updated], ['nurl' => $contact['nurl']]);
Contact::markForArchival($contact);
} else {
self::updateContact($contact, ['last-update' => $updated]);
}
Logger::log('End');
return;
}
private static function RemoveReply($subject)
{
while (in_array(strtolower(substr($subject, 0, 3)), ["re:", "aw:"])) {
$subject = trim(substr($subject, 4));
}
return $subject;
}
/**
* @brief Updates a personal contact entry and the public contact entry
*
* @param array $contact The personal contact entry
* @param array $fields The fields that are updated
* @throws \Exception
*/
private static function updateContact(array $contact, array $fields)
{
DBA::update('contact', $fields, ['id' => $contact['id']]);
}
/**
* @brief Poll DFRN contacts
*
* @param array $contact The personal contact entry
* @param string $updated The updated date
* @return string polled XML
* @throws \Exception
*/
private static function pollDFRN(array $contact, $updated)
{
$idtosend = $orig_id = (($contact['dfrn-id']) ? $contact['dfrn-id'] : $contact['issued-id']);
if (intval($contact['duplex']) && $contact['dfrn-id']) {
$idtosend = '0:' . $orig_id;
@ -146,7 +255,6 @@ class OnePoll
$perm = 'rw';
// But this may be our first communication, so set the writable flag if it isn't set already.
if (!intval($contact['writable'])) {
$fields = ['writable' => true];
DBA::update('contact', $fields, ['id' => $contact['id']]);
@ -164,7 +272,7 @@ class OnePoll
self::updateContact($contact, ['last-update' => $updated]);
Contact::markForArchival($contact);
Logger::log('Contact archived');
return;
return false;
}
$handshake_xml = $curlResult->getBody();
@ -182,7 +290,7 @@ class OnePoll
$fields = ['last-update' => $updated, 'failure_update' => $updated];
self::updateContact($contact, $fields);
Contact::markForArchival($contact);
return;
return false;
}
if (!strstr($handshake_xml, '<')) {
@ -191,10 +299,9 @@ class OnePoll
$fields = ['last-update' => $updated, 'failure_update' => $updated];
self::updateContact($contact, $fields);
Contact::markForArchival($contact);
return;
return false;
}
$res = XML::parseString($handshake_xml);
if (intval($res->status) == 1) {
@ -213,7 +320,7 @@ class OnePoll
// set the last-update so we don't keep polling
DBA::update('contact', ['last-update' => $updated], ['id' => $contact['id']]);
Logger::log('Contact status is ' . $res->status);
return;
return false;
}
if (((float)$res->dfrn_version > 2.21) && ($contact['poco'] == '')) {
@ -246,7 +353,7 @@ class OnePoll
// Since we mostly don't use it anyway, we won't dig into it deeper, but simply ignore it.
if (empty($final_dfrn_id) || empty($orig_id)) {
Logger::log('Contact has got no ID - quitting');
return;
return false;
}
if ($final_dfrn_id != $orig_id) {
@ -256,19 +363,27 @@ class OnePoll
// set the last-update so we don't keep polling
DBA::update('contact', ['last-update' => $updated], ['id' => $contact['id']]);
Contact::markForArchival($contact);
return;
return false;
}
$postvars['dfrn_id'] = $idtosend;
$postvars['dfrn_version'] = DFRN_PROTOCOL_VERSION;
$postvars['perm'] = 'rw';
$xml = Network::post($contact['poll'], $postvars)->getBody();
} elseif (($protocol === Protocol::OSTATUS)
|| ($protocol === Protocol::DIASPORA)
|| ($protocol === Protocol::FEED)) {
return Network::post($contact['poll'], $postvars)->getBody();
}
/**
* @brief Poll Feed/OStatus contacts
*
* @param array $contact The personal contact entry
* @param string $protocol The used protocol of the contact
* @param string $updated The updated date
* @return string polled XML
* @throws \Exception
*/
private static function pollFeed(array $contact, $protocol, $updated)
{
// Upgrading DB fields from an older Friendica version
// Will only do this once per notify-enabled OStatus contact
// or if relationship changes
@ -290,7 +405,7 @@ class OnePoll
// set the last-update so we don't keep polling
DBA::update('contact', ['last-update' => $updated], ['id' => $contact['id']]);
Logger::log('Contact is blocked or only a follower');
return;
return false;
}
$cookiejar = tempnam(get_temppath(), 'cookiejar-onepoll-');
@ -302,12 +417,22 @@ class OnePoll
self::updateContact($contact, ['last-update' => $updated]);
Contact::markForArchival($contact);
Logger::log('Contact archived');
return;
return false;
}
$xml = $curlResult->getBody();
return $curlResult->getBody();
}
} elseif ($protocol === Protocol::MAIL) {
/**
* @brief Poll Mail contacts
*
* @param array $contact The personal contact entry
* @param integer $importer_uid The UID of the importer
* @param string $updated The updated date
* @throws \Exception
*/
private static function pollMail(array $contact, $importer_uid, $updated)
{
Logger::log("Mail: Fetching for ".$contact['addr'], Logger::DEBUG);
$mail_disabled = ((function_exists('imap_open') && !Config::get('system', 'imap_disabled')) ? 0 : 1);
@ -342,7 +467,10 @@ class OnePoll
}
}
if ($mbox) {
if (!$mbox) {
return;
}
$msgs = Email::poll($mbox, $contact['addr']);
if (count($msgs)) {
@ -403,7 +531,6 @@ class OnePoll
continue;
}
// look for a 'references' or an 'in-reply-to' header and try to match with a parent item we have locally.
$raw_refs = ((property_exists($meta, 'references')) ? str_replace("\t", '', $meta->references) : '');
if (!trim($raw_refs)) {
@ -507,7 +634,7 @@ class OnePoll
if ($datarray['parent-uri'] === $datarray['uri']) {
$datarray['private'] = 1;
}
if (($protocol === Protocol::MAIL) && !PConfig::get($importer_uid, 'system', 'allow_public_email_replies')) {
if (!PConfig::get($importer_uid, 'system', 'allow_public_email_replies')) {
$datarray['private'] = 1;
$datarray['allow_cid'] = '<' . $contact['id'] . '>';
}
@ -544,94 +671,3 @@ class OnePoll
imap_close($mbox);
}
}
if ($xml) {
Logger::log('received xml : ' . $xml, Logger::DATA);
if (!strstr($xml, '<')) {
Logger::log('post_handshake: response from ' . $url . ' did not contain XML.');
$fields = ['last-update' => $updated, 'failure_update' => $updated];
self::updateContact($contact, $fields);
Contact::markForArchival($contact);
return;
}
Logger::log("Consume feed of contact ".$contact['id']);
consume_feed($xml, $importer, $contact, $hub);
// do it a second time for DFRN so that any children find their parents.
if ($protocol === Protocol::DFRN) {
consume_feed($xml, $importer, $contact, $hub);
}
$hubmode = 'subscribe';
if ($protocol === Protocol::DFRN || $contact['blocked']) {
$hubmode = 'unsubscribe';
}
if (($protocol === Protocol::OSTATUS || $protocol == Protocol::FEED) && (! $contact['hub-verify'])) {
$hub_update = true;
}
if ($force) {
$hub_update = true;
}
Logger::log("Contact ".$contact['id']." returned hub: ".$hub." Network: ".$protocol." Relation: ".$contact['rel']." Update: ".$hub_update);
if (strlen($hub) && $hub_update && (($contact['rel'] != Contact::FOLLOWER) || $protocol == Protocol::FEED)) {
Logger::log('hub ' . $hubmode . ' : ' . $hub . ' contact name : ' . $contact['name'] . ' local user : ' . $importer['name']);
$hubs = explode(',', $hub);
if (count($hubs)) {
foreach ($hubs as $h) {
$h = trim($h);
if (!strlen($h)) {
continue;
}
subscribe_to_hub($h, $importer, $contact, $hubmode);
}
}
}
self::updateContact($contact, ['last-update' => $updated, 'success_update' => $updated]);
DBA::update('gcontact', ['last_contact' => $updated], ['nurl' => $contact['nurl']]);
Contact::unmarkForArchival($contact);
} elseif (in_array($contact["network"], [Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS, Protocol::FEED])) {
self::updateContact($contact, ['last-update' => $updated, 'failure_update' => $updated]);
DBA::update('gcontact', ['last_failure' => $updated], ['nurl' => $contact['nurl']]);
Contact::markForArchival($contact);
} else {
self::updateContact($contact, ['last-update' => $updated]);
}
Logger::log('End');
return;
}
private static function RemoveReply($subject)
{
while (in_array(strtolower(substr($subject, 0, 3)), ["re:", "aw:"])) {
$subject = trim(substr($subject, 4));
}
return $subject;
}
/**
* @brief Updates a personal contact entry and the public contact entry
*
* @param array $contact The personal contact entry
* @param array $fields The fields that are updated
* @throws \Exception
*/
private static function updateContact(array $contact, array $fields)
{
DBA::update('contact', $fields, ['id' => $contact['id']]);
// DBA::update('contact', $fields, ['uid' => 0, 'nurl' => $contact['nurl']]);
}
}