a6f9f65d5da2af1e0be7209e7abde8e48be49fe1
[moodle.git] / message / output / jabber / message_output_jabber.php
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
28  *
29  * @author Luis Rodrigues
30  * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
31  * @package
32  */
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
43      */
44     function send_message($eventdata){
45         global $CFG;
47         if (message_output_jabber::_jabber_configured()) {
48             if (!empty($CFG->noemailever)) {
49                 // hidden setting for development sites, set in config.php if needed
50                 debugging('$CFG->noemailever active, no jabber message sent.', DEBUG_MINIMAL);
51                 return true;
52             }
54             //hold onto jabber id preference because /admin/cron.php sends a lot of messages at once
55             static $jabberaddresses = array();
57             if (!array_key_exists($eventdata->userto->id, $jabberaddresses)) {
58                 $jabberaddresses[$eventdata->userto->id] = get_user_preferences('message_processor_jabber_jabberid', $eventdata->userto->email, $eventdata->userto->id);
59             }
60             $jabberaddress = $jabberaddresses[$eventdata->userto->id];
62             $jabbermessage = fullname($eventdata->userfrom).': '.$eventdata->smallmessage;
64             if (!empty($eventdata->contexturl)) {
65                 $jabbermessage .= "\n".get_string('view').': '.$eventdata->contexturl;
66             }
68             $jabbermessage .= "\n(".get_string('noreply','message').')';
70             $conn = new XMPPHP_XMPP($CFG->jabberhost,$CFG->jabberport,$CFG->jabberusername,$CFG->jabberpassword,'moodle',$CFG->jabberserver);
72             try {
73                 //$conn->useEncryption(false);
74                 $conn->connect();
75                 $conn->processUntil('session_start');
76                 $conn->presence();
77                 $conn->message($jabberaddress, $jabbermessage);
78                 $conn->disconnect();
79             } catch(XMPPHP_Exception $e) {
80                 debugging($e->getMessage());
81                 return false;
82             }
83         }
85         //note that we're reporting success if message was sent or if Jabber simply isnt configured
86         return true;
87     }
89     /**
90      * Creates necessary fields in the messaging config form.
91      * @param object $mform preferences form class
92      */
93     function config_form($preferences){
94         global $CFG;
96         if (!message_output_jabber::_jabber_configured()) {
97             return get_string('notconfigured','message_jabber');
98         } else {
99             return get_string('jabberid', 'message_jabber').': <input size="30" name="jabber_jabberid" value="'.$preferences->jabber_jabberid.'" />';
100         }
101     }
103     /**
104      * Parses the form submitted data and saves it into preferences array.
105      * @param object $mform preferences form class
106      * @param array $preferences preferences array
107      */
108     function process_form($form, &$preferences){
109         $preferences['message_processor_jabber_jabberid'] = $form->jabber_jabberid;
110     }
112     /**
113      * Loads the config data from database to put on the form (initial load)
114      * @param array $preferences preferences array
115      * @param int $userid the user id
116      */
117     function load_data(&$preferences, $userid){
118         $preferences->jabber_jabberid = get_user_preferences( 'message_processor_jabber_jabberid', '', $userid);
119     }
121     /**
122      * Tests whether the Jabber settings have been configured
123      * @return boolean true if Jabber is configured
124      */
125     private function _jabber_configured() {
126         global $CFG;
127         return (!empty($CFG->jabberhost) && !empty($CFG->jabberport) && !empty($CFG->jabberusername) && !empty($CFG->jabberpassword));
128     }
132 /*
133  *
134  *         $f = fopen('/tmp/event_jabberx', 'a+');
135         fwrite($f, date('l dS \of F Y h:i:s A')."\n");
136         fwrite($f, "from: $message->userfromid\n");
137         fwrite($f, "userto: $message->usertoid\n");
138         fwrite($f, "subject: $message->subject\n");
139         fclose($f);
142 $savemessage = new stdClass();
143     $savemessage->useridfrom        = 3;
144     $savemessage->useridto          = 2;
145     $savemessage->subject           = 'IM';
146     $savemessage->fullmessage       = 'full';
147     $savemessage->timecreated       = time();
150 $a = new message_output_jabber();
152 $a->send_message($savemessage);
153 * */