module EhReport
EhReport — IRC card formatting for ethical-hacking CTF challenge lists. Public: format_line, build_list, build_detail, build_not_found, build_error, build_usage, format Depends: EhSearch, IrcReply, TextUtil Tests: test/lib/eh_report_test.rb
Constants
- CTFTIME_URL
- DESCRIPTION_LIMIT
- DETAIL_TITLE
- FOOTER_SOURCE
- TAG
- TITLE
Public Instance Methods
Source
# File lib/eh_report.rb, line 73 def build_detail(result) event = result[:item] index = result[:index] listed = result[:listed] IrcReply.card( tag: TAG, title: "##{index} \u00b7 #{event[:title]}", body_lines: detail_body_lines(event), footer_parts: [FOOTER_SOURCE, "##{index} of #{listed}"] ) end
Source
# File lib/eh_report.rb, line 69 def build_error(message) IrcReply.error(tag: TAG, message: message) end
Source
# File lib/eh_report.rb, line 31 def build_list(result) items = Array(result[:items]) if items.empty? return IrcReply.card( tag: TAG, title: TITLE, body_lines: [ "No open challenges with prizes found.", "Browse: #{CTFTIME_URL}" ], footer_parts: [FOOTER_SOURCE, "sorted by prize"] ) end body_lines = items.each_with_index.map { |event, index| format_line(index + 1, event) } footer_parts = [ FOOTER_SOURCE, "#{TextUtil.format_count(result[:listed])} listed", "sorted by prize" ] IrcReply.card( tag: TAG, title: TITLE, body_lines: body_lines, footer_parts: footer_parts ) end
Source
# File lib/eh_report.rb, line 86 def build_not_found(index, listed) IrcReply.card( tag: TAG, title: DETAIL_TITLE, body_lines: [ "Challenge ##{index} not found.", "Pick a number from 1 to #{listed} after !eh list." ], footer_parts: [FOOTER_SOURCE] ) end
Source
# File lib/eh_report.rb, line 60 def build_usage IrcReply.card( tag: TAG, title: TITLE, body_lines: [EhSearch.usage_message], footer_parts: [FOOTER_SOURCE] ) end
Source
# File lib/eh_report.rb, line 98 def detail_body_lines(event) lines = [] closing = event[:finish_at].utc.strftime("%d %b %Y UTC") meta = [ event[:format].to_s.strip, event[:prize_label].to_s.strip, "closes #{closing}" ].reject(&:empty?) lines << meta.join(" \u00b7 ") unless meta.empty? start_line = detail_start_line(event) lines << start_line unless start_line.empty? prizes_line = detail_prizes_line(event) lines << prizes_line unless prizes_line.empty? description = event[:description].to_s.strip unless description.empty? lines << TextUtil.truncate(description, DESCRIPTION_LIMIT) end url_lines = detail_url_lines(event) lines.concat(url_lines) lines end
Source
# File lib/eh_report.rb, line 135 def detail_prizes_line(event) prizes_text = event[:prizes_text].to_s.strip prize_label = event[:prize_label].to_s.strip return "" if prizes_text.empty? return "" if prize_label.casecmp?(prizes_text) return "" if prizes_text.casecmp?(prize_label) prizes_text end
Source
# File lib/eh_report.rb, line 125 def detail_start_line(event) start_at = event[:start_at] host = event[:host].to_s.strip host = event[:url].to_s.strip if host.empty? parts = [] parts << "Starts #{start_at.utc.strftime('%d %b %Y UTC')}" if start_at parts << host unless host.empty? parts.join(" \u00b7 ") end
Source
# File lib/eh_report.rb, line 145 def detail_url_lines(event) lines = [] url = event[:url].to_s.strip ctftime_url = event[:ctftime_url].to_s.strip lines << url unless url.empty? lines << ctftime_url if !ctftime_url.empty? && ctftime_url != url lines end
Source
# File lib/eh_report.rb, line 154 def format(result) case result[:error] when :usage build_usage when :fetch build_error("CTFtime unavailable right now. Try again later or browse #{CTFTIME_URL}") when :not_found build_not_found(result[:index], result[:listed]) when nil result[:item] ? build_detail(result) : build_list(result) else build_list(result) end end
Source
# File lib/eh_report.rb, line 24 def format_line(index, event) closing = event[:finish_at].utc.strftime("%d %b %Y UTC") host = event[:host].to_s.strip host = event[:url].to_s.strip if host.empty? "#{index}. #{event[:title]} \u2014 #{event[:prize_label]} \u2014 closes #{closing} \u2014 #{host}" end