Module: Autumn::StemFacade

Included in:
Stem
Defined in:
lib/autumn/stem_facade.rb

Overview

A collection of convenience methods that are added to the Stem class. These methods serve two purposes; one, to allow easier backwards compatibility with Autumn Leaves 1.0 (which had a simpler one-stem-per-leaf approach), and two, to make it easier or more Ruby-like to perform certain IRC actions.

Instance Method Summary collapse

Instance Method Details

#change_nick(new_nick) ⇒ Object

Changes this stem’s IRC nick. Note that the stem’s original nick will still be used by the logger.



68
69
70
# File 'lib/autumn/stem_facade.rb', line 68

def change_nick(new_nick)
  nick new_nick
end

#grant_user_privilege(channel, nick, privilege) ⇒ Object

Grants a privilege to a channel member, such as voicing a member. The stem must have the required privilege level to perform this command. privilege can either be a symbol from the Daemon instance or a string with the letter code for the privilege.

grant_user_privilege 'mychannel', 'Somedude', :operator
grant_user_privilege '#mychannel', 'Somedude', 'oa'


80
81
82
83
84
85
# File 'lib/autumn/stem_facade.rb', line 80

def grant_user_privilege(channel, nick, privilege)
  channel = normalized_channel_name(channel)
  privcode = server_type.privilege.index(privilege).chr if server_type.privilege.value? privilege
  privcode ||= privilege
  mode channel, "+#{privcode}", nick
end

#grant_usermode(nick, property) ⇒ Object

Grants a usermode to an IRC nick, such as making a nick invisible. The stem must have the required privilege level to perform this command. (Generally, one can only change his own usermode unless he is a server op.) mode can either be a symbol from the Daemon instance or a string with the letter code for the usermode.

grant_usermode 'Spycloak', :invisible
grant_usermode 'UpMobility', 'os'


108
109
110
111
112
# File 'lib/autumn/stem_facade.rb', line 108

def grant_usermode(nick, property)
  propcode = server_type.usermode.index(property).chr if server_type.usermode.value? property
  propcode ||= property
  mode nick, "+#{property}"
end

#join_channel(channel, password = nil) ⇒ Object

Joins a channel by name. If the channel is password-protected, specify the password parameter. Of course, you could always just call the join method (since each IRC command has a method named after it), but the advantage to using this method is that it will also update the @channel_passwords instance variable. Internal consistency is a good thing, so please use this method.



50
51
52
53
54
55
# File 'lib/autumn/stem_facade.rb', line 50

def join_channel(channel, password=nil)
  channel = normalized_channel_name(channel)
  return if channels.include? channel
  join channel, password
  @channel_passwords[channel] = password if password
end

#leave_channel(channel) ⇒ Object

Leaves a channel, specified by name.



59
60
61
62
63
# File 'lib/autumn/stem_facade.rb', line 59

def leave_channel(channel)
  channel = normalized_channel_name(channel)
  return unless channels.include? channel
  part channel
end

#message(msg, *chans) ⇒ Object

Sends a message to one or more channels or nicks. If no channels or nicks are specified, broadcasts the message to every channel the stem is in. If you are sending a message to a channel you must prefix it correctly; the “#” will not be added before the channel name for you.

message "Look at me!" # Broadcasts to all channels
message "I love kitties", '#kitties' # Sends a message to one channel or person
message "Learn to RTFM", '#help', 'NoobGuy' # Sends a message to two channels or people


22
23
24
25
26
27
28
29
30
# File 'lib/autumn/stem_facade.rb', line 22

def message(msg, *chans)
  return if msg.nil? or msg.empty?
  chans = channels if chans.empty?
  if @throttle then
    Thread.exclusive { msg.each_line { |line| privmsgt chans.to_a, line.strip unless line.strip.empty? } }
  else
    msg.each_line { |line| privmsg chans.to_a, line.strip unless line.strip.empty? }
  end
end

#privilege(channel, user) ⇒ Object

Returns the privilege level of a channel member. The privilege level will be a symbol from the Daemon instance. Returns nil if the channel member doesn’t exist or if the bot is not on the given channel. Returns an array of privileges if the server supports multiple privileges per user, and the user has more than one privilege.

user can be a nick or a sender hash.



171
172
173
174
# File 'lib/autumn/stem_facade.rb', line 171

def privilege(channel, user)
  user = user[:nick] if user.kind_of? Hash
  @chan_mutex.synchronize { @channel_members[channel] && @channel_members[channel][user] }
end

#remove_user_privilege(channel, nick, privilege) ⇒ Object

Removes a privilege to a channel member, such as voicing a member. The stem must have the required privilege level to perform this command. privilege can either be a symbol from the Daemon instance or a string with the letter code for the privilege.



92
93
94
95
96
97
# File 'lib/autumn/stem_facade.rb', line 92

def remove_user_privilege(channel, nick, privilege)
  channel = normalized_channel_name(channel)
  privcode = server_type.privilege.index(privilege).chr if server_type.privilege.value? privilege
  privcode ||= privilege
  mode channel, "-#{privcode}", nick
end

#remove_usermode(nick, property) ⇒ Object

Revokes a usermode from an IRC nick, such as removing invisibility. The stem must have the required privilege level to perform this command. (Generally, one can only change his own usermode unless he is a server op.) mode can either be a symbol from the Daemon instance or a string with the letter code for the usermode.



120
121
122
123
124
# File 'lib/autumn/stem_facade.rb', line 120

def remove_usermode(nick, property)
  propcode = server_type.usermode.index(property).chr if server_type.usermode.value? property
  propcode ||= property
  mode nick, "-#{property}"
end

#set_channel_property(channel, property, argument = nil) ⇒ Object

Sets a property of a channel, such as moderated. The stem must have the required privilege level to perform this command. property can either be a symbol from the Daemon instance or a string with the letter code for the property. If the property takes an argument (such as when setting a channel password), pass it as the argument paramter.

set_channel_property '#mychannel', :secret
set_channel_property 'mychannel', :keylock, 'mypassword'
set_channel_property '#mychannel', 'ntr'


136
137
138
139
140
141
# File 'lib/autumn/stem_facade.rb', line 136

def set_channel_property(channel, property, argument=nil)
  channel = normalized_channel_name(channel)
  propcode = server_type.channel_property.index(property).chr if server_type.channel_property.value? property
  propcode ||= property
  mode channel, "+#{propcode}", argument
end

#set_topic(motd, *chans) ⇒ Object

Sets the topic for one or more channels. If no channels are specified, sets the topic of every channel the stem is in.

set_topic "Bots sure are fun!", 'bots', 'morebots'


37
38
39
40
41
# File 'lib/autumn/stem_facade.rb', line 37

def set_topic(motd, *chans)
  return if motd.nil?
  chans = chans.empty? ? channels : chans.map { |chan| normalized_channel_name chan }
  chans.each { |chan| topic chan, motd }
end

#unset_channel_property(channel, property, argument = nil) ⇒ Object

Removes a property of a channel, such as moderated. The stem must have the required privilege level to perform this command. property can either be a symbol from the Daemon instance or a string with the letter code for the property. If the property takes an argument (such as when removing a channel password), pass it as the argument paramter.



149
150
151
152
153
154
# File 'lib/autumn/stem_facade.rb', line 149

def unset_channel_property(channel, property, argument=nil)
  channel = normalized_channel_name(channel)
  propcode = server_type.channel_property.index(property).chr if server_type.channel_property.value? property
  propcode ||= property
  mode channel, "-#{propcode}", argument
end

#users(channel) ⇒ Object

Returns an array of nicks for users that are in a channel.



158
159
160
161
# File 'lib/autumn/stem_facade.rb', line 158

def users(channel)
  channel = normalized_channel_name(channel)
  @chan_mutex.synchronize { @channel_members[channel] && @channel_members[channel].keys }
end