diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..f6400d9
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,4 @@
+
+\.idea/
+
+vendor/
diff --git a/CHANGELOG.md b/CHANGELOG.md
new file mode 100644
index 0000000..ad1d626
--- /dev/null
+++ b/CHANGELOG.md
@@ -0,0 +1,3 @@
+# Changelog
+All notable changes to this project will be documented in this file.
+
diff --git a/LICENSE b/LICENSE
index 9cecc1d..4ec8c3f 100644
--- a/LICENSE
+++ b/LICENSE
@@ -1,5 +1,5 @@
- GNU GENERAL PUBLIC LICENSE
- Version 3, 29 June 2007
+ GNU AFFERO GENERAL PUBLIC LICENSE
+ Version 3, 19 November 2007
Copyright (C) 2007 Free Software Foundation, Inc.
Everyone is permitted to copy and distribute verbatim copies
@@ -7,17 +7,15 @@
Preamble
- The GNU General Public License is a free, copyleft license for
-software and other kinds of works.
+ The GNU Affero General Public License is a free, copyleft license for
+software and other kinds of works, specifically designed to ensure
+cooperation with the community in the case of network server software.
The licenses for most software and other practical works are designed
to take away your freedom to share and change the works. By contrast,
-the GNU General Public License is intended to guarantee your freedom to
+our General Public Licenses are intended to guarantee your freedom to
share and change all versions of a program--to make sure it remains free
-software for all its users. We, the Free Software Foundation, use the
-GNU General Public License for most of our software; it applies also to
-any other work released this way by its authors. You can apply it to
-your programs, too.
+software for all its users.
When we speak of free software, we are referring to freedom, not
price. Our General Public Licenses are designed to make sure that you
@@ -26,44 +24,34 @@ them if you wish), that you receive source code or can get it if you
want it, that you can change the software or use pieces of it in new
free programs, and that you know you can do these things.
- To protect your rights, we need to prevent others from denying you
-these rights or asking you to surrender the rights. Therefore, you have
-certain responsibilities if you distribute copies of the software, or if
-you modify it: responsibilities to respect the freedom of others.
-
- For example, if you distribute copies of such a program, whether
-gratis or for a fee, you must pass on to the recipients the same
-freedoms that you received. You must make sure that they, too, receive
-or can get the source code. And you must show them these terms so they
-know their rights.
-
- Developers that use the GNU GPL protect your rights with two steps:
-(1) assert copyright on the software, and (2) offer you this License
-giving you legal permission to copy, distribute and/or modify it.
-
- For the developers' and authors' protection, the GPL clearly explains
-that there is no warranty for this free software. For both users' and
-authors' sake, the GPL requires that modified versions be marked as
-changed, so that their problems will not be attributed erroneously to
-authors of previous versions.
-
- Some devices are designed to deny users access to install or run
-modified versions of the software inside them, although the manufacturer
-can do so. This is fundamentally incompatible with the aim of
-protecting users' freedom to change the software. The systematic
-pattern of such abuse occurs in the area of products for individuals to
-use, which is precisely where it is most unacceptable. Therefore, we
-have designed this version of the GPL to prohibit the practice for those
-products. If such problems arise substantially in other domains, we
-stand ready to extend this provision to those domains in future versions
-of the GPL, as needed to protect the freedom of users.
-
- Finally, every program is threatened constantly by software patents.
-States should not allow patents to restrict development and use of
-software on general-purpose computers, but in those that do, we wish to
-avoid the special danger that patents applied to a free program could
-make it effectively proprietary. To prevent this, the GPL assures that
-patents cannot be used to render the program non-free.
+ Developers that use our General Public Licenses protect your rights
+with two steps: (1) assert copyright on the software, and (2) offer
+you this License which gives you legal permission to copy, distribute
+and/or modify the software.
+
+ A secondary benefit of defending all users' freedom is that
+improvements made in alternate versions of the program, if they
+receive widespread use, become available for other developers to
+incorporate. Many developers of free software are heartened and
+encouraged by the resulting cooperation. However, in the case of
+software used on network servers, this result may fail to come about.
+The GNU General Public License permits making a modified version and
+letting the public access it on a server without ever releasing its
+source code to the public.
+
+ The GNU Affero General Public License is designed specifically to
+ensure that, in such cases, the modified source code becomes available
+to the community. It requires the operator of a network server to
+provide the source code of the modified version running there to the
+users of that server. Therefore, public use of a modified version, on
+a publicly accessible server, gives the public access to the source
+code of the modified version.
+
+ An older license, called the Affero General Public License and
+published by Affero, was designed to accomplish similar goals. This is
+a different license, not a version of the Affero GPL, but Affero has
+released a new version of the Affero GPL which permits relicensing under
+this license.
The precise terms and conditions for copying, distribution and
modification follow.
@@ -72,7 +60,7 @@ modification follow.
0. Definitions.
- "This License" refers to version 3 of the GNU General Public License.
+ "This License" refers to version 3 of the GNU Affero General Public License.
"Copyright" also means copyright-like laws that apply to other kinds of
works, such as semiconductor masks.
@@ -549,35 +537,45 @@ to collect a royalty for further conveying from those to whom you convey
the Program, the only way you could satisfy both those terms and this
License would be to refrain entirely from conveying the Program.
- 13. Use with the GNU Affero General Public License.
+ 13. Remote Network Interaction; Use with the GNU General Public License.
+
+ Notwithstanding any other provision of this License, if you modify the
+Program, your modified version must prominently offer all users
+interacting with it remotely through a computer network (if your version
+supports such interaction) an opportunity to receive the Corresponding
+Source of your version by providing access to the Corresponding Source
+from a network server at no charge, through some standard or customary
+means of facilitating copying of software. This Corresponding Source
+shall include the Corresponding Source for any work covered by version 3
+of the GNU General Public License that is incorporated pursuant to the
+following paragraph.
Notwithstanding any other provision of this License, you have
permission to link or combine any covered work with a work licensed
-under version 3 of the GNU Affero General Public License into a single
+under version 3 of the GNU General Public License into a single
combined work, and to convey the resulting work. The terms of this
License will continue to apply to the part which is the covered work,
-but the special requirements of the GNU Affero General Public License,
-section 13, concerning interaction through a network will apply to the
-combination as such.
+but the work with which it is combined will remain governed by version
+3 of the GNU General Public License.
14. Revised Versions of this License.
The Free Software Foundation may publish revised and/or new versions of
-the GNU General Public License from time to time. Such new versions will
-be similar in spirit to the present version, but may differ in detail to
+the GNU Affero General Public License from time to time. Such new versions
+will be similar in spirit to the present version, but may differ in detail to
address new problems or concerns.
Each version is given a distinguishing version number. If the
-Program specifies that a certain numbered version of the GNU General
+Program specifies that a certain numbered version of the GNU Affero General
Public License "or any later version" applies to it, you have the
option of following the terms and conditions either of that numbered
version or of any later version published by the Free Software
Foundation. If the Program does not specify a version number of the
-GNU General Public License, you may choose any version ever published
+GNU Affero General Public License, you may choose any version ever published
by the Free Software Foundation.
If the Program specifies that a proxy can decide which future
-versions of the GNU General Public License can be used, that proxy's
+versions of the GNU Affero General Public License can be used, that proxy's
public statement of acceptance of a version permanently authorizes you
to choose that version for the Program.
@@ -619,56 +617,3 @@ Program, unless a warranty or assumption of liability accompanies a
copy of the Program in return for a fee.
END OF TERMS AND CONDITIONS
-
- How to Apply These Terms to Your New Programs
-
- If you develop a new program, and you want it to be of the greatest
-possible use to the public, the best way to achieve this is to make it
-free software which everyone can redistribute and change under these terms.
-
- To do so, attach the following notices to the program. It is safest
-to attach them to the start of each source file to most effectively
-state the exclusion of warranty; and each file should have at least
-the "copyright" line and a pointer to where the full notice is found.
-
- {one line to give the program's name and a brief idea of what it does.}
- Copyright (C) {year} {name of author}
-
- This program is free software: you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation, either version 3 of the License, or
- (at your option) any later version.
-
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
-
- You should have received a copy of the GNU General Public License
- along with this program. If not, see .
-
-Also add information on how to contact you by electronic and paper mail.
-
- If the program does terminal interaction, make it output a short
-notice like this when it starts in an interactive mode:
-
- {project} Copyright (C) {year} {fullname}
- This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
- This is free software, and you are welcome to redistribute it
- under certain conditions; type `show c' for details.
-
-The hypothetical commands `show w' and `show c' should show the appropriate
-parts of the General Public License. Of course, your program's commands
-might be different; for a GUI interface, you would use an "about box".
-
- You should also get your employer (if you work as a programmer) or school,
-if any, to sign a "copyright disclaimer" for the program, if necessary.
-For more information on this, and how to apply and follow the GNU GPL, see
-.
-
- The GNU General Public License does not permit incorporating your program
-into proprietary programs. If your program is a subroutine library, you
-may consider it more useful to permit linking proprietary applications with
-the library. If this is what you want to do, use the GNU Lesser General
-Public License instead of this License. But first, please read
-.
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..0a92c37
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,64 @@
+app_name=files_frommail
+
+project_dir=$(CURDIR)/../$(app_name)
+build_dir=$(CURDIR)/build/artifacts
+appstore_dir=$(build_dir)/appstore
+source_dir=$(build_dir)/source
+sign_dir=$(build_dir)/sign
+package_name=$(app_name)
+cert_dir=$(HOME)/.nextcloud/certificates
+codecov_token_dir=$(HOME)/.nextcloud/codecov_token
+version+=0.1.0
+
+all: appstore
+
+release: appstore create-tag
+
+create-tag:
+ git tag -s -a v$(version) -m "Tagging the $(version) release."
+ git push origin v$(version)
+
+clean:
+ rm -rf $(build_dir)
+ rm -rf node_modules
+
+composer:
+ composer install
+
+test: SHELL:=/bin/bash
+test: composer
+ phpunit --coverage-clover=coverage.xml --configuration=tests/phpunit.xml tests
+ @if [ -f $(codecov_token_dir)/$(app_name) ]; then \
+ bash <(curl -s https://codecov.io/bash) -t @$(codecov_token_dir)/$(app_name) ; \
+ fi
+
+
+appstore: composer clean
+ mkdir -p $(sign_dir)
+ rsync -a \
+ --exclude=/build \
+ --exclude=/docs \
+ --exclude=/l10n/templates \
+ --exclude=/l10n/.tx \
+ --exclude=/tests \
+ --exclude=/.git \
+ --exclude=/.github \
+ --exclude=/composer.json \
+ --exclude=/composer.lock \
+ --exclude=/l10n/l10n.pl \
+ --exclude=/CONTRIBUTING.md \
+ --exclude=/issue_template.md \
+ --exclude=/README.md \
+ --exclude=/.gitattributes \
+ --exclude=.gitignore \
+ --exclude=/.scrutinizer.yml \
+ --exclude=/.travis.yml \
+ --exclude=/Makefile \
+ --exclude=/vendor/picocms/pico/index.php \
+ $(project_dir)/ $(sign_dir)/$(app_name)
+ tar -czf $(build_dir)/$(app_name)-$(version).tar.gz \
+ -C $(sign_dir) $(app_name)
+ @if [ -f $(cert_dir)/$(app_name).key ]; then \
+ echo "Signing packageā¦"; \
+ openssl dgst -sha512 -sign $(cert_dir)/$(app_name).key $(build_dir)/$(app_name)-$(version).tar.gz | openssl base64; \
+ fi
diff --git a/README.md b/README.md
index 0b54698..bd996ea 100644
--- a/README.md
+++ b/README.md
@@ -1 +1,47 @@
-# files_frommail
\ No newline at end of file
+### installation
+
+- need mailparse
+
+```
+pecl install mailparse
+```
+
+if fail:
+
+```
+pecl download mailparse
+tar -zxvf mailparse-3.0.2.tgz
+cd mailparse-3.0.2/
+phpize
+edit mailparse.c
+```
+
+remove lines 34-37:
+> #if !HAVE_MBSTRING
+> #error The mailparse extension requires the mbstring extension!
+> #endif
+
+```
+./configure --with-php-config=/usr/bin/php-config
+make
+make install
+
+echo "extension=mailparse.so" > /etc/php/7.0/mods-available/mailparse.ini
+
+ cd /etc/php/7.0/apache2/conf.d/
+ln -s /etc/php/7.0/mods-available/mailparse.ini ./20-mailparse.ini
+apachectl restart
+```
+
+
+
+### configuration MTA:
+
+new entry in /etc/aliases:
+
+> files-artificial: "|/usr/bin/php -f /path/to/NextcloudMailCatcher.php"
+
+- execute _newaliases_
+
+- if virtual alias, create a new alias: files@artificial-owl.com -> files@hostname.example.net
+
diff --git a/appinfo/app.php b/appinfo/app.php
new file mode 100644
index 0000000..f23c3ca
--- /dev/null
+++ b/appinfo/app.php
@@ -0,0 +1,32 @@
+
+ * @copyright 2017
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see .
+ *
+ */
+
+namespace OCA\Files_FromMail\AppInfo;
+
+require_once __DIR__ . '/autoload.php';
+
+new Application();
+
diff --git a/appinfo/autoload.php b/appinfo/autoload.php
new file mode 100644
index 0000000..87886c7
--- /dev/null
+++ b/appinfo/autoload.php
@@ -0,0 +1,34 @@
+
+ * @copyright 2017
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see .
+ *
+ */
+
+namespace OCA\Files_FromMail\AppInfo;
+
+$composerDir = __DIR__ . '/../vendor/';
+
+if (is_dir($composerDir) && file_exists($composerDir . 'autoload.php')) {
+ require_once $composerDir . 'autoload.php';
+}
+
diff --git a/appinfo/info.xml b/appinfo/info.xml
new file mode 100644
index 0000000..25d5e48
--- /dev/null
+++ b/appinfo/info.xml
@@ -0,0 +1,35 @@
+
+
+ files_frommail
+ Files From Mail
+ Recover your email attachments from your cloud
+
+
+ 0.1.0
+ agpl
+ Maxence Lange
+ Files_FromMail
+
+ https://github.com/daita/files_frommail/wiki
+
+ tools
+ https://github.com/daita/files_frommail
+ https://github.com/daita/files_frommail/issues
+ https://github.com/daita/files_frommail.git
+
+
+
+
+
+
+
+
+ OCA\Files_FromMail\Command\Addresses
+
+
+
diff --git a/appinfo/routes.php b/appinfo/routes.php
new file mode 100644
index 0000000..35ffd3a
--- /dev/null
+++ b/appinfo/routes.php
@@ -0,0 +1,33 @@
+
+ * @copyright 2017
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see .
+ *
+ */
+
+return [
+ 'routes' => [
+ ['name' => 'Remote#getContent', 'url' => '/remote/', 'verb' => 'PUT']
+ ]
+];
+
+
diff --git a/composer.json b/composer.json
new file mode 100644
index 0000000..cb43364
--- /dev/null
+++ b/composer.json
@@ -0,0 +1,15 @@
+{
+ "name": "nextcloud/files_frommail",
+ "description": "Files From Mail",
+ "minimum-stability": "stable",
+ "license": "proprietary",
+ "authors": [
+ {
+ "name": "Maxence Lange",
+ "email": "maxence@artificial-owl.com"
+ }
+ ],
+ "require": {
+ "php-mime-mail-parser/php-mime-mail-parser": "2.9.2"
+ }
+}
diff --git a/composer.lock b/composer.lock
new file mode 100644
index 0000000..e233a0b
--- /dev/null
+++ b/composer.lock
@@ -0,0 +1,99 @@
+{
+ "_readme": [
+ "This file locks the dependencies of your project to a known state",
+ "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",
+ "packages": [
+ {
+ "name": "php-mime-mail-parser/php-mime-mail-parser",
+ "version": "2.9.2",
+ "source": {
+ "type": "git",
+ "url": "https://github.com/php-mime-mail-parser/php-mime-mail-parser.git",
+ "reference": "a3d5b16a4282e4afa5e4dd3bde7ce3213f929b9d"
+ },
+ "dist": {
+ "type": "zip",
+ "url": "https://api.github.com/repos/php-mime-mail-parser/php-mime-mail-parser/zipball/a3d5b16a4282e4afa5e4dd3bde7ce3213f929b9d",
+ "reference": "a3d5b16a4282e4afa5e4dd3bde7ce3213f929b9d",
+ "shasum": ""
+ },
+ "require": {
+ "ext-mailparse": "*",
+ "php": "^5.4.0 || ^7.0"
+ },
+ "replace": {
+ "exorus/php-mime-mail-parser": "*",
+ "messaged/php-mime-mail-parser": "*"
+ },
+ "require-dev": {
+ "phpunit/php-token-stream": "^1.3.0",
+ "phpunit/phpunit": "^4.0 || ^5.0",
+ "satooshi/php-coveralls": "0.*",
+ "squizlabs/php_codesniffer": "2.*"
+ },
+ "type": "library",
+ "autoload": {
+ "psr-4": {
+ "PhpMimeMailParser\\": "src/"
+ }
+ },
+ "notification-url": "https://packagist.org/downloads/",
+ "license": [
+ "MIT"
+ ],
+ "authors": [
+ {
+ "name": "bucabay",
+ "email": "gabe@fijiwebdesign.com",
+ "homepage": "http://www.fijiwebdesign.com",
+ "role": "Developer"
+ },
+ {
+ "name": "eXorus",
+ "email": "exorus.spam@gmail.com",
+ "homepage": "https://github.com/eXorus/",
+ "role": "Developer"
+ },
+ {
+ "name": "M.Valinskis",
+ "email": "M.Valins@gmail.com",
+ "homepage": "https://code.google.com/p/php-mime-mail-parser",
+ "role": "Developer"
+ },
+ {
+ "name": "eugene.emmett.wood",
+ "email": "gene_w@cementhorizon.com",
+ "homepage": "https://code.google.com/p/php-mime-mail-parser",
+ "role": "Developer"
+ },
+ {
+ "name": "alknetso",
+ "email": "alkne@gmail.com",
+ "homepage": "https://code.google.com/p/php-mime-mail-parser",
+ "role": "Developer"
+ }
+ ],
+ "description": "Fully Tested Mailparse Extension Wrapper for PHP 5.4+",
+ "homepage": "https://github.com/php-mime-mail-parser/php-mime-mail-parser",
+ "keywords": [
+ "MimeMailParser",
+ "mail",
+ "mailparse",
+ "mime"
+ ],
+ "time": "2017-09-30 15:15:40"
+ }
+ ],
+ "packages-dev": [],
+ "aliases": [],
+ "minimum-stability": "stable",
+ "stability-flags": [],
+ "prefer-stable": false,
+ "prefer-lowest": false,
+ "platform": [],
+ "platform-dev": []
+}
diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php
new file mode 100644
index 0000000..79dd5f0
--- /dev/null
+++ b/lib/AppInfo/Application.php
@@ -0,0 +1,43 @@
+
+ * @copyright 2017
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see .
+ *
+ */
+
+namespace OCA\Files_FromMail\AppInfo;
+
+use OCP\AppFramework\App;
+
+class Application extends App {
+
+ const APP_NAME = 'files_frommail';
+
+ /**
+ * @param array $params
+ */
+ public function __construct(array $params = array()) {
+ parent::__construct(self::APP_NAME, $params);
+ }
+
+}
+
diff --git a/lib/Command/Addresses.php b/lib/Command/Addresses.php
new file mode 100644
index 0000000..b84f8f5
--- /dev/null
+++ b/lib/Command/Addresses.php
@@ -0,0 +1,199 @@
+
+ * @copyright 2017
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see .
+ *
+ */
+
+namespace OCA\Files_FromMail\Command;
+
+use Exception;
+use OC\Core\Command\Base;
+use OCA\Files_FromMail\Exceptions\FakeException;
+use OCA\Files_FromMail\Exceptions\MissingArgumentException;
+use OCA\Files_FromMail\Service\MailService;
+use Symfony\Component\Console\Input\InputArgument;
+use Symfony\Component\Console\Input\InputInterface;
+use Symfony\Component\Console\Input\InputOption;
+use Symfony\Component\Console\Output\OutputInterface;
+
+class Addresses extends Base {
+
+
+ /** @var MailService */
+ private $mailService;
+
+ /**
+ * Addresses constructor.
+ *
+ * @param MailService $mailService
+ */
+ public function __construct(MailService $mailService) {
+ parent::__construct();
+
+ $this->mailService = $mailService;
+ }
+
+
+ protected function configure() {
+ parent::configure();
+ $this->setName('files_frommail:address')
+ ->setDescription('manage the linked groups')
+ ->addOption('add', 'a', InputOption::VALUE_NONE, 'add a new mail address')
+ ->addOption('list', 'l', InputOption::VALUE_NONE, 'list all mail addresses')
+ ->addOption('remove', 'r', InputOption::VALUE_NONE, 'remove a mail address')
+ ->addOption(
+ 'password', 'p', InputOption::VALUE_NONE, 'add a password to protect a mail address'
+ )
+ ->addArgument('address', InputArgument::OPTIONAL, 'email address')
+ ->addArgument('password', InputArgument::OPTIONAL, 'password');
+ }
+
+
+ protected function execute(InputInterface $input, OutputInterface $output) {
+
+ try {
+ $this->listMailAddresses($input, $output);
+ $this->addMailAddress($input);
+ $this->removeMailAddress($input);
+ $this->setMailAddressPassword($input, $output);
+
+ $output->writeln('please specify an action or use --help');
+ } catch (FakeException $e) {
+ } catch (Exception $e) {
+ throw $e;
+ }
+ }
+
+
+ /**
+ * @param InputInterface $input
+ * @param OutputInterface $output
+ *
+ * @throws FakeException
+ */
+ private function listMailAddresses(InputInterface $input, OutputInterface $output) {
+ if ($input->getOption('list') !== true) {
+ return;
+ }
+
+ $addresses = $this->mailService->getMailAddresses();
+
+ if (sizeof($addresses) === 0) {
+ $output->writeln('no mail address');
+ throw new FakeException();
+ }
+
+ foreach ($addresses as $entry) {
+ $output->writeln($this->formatMailAddress($entry));
+ }
+
+ throw new FakeException();
+ }
+
+
+ /**
+ * @param InputInterface $input
+ *
+ * @throws FakeException
+ */
+ private function addMailAddress(InputInterface $input) {
+ if ($input->getOption('add') !== true) {
+ return;
+ }
+
+ $mail = $this->checkMailAddress($input);
+ $this->mailService->addMailAddress($mail);
+
+ throw new FakeException();
+ }
+
+
+ /**
+ * @param InputInterface $input
+ *
+ * @throws FakeException
+ */
+ private function removeMailAddress(InputInterface $input) {
+ if ($input->getOption('remove') !== true) {
+ return;
+ }
+
+ $mail = $this->checkMailAddress($input);
+ $this->mailService->removeMailAddress($mail);
+
+ throw new FakeException();
+ }
+
+
+ /**
+ * @param InputInterface $input
+ * @param OutputInterface $output
+ *
+ * @throws FakeException
+ */
+ private function setMailAddressPassword(InputInterface $input, OutputInterface $output) {
+ if ($input->getOption('password') !== true) {
+ return;
+ }
+
+ $mail = $this->checkMailAddress($input);
+ $password = $input->getArgument('password');
+ $this->mailService->setMailPassword($mail, $password);
+
+ if ($password === null) {
+ $output->writeln('Password for ' . $mail . ' is now UNSET');
+ throw new FakeException();
+ }
+
+ $output->writeln('Password for ' . $mail . ' is now SET to ' . $password);
+ throw new FakeException();
+ }
+
+
+ private function checkMailAddress(InputInterface $input) {
+ $mail = $input->getArgument('address');
+ if ($mail === null) {
+ throw new MissingArgumentException('missing email address');
+ }
+
+ return $mail;
+
+ }
+
+
+ /**
+ * @param array $entry
+ *
+ * @return string
+ */
+ private function formatMailAddress($entry) {
+ $line = '- ' . $entry['address'];
+ if (array_key_exists('password', $entry) && $entry['password'] !== '') {
+ $line .= ' [:' . $entry['password'] . ']';
+ }
+
+ return $line;
+ }
+
+}
+
diff --git a/lib/Controller/RemoteController.php b/lib/Controller/RemoteController.php
new file mode 100644
index 0000000..e2049b7
--- /dev/null
+++ b/lib/Controller/RemoteController.php
@@ -0,0 +1,83 @@
+
+ * @copyright 2017
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see .
+ *
+ */
+
+namespace OCA\Files_FromMail\Controller;
+
+use OCA\Files_FromMail\AppInfo\Application;
+use OCA\Files_FromMail\Service\MailService;
+use OCA\Files_FromMail\Service\MiscService;
+use OCP\AppFramework\Controller;
+use OCP\AppFramework\Http;
+use OCP\AppFramework\Http\DataResponse;
+use OCP\IRequest;
+
+class RemoteController extends Controller {
+
+ /** @var string */
+ private $userId;
+
+ /** @var MailService */
+ private $mailService;
+
+ /** @var MiscService */
+ private $miscService;
+
+
+ /**
+ * NavigationController constructor.
+ *
+ * @param IRequest $request
+ * @param string $userId
+ * @param MailService $mailService
+ * @param MiscService $miscService
+ */
+ function __construct(IRequest $request, $userId, MailService $mailService, MiscService $miscService
+ ) {
+ parent::__construct(Application::APP_NAME, $request);
+ $this->userId = $userId;
+
+ $this->mailService = $mailService;
+ $this->miscService = $miscService;
+ }
+
+
+ /**
+ * @NoAdminRequired
+ * @NoCSRFRequired
+ *
+ * @param $content
+ *
+ * @return DataResponse
+ */
+ public function getContent($content) {
+ $content = base64_decode(rawurldecode($content));
+ $this->mailService->parseMail($content, $this->userId);
+
+ return new DataResponse(['ok'], Http::STATUS_CREATED);
+ }
+
+
+}
\ No newline at end of file
diff --git a/lib/Exceptions/AddressAlreadyExistException.php b/lib/Exceptions/AddressAlreadyExistException.php
new file mode 100644
index 0000000..c088eb8
--- /dev/null
+++ b/lib/Exceptions/AddressAlreadyExistException.php
@@ -0,0 +1,32 @@
+
+ * @copyright 2017
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see .
+ *
+ */
+
+namespace OCA\Files_FromMail\Exceptions;
+
+class AddressAlreadyExistException extends \Exception {
+
+}
+
diff --git a/lib/Exceptions/AddressInfoException.php b/lib/Exceptions/AddressInfoException.php
new file mode 100644
index 0000000..5e0c412
--- /dev/null
+++ b/lib/Exceptions/AddressInfoException.php
@@ -0,0 +1,32 @@
+
+ * @copyright 2017
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see .
+ *
+ */
+
+namespace OCA\Files_FromMail\Exceptions;
+
+class AddressInfoException extends \Exception {
+
+}
+
diff --git a/lib/Exceptions/FakeException.php b/lib/Exceptions/FakeException.php
new file mode 100644
index 0000000..99ec60f
--- /dev/null
+++ b/lib/Exceptions/FakeException.php
@@ -0,0 +1,32 @@
+
+ * @copyright 2017
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see .
+ *
+ */
+
+namespace OCA\Files_FromMail\Exceptions;
+
+class FakeException extends \Exception {
+
+}
+
diff --git a/lib/Exceptions/InvalidAddressException.php b/lib/Exceptions/InvalidAddressException.php
new file mode 100644
index 0000000..3a946a3
--- /dev/null
+++ b/lib/Exceptions/InvalidAddressException.php
@@ -0,0 +1,32 @@
+
+ * @copyright 2017
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see .
+ *
+ */
+
+namespace OCA\Files_FromMail\Exceptions;
+
+class InvalidAddressException extends \Exception {
+
+}
+
diff --git a/lib/Exceptions/MailHeadersException.php b/lib/Exceptions/MailHeadersException.php
new file mode 100644
index 0000000..b95badd
--- /dev/null
+++ b/lib/Exceptions/MailHeadersException.php
@@ -0,0 +1,32 @@
+
+ * @copyright 2017
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see .
+ *
+ */
+
+namespace OCA\Files_FromMail\Exceptions;
+
+class MailHeadersException extends \Exception {
+
+}
+
diff --git a/lib/Exceptions/MissingArgumentException.php b/lib/Exceptions/MissingArgumentException.php
new file mode 100644
index 0000000..bff2e6f
--- /dev/null
+++ b/lib/Exceptions/MissingArgumentException.php
@@ -0,0 +1,32 @@
+
+ * @copyright 2017
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see .
+ *
+ */
+
+namespace OCA\Files_FromMail\Exceptions;
+
+class MissingArgumentException extends \Exception {
+
+}
+
diff --git a/lib/Exceptions/NotAFolderException.php b/lib/Exceptions/NotAFolderException.php
new file mode 100644
index 0000000..85d5a63
--- /dev/null
+++ b/lib/Exceptions/NotAFolderException.php
@@ -0,0 +1,32 @@
+
+ * @copyright 2017
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see .
+ *
+ */
+
+namespace OCA\Files_FromMail\Exceptions;
+
+class NotAFolderException extends \Exception {
+
+}
+
diff --git a/lib/Exceptions/UnknownAddressException.php b/lib/Exceptions/UnknownAddressException.php
new file mode 100644
index 0000000..3a0e161
--- /dev/null
+++ b/lib/Exceptions/UnknownAddressException.php
@@ -0,0 +1,32 @@
+
+ * @copyright 2017
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see .
+ *
+ */
+
+namespace OCA\Files_FromMail\Exceptions;
+
+class UnknownAddressException extends \Exception {
+
+}
+
diff --git a/lib/NextcloudMailCatcher.php b/lib/NextcloudMailCatcher.php
new file mode 100644
index 0000000..902b8ce
--- /dev/null
+++ b/lib/NextcloudMailCatcher.php
@@ -0,0 +1,222 @@
+
+ * @copyright 2017
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see .
+ *
+ */
+
+
+$config = [
+ 'nextcloud' => 'https://test.artificial-owl.com/',
+ 'username' => 'frommail',
+ 'password' => 'Ledxc-jRFiR-wBMXD-jyyjt-Y87CZ',
+ 'debug' => false
+];
+
+
+// --- do not edit below this line ---
+
+class NextcloudMailCatcher {
+
+ /** @var string */
+ private $content;
+
+ /** @var array */
+ private $config;
+
+
+ /**
+ * RemoteMailCatcher constructor.
+ *
+ * @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');
+ }
+
+
+ /**
+ * @param $content
+ *
+ * @return $this
+ */
+ public function setContent($content) {
+ $this->content = $content;
+
+ return $this;
+ }
+
+
+ /**
+ * @return string
+ */
+ public function getContent() {
+ return $this->content;
+ }
+
+
+ /**
+ *
+ */
+ public function sendToNextcloud() {
+
+ $content = rawurlencode(base64_encode($this->getContent()));
+
+ $curl = $this->generateAuthedCurl(
+ $this->config['nextcloud'] . '/index.php/apps/files_frommail/remote/'
+ );
+ $this->fillCurlWithContent($curl, 'content=' . $content);
+
+ $result = curl_exec($curl);
+
+ $this->debugCurl($curl, $result);
+ }
+
+
+ /**
+ * @param resource $curl
+ * @param string|array $result
+ */
+ private function debugCurl($curl, $result) {
+ if ($result === false) {
+ $this->debug('Mail NOT forwarded: ' . curl_error($curl));
+
+ return;
+ }
+
+ try {
+ $this->debugCurlResponseCode($curl);
+ } catch (Exception $e) {
+ $this->debug('Mail NOT forwarded: ' . $e->getMessage());
+
+ return;
+ }
+
+ $this->debug('Mail forwarded, result was ' . $result);
+ }
+
+
+ private function debugCurlResponseCode($curl) {
+
+ $code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
+ if ($code === 201) {
+ return;
+ }
+
+ if ($code === 401 || $code === 500) {
+ throw new Exception('Unauthorized access');
+ }
+
+ if ($code === 404) {
+ throw new Exception('404 Not Found');
+ }
+
+ if ($code === 503 || $code === 302) {
+ throw new Exception('The \'files_frommail\' app does not seems enabled');
+ }
+
+ throw new Exception('Request returned code ' . $code);
+ }
+
+
+ /**
+ * @param $url
+ *
+ * @return resource
+ */
+ private function generateAuthedCurl($url) {
+
+ $curl = curl_init($url);
+ curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
+ curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
+ curl_setopt(
+ $curl, CURLOPT_USERPWD,
+ $this->config['username'] . ':' . $this->config['password']
+ );
+
+ $this->debug(
+ 'Generate curl request to ' . $url . ' with username \'' . $this->config['username'] . '\''
+ );
+
+ return $curl;
+ }
+
+
+ /**
+ * @param resource $curl
+ * @param string $put
+ */
+ private function fillCurlWithContent(&$curl, $put) {
+
+ curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PUT');
+ curl_setopt($curl, CURLOPT_POSTFIELDS, $put);
+
+ $length = strlen($put);
+ curl_setopt(
+ $curl, CURLOPT_HTTPHEADER,
+ [
+ 'Content-type: application/x-www-form-urlencoded',
+ 'OCS-APIRequest: true',
+ 'Content-Length: ' . $length
+ ]
+ );
+
+ $this->debug('Content-Length: ' . $length . ' (' . round($length / 1024 / 1024, 1) . 'MB)');
+ }
+
+
+ /**
+ * @param string $string
+ */
+ private function debug($string) {
+ if (!array_key_exists('debug', $this->config) || $this->config['debug'] !== true) {
+ return;
+ }
+
+ $log = '/tmp/' . basename(__FILE__, '.php') . '.log';
+ file_put_contents($log, date('Y-m-d H:i:s') . ' ' . $string . "\n", FILE_APPEND);
+ }
+
+}
+
+
+$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/ConfigService.php b/lib/Service/ConfigService.php
new file mode 100644
index 0000000..0817693
--- /dev/null
+++ b/lib/Service/ConfigService.php
@@ -0,0 +1,117 @@
+
+ * @copyright 2017
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see .
+ *
+ */
+
+namespace OCA\Files_FromMail\Service;
+
+use OCP\IConfig;
+use OCP\IRequest;
+
+class ConfigService {
+
+ const FROMMAIL_ADDRESSES = 'frommail_addresses';
+
+ private $defaults = [
+ self::FROMMAIL_ADDRESSES => ''
+ ];
+
+ /** @var string */
+ private $appName;
+
+ /** @var IConfig */
+ private $config;
+
+ /** @var string */
+ private $userId;
+
+ /** @var IRequest */
+ private $request;
+
+ /** @var MiscService */
+ private $miscService;
+
+ /**
+ * ConfigService constructor.
+ *
+ * @param string $appName
+ * @param IConfig $config
+ * @param IRequest $request
+ * @param string $userId
+ * @param MiscService $miscService
+ */
+ public function __construct(
+ $appName, IConfig $config, IRequest $request, $userId, MiscService $miscService
+ ) {
+ $this->appName = $appName;
+ $this->config = $config;
+ $this->request = $request;
+ $this->userId = $userId;
+ $this->miscService = $miscService;
+ }
+
+
+ /**
+ * Get a value by key
+ *
+ * @param string $key
+ *
+ * @return string
+ */
+ public function getAppValue($key) {
+ $defaultValue = null;
+
+ if (array_key_exists($key, $this->defaults)) {
+ $defaultValue = $this->defaults[$key];
+ }
+
+ return $this->config->getAppValue($this->appName, $key, $defaultValue);
+ }
+
+
+ /**
+ * Set a value by key
+ *
+ * @param string $key
+ * @param string $value
+ *
+ * @return void
+ */
+ public function setAppValue($key, $value) {
+ $this->config->setAppValue($this->appName, $key, $value);
+ }
+
+
+ /**
+ * remove a key
+ *
+ * @param string $key
+ *
+ * @return string
+ */
+ public function deleteAppValue($key) {
+ return $this->config->deleteAppValue($this->appName, $key);
+ }
+
+}
diff --git a/lib/Service/MailService.php b/lib/Service/MailService.php
new file mode 100644
index 0000000..d92f6cb
--- /dev/null
+++ b/lib/Service/MailService.php
@@ -0,0 +1,342 @@
+
+ * @copyright 2017
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see .
+ *
+ */
+
+namespace OCA\Files_FromMail\Service;
+
+
+use Exception;
+use OCA\Files_FromMail\Exceptions\AddressAlreadyExistException;
+use OCA\Files_FromMail\Exceptions\AddressInfoException;
+use OCA\Files_FromMail\Exceptions\InvalidAddressException;
+use OCA\Files_FromMail\Exceptions\NotAFolderException;
+use OCA\Files_FromMail\Exceptions\UnknownAddressException;
+use OCP\Files\FileInfo;
+use OCP\Files\Folder;
+use PhpMimeMailParser\Attachment;
+use PhpMimeMailParser\Parser;
+
+class MailService {
+
+
+ /** @var ConfigService */
+ private $configService;
+
+ /** @var MiscService */
+ private $miscService;
+
+ /** @var int */
+ private $count = 0;
+
+ /**
+ * MailService constructor.
+ *
+ * @param ConfigService $configService
+ * @param MiscService $miscService
+ */
+ function __construct(ConfigService $configService, MiscService $miscService) {
+ $this->configService = $configService;
+ $this->miscService = $miscService;
+ }
+
+
+ /**
+ * @param string $content
+ * @param string $userId
+ */
+ public function parseMail($content, $userId) {
+ $mail = new Parser();
+ $mail->setText($content);
+
+ $data = $this->parseMailHeaders($mail);
+ $data['date'] = date('Y-m-d H:i:s');
+ $data['userId'] = $userId;
+
+ $toAddresses = array_merge(
+ $mail->getAddresses('to'),
+ $mail->getAddresses('cc'),
+ $mail->getAddresses('bcc')
+ );
+
+ foreach ($toAddresses as $toAddress) {
+ try {
+ $this->generateLocalContentFromMail($mail, $toAddress['address'], $data);
+ } catch (Exception $e) {
+ // we check next address
+ }
+ }
+ }
+
+
+ /**
+ * @param Parser $mail
+ * @param string $to
+ * @param array $data
+ */
+ private function generateLocalContentFromMail(Parser $mail, $to, $data) {
+
+ $toInfo = $this->getMailAddressInfo($to);
+ $text = $data['text'];
+ $subject = $data['subject'];
+ $from = $data['from'];
+ $userId = $data['userId'];
+ $date = $data['date'];
+
+ $this->verifyInfoAndPassword($text, $toInfo);
+
+ $folder = $this->getMailFolder($userId, $to, $from);
+ $this->createLocalFile($folder, $date, 'mail-' . $subject . '.txt', $text);
+ $this->createLocalFileFromAttachments($date, $folder, $mail->getAttachments());
+ }
+
+
+ /**
+ * @param $content
+ * @param $toInfo
+ *
+ * @throws AddressInfoException
+ */
+ private function verifyInfoAndPassword($content, $toInfo) {
+
+ if ($toInfo === null) {
+ throw new AddressInfoException('address is not known');
+ }
+
+ if (!array_key_exists('password', $toInfo) || $toInfo['password'] === '') {
+ return;
+ }
+
+ if (strpos($content, ':' . $toInfo['password']) !== false) {
+ return;
+ }
+
+ throw new AddressInfoException('password is set but not used in mail');
+ }
+
+
+ /**
+ * @param string $userId
+ * @param string $to
+ * @param string $from
+ *
+ * @return Folder
+ * @throws NotAFolderException
+ */
+ private function getMailFolder($userId, $to, $from) {
+
+ $node = \OC::$server->getUserFolder($userId);
+ $folderPath = 'Mails sent to ' . $to . '/From ' . $from . '/';
+
+ if (!$node->nodeExists($folderPath)) {
+ $node->newFolder($folderPath);
+ }
+
+ $folder = $node->get($folderPath);
+ if ($folder->getType() !== FileInfo::TYPE_FOLDER) {
+ throw new NotAFolderException($folderPath . ' is not a folder');
+ }
+
+ /** @var Folder $folder */
+ return $folder;
+ }
+
+
+ /**
+ * @param Parser $mail
+ *
+ * @return array
+ */
+ private function parseMailHeaders(Parser $mail) {
+ $from = $mail->getAddresses('from')[0]['address'];
+ $subject = $mail->getHeader('subject');
+ $text = $mail->getHeadersRaw() . $mail->getMessageBody('text');
+
+ //TODO: check that data are enough
+ return [
+ 'from' => $from,
+ 'subject' => $subject,
+ 'text' => $text
+ ];
+ }
+
+
+ /**
+ * @param string $id
+ * @param Folder $folder
+ * @param Attachment[] $attachments
+ */
+ private function createLocalFileFromAttachments($id, $folder, $attachments) {
+ foreach ($attachments as $attachment) {
+ $this->createLocalFile(
+ $folder, $id, 'attachment-' . $attachment->getFilename(),
+ $attachment->getContent()
+ );
+ }
+
+ }
+
+
+ /**
+ * @param Folder $folder
+ * @param string $id
+ * @param string $filename
+ * @param string $content
+ */
+ private function createLocalFile($folder, $id, $filename, $content) {
+ $new = $folder->newFile($id . '-' . $this->count . '_' . $filename);
+ $new->putContent($content);
+
+ $this->count++;
+ }
+
+
+ /**
+ * @param $address
+ * @param $password
+ *
+ * @throws UnknownAddressException
+ */
+ public function setMailPassword($address, $password) {
+ if (!$this->mailAddressExist($address)) {
+ throw new UnknownAddressException('address is not known');
+ }
+ if ($password === null) {
+ $password = '';
+ }
+
+ $addresses = $this->getMailAddresses();
+ $new = [];
+ foreach ($addresses as $entry) {
+ if ($entry['address'] === $address) {
+ $entry['password'] = $password;
+ }
+ $new[] = $entry;
+ }
+
+ $this->saveMailAddresses($new);
+ }
+
+
+ /**
+ * @param $address
+ *
+ * @throws UnknownAddressException
+ */
+ public function removeMailAddress($address) {
+ $addresses = $this->getMailAddresses();
+ if (!$this->mailAddressExist($address)) {
+ throw new UnknownAddressException('address is not known');
+ }
+
+ $new = [];
+ foreach ($addresses as $entry) {
+ if ($entry['address'] !== $address) {
+ $new[] = $entry;
+ }
+ }
+
+ $this->saveMailAddresses($new);
+ }
+
+
+ /**
+ * @param $address
+ *
+ * @throws AddressAlreadyExistException
+ */
+ public function addMailAddress($address) {
+ $this->hasToBeAValidMailAddress($address);
+ if ($this->mailAddressExist($address)) {
+ throw new AddressAlreadyExistException('address already exist');
+ }
+
+ $addresses = $this->getMailAddresses();
+ array_push($addresses, ['address' => $address]);
+ $this->saveMailAddresses($addresses);
+ }
+
+
+ /**
+ * @param $address
+ *
+ * @return bool
+ */
+ private function mailAddressExist($address) {
+ return ($this->getMailAddressInfo($address) !== null);
+ }
+
+
+ /**
+ * @param $address
+ *
+ * @return array|null
+ */
+ private function getMailAddressInfo($address) {
+ $addresses = $this->getMailAddresses();
+ foreach ($addresses as $entry) {
+ if ($entry['address'] === $address) {
+ return $entry;
+ }
+ }
+
+ return null;
+ }
+
+
+ /**
+ * @param $address
+ *
+ * @throws InvalidAddressException
+ */
+ private function hasToBeAValidMailAddress($address) {
+ if (filter_var($address, FILTER_VALIDATE_EMAIL)) {
+ return;
+ }
+
+ throw new InvalidAddressException('this mail address is not valid');
+ }
+
+
+ /**
+ * @return array
+ */
+ public function getMailAddresses() {
+ $curr = json_decode($this->configService->getAppValue(ConfigService::FROMMAIL_ADDRESSES), true);
+ if ($curr === null) {
+ return [];
+ }
+
+ return $curr;
+ }
+
+ /**
+ * @param $addresses
+ */
+ private function saveMailAddresses($addresses) {
+ $this->configService->setAppValue(ConfigService::FROMMAIL_ADDRESSES, json_encode($addresses));
+ }
+
+
+}
\ No newline at end of file
diff --git a/lib/Service/MiscService.php b/lib/Service/MiscService.php
new file mode 100644
index 0000000..82338d4
--- /dev/null
+++ b/lib/Service/MiscService.php
@@ -0,0 +1,67 @@
+
+ * @copyright 2017
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see .
+ *
+ */
+
+namespace OCA\Files_FromMail\Service;
+
+use OCA\Files_FromMail\AppInfo\Application;
+use OCP\ILogger;
+
+class MiscService {
+
+ /** @var ILogger */
+ private $logger;
+
+ public function __construct(ILogger $logger) {
+ $this->logger = $logger;
+ }
+
+ public function log($message, $level = 2) {
+ $data = array(
+ 'app' => Application::APP_NAME,
+ 'level' => $level
+ );
+
+ $this->logger->log($level, $message, $data);
+ }
+
+ /**
+ * @param $arr
+ * @param $k
+ *
+ * @param string $default
+ *
+ * @return array|string|integer
+ */
+ public static function get($arr, $k, $default = '') {
+ if (!key_exists($k, $arr)) {
+ return $default;
+ }
+
+ return $arr[$k];
+ }
+
+}
+
diff --git a/screenshots/v0.1.0.png b/screenshots/v0.1.0.png
new file mode 100644
index 0000000..9b4e979
Binary files /dev/null and b/screenshots/v0.1.0.png differ