Skip to content

Commit 2e5359f

Browse files
committed
Merge pull request #13 from colwem/name_changer
Name changer and channel changer
2 parents b33d958 + 2711b9f commit 2e5359f

File tree

8 files changed

+69
-3
lines changed

8 files changed

+69
-3
lines changed

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,11 @@ coverage/
99
data/
1010
.rbx
1111

12+
1213
*.swp
1314
*~
15+
16+
#emacs files
17+
\#*
18+
.#*
19+

Rakefile

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ end
2121
desc "start percival, connect to all channels in the CHANNELS env var"
2222
task :start do
2323
system 'mkdir -p data/timesheets/'
24-
channels = ENV["CHANNELS"].split(/,\s*/)
24+
channels = ENV["CHANNELS"] && ENV["CHANNELS"].split(/,\s*/) || ["#lpmc-bot"]
25+
nick = ENV["NICK"] || "percival"
2526
server = 'irc.freenode.com'
2627

2728
require 'percival'
@@ -30,8 +31,11 @@ task :start do
3031
configure do |c|
3132
c.server = server
3233
c.channels = channels
33-
c.nick = 'percival'
34-
c.plugins.plugins = [ClockPlugin, LoggerPlugin]
34+
c.nick = nick
35+
c.plugins.plugins = [ClockPlugin,
36+
LoggerPlugin,
37+
ChannelChangerPlugin,
38+
NameChangerPlugin]
3539
end
3640
end
3741

lib/percival.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
require 'percival/version'
44
require 'percival/clock'
55
require 'percival/logger'
6+
require 'percival/channel_changer'
7+
require 'percival/name_changer'
68

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

lib/percival/channel_changer.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
require 'percival/channel_changer/plugin'
2+
require 'percival/channel_changer/user_role'
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
class ChannelChangerPlugin
3+
include Cinch::Plugin
4+
5+
match /join-channel\s+(\S+)/, :method => :join
6+
match /leave-channel(?:\s+(\S+))?/, :method => :leave
7+
8+
listen_to :error, method: :error
9+
10+
def error irc
11+
debug( irc.to_s )
12+
end
13+
14+
def leave( irc, channel )
15+
if UserRole.approved? irc.user, :channel_changer
16+
channel ||= irc.channel
17+
Channel(channel).part
18+
end
19+
end
20+
21+
def join( irc, channel )
22+
Channel(channel).join() if UserRole.approved? irc.user, :channel_changer
23+
end
24+
end
25+
26+
27+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class UserRole
2+
@test_users = ["colwem", "jfredett"]
3+
@roles = {
4+
super_user: @test_users,
5+
channel_changer: @test_users,
6+
name_changer: @test_users}
7+
8+
def self.approved?(user, role)
9+
user = user.name if user.is_a? Cinch::User
10+
raise "user not in String, Cinch::User" unless user.is_a? String
11+
@roles[role].include? user
12+
end
13+
end

lib/percival/name_changer.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
require 'percival/name_changer/plugin'
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class NameChangerPlugin
2+
include Cinch::Plugin
3+
4+
match /change-name\s+(\S+)/, :method => :change_name
5+
6+
def change_name( irc, name )
7+
if UserRole.approved? irc.user, :name_changer
8+
bot.nick = name
9+
end
10+
end
11+
end

0 commit comments

Comments
 (0)