1<?php
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * A Handler to re-process messages which previously failed sender verification.
19 *
20 * @package    tool_messageinbound
21 * @category   message
22 * @copyright  2014 Andrew Nicols
23 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26namespace tool_messageinbound\message\inbound;
27
28defined('MOODLE_INTERNAL') || die();
29
30require_once($CFG->dirroot . '/repository/lib.php');
31
32/**
33 * A Handler to re-process messages which previously failed sender verification.
34 *
35 * This may happen if the user did not use their registerd e-mail address,
36 * the verification hash used had expired, or if some erroneous content was
37 * introduced into the content hash.
38 *
39 * @copyright  2014 Andrew Nicols
40 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
41 */
42class invalid_recipient_handler extends \core\message\inbound\handler {
43
44    /**
45     * Do not allow changes to the address validation setting.
46     */
47    public function can_change_validateaddress() {
48        return false;
49    }
50
51    /**
52     * Return a description for the current handler.
53     *
54     * @return string
55     */
56    public function get_description() {
57        return get_string('invalid_recipient_handler', 'tool_messageinbound');
58    }
59
60    /**
61     * Return a name for the current handler.
62     * This appears in the admin pages as a human-readable name.
63     *
64     * @return string
65     */
66    public function get_name() {
67        return get_string('invalid_recipient_handler_name', 'tool_messageinbound');
68    }
69
70    /**
71     * Process a message received and validated by the Inbound Message processor.
72     *
73     * @param \stdClass $record The Inbound Message record
74     * @param \stdClass $data The message data packet.
75     * @return bool Whether the message was successfully processed.
76     * @throws \core\message\inbound\processing_failed_exception when the message can not be found.
77     */
78    public function process_message(\stdClass $record, \stdClass $data) {
79        global $DB;
80
81        if (!$maildata = $DB->get_record('messageinbound_messagelist', array('id' => $record->datavalue))) {
82            // The message requested couldn't be found. Failing here will alert the user that we failed.
83            throw new \core\message\inbound\processing_failed_exception('oldmessagenotfound', 'tool_messageinbound');
84        }
85
86        mtrace("=== Request to re-process message {$record->datavalue} from server.");
87        mtrace("=== Message-Id:\t{$maildata->messageid}");
88        mtrace("=== Recipient:\t{$maildata->address}");
89
90        $manager = new \tool_messageinbound\manager();
91        return $manager->process_existing_message($maildata);
92    }
93
94}
95