Skip to content

Commit

Permalink
Merge pull request #16 from cre-ne-jp/nickserv
Browse files Browse the repository at this point in the history
NickServ にログインする
  • Loading branch information
ochaochaocha3 authored Jan 6, 2017
2 parents 368804e + 2432199 commit 9e39220
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 27 deletions.
28 changes: 13 additions & 15 deletions config/ircbot.yml.example
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
default:
# IRC ボットの設定
IRCBot: &default
# IRC ボットの設定
development:
IRCBot:
Host: irc.example.net
Port: 6667
Password: pa$$word
Expand All @@ -10,19 +10,17 @@ default:
RealName: LogArchiver
Channels:
- ''

development:
IRCBot:
<<: *default
Host: irc.kazagakure.net
Password: null
Channels:
- '#koi-chan'
Plugins:
LoginNickserv:
NickServ:
Nick: 'NickServ'
User: 'NickServ'
Host: 'services.cre.jp'
LoginServer: 'services.cre.jp'
Account:
Nick: 'YOUR NICK'
Pass: 'PA$$WORD'

test:
IRCBot:
<<: *default

production:
IRCBot:
<<: *default
13 changes: 3 additions & 10 deletions lib/ircs/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,15 @@ class Config
# IRC ボットの設定のハッシュ
# @return [Hash]
attr_reader :irc_bot
# プラグイン名の配列
# @return [Array<String>]
# プラグインの設定
# @return [Array<Hash>]
attr_reader :plugins
# プラグイン設定のハッシュ
# @return [Hash]
attr_reader :plugin_config

class << self
# 設定 ID から設定ファイルのパスに変換する
# @param [String] config_id 設定 ID
# @param [String] root_path 設定ファイルのルートディレクトリのパス
# @return [String] 設定ファイルのパス
def config_id_to_path(config_id, root_path)
if config_id.include?('../')
fail(ArgumentError, "#{config_id}: ディレクトリトラバーサルの疑い")
Expand All @@ -45,11 +43,6 @@ def load_yaml_file(config_id, root_path, mode)
def initialize(config_data)
@irc_bot = config_data['IRCBot']
@plugins = config_data['Plugins'] || []
@plugin_config = {}

@plugins.each do |name|
@plugin_config[name] = config_data[name]
end
end
end
end
5 changes: 3 additions & 2 deletions lib/ircs/irc_bot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def run(root_path, argv)

@logger = new_logger(log_level)
config = load_config(config_id, options[:mode])
plugins = load_plugins(%w(ChannelSync SaveLog KickBack))
plugins = load_plugins(%w(ChannelSync SaveLog KickBack LoginNickserv))

bot = new_bot(config, plugins, log_level)

Expand Down Expand Up @@ -154,7 +154,8 @@ def new_bot(config, plugins, log_level)
bot_config = config.irc_bot
plugin_options = {}
plugins.each do |p|
plugin_options[p] = { logger: @logger }
plugin_options[p] = config.plugins[p.plugin_name] || {}
plugin_options[p][:logger] = @logger
end

bot = Cinch::Bot.new do
Expand Down
70 changes: 70 additions & 0 deletions lib/ircs/plugins/login_nickserv.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# vim: fileencoding=utf-8

require 'cinch'

require_relative 'plugin_template'

module LogArchiver
module Plugin
# NickServ にログインする
class LoginNickserv < Template
include Cinch::Plugin

set(plugin_name: 'LoginNickserv')
self.prefix = ''
self.react_on = :notice

# ホスト名を表す正規表現
# @see http://stackoverflow.com/questions/106179/regular-expression-to-match-dns-hostname-or-ip-address
HOSTNAME_RE =
/(?:[a-z\d](?:[-a-z\d]{0,61}[a-z\d])?
(?:\.[a-z\d](?:[-a-z\d]{0,61}[a-z\d])?)*)/ix
# サーバーがネットワークに参加したときのメッセージを表す正規表現
NETJOIN_RE =
/^\*\*\* Notice -- Netjoin #{HOSTNAME_RE} <-> (#{HOSTNAME_RE})/o

# サーバーがネットワークに参加したときのメッセージを表す正規表現
match(NETJOIN_RE, method: :joined)
# サーバーへの接続が完了したときに情報を集める
listen_to(:'002', method: :connected)

def initialize(*)
super

@nickserv = config['NickServ']
@login_server = config['LoginServer']
@myself = config['Account']
end

# サーバ接続メッセージを検知し、NickServ サーバならログインする
# @param [Cinch::Message] m メッセージ
# @param [String] server サーバ
# @return [void]
def joined(m, server)
if m.server && server == @login_server
login
@logger.warn("#{server} がリレーしたため、NickServ へのログインを試行しました")
end
end

# サーバに接続したとき、NickServ にログインする
# @param [Cinch::Message] m メッセージ
# @return [void]
def connected(m)
login
@logger.warn("NickServ へのログインを試行しました")
end

# NickServ にログインする
# @return [void]
def login
sleep 1
Cinch::UserList.new(@bot).find_ensured(
@nickserv['User'],
@nickserv['Nick'],
@nickserv['Host']
).send("IDENTIFY #{@myself['Nick']} #{@myself['Pass']}", false)
end
end
end
end

0 comments on commit 9e39220

Please sign in to comment.