class Raspberg
Public Class Methods
Source
# File plugins/raspberg.rb, line 30 def self.command_pattern /raspberg(?:\s+(.+))?$/i end
Source
# File plugins/raspberg.rb, line 166 def self.dispatch(text, rand: RaspbergPassword.method(:default_rand), store: RabbotDb, uptime_reader: RaspbergUptime.method(:default_uptime_reader), nick: nil, at: Time.now) parsed = parse_subcommand(text) return { error: parsed[:error] } if parsed[:error] case parsed[:action] when :help { reply: help_reply } when :password result = generate_password(parsed[:options_text], rand: rand) return { error: result[:error] } if result[:error] { reply: format_password_reply( password: result[:password], passwords: result[:passwords], options: result[:options] ) } when :serial dispatch_serial(name: parsed[:name]) when :serial_verify dispatch_serial_verify(name: parsed[:name], serial: parsed[:serial]) when :filenamer dispatch_filenamer(args_text: parsed[:args_text]) when :uptime dispatch_uptime(mode: parsed[:mode], store: store, uptime_reader: uptime_reader, at: at) when :bootlog dispatch_bootlog(mode: parsed[:mode], limit: parsed[:limit], store: store) when :address dispatch_address(mode: parsed[:mode], nick: nick, store: store, **parsed.slice(:letter, :name, :text)) end end
Source
# File plugins/raspberg.rb, line 266 def self.dispatch_address(mode:, nick:, store:, letter: nil, name: nil, text: nil) return { error: "Nick required for address book commands." } if nick.to_s.strip.empty? result = case mode when :list RaspbergAddress.list(nick: nick, letter: letter, store: store) when :show RaspbergAddress.show(nick: nick, name: name, store: store) when :add RaspbergAddress.add(nick: nick, text: text, store: store) when :del RaspbergAddress.del(nick: nick, name: name, store: store) end return { error: result[:error] } if result[:error] reply = case mode when :list RaspbergAddressReport.format_list(contacts: result[:contacts]) when :show RaspbergAddressReport.format_contact(result[:contact]) when :add RaspbergAddressReport.format_added(result[:contact]) when :del RaspbergAddressReport.format_deleted(result[:contact]) end { reply: reply } end
Source
# File plugins/raspberg.rb, line 254 def self.dispatch_bootlog(mode:, store:, limit: nil) case mode when :list cap = limit.nil? || limit.to_s.strip.empty? ? nil : limit.to_i entries = RaspbergBootlog.list(store: store, limit: cap) { reply: RaspbergBootlogReport.format_list(entries: entries) } when :clear RaspbergBootlog.clear(store: store) { reply: RaspbergBootlogReport.format_cleared } end end
Source
# File plugins/raspberg.rb, line 214 def self.dispatch_filenamer(args_text:) parsed = RaspbergFilenamer.parse(args_text) return { error: parsed[:error] } if parsed[:error] preview = RaspbergFilenamer.preview( prefix: parsed[:prefix], start: parsed[:start], ext: parsed[:ext], sources: parsed[:sources] ) return { error: preview[:error] } if preview[:error] { reply: RaspbergSerialReport.format_filenamer( names: preview[:names], prefix: preview[:prefix], start: preview[:start], ext: preview[:ext] ) } end
Source
# File plugins/raspberg.rb, line 200 def self.dispatch_serial(name:) result = RaspbergSerial.generate_result(name) return { error: result[:error] } if result[:error] { reply: RaspbergSerialReport.format_serial(name: name, serial: result[:serial]) } end
Source
# File plugins/raspberg.rb, line 209 def self.dispatch_serial_verify(name:, serial:) valid = RaspbergSerial.verify(name, serial) { reply: RaspbergSerialReport.format_verify(name: name, valid: valid) } end
Source
# File plugins/raspberg.rb, line 236 def self.dispatch_uptime(mode:, store:, uptime_reader:, at:) begin uptime_sec = RaspbergUptime.read_seconds(uptime_reader: uptime_reader) rescue StandardError return { error: "Could not read host uptime." } end tick = RaspbergUptimeStore.tick(uptime_sec: uptime_sec, store: store, at: at) current_minutes = RaspbergUptime.minutes_from_seconds(uptime_sec) reply = if mode == :stats RaspbergUptimeReport.format_stats(stats: tick[:stats], current_minutes: current_minutes) else RaspbergUptimeReport.format_current(minutes: current_minutes) end { reply: reply } end
Source
# File plugins/raspberg.rb, line 155 def self.format_password_reply(password:, options:, passwords: nil) RaspbergPasswordReport.format_password( passwords: passwords || [password], options: options ) end
Source
# File plugins/raspberg.rb, line 137 def self.generate_password(options_text, rand: RaspbergPassword.method(:default_rand)) opts = RaspbergPasswordOptions.parse(options_text) err = RaspbergPasswordOptions.validate(**opts.slice(:length, :lower, :upper, :numbers, :extra, :vowl, :custom, :custom_enabled)) return { error: err } if err if opts[:count].to_i < 1 || opts[:count].to_i > RaspbergPasswordOptions::MAX_COUNT return { error: "count must be 1..#{RaspbergPasswordOptions::MAX_COUNT}." } end passwords = RaspbergPassword.generate_many(opts, rand: rand) { password: passwords.first, passwords: passwords, options: opts } end
Source
# File plugins/raspberg.rb, line 162 def self.help_reply RaspbergReport.format_help end
Source
# File plugins/raspberg.rb, line 108 def self.parse_address(rest) text = rest.to_s.strip return { error: "Usage: !raspberg address list|show|add|del ..." } if text.empty? parts = text.split(/\s+/, 2) sub = parts[0].downcase tail = parts[1].to_s case sub when "list" letter = tail.strip.empty? ? nil : tail.strip { action: :address, mode: :list, letter: letter } when "show" return { error: "Usage: !raspberg address show <name>" } if tail.strip.empty? { action: :address, mode: :show, name: tail.strip } when "add" return { error: "Usage: !raspberg address add <name> | <phone> | <address>" } if tail.strip.empty? { action: :address, mode: :add, text: tail } when "del", "delete", "rm" return { error: "Usage: !raspberg address del <name>" } if tail.strip.empty? { action: :address, mode: :del, name: tail.strip } else { error: "Usage: !raspberg address list|show|add|del ..." } end end
Source
# File plugins/raspberg.rb, line 91 def self.parse_bootlog(rest) text = rest.to_s.strip.downcase case text when "", "list" { action: :bootlog, mode: :list } when "clear" { action: :bootlog, mode: :clear } else if text.start_with?("list ") limit = text.split(/\s+/, 2)[1] { action: :bootlog, mode: :list, limit: limit } else { error: "Usage: !raspberg bootlog [list [n]] | clear" } end end end
Source
# File plugins/raspberg.rb, line 64 def self.parse_serial(rest) text = rest.to_s.strip return { error: "Usage: !raspberg serial <name>" } if text.empty? if text.downcase.start_with?("verify ") verify_rest = text.split(/\s+/, 2)[1].to_s name, serial = verify_rest.split(/\s+/, 2) return { error: "Usage: !raspberg serial verify <name> <serial>" } if name.to_s.strip.empty? || serial.to_s.strip.empty? { action: :serial_verify, name: name, serial: serial } else { action: :serial, name: text } end end
Source
# File plugins/raspberg.rb, line 36 def self.parse_subcommand(text) args = text.to_s.strip return { action: :help } if args.empty? parts = args.split(/\s+/, 2) head = parts[0].downcase rest = parts[1].to_s case head when "help" { action: :help } when "password", "pass", "pw" { action: :password, options_text: rest } when "serial", "keygen" parse_serial(rest) when "filenamer", "rename" { action: :filenamer, args_text: rest } when "uptime" parse_uptime(rest) when "bootlog" parse_bootlog(rest) when "address", "addr" parse_address(rest) else { error: "Unknown subcommand. Try !raspberg help" } end end
Source
# File plugins/raspberg.rb, line 79 def self.parse_uptime(rest) mode = rest.to_s.strip.downcase case mode when "", "current" { action: :uptime, mode: :current } when "stats", "detail", "details" { action: :uptime, mode: :stats } else { error: "Usage: !raspberg uptime [stats]" } end end
Public Instance Methods
Source
# File plugins/raspberg.rb, line 298 def execute(m, args = nil) result = self.class.dispatch( args, nick: m.user.nick, store: RabbotDb ) if result[:error] flood_safe_reply(m, RaspbergReport.format_error(result[:error]), with_progress: false) return end themed_flood_safe_reply(m, result[:reply]) end
Source
# File plugins/raspberg.rb, line 294 def generate_password(options_text, **options) self.class.generate_password(options_text, **options) end