mail changes

This commit is contained in:
Friendika 2011-04-16 08:45:08 -07:00
parent c7bcdf0c18
commit 863ddf1676
6 changed files with 137 additions and 43 deletions

View file

@ -513,7 +513,7 @@ CREATE TABLE IF NOT EXISTS `mailacct` (
`ssltype` CHAR( 16 ) NOT NULL, `ssltype` CHAR( 16 ) NOT NULL,
`mailbox` CHAR( 255 ) NOT NULL, `mailbox` CHAR( 255 ) NOT NULL,
`user` CHAR( 255 ) NOT NULL , `user` CHAR( 255 ) NOT NULL ,
`pass` CHAR( 255 ) NOT NULL , `pass` TEXT NOT NULL ,
`reply_to` CHAR( 255 ) NOT NULL , `reply_to` CHAR( 255 ) NOT NULL ,
`last_check` DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00' `last_check` DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00'
) ENGINE = MYISAM DEFAULT CHARSET=utf8; ) ENGINE = MYISAM DEFAULT CHARSET=utf8;

View file

@ -264,6 +264,7 @@ function scrape_feed($url) {
function probe_url($url) { function probe_url($url) {
require_once('include/email.php');
$result = array(); $result = array();
@ -313,8 +314,38 @@ function probe_url($url) {
} }
} }
else { else {
// Check email
$orig_url = $url;
if((strpos($orig_url,'@')) && validate_email($orig_url)) { if((strpos($orig_url,'@')) && validate_email($orig_url)) {
$email_conversant = true; $x = q("SELECT `prvkey` FROM `user` WHERE `uid` = %d LIMIT 1",
intval(local_user())
);
$r = q("SELECT * FROM `mailacct` WHERE `uid` = %d LIMIT 1",
intval(local_user())
);
if(count($x) && count($r)) {
$mailbox = construct_mailbox_name($r[0]);
$password = '';
openssl_private_decrypt(hex2bin($r[0]['pass']),$password,$x[0]['prvkey']);
$mbox = email_connect($mailbox,$r[0]['user'],$password);
unset($password);
}
if($mbox) {
$msgs = email_poll($mbox,$orig_url);
if(count($msgs)) {
$addr = $orig_url;
$network = NETWORK_MAIL;
$name = substr($url,0,strpos($url,'@'));
// fix nick
$vcard = array('fn' => $name, 'nick' => $name, 'photo' => gravatar_img($url));
$notify = 'smtp';
$poll = 'email';
$priority = 0;
}
imap_close($mbox);
}
} }
} }
} }
@ -330,7 +361,7 @@ function probe_url($url) {
} }
} }
if($network !== NETWORK_DFRN) { if($network !== NETWORK_DFRN && $network !== NETWORK_MAIL) {
$network = NETWORK_OSTATUS; $network = NETWORK_OSTATUS;
$priority = 0; $priority = 0;
@ -435,9 +466,11 @@ function probe_url($url) {
$vcard['fn'] = notags($vcard['fn']); $vcard['fn'] = notags($vcard['fn']);
$vcard['nick'] = notags($vcard['nick']); $vcard['nick'] = notags($vcard['nick']);
$result['name'] = $vcard['fn']; $result['name'] = $vcard['fn'];
$result['nick'] = $vcard['nick']; $result['nick'] = $vcard['nick'];
$result['url'] = $profile; $result['url'] = $profile;
$result['addr'] = $addr;
$result['notify'] = $notify; $result['notify'] = $notify;
$result['poll'] = $poll; $result['poll'] = $poll;
$result['request'] = $request; $result['request'] = $request;

View file

@ -1,7 +1,7 @@
<?php <?php
function email_connect($mailbox,$username,$password) { function email_connect($mailbox,$username,$password) {
if(! (local_user() && function_exists('imap_open'))) if(! function_exists('imap_open'))
return false; return false;
$mbox = imap_open($mailbox,$username,$password); $mbox = imap_open($mailbox,$username,$password);
@ -33,36 +33,41 @@ function email_msg_meta($mbox,$uid) {
} }
function email_get_msg($mbox,$uid) {
$ret = array();
function getmsg($mbox,$mid) { $struc = (($mbox && $uid) ? imap_fetchstructure($mbox,$uid,FT_UID) : null);
// input $mbox = IMAP stream, $mid = message id
// output all the following:
global $charset,$htmlmsg,$plainmsg,$attachments;
$htmlmsg = $plainmsg = $charset = '';
$attachments = array();
// HEADER if(! $struc)
$h = imap_header($mbox,$mid); return $ret;
// add code here to get date, from, to, cc, subject...
// BODY if(! $struc->parts) {
$s = imap_fetchstructure($mbox,$mid); $ret['body'] = email_get_part($mbox,$uid,$struc,0);
if (!$s->parts) // simple }
getpart($mbox,$mid,$s,0); // pass 0 as part-number else {
else { // multipart: cycle through each part foreach($struc->parts as $ptop => $p) {
foreach ($s->parts as $partno0=>$p) $x = email_get_part($mbox,$uid,$p,$ptop + 1);
getpart($mbox,$mid,$p,$partno0+1); if($x)
} $ret['body'] = $x;
}
}
return $ret;
} }
function getpart($mbox,$mid,$p,$partno) { // At the moment - only return plain/text.
// Later we'll repackage inline images as data url's and make the HTML safe
function email_get_part($mbox,$uid,$p,$partno) {
// $partno = '1', '2', '2.1', '2.1.3', etc for multipart, 0 if simple // $partno = '1', '2', '2.1', '2.1.3', etc for multipart, 0 if simple
global $htmlmsg,$plainmsg,$charset,$attachments; global $htmlmsg,$plainmsg,$charset,$attachments;
echo $partno;
// DECODE DATA // DECODE DATA
$data = ($partno)? $data = ($partno)
imap_fetchbody($mbox,$mid,$partno): // multipart ? imap_fetchbody($mbox,$uid,$partno, FT_UID|FT_PEEK)
imap_body($mbox,$mid); // simple : imap_body($mbox,$uid,FT_UID|FT_PEEK);
// Any part may be encoded, even plain text messages, so check everything. // Any part may be encoded, even plain text messages, so check everything.
if ($p->encoding==4) if ($p->encoding==4)
$data = quoted_printable_decode($data); $data = quoted_printable_decode($data);
@ -82,6 +87,7 @@ function getpart($mbox,$mid,$p,$partno) {
// ATTACHMENT // ATTACHMENT
// Any part with a filename is an attachment, // Any part with a filename is an attachment,
// so an attached text file (type 0) is not mistaken as the message. // so an attached text file (type 0) is not mistaken as the message.
if ($params['filename'] || $params['name']) { if ($params['filename'] || $params['name']) {
// filename may be given as 'Filename' or 'Name' or both // filename may be given as 'Filename' or 'Name' or both
$filename = ($params['filename'])? $params['filename'] : $params['name']; $filename = ($params['filename'])? $params['filename'] : $params['name'];
@ -90,13 +96,15 @@ function getpart($mbox,$mid,$p,$partno) {
} }
// TEXT // TEXT
if ($p->type==0 && $data) { if ($p->type == 0 && $data) {
// Messages may be split in different parts because of inline attachments, // Messages may be split in different parts because of inline attachments,
// so append parts together with blank row. // so append parts together with blank row.
if (strtolower($p->subtype)=='plain') if (strtolower($p->subtype)=='plain')
$plainmsg .= trim($data) ."\n\n"; return (trim($data) ."\n\n");
else else
$htmlmsg .= $data ."<br><br>"; $data = '';
// $htmlmsg .= $data ."<br><br>";
$charset = $params['charset']; // assume all parts are same charset $charset = $params['charset']; // assume all parts are same charset
} }
@ -105,13 +113,19 @@ function getpart($mbox,$mid,$p,$partno) {
// but AOL uses type 1 (multipart), which is not handled here. // but AOL uses type 1 (multipart), which is not handled here.
// There are no PHP functions to parse embedded messages, // There are no PHP functions to parse embedded messages,
// so this just appends the raw source to the main message. // so this just appends the raw source to the main message.
elseif ($p->type==2 && $data) { // elseif ($p->type==2 && $data) {
$plainmsg .= $data."\n\n"; // $plainmsg .= $data."\n\n";
} // }
// SUBPART RECURSION // SUBPART RECURSION
if ($p->parts) { if ($p->parts) {
foreach ($p->parts as $partno0=>$p2) foreach ($p->parts as $partno0=>$p2) {
getpart($mbox,$mid,$p2,$partno.'.'.($partno0+1)); // 1.2, 1.2.1, etc. $x = email_get_part($mbox,$uid,$p2,$partno . '.' . ($partno0+1)); // 1.2, 1.2.1, etc.
if($x)
return $x;
}
} }
} }

View file

@ -289,7 +289,7 @@ function poller_run($argv, $argc){
} }
elseif($contact['network'] === NETWORK_MAIL) { elseif($contact['network'] === NETWORK_MAIL) {
if(! $mbox) { if(! $mbox) {
$x = q("SELECT `prvkey` FROM `user` WHERE `uid` = $d LIMIT 1", $x = q("SELECT `prvkey` FROM `user` WHERE `uid` = %d LIMIT 1",
intval($importer_uid) intval($importer_uid)
); );
$r = q("SELECT * FROM `mailacct` WHERE `uid` = %d LIMIT 1", $r = q("SELECT * FROM `mailacct` WHERE `uid` = %d LIMIT 1",
@ -298,7 +298,7 @@ function poller_run($argv, $argc){
if(count($x) && count($r)) { if(count($x) && count($r)) {
$mailbox = construct_mailbox_name($r[0]); $mailbox = construct_mailbox_name($r[0]);
$password = ''; $password = '';
openssl_private_decrypt($r[0]['pass'],$password,$x[0]['prvkey']); openssl_private_decrypt(hex2bin($r[0]['pass']),$password,$x[0]['prvkey']);
$mbox = email_connect($mailbox,$r[0]['user'],$password); $mbox = email_connect($mailbox,$r[0]['user'],$password);
unset($password); unset($password);
} }
@ -306,7 +306,42 @@ function poller_run($argv, $argc){
if($mbox) { if($mbox) {
$msgs = email_poll($mbox,$contact['addr']); $msgs = email_poll($mbox,$contact['addr']);
if(count($msgs)) { if(count($msgs)) {
// TODO: loop through, fetch, check duplicates, and import foreach($msgs as $msg_uid) {
$datarray = array();
$meta = email_msg_meta($mbox,$msg_uid);
$datarray['uri'] = trim($meta->message_id,'<>');
//FIXME
$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),
dbesc($datarray['uri'])
);
if(count($r)) {
if($meta->deleted && ! $r[0]['deleted']) {
q("UPDATE `item` SET `deleted` = `, `changed` = '%s' WHERE `id` = %d LIMIT 1",
dbesc(datetime_convert()),
intval($r[0]['id'])
);
}
continue;
}
$datarray['title'] = notags(trim($meta->subject));
$datarray['created'] = datetime_convert('UTC','UTC',$meta->date);
$r = email_get_msg($mbox,$msg_uid);
if(! $r)
continue;
$datarray['body'] = escape_tags($r['body']);
$datarray['uid'] = $importer_uid;
$datarray['contact-id'] = $contact['id'];
$datarray['private'] = 1;
$datarray['author-name'] = $contact['name'];
$datarray['author-link'] = 'mailbox';
$datarray['author-avatar'] = $contact['photo'];
item_store($datarray);
}
} }
} }
} }
@ -359,6 +394,9 @@ function poller_run($argv, $argc){
// loop - next contact // loop - next contact
} }
} }
if($mbox && function_exists('imap_close'))
imap_close($mbox);
return; return;
} }

View file

@ -28,7 +28,7 @@ function follow_post(&$a) {
// do we have enough information? // do we have enough information?
if(! ((x($ret,'name')) && (x($ret,'poll')) && (x($ret,'url')))) { if(! ((x($ret,'name')) && (x($ret,'poll')) && ((x($ret,'url')) || (x($ret,'addr'))))) {
notice( t('The profile address specified does not provide adequate information.') . EOL); notice( t('The profile address specified does not provide adequate information.') . EOL);
goaway($_SESSION['return_url']); goaway($_SESSION['return_url']);
} }
@ -39,7 +39,10 @@ function follow_post(&$a) {
} }
$writeable = ((($ret['network'] === NETWORK_OSTATUS) && ($ret['notify'])) ? 1 : 0); $writeable = ((($ret['network'] === NETWORK_OSTATUS) && ($ret['notify'])) ? 1 : 0);
if($ret['network'] === NETWORK_MAIL) {
$writeable = 1;
}
// check if we already have a contact // check if we already have a contact
// the poll url is more reliable than the profile url, as we may have // the poll url is more reliable than the profile url, as we may have
// indirect links or webfinger links // indirect links or webfinger links
@ -61,12 +64,13 @@ function follow_post(&$a) {
} }
else { else {
// create contact record // create contact record
$r = q("INSERT INTO `contact` ( `uid`, `created`, `url`, `alias`, `notify`, `poll`, `name`, `nick`, `photo`, `network`, `rel`, `priority`, $r = q("INSERT INTO `contact` ( `uid`, `created`, `url`, `addr`, `alias`, `notify`, `poll`, `name`, `nick`, `photo`, `network`, `rel`, `priority`,
`writable`, `blocked`, `readonly`, `pending` ) `writable`, `blocked`, `readonly`, `pending` )
VALUES ( %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, %d, 0, 0, 0 ) ", VALUES ( %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, %d, 0, 0, 0 ) ",
intval(local_user()), intval(local_user()),
dbesc(datetime_convert()), dbesc(datetime_convert()),
dbesc($ret['url']), dbesc($ret['url']),
dbesc($ret['addr']),
dbesc($ret['alias']), dbesc($ret['alias']),
dbesc($ret['notify']), dbesc($ret['notify']),
dbesc($ret['poll']), dbesc($ret['poll']),
@ -74,9 +78,9 @@ function follow_post(&$a) {
dbesc($ret['nick']), dbesc($ret['nick']),
dbesc($ret['photo']), dbesc($ret['photo']),
dbesc($ret['network']), dbesc($ret['network']),
intval(REL_FAN), intval(($ret['network'] === NETWORK_MAIL) ? REL_BUD : REL_FAN),
intval($ret['priority']), intval($ret['priority']),
intval($writable) intval($writeable)
); );
} }

View file

@ -469,3 +469,8 @@ function update_1051() {
q("ALTER TABLE `contact` ADD `addr` CHAR( 255 ) NOT NULL AFTER `url` "); q("ALTER TABLE `contact` ADD `addr` CHAR( 255 ) NOT NULL AFTER `url` ");
} }
function update_1052() {
q("ALTER TABLE `mailacct` CHANGE `pass` `pass` TEXT CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL");
}