2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * Jabber message processor to send messages by jabber
20 * @package message_jabber
21 * @copyright 2008 Luis Rodrigues
22 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
25 require_once($CFG->dirroot.'/message/output/lib.php');
27 * The jabber message processor
29 * @package message_jabber
30 * @copyright 2008 Luis Rodrigues
31 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33 class message_output_jabber extends message_output {
36 * Processes the message and sends a notification via jabber
38 * @param stdClass $eventdata the event data submitted by the message sender plus $eventdata->savedmessageid
39 * @return true if ok, false if error
41 function send_message($eventdata){
44 // Skip any messaging of suspended and deleted users.
45 if ($eventdata->userto->auth === 'nologin' or $eventdata->userto->suspended or $eventdata->userto->deleted) {
49 if (!empty($CFG->noemailever)) {
50 // hidden setting for development sites, set in config.php if needed
51 debugging('$CFG->noemailever is active, no jabber message sent.', DEBUG_MINIMAL);
56 // No connection to external servers allowed in phpunit tests.
60 //hold onto jabber id preference because /admin/cron.php sends a lot of messages at once
61 static $jabberaddresses = array();
63 if (!array_key_exists($eventdata->userto->id, $jabberaddresses)) {
64 $jabberaddresses[$eventdata->userto->id] = get_user_preferences('message_processor_jabber_jabberid', null, $eventdata->userto->id);
66 $jabberaddress = $jabberaddresses[$eventdata->userto->id];
68 //calling s() on smallmessage causes Jabber to display things like < Jabber != a browser
69 $jabbermessage = fullname($eventdata->userfrom).': '.$eventdata->smallmessage;
71 if (!empty($eventdata->contexturl)) {
72 $jabbermessage .= "\n".get_string('view').': '.$eventdata->contexturl;
75 $jabbermessage .= "\n(".get_string('noreply','message').')';
77 $conn = new \BirknerAlex\XMPPHP\XMPP($CFG->jabberhost,$CFG->jabberport,$CFG->jabberusername,$CFG->jabberpassword,'moodle',$CFG->jabberserver);
79 // No need to track the presence during the sending message process.
80 $conn->track_presence = false;
83 //$conn->useEncryption(false);
85 $conn->processUntil('session_start');
87 $conn->message($jabberaddress, $jabbermessage);
89 } catch(\BirknerAlex\XMPPHP\Exception $e) {
90 debugging($e->getMessage());
97 * Creates necessary fields in the messaging config form.
99 * @param array $preferences An array of user preferences
101 function config_form($preferences){
104 if (!$this->is_system_configured()) {
105 return get_string('notconfigured','message_jabber');
107 return get_string('jabberid', 'message_jabber').': <input size="30" name="jabber_jabberid" value="'.s($preferences->jabber_jabberid).'" />';
112 * Parses the submitted form data and saves it into preferences array.
114 * @param stdClass $form preferences form class
115 * @param array $preferences preferences array
117 function process_form($form, &$preferences){
118 if (isset($form->jabber_jabberid) && !empty($form->jabber_jabberid)) {
119 $preferences['message_processor_jabber_jabberid'] = $form->jabber_jabberid;
124 * Loads the config data from database to put on the form during initial form display
126 * @param array $preferences preferences array
127 * @param int $userid the user id
129 function load_data(&$preferences, $userid){
130 $preferences->jabber_jabberid = get_user_preferences( 'message_processor_jabber_jabberid', '', $userid);
134 * Tests whether the Jabber settings have been configured
135 * @return boolean true if Jabber is configured
137 function is_system_configured() {
139 return (!empty($CFG->jabberhost) && !empty($CFG->jabberport) && !empty($CFG->jabberusername) && !empty($CFG->jabberpassword));
143 * Tests whether the Jabber settings have been configured on user level
144 * @param object $user the user object, defaults to $USER.
145 * @return bool has the user made all the necessary settings
146 * in their profile to allow this plugin to be used.
148 function is_user_configured($user = null) {
151 if (is_null($user)) {
154 return (bool)get_user_preferences('message_processor_jabber_jabberid', null, $user->id);
160 * $f = fopen('/tmp/event_jabberx', 'a+');
161 fwrite($f, date('l dS \of F Y h:i:s A')."\n");
162 fwrite($f, "from: $message->userfromid\n");
163 fwrite($f, "userto: $message->usertoid\n");
164 fwrite($f, "subject: $message->subject\n");
168 $savemessage = new stdClass();
169 $savemessage->useridfrom = 3;
170 $savemessage->useridto = 2;
171 $savemessage->subject = 'IM';
172 $savemessage->fullmessage = 'full';
173 $savemessage->timecreated = time();
176 $a = new message_output_jabber();
178 $a->send_message($savemessage);