Skip to content

Instantly share code, notes, and snippets.

@HelixSpiral
Last active December 27, 2015 00:59
Show Gist options
  • Save HelixSpiral/7241495 to your computer and use it in GitHub Desktop.
Save HelixSpiral/7241495 to your computer and use it in GitHub Desktop.
Generate random passwords from inside Weechat.
# Copyright (c) 2013 Shawn Smith <ShawnSmith0828@gmail.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
def weechat_init
Weechat.register("passgen",
"Shawn Smith",
"0.1",
"GPL3",
"Generate random passwords.",
"",
"")
Weechat.hook_command("passgen",
"Generate random passwords.",
"[length]",
"length: Specifies the length of the password to generate. Defaults to 32",
"",
"passgen_command_cb",
"")
return Weechat::WEECHAT_RC_OK
end
def passgen_command_cb(data, buffer, args)
# Print to the user
Weechat.print(buffer, "Generated Password: #{generate_pass(args)}")
return Weechat::WEECHAT_RC_OK
end
# Generate our password
def generate_pass(length)
# Default to 32 is length < 1
length = 32 if length.to_i < 1
# This limits the password to certain characters.
# Some characters may throw errors, like commas and semicolons. So we filter out the really weird ones.
chars = (36..43).to_a.pack('U*') + (48..57).to_a.pack('U*') + (65..95).to_a.pack('U*') + (97..122).to_a.pack('U*')
password = ''
length.to_i.times { password << chars[rand(chars.size)] }
return password
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment