BigW Consortium Gitlab

irker_service.rb 3.53 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
Aorimn committed
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
  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

24 25 26 27 28 29 30
  def supported_events
    %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 36 37 38
                              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
39 40 41 42
  end

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

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

71
  private
Aorimn committed
72 73

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

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

    # Try to parse the chan as a full URI
    begin
95
      uri = consider_uri(URI.parse(recipient))
Aorimn committed
96
    rescue URI::InvalidURIError
97 98 99 100 101 102 103 104
    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
105 106
      end
    end
107 108

    uri
Aorimn committed
109 110 111
  end

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

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