Difference between revisions of "Jabber Send Message"

From YobiWiki
Jump to navigation Jump to search
m
 
m (Reverted edits by Etegohy (Talk) to last revision by PhilippeTeuwen)
 
(6 intermediate revisions by the same user not shown)
Line 1: Line 1:
  +
See also sendxmpp in [[Alert notifications]]
  +
----
 
Envoi de messages Jabber via une page PHP
 
Envoi de messages Jabber via une page PHP
   
Line 10: Line 12:
 
jabber_sendmsg 0.0.2 http://wiki.teuwen.org/index.php/JabberSendMessage/
 
jabber_sendmsg 0.0.2 http://wiki.teuwen.org/index.php/JabberSendMessage/
 
Copyright (C) 2003 Philippe Teuwen <phil_at_teuwen.org>
 
Copyright (C) 2003 Philippe Teuwen <phil_at_teuwen.org>
  +
License: GPLv3 or later
 
This program is free software; you can redistribute it and/or
 
modify it under the terms of the GNU General Public License
 
as published by the Free Software Foundation; either version 2
 
of the License, or (at your option) any later version.
 
 
This program is distributed in the hope that it will be useful,
 
but WITHOUT ANY WARRANTY; without even the implied warranty of
 
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 
GNU General Public License for more details.
 
 
You should have received a copy of the GNU General Public License
 
along with this program; if not, write to the Free Software
 
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA , USA.
 
 
 
 
ChangeLog
 
ChangeLog

Latest revision as of 21:32, 24 November 2010

See also sendxmpp in Alert notifications


Envoi de messages Jabber via une page PHP

Prérequis: le fichier class.jabber.php doit accompagner le script PHP sur le serveur.
Ce script PHP est utilisable de deux façons côté client:
Soit interactivement soit dans un script.

Le script jabber_sendmsg.php:

Attention, le copier depuis le source de la page!!

<?php
/*
jabber_sendmsg 0.0.2  http://wiki.teuwen.org/index.php/JabberSendMessage/
Copyright (C) 2003  Philippe Teuwen <phil_at_teuwen.org>
License:  GPLv3 or later

ChangeLog
0.0.2 Handle delivery error, add UTF8 support & more resistant to input
0.0.1 Initial release
*/

require("class.jabber.php");

$JABBER = new Jabber;

$JABBER->server         = "jabber.reseaucitoyen.be";
$JABBER->port           = 5222;
$JABBER->username       = "rcbot";
$JABBER->password       = "xxxxxx";
$JABBER->resource       = "ClassJabberPHP";
//For debug purpose
//$JABBER->enable_logging = TRUE;
//$JABBER->log_filename   = '/var/log/jabber/rcbot.txt';

function HtmlBegin() {
    print "<html>";
    print "  <head>";
    print "    <title>Envoi de messages Jabber</title>";
    print "  </head>";
    print "  <body style=\"color: rgb(0, 0, 0); background-color: rgb(255, 255, 204);\">";

print "

"; } function HtmlForm() { print "

";

    print "<form action=\"jabber_sendmsg.php\" method=\"POST\">";

print "

"; print ""; print ""; print ""; print ""; print "
DE: <input type=\"text\" name=\"from\" value=\"\">
A: <input type=\"text\" name=\"jid\" value=\"\">
MSG:<textarea rows=\"5\" name=\"body\" cols=\"20\"></TEXTAREA>
<input type=\"hidden\" name=\"form\" value=\"true\"><input type=\"submit\" value=\"Envoi\">

";

    print "</form>";

print "

"; // print ""; // print "ps: évitez les caractères accentués"; // print ""; } function HtmlEnd() { print "

";

    print "  </body>";
    print "</html>";
    }

if( $form != 'true' && $jid ==  && $body ==  ) {
    HtmlBegin();
    HtmlForm();
    HtmlEnd();
    die();
    }

if( $jid ==  || $body ==  ) {
    // this is a bad thing
    if( $form != 'true' ) {
        print "ERROR: jid or body missing\n";
        die();
        }
   HtmlBegin();
   print "Erreur: Dites-moi au moins à qui et ce que je suis sensé envoyer!
"; HtmlForm(); HtmlEnd(); die(); } $jid = array_shift(explode("@$JABBER->server",$jid)); if( preg_match("/@/",$jid) ) { if( $form != 'true' ) { print "ERROR: accept only local recipients\n"; die(); } HtmlBegin(); print "Erreur: Je refuse d'envoyer qqch à des utilisateurs autres que @$JABBER->server!
"; HtmlForm(); HtmlEnd(); die(); } if( preg_match("/[^a-zA-Z0-9_.-]/",$jid) ) { if( $form != 'true' ) { print "ERROR: malformed recipient\n"; die(); } HtmlBegin(); print "Erreur: Le nom du récipiendaire semble contenir des caractères non autoris& HtmlForm(); HtmlEnd(); die(); } $jid.="@$JABBER->server"; $message = "Message "; if( $from != ) { $from = utf8_encode(htmlspecialchars($from)); $message .= "soi-disant de ".$from.":\n"; } else $message .= "anonyme:\n"; $message .= utf8_encode(htmlspecialchars($body)); $JABBER->Connect() or die("ERROR: couldn't connect!\n"); $JABBER->SendAuth() or die("ERROR: couldn't authenticate!\n"); $return = $JABBER->SendMessage($jid, "normal", NULL, array( // body, thread... whatever "body" => $message ) ); $JABBER->Disconnect(); if( $form != 'true' ) { if( $return == TRUE ) print "SUCCESS\n"; else print "ERROR: couldn't send message!\n"; die(); } HtmlBegin(); if( $return == TRUE ) print "Message envoyé!
"; else print "Erreur: Je n'ai pu envoyer le message\n"; HtmlForm(); HtmlEnd(); ?>

Exemple de code HTML

à incorporer à une page web pour une utilisation interactive

<a href="" onClick=
  "javascript:window.open(
  'utils/jabber_sendmsg.php',
  'SendMsgs',
  'width=300,height=300,menubar=no,scrollable=no,resizable=yes,left=0,top=0,screenX=0,screenY=0'
  )">
  Envoi de messages Jabber
</a>

cf http://jabber.reseaucitoyen.be lien "Envoi de messages Jabber"

Exemple de requête à incorporer dans un script

wget -O - -q "http://jabber.reseaucitoyen.be/utils/jabber_sendmsg.php?jid=phil&body=alerte"

En cas d'utilisation depuis un script, il y a deux types de messages de retour possible: "SUCCESS" et "ERROR:..."

Utilisateur robot

Nous avons vu qu'il est nécessaire qu'un compte existe pour envoyer les messages, "rcbot" dans notre exemple.
Il peut être utile de lui donner une règle de filtre telle qu'elle prévienne les malheureux qui tenteraient de répondre aux messages qu'il envoie.

<query xmlns='jabber:iq:filter' xdbns='jabber:iq:filter'>
 <rule name='Refuse all'>
   <reply>Je ne suis qu'un robot, il est vain de vouloir parler avec moi!
   </reply>
   <unavailable/>
 </rule>
</query>