summaryrefslogtreecommitdiff
path: root/app/models/project_services/irker_service.rb
blob: 04c714bfaad4fddee9d29a4fd0ecb4591a26e0af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# == Schema Information
#
# Table name: services
#
#  id                    :integer          not null, primary key
#  type                  :string(255)
#  title                 :string(255)
#  project_id            :integer
#  created_at            :datetime
#  updated_at            :datetime
#  active                :boolean          default(FALSE), not null
#  properties            :text
#  template              :boolean          default(FALSE)
#  push_events           :boolean          default(TRUE)
#  issues_events         :boolean          default(TRUE)
#  merge_requests_events :boolean          default(TRUE)
#  tag_push_events       :boolean          default(TRUE)
#  note_events           :boolean          default(TRUE), not null
#  build_events          :boolean          default(FALSE), not null
#

require 'uri'

class IrkerService < Service
  prop_accessor :server_host, :server_port, :default_irc_uri
  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

  def supported_events
    %w(push)
  end

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

    IrkerWorker.perform_async(project_id, channels,
                              colorize_messages, data, settings)
  end

  def settings
    { server_host: server_host.present? ? server_host : 'localhost',
      server_port: server_port.present? ? server_port : 6659
    }
  end

  def fields
    [
      { 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/' },
      { type: 'textarea', name: 'recipients',
        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, ' \
        ' 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.'  },
      { type: 'checkbox', name: 'colorize_messages' },
    ]
  end

  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.'
  end

  private

  def get_channels
    return true unless :activated?
    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|
      format_channel(recipient)
    end
    channels.reject! &:nil?
  end

  def format_channel(recipient)
    uri = nil

    # Try to parse the chan as a full URI
    begin
      uri = consider_uri(URI.parse(recipient))
    rescue URI::InvalidURIError
    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}")
      end
    end

    uri
  end

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

    # Authorize both irc://domain.com/#chan and irc://domain.com/chan
    if uri.is_a?(URI) && uri.scheme[/^ircs?\z/] && !uri.path.nil?
      # Do not authorize irc://domain.com/
      if uri.fragment.nil? && uri.path.length > 1
        uri.to_s
      else
        # Authorize irc://domain.com/smthg#chan
        # The irker daemon will deal with it by concatenating smthg and
        # chan, thus sending messages on #smthgchan
        uri.to_s
      end
    end
  end
end