Class: Autumn::ChannelLeaf

Inherits:
Leaf
  • Object
show all
Defined in:
lib/autumn/channel_leaf.rb

Overview

A special kind of leaf that only responds to messages sent to certain channels. Leaves that subclass ChannelLeaf can, in their config, specify a channels option that narrows down which channels the leaf listens to. The leaf will not invoke the hook methods nor the *_command methods for IRC events that are not associated with those channels. It will respond to global, non-channel-specific events as well.

You can combine multiple ChannelLeaf subclasses in one Stem to allow you to run two leaves off of one nick, but have the nick running different leaves in different channels.

The channels option should be a list of stems, and for each stem, a valid channel. For example, if you ran your leaf on two servers, your stems.yml file might look like:

GamingServer:
  channels: fishinggames, games, drivinggames
  [...]
FishingServer:
  channels: fishinggames, flyfishing
  [...]

Now let’s say you had a trivia leaf that asked questions about fishing games. You’d want to run that leaf on the “#fishinggames” channel of each server, and the “#games” channel of the GamingServer, but not the other channels. (Perhaps your Stem was also running other leaves relevant to those channels.) You’d set up your leaves.yml file like so:

FishingGamesTrivia:
  channels:
    GamingServer:
      - fishinggames
      - games
    FishingServer: fishinggames
  [...]

Now your leaf will only respond to messages relevant to the specified server channels (as well as global messages).

Interception and filtering of messages is done at the leaf level, not the stem level. Therefore, for instance, if you override someone_did_join_channel, it will only be called for the appropriate channels; however, if you implement irc_join_event, it will still be called for all channels the stem is in.

Constant Summary

Constants inherited from Leaf

Leaf::DEFAULT_COMMAND_PREFIX

Instance Attribute Summary collapse

Attributes inherited from Leaf

#logger, #options, #stems

Instance Method Summary collapse

Methods inherited from Leaf

#database, #database_name, #initialize, #inspect, #irc_nick_event, #irc_quit_event, #method_missing, #preconfigure, #stem_ready

Constructor Details

This class inherits a constructor from Autumn::Leaf

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Autumn::Leaf

Instance Attribute Details

#channelsObject (readonly)

The IRC channels that this leaf is responding to, mapped to server names.



53
54
55
# File 'lib/autumn/channel_leaf.rb', line 53

def channels
  @channels
end

Instance Method Details

#irc_invite_event(stem, sender, arguments) ⇒ Object

:nodoc:



89
90
91
# File 'lib/autumn/channel_leaf.rb', line 89

def irc_invite_event(stem, sender, arguments) # :nodoc:
  super if listening?(stem, arguments[:channel]) or not stem.channels.include? arguments[:channel]
end

#irc_join_event(stem, sender, arguments) ⇒ Object

:nodoc:



73
74
75
# File 'lib/autumn/channel_leaf.rb', line 73

def irc_join_event(stem, sender, arguments) # :nodoc:
  super if listening?(stem, arguments[:channel])
end

#irc_kick_event(stem, sender, arguments) ⇒ Object

:nodoc:



93
94
95
# File 'lib/autumn/channel_leaf.rb', line 93

def irc_kick_event(stem, sender, arguments) # :nodoc:
  super if listening?(stem, arguments[:channel])
end

#irc_mode_event(stem, sender, arguments) ⇒ Object

:nodoc:



81
82
83
# File 'lib/autumn/channel_leaf.rb', line 81

def irc_mode_event(stem, sender, arguments) # :nodoc:
  super if arguments[:channel].nil? or listening?(stem, arguments[:channel])
end

#irc_notice_event(stem, sender, arguments) ⇒ Object

:nodoc:



97
98
99
# File 'lib/autumn/channel_leaf.rb', line 97

def irc_notice_event(stem, sender, arguments) # :nodoc:
  super if arguments[:channel].nil? or listening?(stem, arguments[:channel])
end

#irc_part_event(stem, sender, arguments) ⇒ Object

:nodoc:



77
78
79
# File 'lib/autumn/channel_leaf.rb', line 77

def irc_part_event(stem, sender, arguments) # :nodoc:
  super if listening?(stem, arguments[:channel])
end

#irc_privmsg_event(stem, sender, arguments) ⇒ Object

:nodoc:



69
70
71
# File 'lib/autumn/channel_leaf.rb', line 69

def irc_privmsg_event(stem, sender, arguments) # :nodoc:
  super if arguments[:channel].nil? or listening?(stem, arguments[:channel])
end

#irc_topic_event(stem, sender, arguments) ⇒ Object

:nodoc:



85
86
87
# File 'lib/autumn/channel_leaf.rb', line 85

def irc_topic_event(stem, sender, arguments) # :nodoc:
  super if listening?(stem, arguments[:channel])
end

#will_start_upObject

Creates a new instance. (See the Leaf class for more information.)



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/autumn/channel_leaf.rb', line 57

def will_start_up
  @channels = Hash.new
  @options[:channels] ||= Hash.new
  @options[:channels].each do |server, chans|
    stem = Foliater.instance.stems[server]
    raise "Unknown stem #{server}" unless stem
    chans = [ chans ] if chans.kind_of? String
    @channels[stem] = chans.map { |chan| stem.normalized_channel_name chan }
  end
  super
end