Skip to content

Commit

Permalink
added authentication; added a AuthenticatedTrigger class
Browse files Browse the repository at this point in the history
  • Loading branch information
wmeister-old committed Apr 24, 2010
1 parent 8155c8f commit 6f24205
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 4 deletions.
12 changes: 10 additions & 2 deletions arcanine.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
require 'on_irc'
require 'arcanine/trigger'
require 'arcanine/authenticated_trigger'

class Arcanine
attr_reader :monitor, :trigger_char, :help
attr_reader :trigger_char, :help, :password
attr_accessor :monitor, :authed_users

def initialize(template)
@password = template[:password] || template['password'] or raise 'password must be set in configuation!'
@trigger_char = template[:trigger_char] || template['trigger_char'] || '@'
@realname = __realname = template[:realname] || template['realname']
@ident = __ident = template[:ident] || template['ident']
@servers = __servers = template[:servers] || template['servers']
@nick = __nick = template[:nick] || template['nick']
@authed_users = []
@triggers = []
@monitor = {}

Expand All @@ -28,6 +32,10 @@ def initialize(template)
load_triggers
end

def authenticated(sender)
@authed_users.include? [sender.host, sender.nick]
end

def trigger_files
Dir.glob(File.expand_path(File.dirname(__FILE__)) + '/arcanine/trigger/*.rb')
end
Expand All @@ -52,7 +60,7 @@ def load_triggers
def test_triggers(irc, msg)
arcanine = self

for trigger in Arcanine::Trigger.all
for trigger in Arcanine::Trigger.all + Arcanine::AuthenticatedTrigger.all
trigger.send :run, arcanine, irc and break if trigger.send :match, msg
end
end
Expand Down
11 changes: 11 additions & 0 deletions arcanine/authenticated_trigger.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Arcanine
class AuthenticatedTrigger < Trigger
def self.inherited(subclass)
@@triggers << subclass
end

def self.run(arcanine, irc)
super arcanine, irc if arcanine.authenticated irc.sender
end
end
end
8 changes: 6 additions & 2 deletions arcanine/trigger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class Trigger
@@help = nil

def self.inherited(subclass)
@@triggers << subclass
@@triggers << subclass if subclass != AuthenticatedTrigger
end

def self.all
Expand All @@ -28,7 +28,11 @@ def self.match(msg)
end

def self.run(arcanine, irc)
action(arcanine, irc, *@@matches[1..-1])
begin
action(arcanine, irc, *@@matches[1..-1])
rescue Exception => e
irc.respond "Error in trigger #{self}: #{e.message} - #{e.backtrace.join '; '}"
end
end

def self.action
Expand Down
26 changes: 26 additions & 0 deletions arcanine/trigger/auth.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class Arcanine::Trigger::Auth < Arcanine::Trigger

=begin
command: auth
description: authenticate as an admin
=end

def self.regex
/^auth (.*)$/i
end

def self.action(arcanine, irc, password)
if irc.params[0][0,1] == '#'
irc.respond "Not in a channel..."
elsif password == arcanine.password
credentials = [irc.sender.host, irc.sender.nick]
if arcanine.authed_users.include? credentials
irc.respond "Already authenticted."
else
arcanine.authed_users << credentials
irc.respond "Successfully authenticated."
end
end
end
end

1 change: 1 addition & 0 deletions example.config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
:realname: arcanine-bot
:ident: arcanine
:nick: arcanine
:password: changeMe
:servers:
- :channels:
- "#botters"
Expand Down

0 comments on commit 6f24205

Please sign in to comment.