Skip to content

Commit

Permalink
nickserv: NickServ にログインするプラグインの実装開始
Browse files Browse the repository at this point in the history
issue: #14
  • Loading branch information
koi-chan committed Dec 27, 2016
1 parent 368804e commit 734874d
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 12 deletions.
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
4 changes: 2 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 @@ -152,7 +152,7 @@ def new_logger(log_level)
# @return [Cinch::Bot]
def new_bot(config, plugins, log_level)
bot_config = config.irc_bot
plugin_options = {}
plugin_options = config.plugins
plugins.each do |p|
plugin_options[p] = { logger: @logger }
end
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[:login_server]
@myself = config[:myself]
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(m)
sleep 1
Cinch::UserList.find_ensured(
@nickserv[:user],
@nickserv[:nick],
@nickserv[:host]
).send("IDENTIFY #{@myself[:nick]} #{@myself[:pass]}", true)
end
end
end
end

0 comments on commit 734874d

Please sign in to comment.