message MDL-24562 made jabber message notifications work
[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 $message the message to be sent
42      * @return true if ok, false if error
43      */
44     function send_message($message){
45         global $DB, $CFG;
47         if (!$userfrom = $DB->get_record('user', array('id' => $message->useridfrom))) {
48             return false;
49         }
50         if (!$userto = $DB->get_record('user', array('id' => $message->useridto))) {
51             return false;
52         }
53         if (!$jabberaddress = get_user_preferences('message_processor_jabber_jabberid', $userto->email, $userto->id)) {
54             $jabberaddress = $userto->email;
55         }
56         $jabbermessage = fullname($userfrom).': '.$message->smallmessage;
58         $conn = new XMPPHP_XMPP($CFG->jabberhost,$CFG->jabberport,$CFG->jabberusername,$CFG->jabberpassword,'moodle',$CFG->jabberserver);
60         try {
61             //$conn->useEncryption(false);
62             $conn->connect();
63             $conn->processUntil('session_start');
64             $conn->presence();
65             $conn->message($jabberaddress, $jabbermessage);
66             $conn->disconnect();
67         } catch(XMPPHP_Exception $e) {
68             debugging($e->getMessage());
69             return false;
70         }
72         return true;
73     }
75     /**
76      * Creates necessary fields in the messaging config form.
77      * @param object $mform preferences form class
78      */
79     function config_form($preferences){
80         return get_string('jabberid', 'message_jabber').': <input size="30" name="jabber_jabberid" value="'.$preferences->jabber_jabberid.'" />';
81     }
83     /**
84      * Parses the form submitted data and saves it into preferences array.
85      * @param object $mform preferences form class
86      * @param array $preferences preferences array
87      */
88     function process_form($form, &$preferences){
89         $preferences['message_processor_jabber_jabberid'] = $form->jabber_jabberid;
90     }
92     /**
93      * Loads the config data from database to put on the form (initial load)
94      * @param array $preferences preferences array
95      * @param int $userid the user id
96      */
97     function load_data(&$preferences, $userid){
98         $preferences->jabber_jabberid = get_user_preferences( 'message_processor_jabber_jabberid', '', $userid);
99     }
103 /*
104  *
105  *         $f = fopen('/tmp/event_jabberx', 'a+');
106         fwrite($f, date('l dS \of F Y h:i:s A')."\n");
107         fwrite($f, "from: $message->userfromid\n");
108         fwrite($f, "userto: $message->usertoid\n");
109         fwrite($f, "subject: $message->subject\n");
110         fclose($f);
113 $savemessage = new stdClass();
114     $savemessage->useridfrom        = 3;
115     $savemessage->useridto          = 2;
116     $savemessage->subject           = 'IM';
117     $savemessage->fullmessage       = 'full';
118     $savemessage->timecreated       = time();
121 $a = new message_output_jabber();
123 $a->send_message($savemessage);
124 * */