From ce02cafd2d39df33b937f7528e838fbca4d634e3 Mon Sep 17 00:00:00 2001 From: Maxence Lange Date: Wed, 3 Jul 2019 23:01:02 -0100 Subject: [PATCH] 0.3.0 --- Makefile | 2 +- appinfo/info.xml | 5 +-- appinfo/routes.php | 4 +- composer.json | 4 +- composer.lock | 21 +++++----- lib/Command/Addresses.php | 16 ++++++++ lib/Controller/RemoteController.php | 21 +++++++--- lib/NextcloudMailCatcher.php | 64 +++++++++++++++++++++++------ lib/Service/MailService.php | 19 +++++++++ 9 files changed, 118 insertions(+), 38 deletions(-) diff --git a/Makefile b/Makefile index 70804f9..300eb60 100644 --- a/Makefile +++ b/Makefile @@ -10,7 +10,7 @@ cert_dir=$(HOME)/.nextcloud/certificates codecov_token_dir=$(HOME)/.nextcloud/codecov_token github_account=nextcloud branch=master -version+=0.2.0 +version+=0.3.0 all: appstore diff --git a/appinfo/info.xml b/appinfo/info.xml index 1cf3329..42904e0 100644 --- a/appinfo/info.xml +++ b/appinfo/info.xml @@ -11,7 +11,7 @@ ]]> - 0.2.0 + 0.3.0 agpl Maxence Lange Files_FromMail @@ -20,7 +20,6 @@ tools files - social https://github.com/nextcloud/files_frommail https://github.com/nextcloud/files_frommail/issues @@ -28,7 +27,7 @@ https://raw.githubusercontent.com/nextcloud/files_frommail/master/screenshots/v0.1.0.png - + diff --git a/appinfo/routes.php b/appinfo/routes.php index 35ffd3a..b6fcb99 100644 --- a/appinfo/routes.php +++ b/appinfo/routes.php @@ -26,8 +26,6 @@ return [ 'routes' => [ - ['name' => 'Remote#getContent', 'url' => '/remote/', 'verb' => 'PUT'] + ['name' => 'Remote#getContent', 'url' => '/remote', 'verb' => 'PUT'] ] ]; - - diff --git a/composer.json b/composer.json index cb43364..c153aad 100644 --- a/composer.json +++ b/composer.json @@ -2,7 +2,7 @@ "name": "nextcloud/files_frommail", "description": "Files From Mail", "minimum-stability": "stable", - "license": "proprietary", + "license": "agpl", "authors": [ { "name": "Maxence Lange", @@ -10,6 +10,6 @@ } ], "require": { - "php-mime-mail-parser/php-mime-mail-parser": "2.9.2" + "php-mime-mail-parser/php-mime-mail-parser": "3.0.4" } } diff --git a/composer.lock b/composer.lock index e233a0b..ad37105 100644 --- a/composer.lock +++ b/composer.lock @@ -4,35 +4,34 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "hash": "4f2c7dbe166d16f4fd62b38788e91db7", - "content-hash": "3063322984d481b8e479615f0c5d3e0c", + "content-hash": "766789d2d56090178ef7dc3605fc278d", "packages": [ { "name": "php-mime-mail-parser/php-mime-mail-parser", - "version": "2.9.2", + "version": "3.0.4", "source": { "type": "git", "url": "https://github.com/php-mime-mail-parser/php-mime-mail-parser.git", - "reference": "a3d5b16a4282e4afa5e4dd3bde7ce3213f929b9d" + "reference": "b1319a9899dd6f27edf425ef1e90cb6c1d7d2a0d" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/php-mime-mail-parser/php-mime-mail-parser/zipball/a3d5b16a4282e4afa5e4dd3bde7ce3213f929b9d", - "reference": "a3d5b16a4282e4afa5e4dd3bde7ce3213f929b9d", + "url": "https://api.github.com/repos/php-mime-mail-parser/php-mime-mail-parser/zipball/b1319a9899dd6f27edf425ef1e90cb6c1d7d2a0d", + "reference": "b1319a9899dd6f27edf425ef1e90cb6c1d7d2a0d", "shasum": "" }, "require": { "ext-mailparse": "*", - "php": "^5.4.0 || ^7.0" + "php": "^5.6.0 || ^7.0" }, "replace": { "exorus/php-mime-mail-parser": "*", "messaged/php-mime-mail-parser": "*" }, "require-dev": { + "php-coveralls/php-coveralls": "0.*", "phpunit/php-token-stream": "^1.3.0", - "phpunit/phpunit": "^4.0 || ^5.0", - "satooshi/php-coveralls": "0.*", + "phpunit/phpunit": "^5.7", "squizlabs/php_codesniffer": "2.*" }, "type": "library", @@ -77,7 +76,7 @@ "role": "Developer" } ], - "description": "Fully Tested Mailparse Extension Wrapper for PHP 5.4+", + "description": "Fully Tested Mailparse Extension Wrapper for PHP 5.6+", "homepage": "https://github.com/php-mime-mail-parser/php-mime-mail-parser", "keywords": [ "MimeMailParser", @@ -85,7 +84,7 @@ "mailparse", "mime" ], - "time": "2017-09-30 15:15:40" + "time": "2019-03-19T05:24:58+00:00" } ], "packages-dev": [], diff --git a/lib/Command/Addresses.php b/lib/Command/Addresses.php index 5c33bb8..f4c2a00 100644 --- a/lib/Command/Addresses.php +++ b/lib/Command/Addresses.php @@ -28,8 +28,11 @@ use Exception; use OC\Core\Command\Base; +use OCA\Files_FromMail\Exceptions\AddressAlreadyExistException; use OCA\Files_FromMail\Exceptions\FakeException; +use OCA\Files_FromMail\Exceptions\InvalidAddressException; use OCA\Files_FromMail\Exceptions\MissingArgumentException; +use OCA\Files_FromMail\Exceptions\UnknownAddressException; use OCA\Files_FromMail\Service\MailService; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; @@ -131,6 +134,9 @@ private function listMailAddresses(InputInterface $input, OutputInterface $outpu * @param InputInterface $input * * @throws FakeException + * @throws MissingArgumentException + * @throws AddressAlreadyExistException + * @throws InvalidAddressException */ private function addMailAddress(InputInterface $input) { if ($input->getOption('add') !== true) { @@ -148,6 +154,8 @@ private function addMailAddress(InputInterface $input) { * @param InputInterface $input * * @throws FakeException + * @throws MissingArgumentException + * @throws UnknownAddressException */ private function removeMailAddress(InputInterface $input) { if ($input->getOption('remove') !== true) { @@ -166,6 +174,8 @@ private function removeMailAddress(InputInterface $input) { * @param OutputInterface $output * * @throws FakeException + * @throws MissingArgumentException + * @throws UnknownAddressException */ private function setMailAddressPassword(InputInterface $input, OutputInterface $output) { if ($input->getOption('password') !== true) { @@ -186,6 +196,12 @@ private function setMailAddressPassword(InputInterface $input, OutputInterface $ } + /** + * @param InputInterface $input + * + * @return string|string[]|null + * @throws MissingArgumentException + */ private function checkMailAddress(InputInterface $input) { $mail = $input->getArgument('address'); if ($mail === null) { diff --git a/lib/Controller/RemoteController.php b/lib/Controller/RemoteController.php index f22adb1..3d11639 100644 --- a/lib/Controller/RemoteController.php +++ b/lib/Controller/RemoteController.php @@ -26,6 +26,7 @@ namespace OCA\Files_FromMail\Controller; +use Exception; use OCA\Files_FromMail\AppInfo\Application; use OCA\Files_FromMail\Service\MailService; use OCA\Files_FromMail\Service\MiscService; @@ -54,7 +55,8 @@ class RemoteController extends Controller { * @param MailService $mailService * @param MiscService $miscService */ - function __construct(IRequest $request, $userId, MailService $mailService, MiscService $miscService + function __construct( + IRequest $request, $userId, MailService $mailService, MiscService $miscService ) { parent::__construct(Application::APP_NAME, $request); $this->userId = $userId; @@ -75,11 +77,20 @@ function __construct(IRequest $request, $userId, MailService $mailService, MiscS * @return DataResponse */ public function getContent($content) { - $content = base64_decode(rawurldecode($content)); - $this->mailService->parseMail($content, $this->userId); - return new DataResponse(['ok'], Http::STATUS_CREATED); + try { + if ($content !== 'null') { + $content = base64_decode(rawurldecode($content)); + $this->mailService->parseMail($content, $this->userId); + } + + return new DataResponse(['ok'], Http::STATUS_CREATED); + } catch (Exception $e) { + $this->miscService->log('issue while getContent() : ' . $e->getMessage()); + + return new DataResponse(['error' => $e->getMessage()], Http::STATUS_CREATED); + } } -} \ No newline at end of file +} diff --git a/lib/NextcloudMailCatcher.php b/lib/NextcloudMailCatcher.php index f59f3a0..a005018 100644 --- a/lib/NextcloudMailCatcher.php +++ b/lib/NextcloudMailCatcher.php @@ -37,6 +37,7 @@ class NextcloudMailCatcher { + /** @var string */ private $content; @@ -50,15 +51,12 @@ class NextcloudMailCatcher { * @param array $config */ public function __construct($config) { - $nextcloud = $config['nextcloud']; if (substr($nextcloud, -1) === '/') { $config['nextcloud'] = substr($nextcloud, 0, -1); } - $this->config = $config; - $this->debug('Catching a new mail'); } @@ -89,9 +87,7 @@ public function sendToNextcloud() { $content = rawurlencode(base64_encode($this->getContent())); - $curl = $this->generateAuthedCurl( - $this->config['nextcloud'] . '/index.php/apps/files_frommail/remote/' - ); + $curl = $this->generateAuthedCurl(); $this->fillCurlWithContent($curl, 'content=' . $content); $result = curl_exec($curl); @@ -100,6 +96,32 @@ public function sendToNextcloud() { } + /** + * + */ + public function test() { + $this->config['debug'] = true; + $this->debug('testing!'); + + $pwd = $this->config['password']; + if (substr($pwd, 5, 1) !== '-') { + $this->debug(''); + $this->debug('Error: password have to be a generated token'); + $this->debug( + 'Generate a token on the webclient: Settings / Security / Devices & session' + ); + exit(); + } + + $curl = $this->generateAuthedCurl(); + $this->fillCurlWithContent($curl, 'content=null'); + + $result = curl_exec($curl); + + $this->debugCurl($curl, $result); + } + + /** * @param resource $curl * @param string|array $result @@ -123,6 +145,11 @@ private function debugCurl($curl, $result) { } + /** + * @param $curl + * + * @throws Exception + */ private function debugCurlResponseCode($curl) { $code = curl_getinfo($curl, CURLINFO_HTTP_CODE); @@ -147,13 +174,13 @@ private function debugCurlResponseCode($curl) { /** - * @param $url - * * @return resource */ - private function generateAuthedCurl($url) { + private function generateAuthedCurl() { + $url = $this->config['nextcloud'] . '/index.php/apps/files_frommail/remote'; $curl = curl_init($url); + curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); curl_setopt( @@ -162,7 +189,8 @@ private function generateAuthedCurl($url) { ); $this->debug( - 'Generate curl request to ' . $url . ' with username \'' . $this->config['username'] . '\'' + 'Generate curl request to ' . $url . ' with username \'' . $this->config['username'] + . '\'' ); return $curl; @@ -200,20 +228,30 @@ private function debug($string) { return; } - $log = '/tmp/' . basename(__FILE__, '.php') . '.log'; - file_put_contents($log, date('Y-m-d H:i:s') . ' ' . $string . "\n", FILE_APPEND); + echo $string . "\n"; +// $log = '/tmp/' . basename(__FILE__, '.php') . '.log'; +// file_put_contents($log, date('Y-m-d H:i:s') . ' ' . $string . "\n", FILE_APPEND); } } +$mailCatcher = new NextcloudMailCatcher($config); + +if (sizeof($argv) === 2 && $argv[1] === 'test') { + $mailCatcher->test(); + + return; +} + +echo 'Catching a new mail'; + $content = ''; $fd = fopen('php://stdin', 'r'); while (!feof($fd)) { $content .= fread($fd, 1024); } -$mailCatcher = new NextcloudMailCatcher($config); $mailCatcher->setContent($content); $mailCatcher->sendToNextcloud(); diff --git a/lib/Service/MailService.php b/lib/Service/MailService.php index 3f0fa84..fdee9be 100644 --- a/lib/Service/MailService.php +++ b/lib/Service/MailService.php @@ -35,6 +35,9 @@ use OCA\Files_FromMail\Exceptions\UnknownAddressException; use OCP\Files\FileInfo; use OCP\Files\Folder; +use OCP\Files\GenericFileException; +use OCP\Files\NotFoundException; +use OCP\Files\NotPermittedException; use PhpMimeMailParser\Attachment; use PhpMimeMailParser\Parser; @@ -95,6 +98,7 @@ public function parseMail($content, $userId) { try { $this->generateLocalContentFromMail($mail, $to, $data); } catch (Exception $e) { + $this->miscService->log('could not generate LocalContent from Mail - ' . $e->getMessage()); } $done[] = $to; @@ -106,6 +110,12 @@ public function parseMail($content, $userId) { * @param Parser $mail * @param string $to * @param array $data + * + * @throws AddressInfoException + * @throws GenericFileException + * @throws NotAFolderException + * @throws NotFoundException + * @throws NotPermittedException */ private function generateLocalContentFromMail(Parser $mail, $to, $data) { @@ -156,6 +166,8 @@ private function verifyInfoAndPassword($content, $toInfo) { * * @return Folder * @throws NotAFolderException + * @throws NotFoundException + * @throws NotPermittedException */ private function getMailFolder($userId, $to, $from) { @@ -199,6 +211,9 @@ private function parseMailHeaders(Parser $mail) { * @param string $id * @param Folder $folder * @param Attachment[] $attachments + * + * @throws GenericFileException + * @throws NotPermittedException */ private function createLocalFileFromAttachments($id, $folder, $attachments) { foreach ($attachments as $attachment) { @@ -216,6 +231,9 @@ private function createLocalFileFromAttachments($id, $folder, $attachments) { * @param string $id * @param string $filename * @param string $content + * + * @throws NotPermittedException + * @throws GenericFileException */ private function createLocalFile($folder, $id, $filename, $content) { $new = $folder->newFile($id . '-' . $this->count . '_' . $filename); @@ -278,6 +296,7 @@ public function removeMailAddress($address) { * @param $address * * @throws AddressAlreadyExistException + * @throws InvalidAddressException */ public function addMailAddress($address) { $this->hasToBeAValidMailAddress($address);