diff --git a/common.yaml b/common.yaml index 8a41e22..53caffa 100644 --- a/common.yaml +++ b/common.yaml @@ -90,15 +90,15 @@ services: image: roundcube/roundcubemail environment: ROUNDCUBEMAIL_DB_TYPE: sqlite - ROUNDCUBEMAIL_COMPOSER_PLUGINS: "texxasrulez/persistent_login" - ROUNDCUBEMAIL_PLUGINS: "show_additional_headers,persistent_login,autocreate_identity" + ROUNDCUBEMAIL_PLUGINS: "show_additional_headers,autocreate_identity,devel_autologon" + ROUNDCUBEMAIL_INSTALL_PLUGINS: 1 depends_on: - mailserver volumes: - ./mail-testing-config/roundcube/config/roundcube_user_settings.inc.php:/var/roundcube/config/config.inc.php:ro - ./mail-testing-config/roundcube/plugins/autocreate_identity:/var/www/html/plugins/autocreate_identity + - ./mail-testing-config/roundcube/plugins/devel_autologon:/var/www/html/plugins/devel_autologon - roundcube:/var/www/html:rw - - roundcube_db:/var/roundcube/db:rw smtpreal: image: ghcr.io/docker-mailserver/docker-mailserver:latest diff --git a/devel.yaml b/devel.yaml index 6d7709b..c30a904 100644 --- a/devel.yaml +++ b/devel.yaml @@ -134,8 +134,8 @@ services: ODOO_USER_ID: 2 MAILGATE_SMTP_PORT: "13525" MAILBOX_DOMAIN: *mailserver - MAILBOX_USER: admin - ADMIN_PASSWORD: admin + MAILBOX_USER: &mailbox_user admin + ADMIN_PASSWORD: &mailbox_password admin SMTP_SERVICE_NAME: smtp roundcube: @@ -150,6 +150,9 @@ services: ROUNDCUBEMAIL_DEFAULT_HOST: *mailserver ROUNDCUBEMAIL_SMTP_SERVER: *mailserver ROUNDCUBEMAIL_SMTP_PORT: 13525 + MAILBOX_USER: *mailbox_user + MAILBOX_DOMAIN: *mailserver + ADMIN_PASSWORD: *mailbox_password wdb: image: docker.io/kozea/wdb diff --git a/mail-testing-config/docker-mailserver/user-patches.sh b/mail-testing-config/docker-mailserver/user-patches.sh index 462e0dd..db02607 100755 --- a/mail-testing-config/docker-mailserver/user-patches.sh +++ b/mail-testing-config/docker-mailserver/user-patches.sh @@ -84,7 +84,7 @@ try: "-d", DB, "-u", USER, "-p", PASSWORD, - "--host", "odoo", + "--host", "gatekeeper", "--port", "8069", ] return_code = subprocess.run(cmd, stdin=sys.stdin.buffer).returncode diff --git a/mail-testing-config/roundcube/config/roundcube_user_settings.inc.php b/mail-testing-config/roundcube/config/roundcube_user_settings.inc.php index 6f418a3..19c23b2 100644 --- a/mail-testing-config/roundcube/config/roundcube_user_settings.inc.php +++ b/mail-testing-config/roundcube/config/roundcube_user_settings.inc.php @@ -11,3 +11,6 @@ // https://github.com/roundcube/roundcubemail-docker/blob/9fd84b3e4f7943c7b2046d0a443a39d1a702edf2/fpm/docker-entrypoint.sh#L177-#L188 $config['smtp_user'] = ''; $config['smtp_pass'] = ''; + +// Session lifetime of 30 days (in minutes). +$config['session_lifetime'] = 43200; diff --git a/mail-testing-config/roundcube/plugins/autocreate_identity/autocreate_identity.php b/mail-testing-config/roundcube/plugins/autocreate_identity/autocreate_identity.php index 59c07a1..98caf39 100644 --- a/mail-testing-config/roundcube/plugins/autocreate_identity/autocreate_identity.php +++ b/mail-testing-config/roundcube/plugins/autocreate_identity/autocreate_identity.php @@ -1,53 +1,108 @@ add_hook('message_load', array($this, 'create_identity_from_message')); + $this->add_hook('message_load', [$this, 'create_identities']); + $this->add_hook('identity_select', [$this, 'select_reply_identity']); } - function create_identity_from_message($args) + public function create_identities($args) { - // Only process if we have headers and it's a message object - if (!isset($args['object']) || !isset($args['object']->headers)) { + $user = rcmail::get_instance()->user; + $headers = isset($args['object']->headers) ? $args['object']->headers : null; + if (!$user || !$headers) { return $args; } - $rcmail = rcmail::get_instance(); - $user = $rcmail->user; - - // Get existing identities - $existing_emails = array(); + $known = []; foreach ($user->list_identities() as $identity) { - $existing_emails[] = strtolower($identity['email']); + $known[strtolower($identity['email'])] = true; } - // Get To and Cc addresses - $recipients = array(); - if (!empty($args['object']->headers->to)) { - $recipients = array_merge($recipients, rcube_mime::decode_address_list($args['object']->headers->to)); + foreach ($this->recipients($headers) as $recipient) { + $email = strtolower($recipient['email']); + if (isset($known[$email])) { + continue; + } + + $inserted = $user->insert_identity([ + 'name' => $recipient['name'], + 'email' => $recipient['email'], + 'standard' => 0, + 'signature' => '', + 'html_signature' => 0, + ]); + if ($inserted) { + $known[$email] = true; + } } - if (!empty($args['object']->headers->cc)) { - $recipients = array_merge($recipients, rcube_mime::decode_address_list($args['object']->headers->cc)); + + return $args; + } + + public function select_reply_identity($args) + { + $output = rcmail::get_instance()->output; + if (!$output || $output->get_env('compose_mode') !== 'reply' || empty($args['identities'])) { + return $args; } - foreach ($recipients as $recipient) { - $email = $recipient['mailto']; - if (!in_array(strtolower($email), $existing_emails)) { - $user->insert_identity(array( - 'name' => $recipient['name'] ?: '', - 'email' => $email, - 'standard' => 0, - 'signature' => '', - 'html_signature' => 0 - )); - $existing_emails[] = strtolower($email); + $headers = isset($args['message']->headers) ? $args['message']->headers : null; + if (!$headers) { + return $args; + } + + $recipients = $this->recipients($headers); + if (!$recipients) { + return $args; + } + + $target = strtolower($recipients[0]['email']); + foreach ($args['identities'] as $idx => $identity) { + $email = strtolower($identity['email_ascii'] ?? $identity['email']); + if ($email === $target) { + $args['selected'] = $idx; + return $args; } } return $args; } + + /** + * To addresses first, then Cc. Entries without an address are skipped, + * so the first item is the reply sender. + */ + private function recipients($headers) + { + $recipients = []; + $charset = $headers->charset ?? null; + + foreach (['to', 'cc'] as $header) { + if (empty($headers->$header)) { + continue; + } + foreach (rcube_mime::decode_address_list($headers->$header, null, true, $charset) as $address) { + $email = $address['mailto'] ?? ''; + if (!str_contains($email, '@')) { + continue; + } + $recipients[] = [ + 'email' => $email, + 'name' => (string) ($address['name'] ?? ''), + ]; + } + } + + return $recipients; + } } diff --git a/mail-testing-config/roundcube/plugins/devel_autologon/devel_autologon.php b/mail-testing-config/roundcube/plugins/devel_autologon/devel_autologon.php new file mode 100644 index 0000000..318dbed --- /dev/null +++ b/mail-testing-config/roundcube/plugins/devel_autologon/devel_autologon.php @@ -0,0 +1,53 @@ +add_hook('startup', [$this, 'startup']); + $this->add_hook('authenticate', [$this, 'authenticate']); + } + + /** + * Turn an anonymous request into a login so the form is never shown. + * A live session, including logout, is left alone; the next request logs in. + */ + public function startup($args) + { + if (empty($_SESSION['user_id'])) { + $args['task'] = 'login'; + $args['action'] = 'login'; + } + + return $args; + } + + /** + * Supply the devel mailbox. valid skips the CSRF token the login form + * would have posted; cookiecheck is skipped because this request has + * not received a session cookie yet. + */ + public function authenticate($args) + { + $user = getenv('MAILBOX_USER') ?: 'admin'; + $domain = getenv('MAILBOX_DOMAIN') ?: 'mailserver'; + $password = getenv('ADMIN_PASSWORD') ?: 'admin'; + + $args['user'] = $user . '@' . $domain; + $args['pass'] = $password; + $args['cookiecheck'] = false; + $args['valid'] = true; + + return $args; + } +}