| 1 | ## |
|---|
| 2 | # (c) Copyright 2009 Daniele Tricoli aka Eriol <eriol@mornie.org> |
|---|
| 3 | # |
|---|
| 4 | # This program is free software: you can redistribute it and/or modify |
|---|
| 5 | # it under the terms of the GNU General Public License as published by |
|---|
| 6 | # the Free Software Foundation, either version 3 of the License, or |
|---|
| 7 | # (at your option) any later version. |
|---|
| 8 | # |
|---|
| 9 | # This program is distributed in the hope that it will be useful, |
|---|
| 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 12 | # GNU General Public License for more details. |
|---|
| 13 | # |
|---|
| 14 | # You should have received a copy of the GNU General Public License |
|---|
| 15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|---|
| 16 | ## |
|---|
| 17 | |
|---|
| 18 | import subprocess |
|---|
| 19 | |
|---|
| 20 | import weechat |
|---|
| 21 | |
|---|
| 22 | DESC = '''Execute command(s) and print the output (only if called with -o). |
|---|
| 23 | e.g: |
|---|
| 24 | /exec ls |
|---|
| 25 | /exec -o toilet --irc Ciao! |
|---|
| 26 | ''' |
|---|
| 27 | |
|---|
| 28 | def exec_(server, args): |
|---|
| 29 | |
|---|
| 30 | send_output = False |
|---|
| 31 | if not args: |
|---|
| 32 | weechat.prnt('You must provide a command to execute!') |
|---|
| 33 | return weechat.PLUGIN_RC_OK |
|---|
| 34 | elif args[:2] == '-o': |
|---|
| 35 | send_output = True |
|---|
| 36 | args = args[2:] |
|---|
| 37 | |
|---|
| 38 | proc = subprocess.Popen(args, |
|---|
| 39 | stdin=subprocess.PIPE, |
|---|
| 40 | stdout=subprocess.PIPE, |
|---|
| 41 | stderr=subprocess.PIPE, |
|---|
| 42 | close_fds=True, |
|---|
| 43 | shell=True) |
|---|
| 44 | |
|---|
| 45 | stdout_value, stderr_value = proc.communicate() |
|---|
| 46 | |
|---|
| 47 | if stderr_value: |
|---|
| 48 | error = ' '.join(stderr_value.split()[1:]) |
|---|
| 49 | weechat.prnt('\x035' + error + '\x0F') |
|---|
| 50 | return weechat.PLUGIN_RC_OK |
|---|
| 51 | |
|---|
| 52 | if send_output: |
|---|
| 53 | weechat.command(stdout_value) |
|---|
| 54 | else: |
|---|
| 55 | weechat.prnt(stdout_value) |
|---|
| 56 | return weechat.PLUGIN_RC_OK |
|---|
| 57 | |
|---|
| 58 | |
|---|
| 59 | weechat.register('exec', |
|---|
| 60 | '0.1', |
|---|
| 61 | '', |
|---|
| 62 | DESC) |
|---|
| 63 | weechat.add_command_handler ('exec', |
|---|
| 64 | 'exec_', |
|---|
| 65 | 'Execute command(s)', |
|---|
| 66 | '[-o] <command line>', |
|---|
| 67 | ' -o : print output on current' |
|---|
| 68 | 'server/channel\n') |
|---|