Skip to content

Commit

Permalink
MailGeneratorをIRCアダプタで使う
Browse files Browse the repository at this point in the history
refs #71

少なくともテスト用のIRCアダプタではメールの送信が行えることを
確認した
  • Loading branch information
ochaochaocha3 committed Jan 8, 2018
1 parent 9b7a747 commit 1376fee
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 65 deletions.
21 changes: 9 additions & 12 deletions lib/rgrb/plugin/server_connection_report/charybdis/irc_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
require 'cinch'

require 'rgrb/plugin/server_connection_report/constants'
require 'rgrb/plugin/server_connection_report/common_disposal'
require 'rgrb/plugin/server_connection_report/irc_adapter_base'

module RGRB
module Plugin
Expand All @@ -20,7 +20,7 @@ module Charybdis
# ServerConnectionReport::Charybdis の IRC アダプター
class IrcAdapter
include Cinch::Plugin
include ServerConnectionReport::CommonDisposal
include ServerConnectionReport::IrcAdapterBase

# サーバーがネットワークに参加したときのメッセージを表す正規表現
NETJOIN_RE =
Expand All @@ -40,7 +40,7 @@ class IrcAdapter
# 正規表現
match(NETSPLIT_RE, method: :disconnected)
# サーバーへの接続が完了したときに情報を集める
listen_to(:'002', method: :connected)
listen_to(:'002', method: :set_connection_info)

def initialize(*)
super
Expand All @@ -51,7 +51,9 @@ def initialize(*)
# @param [String] server サーバ
# @return [void]
def joined(m, server)
_joined(m,server)
if m.server
notice_joined(m, server)
end
end

# サーバ切断メッセージを NOTICE する
Expand All @@ -60,14 +62,9 @@ def joined(m, server)
# @param [String] comment コメント
# @return [void]
def disconnected(m, server, comment)
_disconnected(m, server, comment)
end

# サーバーへの接続が完了したとき、情報を集める
# @param [Cinch::Message] m メッセージ
# @return [void]
def connected(m)
_connected(m)
if m.server
notice_disconnected(m, server, comment)
end
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,21 @@
require 'rgrb/plugin/util/notice_on_each_channel'
require 'rgrb/plugin/server_connection_report/constants'
require 'rgrb/plugin/server_connection_report/generator'
require 'rgrb/plugin/server_connection_report/common_disposal'
require 'rgrb/plugin/server_connection_report/mail_generator'

module RGRB
module Plugin
module ServerConnectionReport
# サーバリレー監視プラグイン アダプター共通モジュール
#
# サーバの接続状態が変化したとき、各デーモンで共通な処理を処理する。
module CommonDisposal
module IrcAdapterBase
include Cinch::Plugin
include Util::NoticeOnEachChannel

# メッセージを送信するチャンネルのリスト
attr_reader :channels_to_send

set(plugin_name: 'ServerConnectionReport::CommonDisposal')
self.prefix = ''

def initialize(*)
super

Expand All @@ -32,46 +29,67 @@ def initialize(*)
@channels_to_send = config_data['ChannelsToSend'] || []

@generator = Generator.new
@generator.root_path = config[:root_path]
@generator.configure(config_data)

@mail_generator = nil
if config_data['Mail'] && config_data['MessageTemplate']
@mail_generator = MailGenerator.new
@mail_generator.root_path = config[:root_path]
@mail_generator.configure(config_data)
@mail_generator.load_mail_template_by_name(
config_data['MessageTemplate']
)

warn('メール送信を行います')
else
warn('メール送信を行いません')
end
end

# サーバ接続メッセージを NOTICE する
# @param [Cinch::Message] m メッセージ
# @param [String] server サーバ
# @return [void]
def _joined(m, server)
if m.server
log_incoming(m)
sleep 1
notice_on_each_channel(@generator.joined(server, m.time))
end
def notice_joined(m, server)
log_incoming(m)
sleep 1
notice_on_each_channel(@generator.joined(server))

mail = @mail_generator&.generate(
server,
:joined,
m.time
)
mail&.deliver
end

# サーバ切断メッセージを NOTICE する
# @param [Cinch::Message] m メッセージ
# @param [String] server サーバ
# @param [String] comment コメント
# @return [void]
def _disconnected(m, server, comment)
if m.server
log_incoming(m)
notice_on_each_channel(
@generator.disconnected(server, m.time, comment)
)
end
def notice_disconnected(m, server, comment)
log_incoming(m)
notice_on_each_channel(@generator.disconnected(server, comment))

mail = @mail_generator&.generate(
server,
:disconnected,
m.time,
comment
)
mail&.deliver
end

# サーバーへの接続が完了したとき、情報を集める
# @param [Cinch::Message] m メッセージ
# @return [void]
def _connected(m)
def set_connection_info(*)
return unless @mail_generator

sleep 1
@generator.connection_datas = {
host: bot.host,
nick: bot.nick,
network: bot.irc.isupport['NETWORK']
}

@mail_generator.irc_host = bot.host
@mail_generator.irc_nick = bot.nick
@mail_generator.irc_network = bot.irc.isupport['NETWORK']
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/rgrb/plugin/server_connection_report/mail_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ def load_mail_template_by_name(name)
# @param [DateTime] time 接続・切断時間
# @param [String] message 補足メッセージ
# @return [MailData]
def generate(server, status, time, message)
def generate(server, status, time, message = '')
data_parts = {
host: @irc_host,
nick: @irc_nick,
Expand Down
33 changes: 8 additions & 25 deletions lib/rgrb/plugin/server_connection_report/test/irc_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

require 'rgrb/plugin/server_connection_report/constants'
require 'rgrb/plugin/server_connection_report/generator'
require 'rgrb/plugin/server_connection_report/common_disposal'
require 'rgrb/plugin/server_connection_report/irc_adapter_base'

module RGRB
module Plugin
Expand All @@ -22,7 +22,7 @@ module Test
# ServerConnectionReport::Test の IRC アダプター
class IrcAdapter
include Cinch::Plugin
include ServerConnectionReport::CommonDisposal
include ServerConnectionReport::IrcAdapterBase

# サーバーがネットワークに参加したときのメッセージを表す正規表現
NETJOIN_RE =
Expand All @@ -41,29 +41,22 @@ class IrcAdapter
# 正規表現
match(NETSPLIT_RE, method: :disconnected)
# サーバーへの接続が完了したときに情報を集める
listen_to(:'002', method: :connected)
listen_to(:'002', method: :set_connection_info)

def initialize(*)
super

config_data = config[:plugin]
@channels_to_send = config_data['ChannelsToSend'] || []
@testchannel = config_data['TestChannel'] || '#irc_test'

@generator = Generator.new
@generator.root_path = config[:root_path]
@generator.configure(config[:plugin])
@test_channel = config_data['TestChannel'] || '#irc_test'
end

# サーバ接続メッセージを NOTICE する
# @param [Cinch::Message] m メッセージ
# @param [String] server サーバ
# @return [void]
def joined(m, server)
if m.channel == @testchannel
log_incoming(m)
sleep 1
notice_on_each_channel(@generator.joined(server, m.time))
if m.channel == @test_channel
notice_joined(m, server)
end
end

Expand All @@ -73,20 +66,10 @@ def joined(m, server)
# @param [String] comment コメント
# @return [void]
def disconnected(m, server, comment)
if m.channel == @testchannel
log_incoming(m)
notice_on_each_channel(
@generator.disconnected(server, m.time, comment)
)
if m.channel == @test_channel
notice_disconnected(m, server, comment)
end
end

# サーバーへの接続が完了したとき、情報を集める
# @param [Cinch::Message] m メッセージ
# @return [void]
def connected(m)
_connected(m)
end
end
end
end
Expand Down

0 comments on commit 1376fee

Please sign in to comment.