2018-08-10 02:45:01 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-02-04 15:31:55 -05:00
|
|
|
require 'uri'
|
|
|
|
|
|
|
|
class IrkerService < Service
|
2015-07-03 02:17:10 -04:00
|
|
|
prop_accessor :server_host, :server_port, :default_irc_uri
|
2016-12-05 09:40:53 -05:00
|
|
|
prop_accessor :recipients, :channels
|
|
|
|
boolean_accessor :colorize_messages
|
2018-01-26 08:48:47 -05:00
|
|
|
validates :recipients, presence: true, if: :valid_recipients?
|
2015-02-04 15:31:55 -05:00
|
|
|
|
|
|
|
before_validation :get_channels
|
|
|
|
|
|
|
|
def title
|
|
|
|
'Irker (IRC gateway)'
|
|
|
|
end
|
|
|
|
|
|
|
|
def description
|
|
|
|
'Send IRC messages, on update, to a list of recipients through an Irker '\
|
|
|
|
'gateway.'
|
|
|
|
end
|
|
|
|
|
2016-12-27 07:44:24 -05:00
|
|
|
def self.to_param
|
2015-02-04 15:31:55 -05:00
|
|
|
'irker'
|
|
|
|
end
|
|
|
|
|
2016-12-27 07:44:24 -05:00
|
|
|
def self.supported_events
|
2015-03-04 17:31:28 -05:00
|
|
|
%w(push)
|
|
|
|
end
|
|
|
|
|
|
|
|
def execute(data)
|
|
|
|
return unless supported_events.include?(data[:object_kind])
|
|
|
|
|
2015-02-04 15:31:55 -05:00
|
|
|
IrkerWorker.perform_async(project_id, channels,
|
2015-07-03 02:17:10 -04:00
|
|
|
colorize_messages, data, settings)
|
|
|
|
end
|
|
|
|
|
|
|
|
def settings
|
2017-02-22 12:44:44 -05:00
|
|
|
{
|
|
|
|
server_host: server_host.present? ? server_host : 'localhost',
|
2015-07-03 02:17:10 -04:00
|
|
|
server_port: server_port.present? ? server_port : 6659
|
|
|
|
}
|
2015-02-04 15:31:55 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def fields
|
|
|
|
[
|
2015-07-03 02:17:10 -04:00
|
|
|
{ type: 'text', name: 'server_host', placeholder: 'localhost',
|
|
|
|
help: 'Irker daemon hostname (defaults to localhost)' },
|
|
|
|
{ type: 'text', name: 'server_port', placeholder: 6659,
|
|
|
|
help: 'Irker daemon port (defaults to 6659)' },
|
2015-07-07 08:48:32 -04:00
|
|
|
{ type: 'text', name: 'default_irc_uri', title: 'Default IRC URI',
|
2015-07-03 02:17:10 -04:00
|
|
|
help: 'A default IRC URI to prepend before each recipient (optional)',
|
|
|
|
placeholder: 'irc://irc.network.net:6697/' },
|
2015-02-04 15:31:55 -05:00
|
|
|
{ type: 'textarea', name: 'recipients',
|
2017-05-22 06:07:12 -04:00
|
|
|
placeholder: 'Recipients/channels separated by whitespaces', required: true,
|
2015-07-03 02:17:10 -04:00
|
|
|
help: 'Recipients have to be specified with a full URI: '\
|
|
|
|
'irc[s]://irc.network.net[:port]/#channel. Special cases: if '\
|
|
|
|
'you want the channel to be a nickname instead, append ",isnick" to ' \
|
|
|
|
'the channel name; if the channel is protected by a secret password, ' \
|
2016-01-12 17:54:09 -05:00
|
|
|
' append "?key=secretpassword" to the URI (Note that due to a bug, if you ' \
|
|
|
|
' want to use a password, you have to omit the "#" on the channel). If you ' \
|
|
|
|
' specify a default IRC URI to prepend before each recipient, you can just ' \
|
|
|
|
' give a channel name.' },
|
2017-05-03 07:22:03 -04:00
|
|
|
{ type: 'checkbox', name: 'colorize_messages' }
|
2015-02-04 15:31:55 -05:00
|
|
|
]
|
|
|
|
end
|
|
|
|
|
2015-07-03 02:17:10 -04:00
|
|
|
def help
|
|
|
|
' NOTE: Irker does NOT have built-in authentication, which makes it' \
|
|
|
|
' vulnerable to spamming IRC channels if it is hosted outside of a ' \
|
|
|
|
' firewall. Please make sure you run the daemon within a secured network ' \
|
|
|
|
' to prevent abuse. For more details, read: http://www.catb.org/~esr/irker/security.html.'
|
2015-02-04 15:31:55 -05:00
|
|
|
end
|
|
|
|
|
2015-07-03 02:17:10 -04:00
|
|
|
private
|
2015-02-04 15:31:55 -05:00
|
|
|
|
|
|
|
def get_channels
|
2016-05-30 06:34:25 -04:00
|
|
|
return true unless activated?
|
2015-02-04 15:31:55 -05:00
|
|
|
return true if recipients.nil? || recipients.empty?
|
|
|
|
|
|
|
|
map_recipients
|
|
|
|
|
|
|
|
errors.add(:recipients, 'are all invalid') if channels.empty?
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def map_recipients
|
|
|
|
self.channels = recipients.split(/\s+/).map do |recipient|
|
2015-07-03 02:17:10 -04:00
|
|
|
format_channel(recipient)
|
2015-02-04 15:31:55 -05:00
|
|
|
end
|
2016-06-03 04:59:43 -04:00
|
|
|
channels.reject!(&:nil?)
|
2015-02-04 15:31:55 -05:00
|
|
|
end
|
|
|
|
|
2015-07-03 02:17:10 -04:00
|
|
|
def format_channel(recipient)
|
|
|
|
uri = nil
|
2015-02-04 15:31:55 -05:00
|
|
|
|
|
|
|
# Try to parse the chan as a full URI
|
|
|
|
begin
|
2015-07-03 02:17:10 -04:00
|
|
|
uri = consider_uri(URI.parse(recipient))
|
2015-02-04 15:31:55 -05:00
|
|
|
rescue URI::InvalidURIError
|
2015-07-03 02:17:10 -04:00
|
|
|
end
|
|
|
|
|
2017-02-21 17:31:14 -05:00
|
|
|
unless uri.present? && default_irc_uri.nil?
|
2015-07-03 02:17:10 -04:00
|
|
|
begin
|
|
|
|
new_recipient = URI.join(default_irc_uri, '/', recipient).to_s
|
|
|
|
uri = consider_uri(URI.parse(new_recipient))
|
|
|
|
rescue
|
2018-08-20 14:34:07 -04:00
|
|
|
log_error("Unable to create a valid URL", default_irc_uri: default_irc_uri, recipient: recipient)
|
2015-02-04 15:31:55 -05:00
|
|
|
end
|
|
|
|
end
|
2015-07-03 02:17:10 -04:00
|
|
|
|
|
|
|
uri
|
2015-02-04 15:31:55 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def consider_uri(uri)
|
2015-07-03 02:17:10 -04:00
|
|
|
return nil if uri.scheme.nil?
|
|
|
|
|
2015-02-04 15:31:55 -05:00
|
|
|
# Authorize both irc://domain.com/#chan and irc://domain.com/chan
|
2015-04-10 12:30:02 -04:00
|
|
|
if uri.is_a?(URI) && uri.scheme[/^ircs?\z/] && !uri.path.nil?
|
2016-06-30 07:20:15 -04:00
|
|
|
uri.to_s
|
2015-02-04 15:31:55 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|