debian-mirror-gitlab/app/models/project_services/irker_service.rb

122 lines
3.5 KiB
Ruby
Raw Normal View History

2018-11-20 20:47:30 +05:30
# frozen_string_literal: true
2015-04-26 12:48:37 +05:30
require 'uri'
2021-06-08 01:23:25 +05:30
class IrkerService < Integration
2015-09-11 14:41:01 +05:30
prop_accessor :server_host, :server_port, :default_irc_uri
2017-08-17 22:00:37 +05:30
prop_accessor :recipients, :channels
boolean_accessor :colorize_messages
2021-04-29 21:17:54 +05:30
validates :recipients, presence: true, if: :validate_recipients?
2015-04-26 12:48:37 +05:30
before_validation :get_channels
def title
'Irker (IRC gateway)'
end
def description
2021-06-08 01:23:25 +05:30
'Send IRC messages.'
2015-04-26 12:48:37 +05:30
end
2017-08-17 22:00:37 +05:30
def self.to_param
2015-04-26 12:48:37 +05:30
'irker'
end
2017-08-17 22:00:37 +05:30
def self.supported_events
2015-04-26 12:48:37 +05:30
%w(push)
end
def execute(data)
return unless supported_events.include?(data[:object_kind])
IrkerWorker.perform_async(project_id, channels,
2015-09-11 14:41:01 +05:30
colorize_messages, data, settings)
end
def settings
2017-08-17 22:00:37 +05:30
{
2019-12-21 20:55:43 +05:30
server_host: server_host.presence || 'localhost',
server_port: server_port.presence || 6659
2015-09-11 14:41:01 +05:30
}
2015-04-26 12:48:37 +05:30
end
def fields
[
2015-09-11 14:41:01 +05:30
{ 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)' },
{ type: 'text', name: 'default_irc_uri', title: 'Default IRC URI',
help: 'A default IRC URI to prepend before each recipient (optional)',
placeholder: 'irc://irc.network.net:6697/' },
2015-04-26 12:48:37 +05:30
{ type: 'textarea', name: 'recipients',
2017-09-10 17:25:29 +05:30
placeholder: 'Recipients/channels separated by whitespaces', required: true,
2015-09-11 14:41:01 +05:30
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, ' \
' 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 ' \
2019-03-02 22:35:43 +05:30
' give a channel name.' },
2017-09-10 17:25:29 +05:30
{ type: 'checkbox', name: 'colorize_messages' }
2015-04-26 12:48:37 +05:30
]
end
2015-09-11 14:41:01 +05:30
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-04-26 12:48:37 +05:30
end
2015-09-11 14:41:01 +05:30
private
2015-04-26 12:48:37 +05:30
def get_channels
return true unless activated?
2015-04-26 12:48:37 +05:30
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-09-11 14:41:01 +05:30
format_channel(recipient)
2015-04-26 12:48:37 +05:30
end
channels.reject!(&:nil?)
2015-04-26 12:48:37 +05:30
end
2015-09-11 14:41:01 +05:30
def format_channel(recipient)
uri = nil
2015-04-26 12:48:37 +05:30
# Try to parse the chan as a full URI
begin
2015-09-11 14:41:01 +05:30
uri = consider_uri(URI.parse(recipient))
2015-04-26 12:48:37 +05:30
rescue URI::InvalidURIError
2015-09-11 14:41:01 +05:30
end
2017-08-17 22:00:37 +05:30
unless uri.present? && default_irc_uri.nil?
2015-09-11 14:41:01 +05:30
begin
new_recipient = URI.join(default_irc_uri, '/', recipient).to_s
uri = consider_uri(URI.parse(new_recipient))
2021-06-08 01:23:25 +05:30
rescue StandardError
2018-11-20 20:47:30 +05:30
log_error("Unable to create a valid URL", default_irc_uri: default_irc_uri, recipient: recipient)
2015-04-26 12:48:37 +05:30
end
end
2015-09-11 14:41:01 +05:30
uri
2015-04-26 12:48:37 +05:30
end
def consider_uri(uri)
2019-07-07 11:18:12 +05:30
return if uri.scheme.nil?
2015-09-11 14:41:01 +05:30
2015-04-26 12:48:37 +05:30
# Authorize both irc://domain.com/#chan and irc://domain.com/chan
if uri.is_a?(URI) && uri.scheme[/^ircs?\z/] && !uri.path.nil?
2016-08-24 12:49:21 +05:30
uri.to_s
2015-04-26 12:48:37 +05:30
end
end
end