MDL-30948 stop messaging suspended and deleted users
[moodle.git] / message / output / jabber / message_output_jabber.php
blobc4dbbba1db7fd4c6ec5eced6e122938d6f1def0d
1 <?php
3 ///////////////////////////////////////////////////////////////////////////
4 // //
5 // NOTICE OF COPYRIGHT //
6 // //
7 // Moodle - Modular Object-Oriented Dynamic Learning Environment //
8 // http://moodle.com //
9 // //
10 // Copyright (C) 1999 onwards Martin Dougiamas http://moodle.com //
11 // //
12 // This program is free software; you can redistribute it and/or modify //
13 // it under the terms of the GNU General Public License as published by //
14 // the Free Software Foundation; either version 2 of the License, or //
15 // (at your option) any later version. //
16 // //
17 // This program is distributed in the hope that it will be useful, //
18 // but WITHOUT ANY WARRANTY; without even the implied warranty of //
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
20 // GNU General Public License for more details: //
21 // //
22 // http://www.gnu.org/copyleft/gpl.html //
23 // //
24 ///////////////////////////////////////////////////////////////////////////
26 /**
27 * Jabber message processor - send a given message by jabber
29 * @author Luis Rodrigues
30 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
31 * @package
34 require_once($CFG->dirroot.'/message/output/lib.php');
35 require_once($CFG->libdir.'/jabber/XMPP/XMPP.php');
37 class message_output_jabber extends message_output {
39 /**
40 * Processes the message (sends using jabber).
41 * @param object $eventdata the event data submitted by the message sender plus $eventdata->savedmessageid
42 * @return true if ok, false if error
44 function send_message($eventdata){
45 global $CFG;
47 if (!empty($CFG->noemailever)) {
48 // hidden setting for development sites, set in config.php if needed
49 debugging('$CFG->noemailever active, no jabber message sent.', DEBUG_MINIMAL);
50 return true;
53 // skip any messaging suspended and deleted users
54 if ($eventdata->userto->auth === 'nologin' or $eventdata->userto->suspended or $eventdata->userto->deleted) {
55 return true;
58 //hold onto jabber id preference because /admin/cron.php sends a lot of messages at once
59 static $jabberaddresses = array();
61 if (!array_key_exists($eventdata->userto->id, $jabberaddresses)) {
62 $jabberaddresses[$eventdata->userto->id] = get_user_preferences('message_processor_jabber_jabberid', null, $eventdata->userto->id);
64 $jabberaddress = $jabberaddresses[$eventdata->userto->id];
66 //calling s() on smallmessage causes Jabber to display things like &lt; Jabber != a browser
67 $jabbermessage = fullname($eventdata->userfrom).': '.$eventdata->smallmessage;
69 if (!empty($eventdata->contexturl)) {
70 $jabbermessage .= "\n".get_string('view').': '.$eventdata->contexturl;
73 $jabbermessage .= "\n(".get_string('noreply','message').')';
75 $conn = new XMPPHP_XMPP($CFG->jabberhost,$CFG->jabberport,$CFG->jabberusername,$CFG->jabberpassword,'moodle',$CFG->jabberserver);
77 try {
78 //$conn->useEncryption(false);
79 $conn->connect();
80 $conn->processUntil('session_start');
81 $conn->presence();
82 $conn->message($jabberaddress, $jabbermessage);
83 $conn->disconnect();
84 } catch(XMPPHP_Exception $e) {
85 debugging($e->getMessage());
86 return false;
88 return true;
91 /**
92 * Creates necessary fields in the messaging config form.
93 * @param object $mform preferences form class
95 function config_form($preferences){
96 global $CFG;
98 if (!$this->is_system_configured()) {
99 return get_string('notconfigured','message_jabber');
100 } else {
101 return get_string('jabberid', 'message_jabber').': <input size="30" name="jabber_jabberid" value="'.s($preferences->jabber_jabberid).'" />';
106 * Parses the form submitted data and saves it into preferences array.
107 * @param object $mform preferences form class
108 * @param array $preferences preferences array
110 function process_form($form, &$preferences){
111 if (isset($form->jabber_jabberid) && !empty($form->jabber_jabberid)) {
112 $preferences['message_processor_jabber_jabberid'] = $form->jabber_jabberid;
117 * Loads the config data from database to put on the form (initial load)
118 * @param array $preferences preferences array
119 * @param int $userid the user id
121 function load_data(&$preferences, $userid){
122 $preferences->jabber_jabberid = get_user_preferences( 'message_processor_jabber_jabberid', '', $userid);
126 * Tests whether the Jabber settings have been configured
127 * @return boolean true if Jabber is configured
129 function is_system_configured() {
130 global $CFG;
131 return (!empty($CFG->jabberhost) && !empty($CFG->jabberport) && !empty($CFG->jabberusername) && !empty($CFG->jabberpassword));
135 * Tests whether the Jabber settings have been configured on user level
136 * @param object $user the user object, defaults to $USER.
137 * @return bool has the user made all the necessary settings
138 * in their profile to allow this plugin to be used.
140 function is_user_configured($user = null) {
141 global $USER;
143 if (is_null($user)) {
144 $user = $USER;
146 return (bool)get_user_preferences('message_processor_jabber_jabberid', null, $user->id);
152 * $f = fopen('/tmp/event_jabberx', 'a+');
153 fwrite($f, date('l dS \of F Y h:i:s A')."\n");
154 fwrite($f, "from: $message->userfromid\n");
155 fwrite($f, "userto: $message->usertoid\n");
156 fwrite($f, "subject: $message->subject\n");
157 fclose($f);
160 $savemessage = new stdClass();
161 $savemessage->useridfrom = 3;
162 $savemessage->useridto = 2;
163 $savemessage->subject = 'IM';
164 $savemessage->fullmessage = 'full';
165 $savemessage->timecreated = time();
168 $a = new message_output_jabber();
170 $a->send_message($savemessage);
171 * */