BigW Consortium Gitlab

irker_service.rb 3.56 KB
Newer Older
Aorimn committed
1 2 3
require 'uri'

class IrkerService < Service
4
  prop_accessor :server_host, :server_port, :default_irc_uri
5 6
  prop_accessor :recipients, :channels
  boolean_accessor :colorize_messages
7
  validates :recipients, presence: true, if: :valid_recipients?
Aorimn committed
8 9 10 11 12 13 14 15 16 17 18 19

  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

20
  def self.to_param
Aorimn committed
21 22 23
    'irker'
  end

24
  def self.supported_events
25 26 27 28 29 30
    %w(push)
  end

  def execute(data)
    return unless supported_events.include?(data[:object_kind])

Aorimn committed
31
    IrkerWorker.perform_async(project_id, channels,
32 33 34 35
                              colorize_messages, data, settings)
  end

  def settings
36 37
    {
      server_host: server_host.present? ? server_host : 'localhost',
38 39
      server_port: server_port.present? ? server_port : 6659
    }
Aorimn committed
40 41 42 43
  end

  def fields
    [
44 45 46 47
      { 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)' },
48
      { type: 'text', name: 'default_irc_uri', title: 'Default IRC URI',
49 50
        help: 'A default IRC URI to prepend before each recipient (optional)',
        placeholder: 'irc://irc.network.net:6697/' },
Aorimn committed
51
      { type: 'textarea', name: 'recipients',
52
        placeholder: 'Recipients/channels separated by whitespaces', required: true,
53 54 55 56
        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, ' \
57 58 59 60
        ' 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.'  },
61
      { type: 'checkbox', name: 'colorize_messages' }
Aorimn committed
62 63 64
    ]
  end

65 66 67 68 69
  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.'
Aorimn committed
70 71
  end

72
  private
Aorimn committed
73 74

  def get_channels
75
    return true unless activated?
Aorimn committed
76 77 78 79 80 81 82 83 84 85
    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|
86
      format_channel(recipient)
Aorimn committed
87
    end
88
    channels.reject!(&:nil?)
Aorimn committed
89 90
  end

91 92
  def format_channel(recipient)
    uri = nil
Aorimn committed
93 94 95

    # Try to parse the chan as a full URI
    begin
96
      uri = consider_uri(URI.parse(recipient))
Aorimn committed
97
    rescue URI::InvalidURIError
98 99
    end

Douwe Maan committed
100
    unless uri.present? && default_irc_uri.nil?
101 102 103 104 105
      begin
        new_recipient = URI.join(default_irc_uri, '/', recipient).to_s
        uri = consider_uri(URI.parse(new_recipient))
      rescue
        Rails.logger.error("Unable to create a valid URL from #{default_irc_uri} and #{recipient}")
Aorimn committed
106 107
      end
    end
108 109

    uri
Aorimn committed
110 111 112
  end

  def consider_uri(uri)
113 114
    return nil if uri.scheme.nil?

Aorimn committed
115
    # Authorize both irc://domain.com/#chan and irc://domain.com/chan
116
    if uri.is_a?(URI) && uri.scheme[/^ircs?\z/] && !uri.path.nil?
117
      uri.to_s
Aorimn committed
118 119 120
    end
  end
end