3 ///////////////////////////////////////////////////////////////////////////
5 // NOTICE OF COPYRIGHT //
7 // Moodle - Modular Object-Oriented Dynamic Learning Environment //
8 // http://moodle.com //
10 // Copyright (C) 1999 onwards Martin Dougiamas http://moodle.com //
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. //
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: //
22 // http://www.gnu.org/copyleft/gpl.html //
24 ///////////////////////////////////////////////////////////////////////////
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
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 {
40 * Processes the message (sends using jabber).
41 * @param object $message the message to be sent
42 * @return true if ok, false if error
44 function send_message($message){
47 if (!$userfrom = $DB->get_record('user', array('id' => $message->useridfrom))) {
50 if (!$userto = $DB->get_record('user', array('id' => $message->useridto))) {
53 if (!$jabberaddress = get_user_preferences('message_processor_jabber_jabberid', $userto->email, $userto->id)) {
54 $jabberaddress = $userto->email;
56 $jabbermessage = fullname($userfrom).': '.$message->smallmessage;
58 $conn = new XMPPHP_XMPP($CFG->jabberhost,$CFG->jabberport,$CFG->jabberusername,$CFG->jabberpassword,'moodle',$CFG->jabberserver);
61 //$conn->useEncryption(false);
63 $conn->processUntil('session_start');
65 $conn->message($jabberaddress, $jabbermessage);
67 } catch(XMPPHP_Exception $e) {
68 debugging($e->getMessage());
76 * Creates necessary fields in the messaging config form.
77 * @param object $mform preferences form class
79 function config_form($preferences){
80 return get_string('jabberid', 'message_jabber').': <input size="30" name="jabber_jabberid" value="'.$preferences->jabber_jabberid.'" />';
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
88 function process_form($form, &$preferences){
89 $preferences['message_processor_jabber_jabberid'] = $form->jabber_jabberid;
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
97 function load_data(&$preferences, $userid){
98 $preferences->jabber_jabberid = get_user_preferences( 'message_processor_jabber_jabberid', '', $userid);
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");
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);