require_once("boot.php");
-function onepoll_run($argv, $argc){
+function onepoll_run(&$argv, &$argc){
global $a, $db;
if(is_null($a)) {
logger('onepoll: start');
- $abandon_days = intval(get_config('system','account_abandon_days'));
- if($abandon_days < 1)
- $abandon_days = 0;
-
-
$manual_id = 0;
$generation = 0;
$hub_update = false;
logger('onepoll: no contact');
return;
}
-
- if(was_recently_delayed($contact_id))
- return;
+
$d = datetime_convert();
// and which have a polling address and ignore Diaspora since
// we are unable to match those posts with a Diaspora GUID and prevent duplicates.
- $abandon_sql = (($abandon_days)
- ? sprintf(" AND `user`.`login_date` > UTC_TIMESTAMP() - INTERVAL %d DAY ", intval($abandon_days))
- : ''
- );
-
$contacts = q("SELECT `contact`.* FROM `contact`
WHERE ( `rel` = %d OR `rel` = %d ) AND `poll` != ''
AND NOT `network` IN ( '%s', '%s' )
$contact = $contacts[0];
-
$xml = false;
$t = $contact['last-update'];
if($contact['subhub']) {
- $interval = get_config('system','pushpoll_frequency');
- $contact['priority'] = (($interval !== false) ? intval($interval) : 3);
+ $poll_interval = get_config('system','pushpoll_frequency');
+ $contact['priority'] = (($poll_interval !== false) ? intval($poll_interval) : 3);
$hub_update = false;
if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 day"))
. '&perm=' . $perm ;
$handshake_xml = fetch_url($url);
+ $html_code = $a->get_curl_code();
logger('onepoll: handshake with url ' . $url . ' returns xml: ' . $handshake_xml, LOGGER_DATA);
- if(! $handshake_xml) {
+ if((! strlen($handshake_xml)) || ($html_code >= 400) || (! $html_code)) {
logger("poller: $url appears to be dead - marking for death ");
+
// dead connection - might be a transient event, or this might
// mean the software was uninstalled or the domain expired.
// Will keep trying for one month.
+
mark_for_death($contact);
// set the last-update so we don't keep polling
if(! strstr($handshake_xml,'<?xml')) {
logger('poller: response from ' . $url . ' did not contain XML.');
+
+ mark_for_death($contact);
+
$r = q("UPDATE `contact` SET `last-update` = '%s' WHERE `id` = %d LIMIT 1",
dbesc(datetime_convert()),
intval($contact['id'])
openssl_private_decrypt(hex2bin($mailconf[0]['pass']),$password,$x[0]['prvkey']);
$mbox = email_connect($mailbox,$mailconf[0]['user'],$password);
unset($password);
- logger("Mail: Connect");
+ logger("Mail: Connect to " . $mailconf[0]['user']);
if($mbox) {
q("UPDATE `mailacct` SET `last_check` = '%s' WHERE `id` = %d AND `uid` = %d LIMIT 1",
dbesc(datetime_convert()),
$msgs = email_poll($mbox,$contact['addr']);
if(count($msgs)) {
- logger("Mail: Parsing ".count($msgs)." mails.", LOGGER_DEBUG);
+ logger("Mail: Parsing ".count($msgs)." mails for ".$mailconf[0]['user'], LOGGER_DEBUG);
- foreach($msgs as $msg_uid) {
+ $metas = email_msg_meta($mbox,implode(',',$msgs));
+ if(count($metas) != count($msgs)) {
+ logger("onepoll: for " . $mailconf[0]['user'] . " there are ". count($msgs) . " messages but received " . count($metas) . " metas", LOGGER_DEBUG);
+ break;
+ }
+ $msgs = array_combine($msgs, $metas);
+
+ foreach($msgs as $msg_uid => $meta) {
logger("Mail: Parsing mail ".$msg_uid, LOGGER_DATA);
$datarray = array();
- $meta = email_msg_meta($mbox,$msg_uid);
- $headers = email_msg_headers($mbox,$msg_uid);
-
- // look for a 'references' header and try and match with a parent item we have locally.
+// $meta = email_msg_meta($mbox,$msg_uid);
+// $headers = email_msg_headers($mbox,$msg_uid);
- $raw_refs = ((x($headers,'references')) ? str_replace("\t",'',$headers['references']) : '');
$datarray['uri'] = msgid2iri(trim($meta->message_id,'<>'));
- if($raw_refs) {
- $refs_arr = explode(' ', $raw_refs);
- if(count($refs_arr)) {
- for($x = 0; $x < count($refs_arr); $x ++)
- $refs_arr[$x] = "'" . msgid2iri(str_replace(array('<','>',' '),array('','',''),dbesc($refs_arr[$x]))) . "'";
- }
- $qstr = implode(',',$refs_arr);
- $r = q("SELECT `uri` , `parent-uri` FROM `item` WHERE `uri` IN ( $qstr ) AND `uid` = %d LIMIT 1",
- intval($importer_uid)
- );
- if(count($r))
- $datarray['parent-uri'] = $r[0]['uri'];
- }
-
-
- if(! x($datarray,'parent-uri'))
- $datarray['parent-uri'] = $datarray['uri'];
-
// Have we seen it before?
$r = q("SELECT * FROM `item` WHERE `uid` = %d AND `uri` = '%s' LIMIT 1",
intval($importer_uid),
);
if(count($r)) {
-// logger("Mail: Seen before ".$msg_uid);
+ logger("Mail: Seen before ".$msg_uid." for ".$mailconf[0]['user'],LOGGER_DEBUG);
if($meta->deleted && ! $r[0]['deleted']) {
q("UPDATE `item` SET `deleted` = 1, `changed` = '%s' WHERE `id` = %d LIMIT 1",
dbesc(datetime_convert()),
intval($r[0]['id'])
);
}
- switch ($mailconf[0]['action']) {
+ /*switch ($mailconf[0]['action']) {
case 0:
+ logger("Mail: Seen before ".$msg_uid." for ".$mailconf[0]['user'].". Doing nothing.", LOGGER_DEBUG);
break;
case 1:
- logger("Mail: Deleting ".$msg_uid);
+ logger("Mail: Deleting ".$msg_uid." for ".$mailconf[0]['user']);
imap_delete($mbox, $msg_uid, FT_UID);
break;
case 2:
- logger("Mail: Mark as seen ".$msg_uid);
+ logger("Mail: Mark as seen ".$msg_uid." for ".$mailconf[0]['user']);
imap_setflag_full($mbox, $msg_uid, "\\Seen", ST_UID);
break;
case 3:
- logger("Mail: Moving ".$msg_uid." to ".$mailconf[0]['movetofolder']);
+ logger("Mail: Moving ".$msg_uid." to ".$mailconf[0]['movetofolder']." for ".$mailconf[0]['user']);
imap_setflag_full($mbox, $msg_uid, "\\Seen", ST_UID);
if ($mailconf[0]['movetofolder'] != "")
imap_mail_move($mbox, $msg_uid, $mailconf[0]['movetofolder'], FT_UID);
break;
- }
+ }*/
continue;
}
+
+ // look for a 'references' or an 'in-reply-to' header and try to match with a parent item we have locally.
+
+// $raw_refs = ((x($headers,'references')) ? str_replace("\t",'',$headers['references']) : '');
+ $raw_refs = ((property_exists($meta,'references')) ? str_replace("\t",'',$meta->references) : '');
+ if(! trim($raw_refs))
+ $raw_refs = ((property_exists($meta,'in_reply_to')) ? str_replace("\t",'',$meta->in_reply_to) : '');
+ $raw_refs = trim($raw_refs); // Don't allow a blank reference in $refs_arr
+
+ if($raw_refs) {
+ $refs_arr = explode(' ', $raw_refs);
+ if(count($refs_arr)) {
+ for($x = 0; $x < count($refs_arr); $x ++)
+ $refs_arr[$x] = "'" . msgid2iri(str_replace(array('<','>',' '),array('','',''),dbesc($refs_arr[$x]))) . "'";
+ }
+ $qstr = implode(',',$refs_arr);
+ $r = q("SELECT `uri` , `parent-uri` FROM `item` WHERE `uri` IN ( $qstr ) AND `uid` = %d LIMIT 1",
+ intval($importer_uid)
+ );
+ if(count($r))
+ $datarray['parent-uri'] = $r[0]['parent-uri']; // Set the parent as the top-level item
+// $datarray['parent-uri'] = $r[0]['uri'];
+ }
+
+
+ if(! x($datarray,'parent-uri'))
+ $datarray['parent-uri'] = $datarray['uri'];
+
// Decoding the header
$subject = imap_mime_header_decode($meta->subject);
$datarray['title'] = "";
$r = email_get_msg($mbox,$msg_uid, $reply);
if(! $r) {
- logger("Mail: can't fetch msg ".$msg_uid);
+ logger("Mail: can't fetch msg ".$msg_uid." for ".$mailconf[0]['user']);
continue;
}
$datarray['body'] = escape_tags($r['body']);
- logger("Mail: Importing ".$msg_uid);
+ logger("Mail: Importing ".$msg_uid." for ".$mailconf[0]['user']);
// some mailing lists have the original author as 'from' - add this sender info to msg body.
// todo: adding a gravatar for the original author would be cool
);
switch ($mailconf[0]['action']) {
case 0:
+ logger("Mail: Seen before ".$msg_uid." for ".$mailconf[0]['user'].". Doing nothing.", LOGGER_DEBUG);
break;
case 1:
- logger("Mail: Deleting ".$msg_uid);
+ logger("Mail: Deleting ".$msg_uid." for ".$mailconf[0]['user']);
imap_delete($mbox, $msg_uid, FT_UID);
break;
case 2:
- logger("Mail: Mark as seen ".$msg_uid);
+ logger("Mail: Mark as seen ".$msg_uid." for ".$mailconf[0]['user']);
imap_setflag_full($mbox, $msg_uid, "\\Seen", ST_UID);
break;
case 3:
- logger("Mail: Moving ".$msg_uid." to ".$mailconf[0]['movetofolder']);
+ logger("Mail: Moving ".$msg_uid." to ".$mailconf[0]['movetofolder']." for ".$mailconf[0]['user']);
imap_setflag_full($mbox, $msg_uid, "\\Seen", ST_UID);
if ($mailconf[0]['movetofolder'] != "")
imap_mail_move($mbox, $msg_uid, $mailconf[0]['movetofolder'], FT_UID);
if($xml) {
logger('poller: received xml : ' . $xml, LOGGER_DATA);
- if((! strstr($xml,'<?xml')) && (! strstr($xml,'<rss'))) {
+ if((! strstr($xml,'<?xml')) && (! strstr($xml,'<rss'))) {
logger('poller: post_handshake: response from ' . $url . ' did not contain XML.');
$r = q("UPDATE `contact` SET `last-update` = '%s' WHERE `id` = %d LIMIT 1",
dbesc(datetime_convert()),