gtalk-bot 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ nbproject
2
+ pkg
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ group :runtime do
2
+ gem "xmpp4r", ">=0.5"
3
+ end
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2008 Daniel Vartanov
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.textile ADDED
@@ -0,0 +1,63 @@
1
+ h1. gtalk-bot
2
+
3
+ A very easy tool for creating Google Talk bots
4
+
5
+ h2. It's really easy!
6
+
7
+ h3. Installation
8
+
9
+ <code>sudo gem install gtalk-bot</code>
10
+
11
+ h3. Usage
12
+
13
+ h4. Getting online
14
+
15
+ <pre>require 'gtalk-bot'
16
+
17
+ bot = GTalk::Bot.new(:email => "bot@gmail.com", :password => 'getbot')
18
+ bot.get_online
19
+ </pre>
20
+
21
+ h4. On invitation
22
+
23
+ <pre>bot.on_invitation do |inviter|
24
+ puts "I have been invited by #{inviter}. Yay!"
25
+
26
+ # do something useful
27
+
28
+ bot.accept_invitation(inviter)
29
+ bot.message(inviter, "Hello there! Thanks for invitation!")
30
+ end
31
+ </pre>
32
+
33
+ h4. On message
34
+
35
+ <pre>bot.on_message do |from, text|
36
+ puts "I got message from #{from}: '#{text}'"
37
+
38
+ # do something useful
39
+
40
+ bot.message from, "I heard that!"
41
+ end</pre>
42
+
43
+ h4. Going infinite
44
+
45
+ <pre># Don't be confused with the name of this method.
46
+ # We actually keep the current (main) thread alive while letting listener thread to do its job.
47
+ # So we have no need to set up an any infinite loop.
48
+ Thread.stop</pre>
49
+
50
+ h2. To-do
51
+
52
+ <code>bot.get_online :invisible => true </code>
53
+
54
+ <code>bot.set_status :away</code>
55
+
56
+ <code>GTalk::Account('john.smith@gmail.com').available?</code>
57
+
58
+ <code>bot.contact_list # => Array</code>
59
+
60
+ <code>bot.in_contact_list?('john.smith@gmail.com')</code>
61
+
62
+
63
+ Copyright © 2011 Brian Scott, released under the MIT license
data/Rakefile ADDED
@@ -0,0 +1,17 @@
1
+ begin
2
+ require 'jeweler'
3
+ Jeweler::Tasks.new do |gemspec|
4
+ gemspec.name = "easy-gtalk-bot"
5
+ gemspec.summary = "A very easy tool for creating Google Talk bots"
6
+ gemspec.description = "easy-gtalk-bot lets you create and run GTalk bot in 5 minutes"
7
+ gemspec.email = "dan@vartanov.net"
8
+ gemspec.homepage = "http://github.com/DanielVartanov/easy-gtalk-bot"
9
+ gemspec.authors = ["Daniel Vartanov"]
10
+
11
+ gemspec.add_dependency('xmpp4r', '>= 0.5')
12
+ end
13
+
14
+ Jeweler::GemcutterTasks.new
15
+ rescue LoadError
16
+ puts "Jeweler not available. Install it with: gem install jeweler"
17
+ end
data/VERSION.yml ADDED
@@ -0,0 +1,5 @@
1
+ ---
2
+ :patch: 2
3
+ :major: 1
4
+ :minor: 0
5
+ :build:
@@ -0,0 +1,28 @@
1
+ #!/usr/bin/ruby
2
+
3
+ require 'gtalk-bot'
4
+
5
+ bot = GTalk::Bot.new(:email => "bot@gmail.com", :password => 'getbot')
6
+ bot.get_online
7
+
8
+ bot.on_invitation do |inviter|
9
+ puts "I have been invited by #{inviter}. Yay!"
10
+
11
+ # do something useful
12
+
13
+ bot.accept_invitation(inviter)
14
+ bot.message(inviter, "Hello there! Thanks for invitation!")
15
+ end
16
+
17
+ bot.on_message do |from, text|
18
+ puts "I got message from #{from}: '#{text}'"
19
+
20
+ # do something useful
21
+
22
+ bot.message from, "I heard that!"
23
+ end
24
+
25
+ # Don't be confused with the name of this method.
26
+ # We actually keep the current (main) thread alive while letting listener thread to do its job.
27
+ # So we have no need to set up an any infinite loop.
28
+ Thread.stop
data/gtalk-bot.gemspec ADDED
@@ -0,0 +1,51 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{gtalk-bot}
8
+ s.version = "1.0.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Brian Scott"]
12
+ s.date = %q{2011-10-01}
13
+ s.description = %q{gtalk-bot lets you create and run GTalk bot in 5 minutes}
14
+ s.email = %q{brainscott@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "README.textile"
17
+ ]
18
+ s.files = [
19
+ ".gitignore",
20
+ "Gemfile",
21
+ "MIT-LICENSE",
22
+ "README.textile",
23
+ "Rakefile",
24
+ "VERSION.yml",
25
+ "gtalk-bot.gemspec",
26
+ "examples/the_simplest_bot.rb",
27
+ "lib/gtalk-bot.rb"
28
+ ]
29
+ s.homepage = %q{https://github.com/bscott/gtalk-bot}
30
+ s.rdoc_options = ["--charset=UTF-8"]
31
+ s.require_paths = ["lib"]
32
+ s.rubygems_version = %q{1.3.6}
33
+ s.summary = %q{A very easy tool for creating Google Talk bots}
34
+ s.test_files = [
35
+ "examples/the_simplest_bot.rb"
36
+ ]
37
+
38
+ if s.respond_to? :specification_version then
39
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
40
+ s.specification_version = 3
41
+
42
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
43
+ s.add_runtime_dependency(%q<xmpp4r>, [">= 0.5"])
44
+ else
45
+ s.add_dependency(%q<xmpp4r>, [">= 0.5"])
46
+ end
47
+ else
48
+ s.add_dependency(%q<xmpp4r>, [">= 0.5"])
49
+ end
50
+ end
51
+
data/lib/gtalk-bot.rb ADDED
@@ -0,0 +1,71 @@
1
+ require 'openssl'
2
+
3
+ require "xmpp4r"
4
+ require 'xmpp4r/roster'
5
+
6
+ module JabberExtensions
7
+ module JIDExtensions
8
+ def to_email
9
+ "#{self.node}@#{self.domain}"
10
+ end
11
+ end
12
+
13
+ Jabber::JID.__send__ :include, JIDExtensions
14
+ end
15
+
16
+ module GTalk
17
+ class Bot
18
+ include Jabber
19
+ attr_reader :email, :password
20
+ attr_reader :jabber_client, :jid
21
+
22
+ def initialize(account_data)
23
+ @email = account_data[:email]
24
+ @password = account_data[:password]
25
+
26
+ @jid = JID::new(self.email)
27
+ @jabber_client = Client.new(self.jid)
28
+ end
29
+
30
+ def get_online(host=nil)
31
+ self.jabber_client.connect(host)
32
+ self.jabber_client.auth(self.password)
33
+ self.jabber_client.send(Presence.new.set_type(:available))
34
+ end
35
+
36
+ def invite(invitee)
37
+ subscription_request = Presence.new.set_type(:subscribe).set_to(JID::new(invitee))
38
+ self.jabber_client.send(subscription_request)
39
+ end
40
+
41
+ def accept_invitation(inviter)
42
+ inviter = JID::new(inviter)
43
+ self.roster.accept_subscription(inviter)
44
+ invite(JID::new(inviter))
45
+ end
46
+
47
+ def message(to, text)
48
+ message = Message::new(JID::new(to), text)
49
+ message.type = :chat
50
+ self.jabber_client.send(message)
51
+ end
52
+
53
+ def on_invitation(&block)
54
+ self.roster.add_subscription_request_callback do |_, presence|
55
+ block.call(presence.from.to_email)
56
+ end
57
+ end
58
+
59
+ def on_message(&block)
60
+ self.jabber_client.add_message_callback do |message|
61
+ block.call(message.from.to_email, message.body)
62
+ end
63
+ end
64
+
65
+ protected
66
+
67
+ def roster
68
+ @roster ||= Roster::Helper.new(self.jabber_client)
69
+ end
70
+ end
71
+ end
metadata ADDED
@@ -0,0 +1,82 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gtalk-bot
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 1
7
+ - 0
8
+ - 0
9
+ version: 1.0.0
10
+ platform: ruby
11
+ authors:
12
+ - Brian Scott
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2011-10-01 00:00:00 -07:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: xmpp4r
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 0
29
+ - 5
30
+ version: "0.5"
31
+ type: :runtime
32
+ version_requirements: *id001
33
+ description: gtalk-bot lets you create and run GTalk bot in 5 minutes
34
+ email: brainscott@gmail.com
35
+ executables: []
36
+
37
+ extensions: []
38
+
39
+ extra_rdoc_files:
40
+ - README.textile
41
+ files:
42
+ - .gitignore
43
+ - Gemfile
44
+ - MIT-LICENSE
45
+ - README.textile
46
+ - Rakefile
47
+ - VERSION.yml
48
+ - gtalk-bot.gemspec
49
+ - examples/the_simplest_bot.rb
50
+ - lib/gtalk-bot.rb
51
+ has_rdoc: true
52
+ homepage: https://github.com/bscott/gtalk-bot
53
+ licenses: []
54
+
55
+ post_install_message:
56
+ rdoc_options:
57
+ - --charset=UTF-8
58
+ require_paths:
59
+ - lib
60
+ required_ruby_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ segments:
65
+ - 0
66
+ version: "0"
67
+ required_rubygems_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ segments:
72
+ - 0
73
+ version: "0"
74
+ requirements: []
75
+
76
+ rubyforge_project:
77
+ rubygems_version: 1.3.6
78
+ signing_key:
79
+ specification_version: 3
80
+ summary: A very easy tool for creating Google Talk bots
81
+ test_files:
82
+ - examples/the_simplest_bot.rb