BigW Consortium Gitlab

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

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

  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

  def to_param
    'irker'
  end

23 24 25 26 27 28 29
  def supported_events
    %w(push)
  end

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

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

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

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

63 64 65 66 67
  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
68 69
  end

70
  private
Aorimn committed
71 72

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

89 90
  def format_channel(recipient)
    uri = nil
Aorimn committed
91 92 93

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

    unless uri.present? and default_irc_uri.nil?
      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
104 105
      end
    end
106 107

    uri
Aorimn committed
108 109 110
  end

  def consider_uri(uri)
111 112
    return nil if uri.scheme.nil?

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