Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

readme and some features

  • Loading branch information...
commit e961f5dba2893f05f21f533590e0758ac9a74c96 1 parent 741ceb6
@akwiatkowski authored
Showing with 84 additions and 7 deletions.
  1. +32 −1 README.rdoc
  2. +52 −6 lib/xmpp_chat_bot/base.rb
View
33 README.rdoc
@@ -2,4 +2,35 @@
Simple xmpp bot for MUC.
-Docs coming soon :)
+Docs coming soon :)
+
+= How to use
+
+= Setup
+
+Be a nice person and create options.yml file in root repo directory. Put there something like this:
+
+ ---
+ :login: sweet_login
+ :server: 'jabbim.cz'
+ :pass: 'admin1'
+ :bot_name: "supadupatron"
+ :room: "suparoom()@conf.netlab.cz"
+
+= Running
+
+Execute
+
+ ruby lib/start_xmpp_chat_bot.rb
+
+= What it does?
+
+* ping! google.pl - ping host
+* <bot name>: df - disk usage
+* <bot name>: ps - 5 most cpu intensive procs
+* <bot name>: start_time - reverse of uptime
+* <bot name>: stats - msg and bytes stats by everyony on chat
+
+= And...
+
+Type url and bot will give you title of this page. Type url to image and you will get size of this image.
View
58 lib/xmpp_chat_bot/base.rb
@@ -11,6 +11,8 @@
require 'iconv'
+require 'yaml'
+
module XmppChatBot
class Base
@@ -25,8 +27,13 @@ def initialize(_options)
@url_regexp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/
@command_regexp = /#{@options[:bot_name]}:\s*(\w+)/
+ @ping_regexp = /ping! ([^\s]+)/
@iconv = ic_ignore = Iconv.new('UTF-8//IGNORE', 'UTF-8')
+
+ @start_time = Time.now
+ @stats_msg = Hash.new
+ @stats_msg_length = Hash.new
end
@@ -59,7 +66,9 @@ def start_bot
register_url_spy
register_simple_commands
-
+ register_ping_command
+
+ register_msg_stats
EM.run { @bc.run }
@@ -84,29 +93,60 @@ def register_url_spy
end
end
+ # register ping
+ def register_ping_command
+ @bc.register_handler :message, :groupchat?, :body => @ping_regexp do |m|
+ if m.body.to_s =~ @ping_regexp
+ url = $1.to_s.strip
+ res = `ping -c 3 #{url}`
+
+ n = Blather::Stanza::Message.new
+ n.to = @options[:room]
+ n.type = :groupchat
+ n.body = "ping to #{url} result:\n#{res}"
+ @bc.write n
+
+ end
+ end
+ end
+
# register simple commands
def register_simple_commands
@bc.register_handler :message, :groupchat?, :body => @command_regexp do |m|
if m.body.to_s =~ @command_regexp
command = $1.to_s.strip
+ short_nick = m.from.to_s[/([^\/]*)$/]
puts "command '#{command}'"
n = Blather::Stanza::Message.new
n.to = @options[:room]
n.type = :groupchat
- n.body = "command #{command}\n" + process_command(command)
+ n.body = "command #{command}\n" + process_command(command, short_nick)
@bc.write n
end
end
end
- def process_command(command)
+ def register_msg_stats
+ @bc.register_handler :message, :groupchat? do |m|
+ short_nick = m.from.to_s[/([^\/]*)$/]
+ @stats_msg[short_nick] = @stats_msg[short_nick].to_i + 1
+ @stats_msg_length[short_nick] = @stats_msg[short_nick].to_i + m.body.to_s.size
+ end
+
+ end
+
+ def process_command(command, from = 'nobody')
return case command.to_s
when 'df' then
`df -hl -x tmpfs`.to_s
when 'ps' then
- `ps -e -o pcpu,ruser,args|sort -nr|grep -v %CPU|head -10`
+ `ps -e -o pcpu,ruser,args|sort -nr|grep -v %CPU|head -5`
+ when 'start_time' then
+ @start_time.to_s
+ when 'stats', 'stats2' then
+ stats_to_s
else
'command not available'
end
@@ -151,8 +191,6 @@ def process_url(url)
puts e.inspect
end
- puts desc
-
return {
:title => title,
:size => size,
@@ -170,5 +208,13 @@ def get_uri_size(url)
return req.request_head(path)['Content-Length'].to_i
end
+ def stats_to_s
+ s = ""
+ @stats_msg.keys.sort.each do |k|
+ s += "#{k} - #{@stats_msg[k]}/#{@stats_msg_length[k]}\n"
+ end
+ return s
+ end
+
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.