MDL-65743 lib: add XMPPHP namespace and update calls
[moodle.git] / message / output / jabber / message_output_jabber.php
CommitLineData
3b120e46 1<?php
6fbd60ef
AD
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/>.
3b120e46 16
17/**
6fbd60ef 18 * Jabber message processor to send messages by jabber
3b120e46 19 *
6fbd60ef
AD
20 * @package message_jabber
21 * @copyright 2008 Luis Rodrigues
22 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
3b120e46 23 */
24
3b120e46 25require_once($CFG->dirroot.'/message/output/lib.php');
6fbd60ef
AD
26/**
27 * The jabber message processor
28 *
29 * @package message_jabber
30 * @copyright 2008 Luis Rodrigues
31 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
32 */
3b120e46 33class message_output_jabber extends message_output {
1d422980 34
3b120e46 35 /**
6fbd60ef
AD
36 * Processes the message and sends a notification via jabber
37 *
38 * @param stdClass $eventdata the event data submitted by the message sender plus $eventdata->savedmessageid
3b120e46 39 * @return true if ok, false if error
40 */
fe983847
AD
41 function send_message($eventdata){
42 global $CFG;
1d422980 43
f007e899
PS
44 // Skip any messaging of suspended and deleted users.
45 if ($eventdata->userto->auth === 'nologin' or $eventdata->userto->suspended or $eventdata->userto->deleted) {
46 return true;
47 }
48
f15097b5
RK
49 if (!empty($CFG->noemailever)) {
50 // hidden setting for development sites, set in config.php if needed
f007e899 51 debugging('$CFG->noemailever is active, no jabber message sent.', DEBUG_MINIMAL);
f15097b5 52 return true;
838a8eb1 53 }
1d422980 54
f007e899
PS
55 if (PHPUNIT_TEST) {
56 // No connection to external servers allowed in phpunit tests.
ca8fe0b0
PS
57 return true;
58 }
59
f15097b5
RK
60 //hold onto jabber id preference because /admin/cron.php sends a lot of messages at once
61 static $jabberaddresses = array();
62
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);
65 }
66 $jabberaddress = $jabberaddresses[$eventdata->userto->id];
67
68 //calling s() on smallmessage causes Jabber to display things like &lt; Jabber != a browser
69 $jabbermessage = fullname($eventdata->userfrom).': '.$eventdata->smallmessage;
70
71 if (!empty($eventdata->contexturl)) {
72 $jabbermessage .= "\n".get_string('view').': '.$eventdata->contexturl;
73 }
74
75 $jabbermessage .= "\n(".get_string('noreply','message').')';
76
1c0baed9 77 $conn = new \BirknerAlex\XMPPHP\XMPP($CFG->jabberhost,$CFG->jabberport,$CFG->jabberusername,$CFG->jabberpassword,'moodle',$CFG->jabberserver);
f15097b5 78
99433e4d
FM
79 // No need to track the presence during the sending message process.
80 $conn->track_presence = false;
81
f15097b5
RK
82 try {
83 //$conn->useEncryption(false);
84 $conn->connect();
85 $conn->processUntil('session_start');
86 $conn->presence();
87 $conn->message($jabberaddress, $jabbermessage);
88 $conn->disconnect();
1c0baed9 89 } catch(\BirknerAlex\XMPPHP\Exception $e) {
f15097b5
RK
90 debugging($e->getMessage());
91 return false;
92 }
3b120e46 93 return true;
94 }
95
1d422980 96 /**
3b120e46 97 * Creates necessary fields in the messaging config form.
6fbd60ef
AD
98 *
99 * @param array $preferences An array of user preferences
3b120e46 100 */
d18b1bbd 101 function config_form($preferences){
861fa912 102 global $CFG;
38d93cb0 103
cd8f78c8 104 if (!$this->is_system_configured()) {
861fa912
AD
105 return get_string('notconfigured','message_jabber');
106 } else {
1f6d1ebf 107 return get_string('jabberid', 'message_jabber').': <input size="30" name="jabber_jabberid" value="'.s($preferences->jabber_jabberid).'" />';
861fa912 108 }
3b120e46 109 }
110
111 /**
6fbd60ef
AD
112 * Parses the submitted form data and saves it into preferences array.
113 *
114 * @param stdClass $form preferences form class
1d422980 115 * @param array $preferences preferences array
3b120e46 116 */
d18b1bbd 117 function process_form($form, &$preferences){
f8351d09 118 if (isset($form->jabber_jabberid) && !empty($form->jabber_jabberid)) {
6c0f13a5
AD
119 $preferences['message_processor_jabber_jabberid'] = $form->jabber_jabberid;
120 }
3b120e46 121 }
122
123 /**
6fbd60ef
AD
124 * Loads the config data from database to put on the form during initial form display
125 *
1d422980 126 * @param array $preferences preferences array
3b120e46 127 * @param int $userid the user id
128 */
129 function load_data(&$preferences, $userid){
130 $preferences->jabber_jabberid = get_user_preferences( 'message_processor_jabber_jabberid', '', $userid);
131 }
132
35eed0ca
AD
133 /**
134 * Tests whether the Jabber settings have been configured
135 * @return boolean true if Jabber is configured
136 */
cd8f78c8 137 function is_system_configured() {
35eed0ca
AD
138 global $CFG;
139 return (!empty($CFG->jabberhost) && !empty($CFG->jabberport) && !empty($CFG->jabberusername) && !empty($CFG->jabberpassword));
140 }
141
21e6c822
RK
142 /**
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.
147 */
148 function is_user_configured($user = null) {
149 global $USER;
150
151 if (is_null($user)) {
152 $user = $USER;
153 }
154 return (bool)get_user_preferences('message_processor_jabber_jabberid', null, $user->id);
155 }
3b120e46 156}
b3f5f2c7 157
158/*
1d422980 159 *
b3f5f2c7 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");
165 fclose($f);
166
167
8e803c3f 168$savemessage = new stdClass();
b3f5f2c7 169 $savemessage->useridfrom = 3;
170 $savemessage->useridto = 2;
171 $savemessage->subject = 'IM';
172 $savemessage->fullmessage = 'full';
173 $savemessage->timecreated = time();
174
175
176$a = new message_output_jabber();
177
178$a->send_message($savemessage);
179* */
1d422980 180