PageRenderTime 40ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/examples/echo_bot.php

https://github.com/onurdegerli/JAXL
PHP | 149 lines | 53 code | 25 blank | 71 comment | 3 complexity | d1e62122b4ead15ab69bd9376cc61bdf MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * Jaxl (Jabber XMPP Library)
  4. *
  5. * Copyright (c) 2009-2012, Abhinav Singh <me@abhinavsingh.com>.
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * * Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * * Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * * Neither the name of Abhinav Singh nor the names of his
  21. * contributors may be used to endorse or promote products derived
  22. * from this software without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  25. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  26. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  27. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  28. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  29. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  30. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  31. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  32. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRIC
  33. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  34. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  35. * POSSIBILITY OF SUCH DAMAGE.
  36. *
  37. */
  38. // Run as:
  39. // php examples/echo_bot.php root@localhost password
  40. // php examples/echo_bot.php root@localhost password DIGEST-MD5
  41. // php examples/echo_bot.php localhost "" ANONYMOUS
  42. if($argc < 3) {
  43. echo "Usage: $argv[0] jid pass auth_type\n";
  44. exit;
  45. }
  46. //
  47. // initialize JAXL object with initial config
  48. //
  49. require_once 'jaxl.php';
  50. $client = new JAXL(array(
  51. // (required) credentials
  52. 'jid' => $argv[1],
  53. 'pass' => $argv[2],
  54. // (optional) srv lookup is done if not provided
  55. //'host' => 'xmpp.domain.tld',
  56. // (optional) result from srv lookup used by default
  57. //'port' => 5222,
  58. // (optional) defaults to false
  59. //'force_tls' => true,
  60. // (optional)
  61. //'resource' => 'resource',
  62. // (optional) defaults to PLAIN if supported, else other methods will be automatically tried
  63. 'auth_type' => @$argv[3] ? $argv[3] : 'PLAIN',
  64. 'log_level' => JAXL_INFO
  65. ));
  66. //
  67. // required XEP's
  68. //
  69. $client->require_xep(array(
  70. '0199' // XMPP Ping
  71. ));
  72. //
  73. // add necessary event callbacks here
  74. //
  75. $client->add_cb('on_auth_success', function() {
  76. global $client;
  77. _info("got on_auth_success cb, jid ".$client->full_jid->to_string());
  78. // fetch roster list
  79. $client->get_roster();
  80. // fetch vcard
  81. $client->get_vcard();
  82. // set status
  83. $client->set_status("available!", "dnd", 10);
  84. });
  85. // by default JAXL instance catches incoming roster list results and updates
  86. // roster list is parsed/cached and an event 'on_roster_update' is emitted
  87. $client->add_cb('on_roster_update', function() {
  88. //global $client;
  89. //print_r($client->roster);
  90. });
  91. $client->add_cb('on_auth_failure', function($reason) {
  92. global $client;
  93. _info("got on_auth_failure cb with reason $reason");
  94. $client->send_end_stream();
  95. });
  96. $client->add_cb('on_chat_message', function($stanza) {
  97. global $client;
  98. // echo back incoming chat message stanza
  99. $stanza->to = $stanza->from;
  100. $stanza->from = $client->full_jid->to_string();
  101. $client->send($stanza);
  102. });
  103. $client->add_cb('on_presence_stanza', function($stanza) {
  104. global $client;
  105. $type = ($stanza->type ? $stanza->type : "available");
  106. $show = ($stanza->show ? $stanza->show : "???");
  107. _info($stanza->from." is now ".$type." ($show)");
  108. if($type == "available") {
  109. // fetch vcard
  110. $client->get_vcard($stanza->from);
  111. }
  112. });
  113. $client->add_cb('on_disconnect', function() {
  114. _info("got on_disconnect cb");
  115. });
  116. //
  117. // finally start configured xmpp stream
  118. //
  119. $client->start(array(
  120. '--with-debug-shell' => true,
  121. '--with-unix-sock' => true
  122. ));
  123. echo "done\n";
  124. ?>