~vorpalblade/envbot/anmaster-trunk

« back to all changes in this revision

Viewing changes to modules/m_commands.sh

  • Committer: Arvid Norlander
  • Date: 2017-03-20 18:21:34 UTC
  • Revision ID: anmaster@kuonet.org-20170320182134-s1v2n1afk2t81owa
envbot migrated to GitHub

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/bin/bash
2
 
# -*- coding: utf-8 -*-
3
 
###########################################################################
4
 
#                                                                         #
5
 
#  envbot - an IRC bot in bash                                            #
6
 
#  Copyright (C) 2007-2009  Arvid Norlander                               #
7
 
#  Copyright (C) 2007-2008  Vsevolod Kozlov                               #
8
 
#                                                                         #
9
 
#  This program is free software: you can redistribute it and/or modify   #
10
 
#  it under the terms of the GNU General Public License as published by   #
11
 
#  the Free Software Foundation, either version 3 of the License, or      #
12
 
#  (at your option) any later version.                                    #
13
 
#                                                                         #
14
 
#  This program is distributed in the hope that it will be useful,        #
15
 
#  but WITHOUT ANY WARRANTY; without even the implied warranty of         #
16
 
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          #
17
 
#  GNU General Public License for more details.                           #
18
 
#                                                                         #
19
 
#  You should have received a copy of the GNU General Public License      #
20
 
#  along with this program.  If not, see <http://www.gnu.org/licenses/>.  #
21
 
#                                                                         #
22
 
###########################################################################
23
 
#---------------------------------------------------------------------
24
 
## Command-related utility commands
25
 
#---------------------------------------------------------------------
26
 
 
27
 
module_commands_INIT() {
28
 
        modinit_API='2'
29
 
        modinit_HOOKS=''
30
 
        commands_register "$1" 'provides' || return 1
31
 
        commands_register "$1" 'commands' || return 1
32
 
        helpentry_module_commands_description="Provides a set of command-related commands."
33
 
 
34
 
        helpentry_commands_provides_syntax='<command>'
35
 
        helpentry_commands_provides_description='Shows which module provides command <command>'
36
 
 
37
 
        helpentry_commands_commands_syntax='[<module>]'
38
 
        helpentry_commands_commands_description='Lists commands available in <module>. If module name is not given, lists all commands'
39
 
}
40
 
 
41
 
module_commands_UNLOAD() {
42
 
        return 0
43
 
}
44
 
 
45
 
module_commands_REHASH() {
46
 
        return 0
47
 
}
48
 
 
49
 
module_commands_handler_provides() {
50
 
        local sender="$1"
51
 
        local parameters="$3"
52
 
        if [[ $parameters =~ ^([a-zA-Z0-9][^ ]*)( [^ ]+)? ]]; then # regex suggested by AnMaster
53
 
                local command_name="${BASH_REMATCH[1]}${BASH_REMATCH[2]}"
54
 
                local target
55
 
                if [[ $2 =~ ^# ]]; then
56
 
                        target="$2"
57
 
                else
58
 
                        parse_hostmask_nick "$sender" 'target'
59
 
                fi
60
 
                local module_name
61
 
                commands_provides "$command_name" module_name
62
 
                if [[ -z $module_name ]]; then # No such command
63
 
                        send_msg "$target" "Command \"$command_name\" does not exist."
64
 
                else
65
 
                        send_msg "$target" "Command \"$command_name\" is provided by module \"$module_name\""
66
 
                fi
67
 
        else
68
 
                local sendernick
69
 
                parse_hostmask_nick "$sender" 'sendernick'
70
 
                feedback_bad_syntax "$sendernick" "provides" "<modulename>"
71
 
        fi
72
 
}
73
 
 
74
 
module_commands_handler_commands() {
75
 
        local parameters="$3"
76
 
        local target
77
 
        if [[ $2 =~ ^# ]]; then
78
 
                target="$2"
79
 
        else
80
 
                parse_hostmask_nick "$1" 'target'
81
 
        fi
82
 
        if [[ -z $parameters ]]; then
83
 
                send_msg "$target" "${format_bold}Available commands${format_bold}: ${commands_commands//,/, }"
84
 
        else
85
 
                # So we got a parameter
86
 
                local module_name
87
 
                if [[ $parameters =~ ^([^ ]+)\ *$ ]]; then
88
 
                        module_name="${BASH_REMATCH[1]}"
89
 
                else
90
 
                        send_notice "$target" "\"$parameters\" is not a valid module name"
91
 
                        return 1
92
 
                fi
93
 
                local commands_in_module
94
 
                commands_in_module "$module_name" 'commands_in_module'
95
 
                if [[ $commands_in_module ]]; then
96
 
                        send_msg "$target" "${format_bold}Available commands (in module \"$module_name\")${format_bold}: ${commands_in_module//,/, }"
97
 
                elif list_contains "modules_loaded" "$module_name"; then
98
 
                        send_notice "$target" "Module \"$module_name\" provides no commands"
99
 
                else
100
 
                        send_notice "$target" "Module \"$module_name\" is not loaded"
101
 
                fi
102
 
        fi
103
 
}