Class: Lita::Handlers::Hcadmin

Inherits:
Handler
  • Object
show all
Defined in:
lib/lita/handlers/hcadmin.rb

Instance Method Summary collapse

Instance Method Details

#fetch_data(api_v, object_type, object_id) ⇒ Object



243
244
245
246
247
248
249
250
251
252
# File 'lib/lita/handlers/hcadmin.rb', line 243

def fetch_data(api_v, object_type, object_id)
  object_id = url_encode_name(object_id) if object_id.class == String
  return 'Wrong API version provided' unless api_v == 'v2'
  return "Wrong HipChat object type (#{object_type}). Only accepts 'room' or 'user'" unless %w('room', 'user').any? { |s| s.include?(object_type.to_s) }
  return 'Please provide the object id of the room or user you want information for' if object_id.nil?
  url = "https://api.hipchat.com/v2/#{object_type}/#{object_id}?auth_token=#{v2_admin_token}&format=json"
  proxy = http_proxy || nil
  json = get url, proxy
  json
end

#hipchat_sponsor(response) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/lita/handlers/hcadmin.rb', line 74

def hipchat_sponsor(response)
  response.args.each do |r|
    next if 'sponsor'.include?(r)
    if email_domain_name.any? { |w| r[w] } # rubocop:disable Style/GuardClause
      sponsored_user = fetch_user_v2(r.strip)
      sponsored_user_json = parse(sponsored_user[:body], 'sponsor')
      if sponsored_user_json['id'] # rubocop:disable Style/GuardClause
        return response.reply "The email address of #{r} already has an account on #{} of #{sponsored_user_json['id']}"
      else
        response.reply "The email address of #{r} was not found. Generating request to #{email_dist_list}"
        response.reply "Admins have been notified of your request to add #{r}, @#{response.user.name}"
        send_mail(r, email_dist_list, response.user.name, smtp_server)
        return response.reply 'Message to admins sent'
      end
    else
      return response.reply "The email address has to be one of: #{email_domain_name.inspect}"
    end
  end
  response.reply 'something'
end

#identify_hipchat_admins(response) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/lita/handlers/hcadmin.rb', line 150

def identify_hipchat_admins(response)
  response.reply 'Admin search takes about a minute ...'
  @all_admins = []
  batch_count = user_count_api_iterations

  time = Benchmark.measure do
    (0..batch_count + 1).each do |i|
      i = i * 1000 - 1 unless i == 0 # fun with math!
      response.reply "Checking users ( through #{i})"
      parse_users = fetch_all_users_v2(i)
      return response.reply "Admin list: I ran into an HTTP error ( HTTP code: #{parse_users[:status]} )" if parse_users[:status] != 200
      all_users = parse(parse_users[:body], 'sponsor')
      all_users['items'].each do |u|
        admin_list(u['mention_name']) if u['is_group_admin'] == true
      end
    end
  end
  response.reply "Admin list: #{@all_admins.join(', ')} ( #{time.real.to_i} s. to respond )"
end

#identify_room_change_admins(response) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/lita/handlers/hcadmin.rb', line 113

def identify_room_change_admins(response)
  response.reply 'Users who can change room ownership:'
  time = Benchmark.measure do
    if room_change_admins.first.to_s.empty? # rubocop:disable Style/GuardClause
      return response.reply 'Room change admins: None found from config'
    else
      room_change_admins.each do |a|
        response.reply a.to_s
      end
    end
  end
  response.reply "room change admins: #{room_change_admins.length} found ( #{time.real.to_i} s. to respond )"
end

#identify_room_count(response) ⇒ Object



141
142
143
144
145
146
147
148
# File 'lib/lita/handlers/hcadmin.rb', line 141

def identify_room_count(response)
  room_count = ''
  time = Benchmark.measure do
    room_count = fetch_rooms
    room_count = parse(room_count[:body], 'rooms')
  end
  response.reply "Rooms in hipchat: #{room_count.length} ( #{time.real.to_i} s. to respond )"
end

#identify_room_owner(response) ⇒ Object



170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/lita/handlers/hcadmin.rb', line 170

def identify_room_owner(response)
  @room_name = nil
  @room_parse = nil
  response.matches.each do |match|
    @room_name = match[0]
  end
  response.reply 'Getting rooms ... one moment'
  room_info = fetch_data('v2', 'room', @room_name)
  @room_parse = JSON.parse(room_info[:body])
  if @room_parse.key?('error')
    return response.reply @room_parse['error']['message']
  end
  response.reply "The owner of #{@room_name} is #{@room_parse['owner']['name']}"
end

#identify_status(response) ⇒ Object



95
96
97
98
99
100
101
# File 'lib/lita/handlers/hcadmin.rb', line 95

def identify_status(response)
  status = ''
  time = Benchmark.measure do
    status = fetch_status
  end
  response.reply "Status of hipchat: Code: #{status[:status]} => #{status[:body]} ( #{time.real.to_i} s. to respond )"
end

#identify_user_change_admins(response) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/lita/handlers/hcadmin.rb', line 127

def identify_user_change_admins(response)
  response.reply 'Users who can change user details:'
  time = Benchmark.measure do
    if user_change_admins.first.to_s.empty? # rubocop:disable Style/GuardClause
      return response.reply 'User change admins: None found from config'
    else
      user_change_admins.each do |a|
        response.reply a.to_s
      end
    end
  end
  response.reply "user change admins: #{user_change_admins.length} found ( #{time.real.to_i} s. to respond )"
end

#identify_user_count(response) ⇒ Object



103
104
105
106
107
108
109
110
111
# File 'lib/lita/handlers/hcadmin.rb', line 103

def identify_user_count(response)
  user_count = ''
  time = Benchmark.measure do
    user_count = fetch_stats
    user_count = parse(user_count[:body], 'users')
    # user_count = parse(fetch_users, 'users')
  end
  response.reply "Users in hipchat: #{user_count} ( #{time.real.to_i} s. to respond )"
end

#parse(json, type) ⇒ Object



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/lita/handlers/hcadmin.rb', line 185

def parse(json, type)
  json = JSON.parse(json)
  case type
  when 'rooms'
    json = json['rooms']
  when 'user'
    json = json['user']
  when 'items'
    json = json['items']
  when 'users'
    json = json['users']
  when 'status'
    json = json['body']
  when 'sponsor'
    json = json
  end
  json
end

#room_owner_change(response) ⇒ Object

Length check => owner: 1, change: 2, member_name: 3, room_name: 4, room_name can be multiple words!



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/lita/handlers/hcadmin.rb', line 204

def room_owner_change(response) # Length check => owner: 1, change: 2, member_name: 3, room_name: 4, room_name can be multiple words!
  return response.reply 'You are not represented in the room owner change admin group' unless user_in_group(response.user.mention_name, room_change_admins) == true
  return response.reply "Wrong number of arguments; expected format of owner_name & room_name received arguments: #{response.args.values_at(2..response.args.length - 1)}" unless response.args.length > 3
  user_details = user_data('@' + response.args[2].to_s)
  return response.reply "Uh-oh, 404 returned! The user #{response.args[2]} not found" if user_details['id'].nil?
  room_details = room_data(response.args[3..-1].join(' ').to_s)
  return response.reply "Uh-oh, 404 returned! The room #{response.args[3..-1].join(' ')} not found" if room_details['id'].nil?
  room_update = {}
  room_update['name'] = room_details['name']
  room_update['privacy'] = room_details['privacy']
  room_update['is_archived'] = room_details['is_archived']
  room_update['is_guest_accessible'] = room_details['is_guest_accessible']
  room_update['topic'] = room_details['topic']
  room_update['owner'] = { id: user_details['id'].to_s }
  unless JSON.parse(room_update.to_json) #
    return response.reply "Unfortunately, I was not able to present valid date to the API: #{room_update}"
  end

  update_room = http_put("https://api.hipchat.com/v2/room/#{room_details['id']}?auth_token=#{v2_admin_token}&format=json", room_update)
  response.reply "Room update request sent. HTTP response code: #{update_room.status}"
end

#room_unarchive(response) ⇒ Object



226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/lita/handlers/hcadmin.rb', line 226

def room_unarchive(response)
  return response.reply 'You are not represented in the room owner change admin group' unless user_in_group(response.user.mention_name, room_change_admins) == true
  return response.reply 'Wrong number of arguments; expected format of room_name received arguments' unless response.args.length > 1
  room_details = room_data(response.args[1..-1].join(' ').to_s)
  return response.reply "Uh-oh, 404 returned! The room #{response.args[1..-1].join(' ')} not found" if room_details['id'].nil?
  room_owner = room_details['owner']['id']
  room_update = {}
  room_update['name'] = room_details['name']
  room_update['privacy'] = room_details['privacy']
  room_update['is_archived'] = false
  room_update['is_guest_accessible'] = room_details['is_guest_accessible']
  room_update['topic'] = room_details['topic']
  room_update['owner'] = { id: room_owner.to_s }
  update_room = http_put("https://api.hipchat.com/v2/room/#{room_details['id']}?auth_token=#{v2_admin_token}&format=json", room_update)
  response.reply "Room update request sent. HTTP response code: #{update_room.status}"
end

#user_activate(response) ⇒ Object



254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'lib/lita/handlers/hcadmin.rb', line 254

def user_activate(response)
  return response.reply 'You are not represented in the USER admin group' unless user_in_group(response.user.mention_name, user_change_admins) == true
  user_data = (response.matches)
  user_email = user_data[0]
  user_name = username_from_email(user_email)
  return response.reply 'First value needs to be a valid email address' unless valid_email?(user_email) == 0
  unless email_domain_name.include? domain_from_email(user_email)
    return response.reply "That email address is not in the approved list to activate. Approved email accounts: #{email_domain_name}"
  end
  user_data_check = user_exists?(user_email)
  return response.reply 'That email is already in use but in a different HipChat account.' if user_data_check[:status] == 409
  return response.reply 'That email is already an active user in this HipChat account' if user_data_check[:status] == 200
  return response.reply "I did not understand the status code from HipChat: #{user_data_check[:status]}" unless user_data_check[:status] == 404
  user_response = http_post("https://api.hipchat.com/v2/user?auth_token=#{v2_admin_token}&format=json", name: user_name, email: user_email)
  return response.reply "Error while trying to add user: #{user_response.status} -- #{user_response.body}" unless user_response.status == 201
  response.reply "Added user with status code #{user_response.status}"
end

#user_invite(response) ⇒ Object



272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# File 'lib/lita/handlers/hcadmin.rb', line 272

def user_invite(response)
  return response.reply 'You are not represented in the USER admin group' unless user_in_group(response.user.mention_name, user_change_admins) == true
  user_data = (response.matches)
  user_email = user_data[0]
  user_name = username_from_email(user_email)
  return response.reply 'First value needs to be a valid email address' unless valid_email?(user_email) == 0
  unless email_domain_name.include? domain_from_email(user_email)
    return response.reply "That email address is not in the approved list to activate. Approved email accounts: #{email_domain_name}"
  end
  user_data_check = user_exists?(user_email)
  return response.reply 'That email is already in use but in a different HipChat account.' if user_data_check[:status] == 409
  return response.reply 'That email is already an active user in this HipChat account' if user_data_check[:status] == 200
  return response.reply "I did not understand the status code from HipChat: #{user_data_check[:status]}" unless user_data_check[:status] == 404
  invite_response = http_post("https://api.hipchat.com/v2/invite/user?auth_token=#{v2_admin_token}&format=json", name: user_name, email: user_email)
  response.reply "Invite to user sent with status code #{invite_response.status}"
end