2006-04-07 18:43:16 +02:00
#
# run.rb - several scripts to redirect shell command output to the world
#
# Copyright (c) 2006 by Paul van Tilburg <paul@luon.net>
#
# 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 2 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, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# Defines:
# sh - run a command, send output to the user only
# run - run a command, send the output to the world
# crun - run a commend, send the output to a channel in the world
# pstrun - run a command, send the output to the world via @paste
# ppstrun - run a command, send the output to a player in the world via @paste
# prefrun - run a command, send the output to the world with each
# line prefixed
2005-10-09 10:53:36 +02:00
def register_functions
2006-04-07 18:43:16 +02:00
$scripts . register ( " sh " , " use: /sh <command> \n runs <command> in a subshell and sends output to the screen " )
2005-10-09 10:53:36 +02:00
$scripts . register ( " run " , " use: /run <command> \n runs <command> in a subshell and sends output to the world " )
2006-04-07 18:43:16 +02:00
$scripts . register ( " crun " , " use: /crun <chanalias> <command> \n runs <command> in a subshell and sends output to the channel identified by the <chanalias> " )
$scripts . register ( " pstrun " , " use: /pstrun <command> \n runs <command> in a subshell and sends output via @paste to the world " )
$scripts . register ( " ppstrun " , " use: /ppstrun <player> <command> \n runs <command> in a subshell and sends output via @paste to <player> " )
$scripts . register ( " prefrun " , " use: /prefun <prefix> <command> \n runs <command> in a subshell and sends output to the world with each line preceeded by <prefix> " )
2005-10-09 10:53:36 +02:00
end
2006-04-07 18:43:16 +02:00
def sh ( cmd )
if cmd
$world . status ( " Executing ` #{ cmd } ` " )
do_cmd ( cmd ) { | line | $world . status ( line ) }
else
error ( " use /sh <command> " )
end
end # def sh
2005-10-09 10:53:36 +02:00
2006-04-09 19:58:37 +02:00
def run ( cmd )
2006-04-07 18:43:16 +02:00
if cmd
$world . sendln ( " emote LINUXes ` #{ cmd } ` " )
do_cmd ( cmd ) { | line | $world . sendln ( " emote | #{ line } " ) }
else
2006-04-09 19:58:37 +02:00
error ( " use /run <command> " )
2006-04-07 18:43:16 +02:00
end
2006-04-09 19:58:37 +02:00
end # def run
2005-10-09 10:53:36 +02:00
2006-04-09 19:58:37 +02:00
def crun ( argstr )
2006-04-07 18:43:16 +02:00
chan , cmd = argstr . split ( / \ s+ / , 2 )
if chan and cmd
2006-04-09 19:58:37 +02:00
$world . sendln ( " #{ chan } emote LINUXes ` #{ cmd } ` " )
2006-04-07 18:43:16 +02:00
do_cmd ( cmd ) { | line | $world . sendln ( " #{ chan } emote | #{ line } " ) }
2005-10-09 10:53:36 +02:00
else
2006-04-09 19:58:37 +02:00
error ( " use /crun <chanalias> <command> " )
2005-10-09 10:53:36 +02:00
end
2006-04-09 19:58:37 +02:00
end # def crun
2005-10-09 10:53:36 +02:00
2006-04-07 18:43:16 +02:00
def pstrun ( cmd )
if cmd
$world . sendln ( " @paste " )
$world . sendln ( " $ #{ cmd } " )
do_cmd ( cmd ) { | line | $world . sendln ( line ) }
$world . sendln ( " . " )
2005-10-09 10:53:36 +02:00
else
2006-04-07 18:43:16 +02:00
error ( " use /pstrun <command> " )
end
end # def pstrun
2005-10-09 10:53:36 +02:00
2006-04-07 18:43:16 +02:00
def ppstrun ( argstr )
player , cmd = argstr . split ( / \ s+ / , 2 )
if player and cmd
$world . sendln ( " @paste #{ player } " )
$world . sendln ( " $ #{ cmd } " )
do_cmd ( cmd ) { | line | $world . sendln ( line ) }
$world . sendln ( " . " )
else
error ( " use /ppstrun <player> <command> " )
end
end # def ppstrun
def prefrun ( argstr )
prefix , cmd = argstr . split ( / \ s+ / , 2 )
if prefix and cmd
do_cmd ( cmd ) { | line | $world . sendln ( " #{ prefix } #{ line } " ) }
else
error ( " use /prefrun <prefix> <command> " )
2005-10-09 10:53:36 +02:00
end
2006-04-07 18:43:16 +02:00
end # def prefrun
# Helper functions.
def do_cmd ( command , & action )
IO . popen ( " #{ command } 2>&1 " ) do | io |
io . each_line { | line | yield line . chomp }
end
end
def error ( msg )
$world . status ( " Script error: #{ msg } " )
2005-10-09 10:53:36 +02:00
end