Skip to content

Name changer and channel changer #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 14, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,11 @@ coverage/
data/
.rbx


*.swp
*~

#emacs files
\#*
.#*

10 changes: 7 additions & 3 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ end
desc "start percival, connect to all channels in the CHANNELS env var"
task :start do
system 'mkdir -p data/timesheets/'
channels = ENV["CHANNELS"].split(/,\s*/)
channels = ENV["CHANNELS"] && ENV["CHANNELS"].split(/,\s*/) || ["#lpmc-bot"]
nick = ENV["NICK"] || "percival"
server = 'irc.freenode.com'

require 'percival'
Expand All @@ -30,8 +31,11 @@ task :start do
configure do |c|
c.server = server
c.channels = channels
c.nick = 'percival'
c.plugins.plugins = [ClockPlugin, LoggerPlugin]
c.nick = nick
c.plugins.plugins = [ClockPlugin,
LoggerPlugin,
ChannelChangerPlugin,
NameChangerPlugin]
end
end

Expand Down
2 changes: 2 additions & 0 deletions lib/percival.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
require 'percival/version'
require 'percival/clock'
require 'percival/logger'
require 'percival/channel_changer'
require 'percival/name_changer'

PERCIVAL_ROOT = File.dirname(File.dirname(__FILE__))

2 changes: 2 additions & 0 deletions lib/percival/channel_changer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require 'percival/channel_changer/plugin'
require 'percival/channel_changer/user_role'
27 changes: 27 additions & 0 deletions lib/percival/channel_changer/plugin.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

class ChannelChangerPlugin
include Cinch::Plugin

match /join-channel\s+(\S+)/, :method => :join
match /leave-channel(?:\s+(\S+))?/, :method => :leave

listen_to :error, method: :error

def error irc
debug( irc.to_s )
end

def leave( irc, channel )
if UserRole.approved? irc.user, :channel_changer
channel ||= irc.channel
Channel(channel).part
end
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a note, this is another good place to use the guard-style as noted in my other comment about UserRole#approved?

end

def join( irc, channel )
Channel(channel).join() if UserRole.approved? irc.user, :channel_changer
end
end



13 changes: 13 additions & 0 deletions lib/percival/channel_changer/user_role.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class UserRole
@test_users = ["colwem", "jfredett"]
@roles = {
super_user: @test_users,
channel_changer: @test_users,
name_changer: @test_users}

def self.approved?(user, role)
user = user.name if user.is_a? Cinch::User
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On thing to note here (not a major issue)

It's good practice to ask what something can do rather than what something is. For instance, if I were to build a proxy object for Cinch::User which provided some extra methods or something, it might not .is_a? the way you think, but it still can fulfill the contract of this method (since it just proxies down to Cinch::User#name, perhaps).

A concrete example might be a multi-name tracker, we might have a model Cinch::MultiUser, which tracks a user who uses multiple names that don't fit some schema, but we want to grant approval to all of them. We might also want to have a Cinch::AuthenticatedUser, which represents a Freenode auth'd user. etc.

Just something to keep in mind.

raise "user not in String, Cinch::User" unless user.is_a? String
@roles[role].include? user
end
end
1 change: 1 addition & 0 deletions lib/percival/name_changer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require 'percival/name_changer/plugin'
11 changes: 11 additions & 0 deletions lib/percival/name_changer/plugin.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class NameChangerPlugin
include Cinch::Plugin

match /change-name\s+(\S+)/, :method => :change_name

def change_name( irc, name )
if UserRole.approved? irc.user, :name_changer
bot.nick = name
end
end
end