Permalink
Browse files

gemified and created minictl, minigen

  • Loading branch information...
1 parent 0a332a8 commit bf23da0091b657d4094c88247b130b54c4e89159 @purzelrakete purzelrakete committed Mar 7, 2009
View
4 CREDITS.markdown
@@ -1,2 +1,2 @@
-- thanks to yakischloba and careo on #eventmachine
-- thanks to masuidrive (http://blog.masuidrive.jp/) for a helpful gist which i can't find anymore. it demonstrated irc+eventmachine.
+- thx 2 yakischloba and careo #eventmachine
+- thx 2 http://blog.masuidrive.jp/ for a irc+eventmachine gist.
View
14 README.markdown
@@ -42,17 +42,21 @@ you have to be on the control channel for the script to execute. this is the fir
post to `hostname:MINI_WEB_PORT/command/MINI_SECRET`. ie:
- curl -dtext="netcat, lolcats, pigs, sweaty snout, nixon!" http://test.com:2345/echo/dscds789svjskdlvsdz789mkvcjvklsd6
+ curl -d "netcat, lolcats, pigs, sweaty snout, nixon" http://localhost:2345/echo/dscds789svjskdlvsdz789mkvcjvklsd6
-of course normally, you'd proxy to startup.com:2345 from somthing sitting behind :80. Set the ENV variables before starting mini.
+of course normally, you'd proxy to startup.com:2345 from something sitting behind :80. Set the ENV variables before starting mini.
-## commands
+## configuration
+
+### commands
-you can create commands simply by providing your own miniminimini script. ARGV.first ist the command, the rest is arguments. mini comes bundled with a ruby based miniminimini script which you can extend by adding procs like this:
+you can create commands simply by providing your own miniminimini script. ARGV.first is the command, the rest is arguments. mini comes bundled with a ruby based miniminimini script which you can extend by adding procs to ~/miniconfig.rb:
Mini::Bot.commands["echo"] = lambda { |*args| puts args }
+
+just write that straight into ~/miniconfig.rb.
-## configuration
+### environment
the following environment variables can be used to configure mini.
View
0 TODO.textile → TODO.markdown
File renamed without changes.
View
2 bin/minicmd
@@ -0,0 +1,2 @@
+command, *args = ARGV
+Mini::Bot.run(command, args)
View
9 bin/minigen
@@ -0,0 +1,9 @@
+require 'erb'
+
+abort("mini create <directory>") unless ARGV.first == "create" && dir = Dir[ARGV.last].first
+
+target = dir.chomp('/') + '/minictl'
+File.open(target, 'w') { |f| f.write(ERB.new(IO.read(File.dirname(__FILE__) + '/../scripts/minictl.erb')).result) }
+%x{ chmod +x #{ target } }
+
+puts "generated minictl into #{ dir }. edit, then start mini with `ruby minictl` in there."
View
2 lib/mini.rb
@@ -1,2 +1,2 @@
-%w{ rubygems eventmachine activesupport ostruct json sinatra }.each { |lib| require lib }
+%w{ rubygems eventmachine activesupport ostruct sinatra }.each { |lib| require lib }
%w{ listener irc web bot }.each { |lib| require File.dirname(__FILE__) + "/mini/#{ lib }" }
View
12 lib/mini/bot.rb
@@ -1,11 +1,17 @@
-#
-# Runs proc stored in Mini::Bot.commands[command].
-#
module Mini
class Bot
cattr_accessor :commands
@@commands = {}
+ def self.start(options)
+ EventMachine::run do
+ Mini::IRC.connect(options)
+ EventMachine::start_server("0.0.0.0", options[:mini_port], Mini::Listener)
+ @@secret = options[:secret]
+ @@web.run! :port => options[:web_port]
+ end
+ end
+
def self.run(command, args)
proc = Bot.commands[command]
proc ? proc.call(args) : (puts "command #{ command } not found. ")
View
8 lib/mini/irc.rb
@@ -1,6 +1,3 @@
-#
-# Connect to and handle IRC.
-#
module Mini
class IRC < EventMachine::Connection
include EventMachine::Protocols::LineText2
@@ -36,12 +33,13 @@ def dequeue(nicks)
while job = @queue.pop
sender, cmd = job
- execute(cmd) if self.moderators.include?(sender)
+ execute(cmd) if self.moderators.include?(sender)
end
end
def execute(cmd)
- say(%x{ miniminimini #{ cmd } })
+ command = "minicmd #{ [*cmd].join(' ') }"
+ say(%x{#{ command }})
end
def self.connect(options)
View
5 lib/mini/listener.rb
@@ -1,11 +1,8 @@
module Mini
module Listener
-
- # echo "#musicteam,#legal,@alice New album uploaded: ..." | nc somemachine 12345.
- def receive_data(data)
+ def receive_data(data) # echo "#musicteam,#legal,@alice New album uploaded: ..." | nc somemachine 12345.
all, targets, *payload = *data.match(/^(([\#@]\S+,? ?)*)(.*)$/)
targets = targets.split(",").map { |target| target.strip }.uniq
-
IRC.connection.say(payload.pop.strip, targets)
end
end
View
5 lib/mini/web.rb
@@ -1,5 +1,6 @@
@@web = Sinatra.new do
- get("/:command/:secret") do
- Mini::IRC.connection.execute([params[:command], params.to_yaml].join(" ")) if params[:secret] == ENV['MINI_SECRET']
+ post("/:command/:secret") do
+ command, secret = params.delete("command"), params.delete("secret")
+ Mini::IRC.connection.execute([command, params].join(" ")) if @@secret
end
end
View
19 mini.gemspec
@@ -0,0 +1,19 @@
+Gem::Specification.new do |s|
+ s.name = "mini"
+ s.version = "0.9"
+
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
+ s.authors = %w{ Rany Keddo }
+ s.date = "2009-03-01"
+ s.email = "purzelrakete@gmail.com"
+ s.extra_rdoc_files = %w{ README.markdown }
+ s.files = ["CREDITS.markdown", "LICENSE.markdown", "README.markdown", "TODO.markdown", "bin/minigen", "bin/minicmd", "lib/mini", "lib/mini/bot.rb", "lib/mini/irc.rb", "lib/mini/listener.rb", "lib/mini/web.rb", "lib/mini.rb", "scripts/minictl.erb",]
+ s.has_rdoc = false
+ s.rdoc_options = ["--line-numbers", "--inline-source"]
+ s.homepage = "http://github.com/purzelrakete/mini"
+ s.require_paths = %w{ lib }
+ s.requirements = %w{ eventmachine activesupport sinatra }
+ s.rubygems_version = "1.3.1"
+ s.executables = %w{ minigen }
+ s.summary = "a ruby eventmachine bot inspired by richard jones' irccat. interact via netcat, irc or web."
+end
View
16 script/minictl
@@ -1,16 +0,0 @@
-require File.dirname(__FILE__) + '/../lib/mini'
-
-abort("minictl <server> <port> <user> <password> <main channel> <channel2>. don't use hashes for the chans. ") unless ARGV.length >= 4
-mini_secret, mini_port, web_port, server, port, user, password, *channels = [ENV['MINI_SECRET'], ENV['MINI_CAT_PORT'], ENV['MINI_WEB_PORT'], ARGV].flatten
-
-EventMachine::run do
- Mini::IRC.connect \
- :server => server,
- :port => port,
- :user => user,
- :password => password,
- :channels => [*channels]
-
- EventMachine::start_server("0.0.0.0", mini_port || 12345, Mini::Listener)
- @@web.run! :port => 2345 || web_port
-end
View
9 script/miniminimini
@@ -1,9 +0,0 @@
-#!/usr/local/bin/ruby
-
-# FIXME: REMOVE
-require File.dirname(__FILE__) + '/../lib/mini'
-Mini::Bot.commands["echo"] = lambda { |*args| puts args }
-# FIXME: REMOVE
-
-command, *args = ARGV
-Mini::Bot.run(command, args)
View
28 scripts/minictl.erb
@@ -0,0 +1,28 @@
+#!<%= %x{ which ruby } %>
+require 'rubygems'
+require 'mini'
+
+#
+# require any plugins you want to load into the default minicmd:
+# require 'mini_my_plugin'
+#
+# or write some commands straight into here, like...
+#
+Mini::Bot.commands["echo"] = lambda { |*args| puts args }
+
+abort("minictl <server> <port> <user> <password> <main channel> <channel2>. no hashes 4 chans pls. ") unless ARGV.length >= 4
+server, port, user, password, *channels = ARGV
+
+#
+# Change defaults as needed here.
+#
+
+Mini::Bot.start \
+ :secret => '<%= (0...40).map{ 65.+(rand(25)).chr }.join %>', # randomly generated for you.
+ :mini_port => 12345,
+ :web_port => 2345,
+ :server => server,
+ :port => port,
+ :user => user,
+ :password => password,
+ :channels => [*channels]

0 comments on commit bf23da0

Please sign in to comment.