Skip to content
This repository has been archived by the owner on Jun 30, 2022. It is now read-only.

Commit

Permalink
Configure extra Cinch options with a proc so nested attributes can be…
Browse files Browse the repository at this point in the history
… set.
  • Loading branch information
jimmycuadra committed Oct 23, 2014
1 parent 8846b5b commit b4b7d56
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 24 deletions.
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,24 @@ gem "lita-irc"

## Configuration

lita-irc has configuration attributes for all the underlying [Cinch](https://github.com/cinchrb/cinch) robot's options. The documentation for [Cinch's options](http://rubydoc.info/github/cinchrb/cinch/file/docs/bot_options.md) detail all of them.

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.

### Required attributes

* `server` (String) - The name of the IRC server Lita should connect to. Default: `nil`.
* `channels` (Array<String>) - An array of channels Lita should join upon connection. Default: `nil`.
* `server` (String) - The name of the IRC server Lita should connect to.
* `channels` (Array<String>) - An array of channels Lita should join upon connection.

### Optional attributes

* `user` (String) - The username for Lita's IRC account. Default: `"Lita"".
* `user` (String) - The username for Lita's IRC account. Default: `"Lita"`.
* `password` (String) - The password for Lita's IRC account. Default: `nil`.
* `realname` (String) - The "real name" field for Lita's IRC account. Default: `"Lita"`.

### Lita-specific attributes

* `log_level` (Symbol) - Sets the log level for Cinch's loggers. By default, Cinch's loggers are disabled. Default: `nil`.

**Note**: `config.robot.name` is used as Lita's IRC nickname. `config.adapters.irc.nick` is ignored.

### Additional Cinch options

Under the hood, lita-irc uses [Cinch](https://github.com/cinchrb/cinch) for the IRC connection. Cinch has several [configuration options](http://www.rubydoc.info/github/cinchrb/cinch/file/docs/bot_options.md) that you may want to set. To do this, assign a proc/lambda to `config.adapters.irc.cinch`. lita-irc will yield the Cinch configuration object to the proc, so you can configure it as you'd like. Note that for the options listed in the sections above, those values will overwrite anything set in the proc.

### Example

``` ruby
Expand All @@ -48,6 +45,9 @@ Lita.configure do |config|
config.adapters.irc.user = "Lita"
config.adapters.irc.realname = "Lita"
config.adapters.irc.password = "secret"
config.adapters.irc.cinch = lambda do |cinch_config|
cinch_config.max_reconnect_delay = 123
end
end
```

Expand Down
24 changes: 13 additions & 11 deletions lib/lita/adapters/irc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@
module Lita
module Adapters
class IRC < Adapter
CONTROLLED_CONFIG_KEYS = %i(channels server user realname)
MANUALLY_ASSIGNED_KEYS = %i(channels nick)
EXTRA_CINCH_OPTIONS = Cinch::Configuration::Bot::KnownOptions - CONTROLLED_CONFIG_KEYS

# Required attributes
config :channels, type: [Array, String], required: true
config :server, type: String, required: true

# Optional attributes
config :user, type: String, default: "Lita"
config :password, type: String
config :realname, type: String, default: "Lita"
config :log_level, type: Symbol

EXTRA_CINCH_OPTIONS.each { |option| config option }
config :cinch do
validate do |value|
"must be a callable object" unless value.respond_to?(:call)
end
end

attr_reader :cinch

Expand Down Expand Up @@ -76,15 +76,17 @@ def channels

def configure_cinch
Lita.logger.debug("Configuring Cinch.")

cinch.configure do |cinch_config|
config.cinch.call(cinch_config) if config.cinch

cinch_config.channels = channels
cinch_config.server = config.server
cinch_config.nick = robot.config.robot.name

Cinch::Configuration::Bot::KnownOptions.each do |key|
next if MANUALLY_ASSIGNED_KEYS.include?(key)
value = config.public_send(key)
cinch_config.public_send("#{key}=", value) unless value.nil?
end
cinch_config.user = config.user if config.user
cinch_config.password = config.password if config.password
cinch_config.realname = config.realname if config.realname
end
end

Expand Down
8 changes: 5 additions & 3 deletions spec/lita/adapters/irc_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@
config.adapters.irc.user = "litabot"
config.adapters.irc.password = "secret"
config.adapters.irc.realname = "Lita the Robot"
config.adapters.irc.nick = "NotLita"
config.adapters.irc.max_reconnect_delay = 123
config.adapters.irc.cinch = lambda do |c|
c.nick = "NotLita"
c.sasl.username = "sasl username"
end
end
end

Expand All @@ -31,7 +33,7 @@
expect(config.user).to eq("litabot")
expect(config.password).to eq("secret")
expect(config.realname).to eq("Lita the Robot")
expect(config.max_reconnect_delay).to eq(123)
expect(config.sasl.username).to eq("sasl username")
end
end

Expand Down

0 comments on commit b4b7d56

Please sign in to comment.