Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

oops, i was already in trunk. move things around some more.

git-svn-id: https://xmpp4r-simple.googlecode.com/svn/trunk@13 1e3c5029-9921-0410-9e82-436f4d538387
  • Loading branch information...
commit 9303f7efca3ae1e19d476fcee6012c3bbdfbf27b 1 parent e246005
romeda authored
View
0  trunk/CHANGELOG → CHANGELOG
File renamed without changes
View
0  trunk/COPYING → COPYING
File renamed without changes
View
0  trunk/README → README
File renamed without changes
View
0  trunk/Rakefile → Rakefile
File renamed without changes
View
0  trunk/lib/xmpp4r-simple.rb → lib/xmpp4r-simple.rb
File renamed without changes
View
6 tags/xmpp4r-simple-0.8.0/CHANGELOG
@@ -1,6 +0,0 @@
-xmpp4r-simple (0.8.0)
-
- [ Blaine Cook ]
- * initial import
-
--- Blaine Cook <blaine@obvious.com> Wed, 08 Nov 2006 20:42:42 -0800
View
281 tags/xmpp4r-simple-0.8.0/COPYING
@@ -1,281 +0,0 @@
- GNU GENERAL PUBLIC LICENSE
- Version 2, June 1991
-
- Copyright (C) 1989, 1991 Free Software Foundation, Inc.
- 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- Everyone is permitted to copy and distribute verbatim copies
- of this license document, but changing it is not allowed.
-
- Preamble
-
- The licenses for most software are designed to take away your
-freedom to share and change it. By contrast, the GNU General Public
-License is intended to guarantee your freedom to share and change free
-software--to make sure the software is free for all its users. This
-General Public License applies to most of the Free Software
-Foundation's software and to any other program whose authors commit to
-using it. (Some other Free Software Foundation software is covered by
-the GNU Lesser General Public License instead.) You can apply it to
-your programs, too.
-
- When we speak of free software, we are referring to freedom, not
-price. Our General Public Licenses are designed to make sure that you
-have the freedom to distribute copies of free software (and charge for
-this service if you wish), that you receive source code or can get it
-if you want it, that you can change the software or use pieces of it
-in new free programs; and that you know you can do these things.
-
- To protect your rights, we need to make restrictions that forbid
-anyone to deny you these rights or to ask you to surrender the rights.
-These restrictions translate to certain responsibilities for you if you
-distribute copies of the software, or if you modify it.
-
- For example, if you distribute copies of such a program, whether
-gratis or for a fee, you must give the recipients all the rights that
-you have. You must make sure that they, too, receive or can get the
-source code. And you must show them these terms so they know their
-rights.
-
- We protect your rights with two steps: (1) copyright the software, and
-(2) offer you this license which gives you legal permission to copy,
-distribute and/or modify the software.
-
- Also, for each author's protection and ours, we want to make certain
-that everyone understands that there is no warranty for this free
-software. If the software is modified by someone else and passed on, we
-want its recipients to know that what they have is not the original, so
-that any problems introduced by others will not reflect on the original
-authors' reputations.
-
- Finally, any free program is threatened constantly by software
-patents. We wish to avoid the danger that redistributors of a free
-program will individually obtain patent licenses, in effect making the
-program proprietary. To prevent this, we have made it clear that any
-patent must be licensed for everyone's free use or not licensed at all.
-
- The precise terms and conditions for copying, distribution and
-modification follow.
-
- GNU GENERAL PUBLIC LICENSE
- TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
-
- 0. This License applies to any program or other work which contains
-a notice placed by the copyright holder saying it may be distributed
-under the terms of this General Public License. The "Program", below,
-refers to any such program or work, and a "work based on the Program"
-means either the Program or any derivative work under copyright law:
-that is to say, a work containing the Program or a portion of it,
-either verbatim or with modifications and/or translated into another
-language. (Hereinafter, translation is included without limitation in
-the term "modification".) Each licensee is addressed as "you".
-
-Activities other than copying, distribution and modification are not
-covered by this License; they are outside its scope. The act of
-running the Program is not restricted, and the output from the Program
-is covered only if its contents constitute a work based on the
-Program (independent of having been made by running the Program).
-Whether that is true depends on what the Program does.
-
- 1. You may copy and distribute verbatim copies of the Program's
-source code as you receive it, in any medium, provided that you
-conspicuously and appropriately publish on each copy an appropriate
-copyright notice and disclaimer of warranty; keep intact all the
-notices that refer to this License and to the absence of any warranty;
-and give any other recipients of the Program a copy of this License
-along with the Program.
-
-You may charge a fee for the physical act of transferring a copy, and
-you may at your option offer warranty protection in exchange for a fee.
-
- 2. You may modify your copy or copies of the Program or any portion
-of it, thus forming a work based on the Program, and copy and
-distribute such modifications or work under the terms of Section 1
-above, provided that you also meet all of these conditions:
-
- a) You must cause the modified files to carry prominent notices
- stating that you changed the files and the date of any change.
-
- b) You must cause any work that you distribute or publish, that in
- whole or in part contains or is derived from the Program or any
- part thereof, to be licensed as a whole at no charge to all third
- parties under the terms of this License.
-
- c) If the modified program normally reads commands interactively
- when run, you must cause it, when started running for such
- interactive use in the most ordinary way, to print or display an
- announcement including an appropriate copyright notice and a
- notice that there is no warranty (or else, saying that you provide
- a warranty) and that users may redistribute the program under
- these conditions, and telling the user how to view a copy of this
- License. (Exception: if the Program itself is interactive but
- does not normally print such an announcement, your work based on
- the Program is not required to print an announcement.)
-
-These requirements apply to the modified work as a whole. If
-identifiable sections of that work are not derived from the Program,
-and can be reasonably considered independent and separate works in
-themselves, then this License, and its terms, do not apply to those
-sections when you distribute them as separate works. But when you
-distribute the same sections as part of a whole which is a work based
-on the Program, the distribution of the whole must be on the terms of
-this License, whose permissions for other licensees extend to the
-entire whole, and thus to each and every part regardless of who wrote it.
-
-Thus, it is not the intent of this section to claim rights or contest
-your rights to work written entirely by you; rather, the intent is to
-exercise the right to control the distribution of derivative or
-collective works based on the Program.
-
-In addition, mere aggregation of another work not based on the Program
-with the Program (or with a work based on the Program) on a volume of
-a storage or distribution medium does not bring the other work under
-the scope of this License.
-
- 3. You may copy and distribute the Program (or a work based on it,
-under Section 2) in object code or executable form under the terms of
-Sections 1 and 2 above provided that you also do one of the following:
-
- a) Accompany it with the complete corresponding machine-readable
- source code, which must be distributed under the terms of Sections
- 1 and 2 above on a medium customarily used for software interchange; or,
-
- b) Accompany it with a written offer, valid for at least three
- years, to give any third party, for a charge no more than your
- cost of physically performing source distribution, a complete
- machine-readable copy of the corresponding source code, to be
- distributed under the terms of Sections 1 and 2 above on a medium
- customarily used for software interchange; or,
-
- c) Accompany it with the information you received as to the offer
- to distribute corresponding source code. (This alternative is
- allowed only for noncommercial distribution and only if you
- received the program in object code or executable form with such
- an offer, in accord with Subsection b above.)
-
-The source code for a work means the preferred form of the work for
-making modifications to it. For an executable work, complete source
-code means all the source code for all modules it contains, plus any
-associated interface definition files, plus the scripts used to
-control compilation and installation of the executable. However, as a
-special exception, the source code distributed need not include
-anything that is normally distributed (in either source or binary
-form) with the major components (compiler, kernel, and so on) of the
-operating system on which the executable runs, unless that component
-itself accompanies the executable.
-
-If distribution of executable or object code is made by offering
-access to copy from a designated place, then offering equivalent
-access to copy the source code from the same place counts as
-distribution of the source code, even though third parties are not
-compelled to copy the source along with the object code.
-
- 4. You may not copy, modify, sublicense, or distribute the Program
-except as expressly provided under this License. Any attempt
-otherwise to copy, modify, sublicense or distribute the Program is
-void, and will automatically terminate your rights under this License.
-However, parties who have received copies, or rights, from you under
-this License will not have their licenses terminated so long as such
-parties remain in full compliance.
-
- 5. You are not required to accept this License, since you have not
-signed it. However, nothing else grants you permission to modify or
-distribute the Program or its derivative works. These actions are
-prohibited by law if you do not accept this License. Therefore, by
-modifying or distributing the Program (or any work based on the
-Program), you indicate your acceptance of this License to do so, and
-all its terms and conditions for copying, distributing or modifying
-the Program or works based on it.
-
- 6. Each time you redistribute the Program (or any work based on the
-Program), the recipient automatically receives a license from the
-original licensor to copy, distribute or modify the Program subject to
-these terms and conditions. You may not impose any further
-restrictions on the recipients' exercise of the rights granted herein.
-You are not responsible for enforcing compliance by third parties to
-this License.
-
- 7. If, as a consequence of a court judgment or allegation of patent
-infringement or for any other reason (not limited to patent issues),
-conditions are imposed on you (whether by court order, agreement or
-otherwise) that contradict the conditions of this License, they do not
-excuse you from the conditions of this License. If you cannot
-distribute so as to satisfy simultaneously your obligations under this
-License and any other pertinent obligations, then as a consequence you
-may not distribute the Program at all. For example, if a patent
-license would not permit royalty-free redistribution of the Program by
-all those who receive copies directly or indirectly through you, then
-the only way you could satisfy both it and this License would be to
-refrain entirely from distribution of the Program.
-
-If any portion of this section is held invalid or unenforceable under
-any particular circumstance, the balance of the section is intended to
-apply and the section as a whole is intended to apply in other
-circumstances.
-
-It is not the purpose of this section to induce you to infringe any
-patents or other property right claims or to contest validity of any
-such claims; this section has the sole purpose of protecting the
-integrity of the free software distribution system, which is
-implemented by public license practices. Many people have made
-generous contributions to the wide range of software distributed
-through that system in reliance on consistent application of that
-system; it is up to the author/donor to decide if he or she is willing
-to distribute software through any other system and a licensee cannot
-impose that choice.
-
-This section is intended to make thoroughly clear what is believed to
-be a consequence of the rest of this License.
-
- 8. If the distribution and/or use of the Program is restricted in
-certain countries either by patents or by copyrighted interfaces, the
-original copyright holder who places the Program under this License
-may add an explicit geographical distribution limitation excluding
-those countries, so that distribution is permitted only in or among
-countries not thus excluded. In such case, this License incorporates
-the limitation as if written in the body of this License.
-
- 9. The Free Software Foundation may publish revised and/or new versions
-of the General Public License from time to time. Such new versions will
-be similar in spirit to the present version, but may differ in detail to
-address new problems or concerns.
-
-Each version is given a distinguishing version number. If the Program
-specifies a version number of this License which applies to it and "any
-later version", you have the option of following the terms and conditions
-either of that version or of any later version published by the Free
-Software Foundation. If the Program does not specify a version number of
-this License, you may choose any version ever published by the Free Software
-Foundation.
-
- 10. If you wish to incorporate parts of the Program into other free
-programs whose distribution conditions are different, write to the author
-to ask for permission. For software which is copyrighted by the Free
-Software Foundation, write to the Free Software Foundation; we sometimes
-make exceptions for this. Our decision will be guided by the two goals
-of preserving the free status of all derivatives of our free software and
-of promoting the sharing and reuse of software generally.
-
- NO WARRANTY
-
- 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
-FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
-OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
-PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
-OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
-MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
-TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
-PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
-REPAIR OR CORRECTION.
-
- 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
-WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
-REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
-INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
-OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
-TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
-YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
-PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
-POSSIBILITY OF SUCH DAMAGES.
-
- END OF TERMS AND CONDITIONS
-
View
59 tags/xmpp4r-simple-0.8.0/README
@@ -1,59 +0,0 @@
-= Name
-
-Jabber::Simple - An extremely easy-to-use Jabber client library.
-
-= Synopsis
-
- # Send a message to a friend, asking for authorization if necessary:
- im = Jabber::Simple.new("user@example.com", "password")
- im.deliver("friend@example.com", "Hey there friend!")
-
- # Get received messages and print them out to the console:
- im.received_messages { |msg| puts msg.body if msg.type == :chat }
-
- # Send an authorization request to a user:
- im.add("friend@example.com")
-
- # Get presence updates from your friends, and print them out to the console:
- # (admittedly, this one needs some work)
- im.presence_updates { |update|
- from = update[0].jid.strip.to_s
- status = update[2].status
- presence = update[2].show
- puts "#{from} went #{presence}: #{status}"
- end
-
- # Remove a user from your contact list:
- im.remove("unfriendly@example.com")
-
- # See the Jabber::Simple documentation for more information.
-
-= Description
-
-Jabber::Simple is intended to make Jabber client programming dead simple. XMPP,
-the Jabber protocol, is extremely powerful but also carries a steep learning
-curve. This library exposes only the most common tasks, and does so in a way
-that is familiar to users of traditional instant messenger clients.
-
-= Known Issues
-
-* None. If you'd like additional functionality, please contact the developer!
-
-= Copyright
-
-Jabber::Simple - An extremely easy-to-use Jabber client library.
-Copyright 2006 Blaine Cook <blaine@obvious.com>, Obvious Corp.
-
-Jabber::Simple 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.
-
-Jabber::Simple 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 Jabber::Simple; if not, write to the Free Software
-Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
View
73 tags/xmpp4r-simple-0.8.0/Rakefile
@@ -1,73 +0,0 @@
-# Jabber::Simple - Ruby Helper for Faking Web Requests
-# Copyright 2006 Blaine Cook <romeda@gmail.com>.
-#
-# Jabber::Simple 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.
-#
-# Jabber::Simple 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 Jabber::Simple; if not, write to the Free Software
-# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
-
-require 'rubygems'
-Gem::manage_gems
-require 'rake/gempackagetask'
-require 'rake/testtask'
-require 'rake/rdoctask'
-require 'rcov/rcovtask'
-
-spec = Gem::Specification.new do |s|
- s.add_dependency('xmpp4r', '>= 0.3.0')
- s.name = "xmpp4r-simple"
- s.version = "0.8.0"
- s.author = "Blaine Cook"
- s.email = "romeda@gmail.com"
- s.homepage = "http://xmpp4r-simple.rubyforge.org/"
- s.platform = Gem::Platform::RUBY
- s.summary = "A simplified Jabber client library."
- s.description = <<-EOF
- Jabber::Simple takes the strong foundation laid by xmpp4r
- and hides the relatively high complexity of maintaining a simple instant
- messenger bot in Ruby.
- EOF
- s.files = FileList["{test,lib}/**/*", '[A-Z]*'].exclude("rdoc", ".svn").to_a
- s.require_path = "lib"
- s.test_files = Dir.glob("test/test_*.rb")
- s.has_rdoc = true
- s.extra_rdoc_files = ["README", "COPYING"]
- s.rubyforge_project = "xmpp4r-simple"
-end
-
-Rake::GemPackageTask.new(spec) do |pkg|
- pkg.gem_spec = spec
- pkg.need_tar = true
- pkg.need_zip = true
-end
-
-desc "Default Task"
-task :default => [:tests]
-
-desc "Run All Tests"
-Rake::TestTask.new :tests do |test|
- test.test_files = ["test/**/*.rb"]
- test.verbose = true
-end
-
-desc "Generate Documentation"
-Rake::RDocTask.new do |rdoc|
- rdoc.main = "README"
- rdoc.rdoc_dir = "doc"
- rdoc.rdoc_files.include("README", "COPYING", "lib/*.rb")
- rdoc.title = "Jabber::Simple"
-end
-
-Rcov::RcovTask.new do |t|
- t.test_files = FileList['test/**/test*.rb']
- t.rcov_opts << "--sort coverage"
-end
View
410 tags/xmpp4r-simple-0.8.0/lib/xmpp4r-simple.rb
@@ -1,410 +0,0 @@
-# Jabber::Simple - An extremely easy-to-use Jabber client library.
-# Copyright 2006 Blaine Cook <blaine@obvious.com>, Obvious Corp.
-#
-# Jabber::Simple 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.
-#
-# Jabber::Simple 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 Jabber::Simple; if not, write to the Free Software
-# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
-
-require 'rubygems'
-require 'xmpp4r'
-require 'xmpp4r/roster'
-
-module Jabber
-
- class ConnectionError < StandardError #:nodoc:
- end
-
- class Contact #:nodoc:
-
- def initialize(client, jid)
- @jid = jid.respond_to?(:resource) ? jid : JID.new(jid)
- @client = client
- end
-
- def inspect
- "Jabber::Contact #{jid.to_s}"
- end
-
- def subscribed?
- [:to, :both].include?(subscription)
- end
-
- def subscription
- roster_item && roster_item.subscription
- end
-
- def ask_for_authorization!
- subscription_request = Presence.new.set_type(:subscribe)
- subscription_request.to = jid
- client.send!(subscription_request)
- end
-
- def unsubscribe!
- unsubscription_request = Presence.new.set_type(:unsubscribe)
- unsubscription_request.to = jid
- client.send!(unsubscription_request)
- client.send!(unsubscription_request.set_type(:unsubscribed))
- end
-
- def jid(bare=true)
- bare ? @jid.strip : @jid
- end
-
- private
-
- def roster_item
- client.roster.items[jid]
- end
-
- def client
- @client
- end
- end
-
- class Simple
-
- # Create a new Jabber::Simple client. You will be automatically connected
- # to the Jabber server and your status message will be set to the string
- # passed in as the status_message argument.
- #
- # jabber = Jabber::Simple.new("me@example.com", "password", "Chat with me - Please!")
- def initialize(jid, password, status = nil, status_message = "Available")
- @jid = jid
- @password = password
- @disconnected = false
- status(status, status_message)
- start_deferred_delivery_thread
- end
-
- def inspect #:nodoc:
- "Jabber::Simple #{@jid}"
- end
-
- # Send a message to jabber user jid.
- #
- # Valid message types are:
- #
- # * :normal (default): a normal message.
- # * :chat: a one-to-one chat message.
- # * :groupchat: a group-chat message.
- # * :headline: a "headline" message.
- # * :error: an error message.
- #
- # If the recipient is not in your contacts list, the message will be queued
- # for later delivery, and the Contact will be automatically asked for
- # authorization (see Jabber::Simple#add).
- def deliver(jid, message, type=:chat)
- contacts(jid) do |friend|
- unless subscribed_to? friend
- add(friend.jid)
- return deliver_deferred(friend.jid, message, type)
- end
- msg = Message.new(friend.jid)
- msg.type = type
- msg.body = message
- send!(msg)
- end
- end
-
- # Set your presence, with a message.
- #
- # Available values for presence are:
- #
- # * nil: online.
- # * :chat: free for chat.
- # * :away: away from the computer.
- # * :dnd: do not disturb.
- # * :xa: extended away.
- #
- # It's not possible to set an offline status - to do that, disconnect! :-)
- def status(presence, message)
- @presence = presence
- @status_message = message
- stat_msg = Presence.new(@presence, @status_message)
- send!(stat_msg)
- end
-
- # Ask the users specified by jids for authorization (i.e., ask them to add
- # you to their contact list). If you are already in the user's contact list,
- # add() will not attempt to re-request authorization. In order to force
- # re-authorization, first remove() the user, then re-add them.
- #
- # Example usage:
- #
- # jabber_simple.add("friend@friendosaurus.com")
- #
- # Because the authorization process might take a few seconds, or might
- # never happen depending on when (and if) the user accepts your
- # request, results are placed in the Jabber::Simple#new_subscriptions queue.
- def add(*jids)
- contacts(*jids) do |friend|
- next if subscribed_to? friend
- friend.ask_for_authorization!
- end
- end
-
- # Remove the jabber users specified by jids from the contact list.
- def remove(*jids)
- contacts(*jids) do |unfriend|
- unfriend.unsubscribe!
- end
- end
-
- # Returns true if this Jabber account is subscribed to status updates for
- # the jabber user jid, false otherwise.
- def subscribed_to?(jid)
- contacts(jid) do |contact|
- return contact.subscribed?
- end
- end
-
- # If contacts is a single contact, returns a Jabber::Contact object
- # representing that user; if contacts is an array, returns an array of
- # Jabber::Contact objects.
- #
- # When called with a block, contacts will yield each Jabber::Contact object
- # in turn. This is mainly used internally, but exposed as an utility
- # function.
- def contacts(*contacts, &block)
- @contacts ||= {}
- contakts = []
- contacts.each do |contact|
- jid = contact.to_s
- unless @contacts[jid]
- @contacts[jid] = contact.respond_to?(:ask_for_authorization!) ? contact : Contact.new(self, contact)
- end
- yield @contacts[jid] if block_given?
- contakts << @contacts[jid]
- end
- contakts.size > 1 ? contakts : contakts.first
- end
-
- # Returns true if the Jabber client is connected to the Jabber server,
- # false otherwise.
- def connected?
- @client ||= nil
- connected = @client.respond_to?(:is_connected?) && @client.is_connected?
- return connected
- end
-
- # Returns an array of messages received since the last time
- # received_messages was called. Passing a block will yield each message in
- # turn, allowing you to break part-way through processing (especially
- # useful when your message handling code is not thread-safe (e.g.,
- # ActiveRecord).
- #
- # e.g.:
- #
- # jabber.received_messages do |message|
- # puts "Received message from #{message.from}: #{message.body}"
- # end
- def received_messages(&block)
- dequeue(:received_messages, &block)
- end
-
- # Returns an array of presence updates received since the last time
- # presence_updates was called. Passing a block will yield each update in
- # turn, allowing you to break part-way through processing (especially
- # useful when your presence handling code is not thread-safe (e.g.,
- # ActiveRecord).
- #
- # e.g.:
- #
- # jabber.presence_updates do |friend, old_presence, new_presence|
- # puts "Received presence update from #{friend.to_s}: #{new_presence}"
- # end
- def presence_updates(&block)
- dequeue(:presence_updates, &block)
- end
-
- # Returns an array of subscription notifications received since the last
- # time new_subscriptions was called. Passing a block will yield each update
- # in turn, allowing you to break part-way through processing (especially
- # useful when your subscription handling code is not thread-safe (e.g.,
- # ActiveRecord).
- #
- # e.g.:
- #
- # jabber.new_subscriptions do |friend, presence|
- # puts "Received presence update from #{friend.to_s}: #{presence}"
- # end
- def new_subscriptions(&block)
- dequeue(:new_subscriptions, &block)
- end
-
- # Returns an array of subscription notifications received since the last
- # time subscription_requests was called. Passing a block will yield each update
- # in turn, allowing you to break part-way through processing (especially
- # useful when your subscription handling code is not thread-safe (e.g.,
- # ActiveRecord).
- #
- # e.g.:
- #
- # jabber.subscription_requests do |friend, presence|
- # puts "Received presence update from #{friend.to_s}: #{presence}"
- # end
- def subscription_requests(&block)
- dequeue(:subscription_requests, &block)
- end
-
-
- # Returns true if auto-accept subscriptions (friend requests) is enabled
- # (default), false otherwise.
- def accept_subscriptions?
- @accept_subscriptions = true if @accept_subscriptions.nil?
- @accept_subscriptions
- end
-
- # Change whether or not subscriptions (friend requests) are automatically accepted.
- def accept_subscriptions=(accept_status)
- @accept_subscriptions = accept_status
- end
-
- # Direct access to the underlying Roster helper.
- def roster
- return @roster if @roster
- self.roster = Roster::Helper.new(client)
- end
-
- # Direct access to the underlying Jabber client.
- def client
- connect!() unless connected?
- @client
- end
-
- # Send a Jabber stanza over-the-wire.
- def send!(msg)
- client.send(msg)
- end
-
- # Use this to force the client to reconnect after a force_disconnect.
- def reconnect
- @disconnected = false
- connect!
- end
-
- # Use this to force the client to disconnect and not automatically
- # reconnect.
- def disconnect
- disconnect!
- end
-
- private
-
- def client=(client)
- self.roster = nil # ensure we clear the roster, since that's now associated with a different client.
- @client = client
- end
-
- def roster=(new_roster)
- @roster = new_roster
- end
-
- def connect!
- raise ConnectionError, "Connections are disabled - use Jabber::Simple::force_connect() to reconnect." if @disconnected
- # Pre-connect
- @connect_mutex ||= Mutex.new
- @connect_mutex.lock
- disconnect!(false) if connected?
-
- # Connect
- jid = JID.new(@jid)
- my_client = Client.new(@jid)
- my_client.connect
- my_client.auth(@password)
- self.client = my_client
-
- # Post-connect
- register_default_callbacks
- status(@presence, @status_message)
- @connect_mutex.unlock
- end
-
- def disconnect!(auto_reconnect = true)
- if client.respond_to?(:is_connected?) && client.is_connected?
- client.close
- end
- client = nil
- @disconnected = auto_reconnect
- end
-
- def register_default_callbacks
- client.add_message_callback do |message|
- queue(:received_messages) << message unless message.body.nil?
- end
-
- roster.add_subscription_callback do |roster_item, presence|
- if presence.type == :subscribed
- queue(:new_subscriptions) << [roster_item, presence]
- end
- end
-
- roster.add_subscription_request_callback do |roster_item, presence|
- if accept_subscriptions?
- roster.accept_subscription(presence.from)
- else
- queue(:subscription_requests) << [roster_item, presence]
- end
- end
-
- roster.add_presence_callback do |roster_item, old_presence, new_presence|
- queue(:presence_updates) << [roster_item, old_presence, new_presence]
- end
- end
-
- # This thread facilitates the delivery of messages to users who haven't yet
- # accepted an invitation from us. When we attempt to deliver a message, if
- # the user hasn't subscribed, we place the message in a queue for later
- # delivery. Once a user has accepted our authorization request, we deliver
- # any messages that have been queued up in the meantime.
- def start_deferred_delivery_thread #:nodoc:
- Thread.new {
- loop {
- messages = [queue(:pending_messages).pop].flatten
- messages.each do |message|
- if subscribed_to?(message[:to])
- deliver(message[:to], message[:message], message[:type])
- else
- queue(:pending_messages) << message
- end
- end
- }
- }
- end
-
- # Queue messages for delivery once a user has accepted our authorization
- # request. Works in conjunction with the deferred delivery thread.
- def deliver_deferred(jid, message, type) #:nodoc:
- msg = {:to => jid, :message => message, :type => type}
- queue(:pending_messages) << [msg]
- end
-
- def queue(queue)
- @queues ||= Hash.new { |h,k| h[k] = Queue.new }
- @queues[queue]
- end
-
- def dequeue(queue, non_blocking = true, &block)
- queue_items = []
- loop do
- queue_item = queue(queue).pop(non_blocking) rescue nil
- break if queue_item.nil?
- queue_items << queue_item
- yield queue_item if block_given?
- end
- queue_items
- end
- end
-end
-
-true
View
241 tags/xmpp4r-simple-0.8.0/test/test_xmpp4r_simple.rb
@@ -1,241 +0,0 @@
-# Jabber::Simple - An extremely easy-to-use Jabber client library.
-# Copyright 2006 Blaine Cook <blaine@obvious.com>, Obvious Corp.
-#
-# Jabber::Simple 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.
-#
-# Jabber::Simple 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 Jabber::Simple; if not, write to the Free Software
-# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
-#
-$:.unshift "#{File.dirname(__FILE__)}/../lib"
-
-require 'test/unit'
-require 'timeout'
-require 'xmpp4r-simple'
-
-class JabberSimpleTest < Test::Unit::TestCase
-
- def setup
- @@connections ||= {}
-
- if @@connections.include?(:client1)
- @client1 = @@connections[:client1]
- @client2 = @@connections[:client2]
- @client1.accept_subscriptions = true
- @client2.accept_subscriptions = true
- @jid1_raw = @@connections[:jid1_raw]
- @jid2_raw = @@connections[:jid2_raw]
- @jid1 = @jid1_raw.strip.to_s
- @jid2 = @jid2_raw.strip.to_s
- return true
- end
-
- logins = []
- begin
- logins = File.readlines(File.expand_path("~/.xmpp4r-simple-test-config")).map! { |login| login.split(" ") }
- raise StandardError unless logins.size == 2
- rescue => e
- puts "\nConfiguration Error!\n\nYou must make available two unique Jabber accounts in order for the tests to pass."
- puts "Place them in ~/.xmpp4r-simple-test-config, one per line like so:\n\n"
- puts "user1@example.com/res password"
- puts "user2@example.com/res password\n\n"
- raise e
- end
-
- @@connections[:client1] = Jabber::Simple.new(*logins[0])
- @@connections[:client2] = Jabber::Simple.new(*logins[1])
-
- @@connections[:jid1_raw] = Jabber::JID.new(logins[0][0])
- @@connections[:jid2_raw] = Jabber::JID.new(logins[1][0])
-
- # Force load the client and roster, just to be safe.
- @@connections[:client1].roster
- @@connections[:client2].roster
-
- # Re-run this method to setup the local instance variables the first time.
- setup
- end
-
- def test_ensure_the_jabber_clients_are_connected_after_setup
- assert @client1.client.is_connected?
- assert @client2.client.is_connected?
- end
-
- def test_inspect_should_be_custom
- assert_equal "Jabber::Simple #{@jid1_raw}", @client1.inspect
- end
-
- def test_inspect_contact_should_be_custom
- assert_equal "Jabber::Contact #{@jid2}", @client1.contacts(@jid2).inspect
- end
-
- def test_remove_users_from_our_roster_should_succeed
- @client2.remove(@jid1)
- @client1.remove(@jid2)
-
- sleep 3
-
- assert_equal false, @client1.subscribed_to?(@jid2)
- assert_equal false, @client2.subscribed_to?(@jid1)
- end
-
- def test_add_users_to_our_roster_should_succeed_with_automatic_approval
- @client1.remove(@jid2)
- @client2.remove(@jid1)
-
- assert_before 60 do
- assert_equal false, @client1.subscribed_to?(@jid2)
- assert_equal false, @client2.subscribed_to?(@jid1)
- end
-
- @client1.new_subscriptions
- @client1.add(@jid2)
-
- assert_before 60 do
- assert @client1.subscribed_to?(@jid2)
- assert @client2.subscribed_to?(@jid1)
- end
-
- new_subscriptions = @client1.new_subscriptions
- assert_equal 1, new_subscriptions.size
- assert_equal @jid2, new_subscriptions[0][0].jid.strip.to_s
- end
-
- def test_sent_message_should_be_received
- # First clear the client's message queue, just in case.
- assert_kind_of Array, @client2.received_messages
-
- # Next ensure that we're not subscribed, so that we can test the deferred message queue.
- @client1.remove(@jid2)
- @client2.remove(@jid1)
- sleep 2
-
- # Deliver the messages; this should be received by the other client.
- @client1.deliver(@jid2, "test message")
-
- sleep 2
-
- # Fetch the message; allow up to ten seconds for the delivery to occur.
- messages = []
- begin
- Timeout::timeout(20) {
- loop do
- messages = @client2.received_messages
- break unless messages.empty?
- sleep 1
- end
- }
- rescue Timeout::Error
- flunk "Timeout waiting for message"
- end
-
- # Ensure that the message was received intact.
- assert_equal @jid1, messages.first.from.strip.to_s
- assert_equal "test message", messages.first.body
- end
-
- def test_presence_updates_should_be_received
-
- @client2.add(@client1)
-
- assert_before(60) { assert @client2.subscribed_to?(@jid1) }
- assert_before(60) { assert_equal 0, @client2.presence_updates.size }
-
- @client1.status(:away, "Doing something else.")
-
- new_statuses = []
- assert_before(60) do
- new_statuses = @client2.presence_updates
- assert_equal 1, new_statuses.size
- end
-
- new_status = new_statuses.first
- assert_equal @jid1, new_status[0].jid.strip.to_s
- assert_equal "Doing something else.", new_status[2].status
- assert_equal :away, new_status[2].show
- end
-
- def test_disable_auto_accept_subscription_requests
- @client1.remove(@jid2)
- @client2.remove(@jid1)
-
- assert_before(60) do
- assert_equal false, @client1.subscribed_to?(@jid2)
- assert_equal false, @client2.subscribed_to?(@jid1)
- end
-
- @client1.accept_subscriptions = false
- assert_equal false, @client1.accept_subscriptions?
-
- assert_before(60) { assert_equal 0, @client1.subscription_requests.size }
-
- @client2.add(@jid1)
-
- new_subscription_requests = []
- assert_before(60) do
- new_subscription_requests = @client1.subscription_requests
- assert_equal 1, new_subscription_requests.size
- end
-
- new_subscription = new_subscription_requests.first
- assert_equal @jid2, new_subscription[0].jid.strip.to_s
- assert_equal :subscribe, new_subscription[1].type
- end
-
- def test_automatically_reconnect
- @client1.client.close
-
- assert_before 60 do
- assert_equal false, @client1.connected?
- end
-
- # empty client 2's received message queue.
- @client2.received_messages
-
- @client1.deliver(@jid2, "Testing")
-
- assert_before(60) { assert @client1.connected? }
- assert @client1.roster.instance_variable_get('@stream').is_connected?
- assert_before(60) { assert_equal 1, @client2.received_messages.size }
- end
-
- def test_disconnect_doesnt_allow_auto_reconnects
- @client1.disconnect
-
- assert_equal false, @client1.connected?
-
- assert_raises Jabber::ConnectionError do
- @client1.deliver(@jid2, "testing")
- end
-
- @client1.reconnect
- end
-
- private
-
- def assert_before(seconds, &block)
- error = nil
- begin
- Timeout::timeout(seconds) {
- begin
- yield
- rescue => e
- error = e
- sleep 0.5
- retry
- end
- }
- rescue Timeout::Error
- raise error
- end
- end
-
-end
View
0  trunk/test/test_xmpp4r_simple.rb → test/test_xmpp4r_simple.rb
File renamed without changes
Please sign in to comment.
Something went wrong with that request. Please try again.