lita-irc 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 267b171c8564fa0e0eb11cc880178646e4f3c3e2
4
+ data.tar.gz: 538ee8f00c2c55061e1d7f9ba0d357f9f4cc9c0b
5
+ SHA512:
6
+ metadata.gz: c319385fdec8f1ba1f1383957e5a7d71a430e3dcbc2288eed733f8f45b94e43ebc0898ae8b3829144130364bf1256ada4e18eea7c4cebc2f80ca5f54ae1b52cf
7
+ data.tar.gz: 7b64d29931dde2ee721e96d2fd17c15c32dfbe3c46553888c850812bc770beac683ee423dbe0676a05bd1fc94dec9ad15826ef1a06f05a3a069ba660f9807a0b
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ lita_config.rb
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
4
+ script: bundle exec rspec
5
+ before_install:
6
+ - gem update --system
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
data/README.md ADDED
@@ -0,0 +1,56 @@
1
+ # lita-irc
2
+
3
+ [![Build Status](https://travis-ci.org/jimmycuadra/lita-irc.png)](https://travis-ci.org/jimmycuadra/lita-irc)
4
+ [![Code Climate](https://codeclimate.com/github/jimmycuadra/lita-irc.png)](https://codeclimate.com/github/jimmycuadra/lita-irc)
5
+ [![Coverage Status](https://coveralls.io/repos/jimmycuadra/lita-irc/badge.png)](https://coveralls.io/r/jimmycuadra/lita-irc)
6
+
7
+ **lita-irc** is an adapter for [Lita](https://github.com/jimmycuadra/lita) that allows you to use the robot with IRC.
8
+
9
+ ## Installation
10
+
11
+ Add lita-irc to your Lita instance's Gemfile:
12
+
13
+ ``` ruby
14
+ gem "lita-irc"
15
+ ```
16
+
17
+ ## Configuration
18
+
19
+ All attributes set on `config.adapter` will be passed on to the underlying [Cinch](https://github.com/cinchrb/cinch) robot. The documentation for [Cinch's options](http://rubydoc.info/github/cinchrb/cinch/file/docs/bot_options.md) detail all of them.
20
+
21
+ The attributes listed below are either fundamental to making the bot work, or have defaults provided by Lita that take precedence over Cinch's defaults.
22
+
23
+ ### Required attributes
24
+
25
+ * `server` (String) - The name of the IRC server Lita should connect to. Default: `nil`.
26
+ * `channels` (Array<String>) - An array of channels Lita should join upon connection. Default: `nil`.
27
+
28
+ ### Optional attributes
29
+
30
+ * `user` (String) - The username for Lita's IRC account. Default: `"Lita"".
31
+ * `password` (String) - The password for Lita's IRC account. Default: `nil`.
32
+ * `realname` (String) - The "real name" field for Lita's IRC account. Default: `"Lita"`.
33
+
34
+ ### Lita-specific attributes
35
+
36
+ * `log_level` (Symbol) - Sets the log level for Cinch's loggers. By default, Cinch's loggers are disabled. Default: `nil`.
37
+
38
+ **Note**: `config.robot.name` is used as Lita's IRC nickname. `config.adapter.nick` is ignored.
39
+
40
+ ### Example
41
+
42
+ ``` ruby
43
+ Lita.configure do |config|
44
+ config.robot.name = "Lita"
45
+ config.robot.adapter = :irc
46
+ config.adapter.server = "irc.freenode.net"
47
+ config.adapter.channels = ["#litabot"]
48
+ config.adapter.user = "Lita"
49
+ config.adapter.realname = "Lita"
50
+ config.adapter.password = "secret"
51
+ end
52
+ ```
53
+
54
+ ## License
55
+
56
+ [MIT](http://opensource.org/licenses/MIT)
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,55 @@
1
+ require "securerandom"
2
+
3
+ module Lita
4
+ module Adapters
5
+ class IRC < Adapter
6
+ class CinchPlugin
7
+ include Cinch::Plugin
8
+
9
+ match /.*/
10
+
11
+ def execute(m)
12
+ body = get_body(m)
13
+ source = get_source(m)
14
+ message = Message.new(robot, body, source)
15
+ message.command! unless source.room
16
+ dispatch(message)
17
+ end
18
+
19
+ private
20
+
21
+ def dispatch(message)
22
+ channel_text = " in #{message.source.room}" if message.source.room
23
+ Lita.logger.debug(<<-MSG.chomp
24
+ Dispatching message to Lita from #{message.source.user.name}#{channel_text}.
25
+ MSG
26
+ )
27
+ robot.receive(message)
28
+ end
29
+
30
+ def get_body(m)
31
+ if m.action?
32
+ m.action_message
33
+ else
34
+ m.message
35
+ end
36
+ end
37
+
38
+ def get_source(m)
39
+ user = user_by_nick(m.user.nick)
40
+ channel = m.channel ? m.channel.name : nil
41
+ Source.new(user, channel)
42
+ end
43
+
44
+ def robot
45
+ config[:robot]
46
+ end
47
+
48
+ def user_by_nick(nick)
49
+ Lita.logger.debug("Looking up user with nick: #{nick}.")
50
+ User.find_by_name(nick) || User.create(SecureRandom.uuid, name: nick)
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,87 @@
1
+ require "cinch"
2
+
3
+ require "lita"
4
+ require "lita/adapters/irc/cinch_plugin"
5
+
6
+ module Lita
7
+ module Adapters
8
+ class IRC < Adapter
9
+ require_configs :server, :channels
10
+
11
+ attr_reader :cinch
12
+
13
+ def initialize(robot)
14
+ super
15
+
16
+ @cinch = Cinch::Bot.new
17
+ normalize_config
18
+ configure_cinch
19
+ configure_logging
20
+ register_plugin
21
+ end
22
+
23
+ def run
24
+ Lita.logger.info("Connecting to IRC.")
25
+ cinch.start
26
+ end
27
+
28
+ def send_messages(target, strings)
29
+ if target.room
30
+ channel = Cinch::Channel.new(target.room, cinch)
31
+ strings.each { |s| channel.msg(s) }
32
+ else
33
+ user = Cinch::User.new(target.user.name, cinch)
34
+ strings.each { |s| user.msg(s) }
35
+ end
36
+ end
37
+
38
+ def set_topic(target, topic)
39
+ room = target.room
40
+ channel = Cinch::Channel.new(target.room, cinch)
41
+ Lita.logger.debug("Setting topic for channel #{room}: #{topic}")
42
+ channel.topic = topic
43
+ end
44
+
45
+ def shut_down
46
+ Lita.logger.info("Disconnecting from IRC.")
47
+ cinch.quit
48
+ end
49
+
50
+ private
51
+
52
+ def normalize_config
53
+ Lita.config.adapter.channels = Array(Lita.config.adapter.channels)
54
+ Lita.config.adapter.nick = Lita.config.robot.name
55
+ end
56
+
57
+ def configure_cinch
58
+ Lita.logger.debug("Configuring Cinch.")
59
+ cinch.configure do |config|
60
+ Lita.config.adapter.each do |key, value|
61
+ if config.class::KnownOptions.include?(key)
62
+ config.send("#{key}=", value)
63
+ end
64
+ end
65
+ end
66
+ end
67
+
68
+ def configure_logging
69
+ if Lita.config.adapter.log_level
70
+ cinch.loggers.level = Lita.config.adapter.log_level
71
+ else
72
+ cinch.loggers.clear
73
+ end
74
+ end
75
+
76
+ def register_plugin
77
+ cinch.configure do |config|
78
+ config.plugins.prefix = nil
79
+ config.plugins.plugins = [CinchPlugin]
80
+ config.plugins.options[CinchPlugin] = { robot: robot }
81
+ end
82
+ end
83
+ end
84
+
85
+ Lita.register_adapter(:irc, IRC)
86
+ end
87
+ end
data/lib/lita-irc.rb ADDED
@@ -0,0 +1 @@
1
+ require "lita/adapters/irc"
data/lita-irc.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ Gem::Specification.new do |spec|
2
+ spec.name = "lita-irc"
3
+ spec.version = "1.0.0"
4
+ spec.authors = ["Jimmy Cuadra"]
5
+ spec.email = ["jimmy@jimmycuadra.com"]
6
+ spec.description = %q{An IRC adapter for Lita.}
7
+ spec.summary = %q{An IRC adapter for the Lita chat robot.}
8
+ spec.homepage = "https://github.com/jimmycuadra/lita-irc"
9
+ spec.license = "MIT"
10
+
11
+ spec.files = `git ls-files`.split($/)
12
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
13
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
14
+ spec.require_paths = ["lib"]
15
+
16
+ spec.add_runtime_dependency "lita", "~> 2.0"
17
+ spec.add_runtime_dependency "cinch", "~> 2.0"
18
+
19
+ spec.add_development_dependency "bundler", "~> 1.3"
20
+ spec.add_development_dependency "rake"
21
+ spec.add_development_dependency "rspec", ">= 2.14.0.rc1"
22
+ spec.add_development_dependency "simplecov"
23
+ spec.add_development_dependency "coveralls"
24
+ end
@@ -0,0 +1,61 @@
1
+ require "spec_helper"
2
+
3
+ describe Lita::Adapters::IRC::CinchPlugin do
4
+ subject { described_class.new(cinch) }
5
+
6
+ let(:robot) { double("Lita::Robot") }
7
+ let(:cinch) { double("Cinch::Bot").as_null_object }
8
+ let(:user) { double("Lita::User", name: "Carl") }
9
+ let(:message) { double("Lita::Message", command!: nil, source: source) }
10
+ let(:source) { double("Lita::Source", room: "#channel", user: user) }
11
+ let(:m) do
12
+ double(
13
+ "Cinch::Message",
14
+ action_message: "foo",
15
+ message: "bar",
16
+ action?: false
17
+ ).as_null_object
18
+ end
19
+
20
+ before do
21
+ allow(subject).to receive(:config).and_return(
22
+ double("Hash", :[] => robot)
23
+ )
24
+ allow(Lita::Source).to receive(:new).and_return(source)
25
+ end
26
+
27
+ describe "#execute" do
28
+ it "dispatches regular messages to the robot" do
29
+ allow(Lita::Message).to receive(:new).with(
30
+ robot,
31
+ "bar",
32
+ source
33
+ ).and_return(message)
34
+ expect(robot).to receive(:receive).with(message)
35
+ subject.execute(m)
36
+ end
37
+
38
+ it "dispatches action messages to the robot" do
39
+ allow(m).to receive(:action?).and_return(true)
40
+ allow(Lita::Message).to receive(:new).with(
41
+ robot,
42
+ "foo",
43
+ source
44
+ ).and_return(message)
45
+ expect(robot).to receive(:receive).with(message)
46
+ subject.execute(m)
47
+ end
48
+
49
+ it "marks private messages as commands" do
50
+ allow(source).to receive(:room).and_return(nil)
51
+ allow(Lita::Message).to receive(:new).with(
52
+ robot,
53
+ "bar",
54
+ source
55
+ ).and_return(message)
56
+ allow(robot).to receive(:receive)
57
+ expect(message).to receive(:command!)
58
+ subject.execute(m)
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,106 @@
1
+ require "spec_helper"
2
+
3
+ describe Lita::Adapters::IRC, lita: true do
4
+ let(:robot) { double("Lita::Robot") }
5
+
6
+ subject { described_class.new(robot) }
7
+
8
+ before do
9
+ Lita.configure do |config|
10
+ config.adapter.server = "irc.example.com"
11
+ config.adapter.channels = "#lita"
12
+ config.adapter.user = "litabot"
13
+ config.adapter.password = "secret"
14
+ config.adapter.realname = "Lita the Robot"
15
+ config.adapter.nick = "NotLita"
16
+ config.adapter.max_reconnect_delay = 123
17
+ end
18
+ end
19
+
20
+ it "registers with Lita" do
21
+ expect(Lita.adapters[:irc]).to eql(described_class)
22
+ end
23
+
24
+ it "requires a server and channels" do
25
+ Lita.clear_config
26
+ expect(Lita.logger).to receive(:fatal).with(/server, channels/)
27
+ expect { subject }.to raise_error(SystemExit)
28
+ end
29
+
30
+ it "configures Cinch" do
31
+ subject.cinch.config.tap do |config|
32
+ expect(config.nick).to eq("Lita")
33
+ expect(config.server).to eq("irc.example.com")
34
+ expect(config.channels).to eq(["#lita"])
35
+ expect(config.user).to eq("litabot")
36
+ expect(config.password).to eq("secret")
37
+ expect(config.realname).to eq("Lita the Robot")
38
+ expect(config.max_reconnect_delay).to eq(123)
39
+ end
40
+ end
41
+
42
+ it "registers a plugin with Cinch" do
43
+ expect(subject.cinch.config.plugins.plugins).to include(
44
+ described_class::CinchPlugin
45
+ )
46
+ end
47
+
48
+ it "turns Cinch's logging on if config.adapter.log_level is set" do
49
+ Lita.config.adapter.log_level = :debug
50
+ expect(subject.cinch.loggers).not_to be_empty
51
+ end
52
+
53
+ describe "#run" do
54
+ it "connects to IRC" do
55
+ expect(subject.cinch).to receive(:start)
56
+ subject.run
57
+ end
58
+ end
59
+
60
+ describe "#send_messages" do
61
+ it "sends messages to rooms" do
62
+ source = double("Lita::Source", room: "#foo")
63
+ channel = double("Cinch::Channel")
64
+ allow(Cinch::Channel).to receive(:new).with(
65
+ "#foo",
66
+ subject.cinch
67
+ ).and_return(channel)
68
+ expect(channel).to receive(:msg).with("Hello!")
69
+ expect(channel).to receive(:msg).with("How are you?")
70
+ subject.send_messages(source, ["Hello!", "How are you?"])
71
+ end
72
+
73
+ it "sends messages to users" do
74
+ user = double("Lita::User", name: "Carl")
75
+ source = double("Lita::Source", room: nil, user: user)
76
+ user = double("Cinch::User")
77
+ allow(Cinch::User).to receive(:new).with(
78
+ "Carl",
79
+ subject.cinch
80
+ ).and_return(user)
81
+ expect(user).to receive(:msg).with("Hello!")
82
+ expect(user).to receive(:msg).with("How are you?")
83
+ subject.send_messages(source, ["Hello!", "How are you?"])
84
+ end
85
+ end
86
+
87
+ describe "#set_topic" do
88
+ it "sets a new topic for the room" do
89
+ source = double("Lita::Source", room: "#foo")
90
+ channel = double("Cinch::Channel")
91
+ expect(Cinch::Channel).to receive(:new).with(
92
+ "#foo",
93
+ subject.cinch
94
+ ).and_return(channel)
95
+ expect(channel).to receive(:topic=).with("Topic")
96
+ subject.set_topic(source, "Topic")
97
+ end
98
+ end
99
+
100
+ describe "#shut_down" do
101
+ it "disconnects from IRC" do
102
+ expect(subject.cinch).to receive(:quit)
103
+ subject.shut_down
104
+ end
105
+ end
106
+ end
@@ -0,0 +1,10 @@
1
+ require "simplecov"
2
+ require "coveralls"
3
+ SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
4
+ SimpleCov::Formatter::HTMLFormatter,
5
+ Coveralls::SimpleCov::Formatter
6
+ ]
7
+ SimpleCov.start { add_filter "/spec/" }
8
+
9
+ require "lita-irc"
10
+ require "lita/rspec"
metadata ADDED
@@ -0,0 +1,158 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lita-irc
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Jimmy Cuadra
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-07-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: lita
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: cinch
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '2.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '2.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '1.3'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '1.3'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: 2.14.0.rc1
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: 2.14.0.rc1
83
+ - !ruby/object:Gem::Dependency
84
+ name: simplecov
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: coveralls
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: An IRC adapter for Lita.
112
+ email:
113
+ - jimmy@jimmycuadra.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - .gitignore
119
+ - .travis.yml
120
+ - Gemfile
121
+ - README.md
122
+ - Rakefile
123
+ - lib/lita-irc.rb
124
+ - lib/lita/adapters/irc.rb
125
+ - lib/lita/adapters/irc/cinch_plugin.rb
126
+ - lita-irc.gemspec
127
+ - spec/lita/adapters/cinch_plugin_spec.rb
128
+ - spec/lita/adapters/irc_spec.rb
129
+ - spec/spec_helper.rb
130
+ homepage: https://github.com/jimmycuadra/lita-irc
131
+ licenses:
132
+ - MIT
133
+ metadata: {}
134
+ post_install_message:
135
+ rdoc_options: []
136
+ require_paths:
137
+ - lib
138
+ required_ruby_version: !ruby/object:Gem::Requirement
139
+ requirements:
140
+ - - '>='
141
+ - !ruby/object:Gem::Version
142
+ version: '0'
143
+ required_rubygems_version: !ruby/object:Gem::Requirement
144
+ requirements:
145
+ - - '>='
146
+ - !ruby/object:Gem::Version
147
+ version: '0'
148
+ requirements: []
149
+ rubyforge_project:
150
+ rubygems_version: 2.0.3
151
+ signing_key:
152
+ specification_version: 4
153
+ summary: An IRC adapter for the Lita chat robot.
154
+ test_files:
155
+ - spec/lita/adapters/cinch_plugin_spec.rb
156
+ - spec/lita/adapters/irc_spec.rb
157
+ - spec/spec_helper.rb
158
+ has_rdoc: