PageRenderTime 41ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/examples/echo_bot.php

http://github.com/abhinavsingh/JAXL
PHP | 161 lines | 64 code | 26 blank | 71 comment | 3 complexity | 9214d727376b26a275dcae8eb341cf74 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. require dirname(__FILE__) . '/_bootstrap.php';
  39. // Run as:
  40. // php examples/echo_bot.php root@localhost password
  41. // php examples/echo_bot.php root@localhost password DIGEST-MD5
  42. // php examples/echo_bot.php localhost "" ANONYMOUS
  43. if ($argc < 3) {
  44. echo "Usage: $argv[0] jid pass auth_type".PHP_EOL;
  45. exit;
  46. }
  47. //
  48. // initialize JAXL object with initial config
  49. //
  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' => isset($argv[3]) ? $argv[3] : 'PLAIN',
  64. 'log_level' => JAXLLogger::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. function on_auth_success_callback()
  76. {
  77. global $client;
  78. JAXLLogger::info("got on_auth_success cb, jid ".$client->full_jid->to_string());
  79. // fetch roster list
  80. $client->get_roster();
  81. // fetch vcard
  82. $client->get_vcard();
  83. // set status
  84. $client->set_status("available!", "dnd", 10);
  85. }
  86. $client->add_cb('on_auth_success', 'on_auth_success_callback');
  87. // by default JAXL instance catches incoming roster list results and updates
  88. // roster list is parsed/cached and an event 'on_roster_update' is emitted
  89. function on_roster_update_callback()
  90. {
  91. //global $client;
  92. //print_r($client->roster);
  93. }
  94. $client->add_cb('on_roster_update', 'on_roster_update_callback');
  95. function on_auth_failure_callback($reason)
  96. {
  97. global $client;
  98. $client->send_end_stream();
  99. JAXLLogger::info("got on_auth_failure cb with reason $reason");
  100. }
  101. $client->add_cb('on_auth_failure', 'on_auth_failure_callback');
  102. function on_chat_message_callback($stanza)
  103. {
  104. global $client;
  105. // echo back incoming chat message stanza
  106. $stanza->to = $stanza->from;
  107. $stanza->from = $client->full_jid->to_string();
  108. $client->send($stanza);
  109. }
  110. $client->add_cb('on_chat_message', 'on_chat_message_callback');
  111. function on_presence_stanza_callback($stanza)
  112. {
  113. global $client;
  114. $type = ($stanza->type ? $stanza->type : "available");
  115. $show = ($stanza->show ? $stanza->show : "???");
  116. JAXLLogger::info($stanza->from." is now ".$type." ($show)");
  117. if ($type == "available") {
  118. // fetch vcard
  119. $client->get_vcard($stanza->from);
  120. }
  121. }
  122. $client->add_cb('on_presence_stanza', 'on_presence_stanza_callback');
  123. function on_disconnect_callback()
  124. {
  125. JAXLLogger::info("got on_disconnect cb");
  126. }
  127. $client->add_cb('on_disconnect', 'on_disconnect_callback');
  128. //
  129. // finally start configured xmpp stream
  130. //
  131. $client->start(array(
  132. '--with-debug-shell' => true,
  133. '--with-unix-sock' => true
  134. ));
  135. echo "done".PHP_EOL;