Project

General

Profile

Download (2.47 KB) Statistics
| Branch: | Tag: | Revision:
205a27cc Marc Dequènes (Duck)
#!/usr/bin/ruby -Ku

#--
# CyborgHood, a distributed system management software.
# Copyright (c) 2009-2011 Marc Dequènes (Duck) <Duck@DuckCorp.org>
#
# 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/>.
#++

# to allow in-place run for test
$: << File.join(File.dirname(__FILE__), "..", "lib")

require 'cyborghood'


module CyborgHood
module MessengerLand
class Messenger < Cyborg
# load config before modules are included
Config.load(self.human_name)

inject_deferred_translations

include BotNet

attr_reader :user

def setup
super

# TODO: find user, do not trust ENV (use sudo trusted env?)
@user = ENV['USER']

define_interface "0.1~"

@terminal_cli = self.services.terminal_cli
@terminal_cli.when_command_received do |cmd, refs, &cb|
task 'execute_command' do
# TODO: handle syms into cmd (useful when refs are used)
clerk_cmd = (['', 'Commands'] + cmd).join('/')
set_user bot.user
ask "Clerk", :cmd_result, clerk_cmd, refs

on_error do
cb.call false
end
on_success do
cb.call true, results[:cmd_result]
end
end
end
@terminal_cli.when_finished do
stop :quickly
end

# TODO: handle middleware_cli, if choosen middleware support it
end

def start_work
@terminal_cli.run
end

def stop(condition)
super do
@terminal_cli.stop
end
end
end
end # MessengerLand
end


reg = Needle::Registry.new
reg.define do |b|
b.require 'cyborghood', CyborgHood
b.require 'cyborghood-messenger/land', CyborgHood::MessengerLand

b.bot { CyborgHood::MessengerLand::Messenger.new(b.messenger_land) }
end

bot = reg.bot


trap('INT') do
bot.stop(:quickly)
end
trap('TERM') do
bot.stop(:quickly)
end

bot.run