Created
March 17, 2014 15:07
-
-
Save Ninjex/9600989 to your computer and use it in GitHub Desktop.
Query a MySQL database from within Weechat. Ruby plugin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/ruby | |
| # Required packages / gems | |
| # libmysql-ruby libmysqlclient-dev | |
| # gem mysql2 | |
| require 'mysql2' | |
| @options = { | |
| "public" => "false", # MySQL query result publicly visible | |
| "host" => "localhost", # MySQL host | |
| "user" => "root", # MySQL username | |
| "pass" => "", # MySQL password | |
| "database" => "", # MySQL Database | |
| } | |
| @flags = { | |
| "vars" => "List all variables and their values: /sql vars", | |
| "flags" => "Show the list of all flags: /sql flags", | |
| "var_info" => "Displays the description for every definable variable.", | |
| } | |
| @variable_info = { | |
| "public" => "If set to true, it will send qeury results for everyone to view. If set to false, only you will see the results.", | |
| "host" => "MySQL hostname to connect to. The default value is: localhost", | |
| "user" => "MySQL username. Username associated with the MySQL host. The default value is: root", | |
| "pass" => "Mysql password. Password associated with the user. The default value is not set.", | |
| "database" => "Mysql database. Database associated with the host. The default value is not set.", | |
| } | |
| def weechat_init | |
| help = [ | |
| '/sql variable: value #=> Set a variable value.', | |
| '/sql variable? #=> Display variable value.', | |
| '/sql variable! #=> Clear variable value.', | |
| '/sql variable~ #=> Decription for specified variable.', | |
| '/sql query #=> Execute a MySQL command.', | |
| '/sql flags #=> List all available flags.', | |
| '/sql var_info #=> Description for every definable variable.', | |
| ] | |
| Weechat.register( | |
| 'sql', # Name : Internal name of script | |
| 'Ninjex', # Author: Author name | |
| '1.0', # Version: Script version | |
| 'GPL3', # License: Sript lisence | |
| # Description: Short script description | |
| 'Execute MySQL command from IRC. For more information: /help sql', | |
| '', # shutdown_function: Name of the function called when the script is unloaded | |
| '', # Charset: String, script charset (UTF-8) if blank | |
| ) | |
| Weechat.hook_command('sql', help.join("\r\n"), '', '', '', 'sql', '') | |
| return Weechat::WEECHAT_RC_OK | |
| end | |
| # Depending on the 'public' option, this will pass a message to the proper buffer | |
| def weechat_message(buffer, message) | |
| if @options['public'] == "false" | |
| Weechat.print(buffer, message) | |
| elsif @options['public'] == "true" | |
| Weechat.command(buffer, message) | |
| else | |
| Weechat.print(buffer, "The public option is not set correctly. Please correct the public option with either of the following commands:") | |
| Weechat.print(buffer, "/sql public: false") | |
| Weechat.print(buffer, "/sql public: true") | |
| end | |
| end | |
| # This method will set option values if found in the sql query, i.e: /sql host = 127.0.0.1 will set host to 127.0.0.1 | |
| def set_opt(values) | |
| handle = values.split(':').map(&:strip) # For example: user = ninjex becomes ['user', 'ninjex'] | |
| option = handle[0] # The option | |
| value = handle[1] # option value | |
| if value.nil? | |
| Weechat.print(Weechat.current_buffer, "Error, value for option: #{option} was nil. If you would like to clear the variable use the use: /sql #{option}!") | |
| elsif option == 'public' && (value != "true" && value != "false") | |
| Weechat.print(Weechat.current_buffer, "Invalid value: #{value} for option public. Use values: true or false.") | |
| return Weechat::WEECHAT_RC_OK | |
| elsif option == 'pass' | |
| Weechat.print(Weechat.current_buffer, "Set option: #{option} to value: #{'*' * value.length}") | |
| @options[option] = value | |
| return Weechat::WEECHAT_RC_OK | |
| else | |
| Weechat.print(Weechat.current_buffer, "Set option: #{option} to value: #{value}") | |
| @options[option] = value | |
| return Weechat::WEECHAT_RC_OK | |
| end | |
| end | |
| def sql(data, buffer, args) | |
| buffer = Weechat.current_buffer | |
| explode = args.split(' ') | |
| flag = explode[0] | |
| opt = flag[0..-2] # 'host?' #=> 'host' | |
| modifier = flag[-1..-1] # 'host?' #=> '?', : #=> set variable value, ? #=> Show variable value | |
| if @options.include?(opt) && explode.size <= 2 | |
| if modifier == ':' | |
| set_opt(args) | |
| elsif modifier == '?' | |
| Weechat.print(buffer, "#{opt} #=> #{@options[opt]}") | |
| elsif modifier == '!' | |
| @options[opt] = '' | |
| Weechat.print(buffer, "Variable: #{opt} has been cleared.") | |
| elsif modifier == '~' | |
| Weechat.print(buffer, "#{opt} #=> #{@variable_info[opt]}") | |
| end | |
| elsif flag == 'vars' | |
| @options.each do |option, value| | |
| if option == 'pass' | |
| Weechat.print(buffer, "#{option} = #{'*' * value.length}") | |
| else | |
| Weechat.print(buffer, "#{option} = #{value}") | |
| end | |
| end | |
| return Weechat::WEECHAT_RC_OK | |
| elsif flag == 'flags' | |
| @flags.each do |flag, desc| | |
| Weechat.print(buffer, "#{flag}: #{desc}") | |
| end | |
| return Weechat::WEECHAT_RC_OK | |
| elsif flag == 'var_info' | |
| @variable_info.each do |var, desc| | |
| Weechat.print(buffer, "#{var}: #{desc}") | |
| end | |
| return Weechat::WEECHAT_RC_OK | |
| else | |
| begin | |
| client = Mysql2::Client.new( | |
| :host => @options['host'], | |
| :username => @options['user'], | |
| :password => @options['pass'], | |
| :database => @options['database'] | |
| ) | |
| result, final_result = [], [] | |
| weechat_message(buffer, "Sending query: #{args}") | |
| sql_res = client.query(args) | |
| client.close # Let's not wait on a garbage collector | |
| sql_res.each(:as => :array).each do |r| | |
| sub = r.to_s.gsub(/[\"\[\]]/, '').strip # Results are returned in array format, strip to plain string: ['hi'] => hi | |
| if sub != '' | |
| result << sub | |
| end | |
| end | |
| hash = Hash.new(0) | |
| result.map{ |res| hash[res] += 1 } | |
| hash.map{ |res, count| final_result << "[#{count}]: ['#{res}']" } # A result such as; [4]: ['root'], [1]: ['john'] => four results of 'root' and one result of 'john' | |
| weechat_message(buffer, final_result.join(', ')) | |
| return Weechat::WEECHAT_RC_OK | |
| rescue Mysql2::Error | |
| Weechat.print(buffer, "MySQL services not started on machine: #{@options['host']}, or an error occured, please start the service and try again!") | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment