Skip to content

Commit

Permalink
gemified and created minictl, minigen
Browse files Browse the repository at this point in the history
  • Loading branch information
purzelrakete committed Mar 7, 2009
1 parent 0a332a8 commit bf23da0
Show file tree
Hide file tree
Showing 14 changed files with 86 additions and 47 deletions.
4 changes: 2 additions & 2 deletions 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.
14 changes: 9 additions & 5 deletions README.markdown
Expand Up @@ -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.

Expand Down
File renamed without changes.
2 changes: 2 additions & 0 deletions bin/minicmd
@@ -0,0 +1,2 @@
command, *args = ARGV
Mini::Bot.run(command, args)
9 changes: 9 additions & 0 deletions 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."
2 changes: 1 addition & 1 deletion 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 }" }
12 changes: 9 additions & 3 deletions 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. ")
Expand Down
8 changes: 3 additions & 5 deletions lib/mini/irc.rb
@@ -1,6 +1,3 @@
#
# Connect to and handle IRC.
#
module Mini
class IRC < EventMachine::Connection
include EventMachine::Protocols::LineText2
Expand Down Expand Up @@ -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)
Expand Down
5 changes: 1 addition & 4 deletions 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
Expand Down
5 changes: 3 additions & 2 deletions 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
19 changes: 19 additions & 0 deletions 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
16 changes: 0 additions & 16 deletions script/minictl

This file was deleted.

9 changes: 0 additions & 9 deletions script/miniminimini

This file was deleted.

28 changes: 28 additions & 0 deletions 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.