2 ###############################################################################
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2017 IPFire Network Development Team #
7 # This program is free software: you can redistribute it and/or modify #
8 # it under the terms of the GNU General Public License as published by #
9 # the Free Software Foundation, either version 3 of the License, or #
10 # (at your option) any later version. #
12 # This program is distributed in the hope that it will be useful, #
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15 # GNU General Public License for more details. #
17 # You should have received a copy of the GNU General Public License #
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
20 ###############################################################################
22 description_format_filename
() {
23 # Format the filename of a description file for a given zone or port
29 echo "${NETWORK_IPSEC_CONNS_DIR}/${name}/description"
32 echo "${NETWORK_ZONES_DIR}/${name}/description"
35 echo "${NETWORK_PORTS_DIR}/${name}/description"
40 description_touch_file
() {
41 # If the description file does not exist
42 # and we have no directory with the given name this function creates the file
46 local file=$
(description_format_filename
${type} ${name})
48 # We use the -e switch here because we want to touch
49 # when also no directory with this name exist
50 if ! [ -e ${file} ]; then
55 description_title_read
() {
56 # This function reads the title out of a given description file
60 [ -r "${file}" ] ||
return ${EXIT_ERROR}
63 read -r title
< ${file}
68 # This function provides a higher level interface special for description files of the editor function.
72 description_touch_file
${type} ${name}
74 local file=$
(description_format_filename
${type} ${name})
75 editor
${file} "description_check"
79 # This function prints a given description file.
83 local file=$
(description_format_filename
${type} ${name})
85 if [ ! -r "${file}" ] ||
[ ! -f "${file}" ]; then
86 warning
"${file} is not readable"
90 local title
=$
(description_title_read
${file})
92 cli_headline
1 "Description"
94 cli_print
2 "${title}"
97 # True if we are in the first line
100 # True if we get from the second line on, only whitespace or empty lines
101 local front_white
=true
103 # How many blank lines did we get
104 local white_counter
=0
107 if ${first_line}; then
108 # We are in the first line and pass they so first_line is now false
112 # Check if the line is blank or contain only whitespace
113 if ${front_white} && [[ "${line}" =~ ^
(|
[[:space
:]]+)$
]]; then
114 # The we do not print them
117 # we have found after the second line which is not blank or contain only white space so
118 # front_white is false. Now ew print empyt line but only if they are follewd by a non empty line.
120 if [[ "${line}" == "" ]] ||
[[ "${line}" =~ ^
[[:space
:]]$
]]; then
121 # If the line is blank or contain only white space we increase the counter.
122 (( white_counter
++ ))
124 # The line is not blank so we print all blank lines till now and print the current line after.
125 if [ ${white_counter} -gt 0 ]; then
126 for (( i
= 1; i
<= ${white_counter}; i
+= 1 )); do
130 # The counter is now zero, because the lines were printed.
133 cli_print
2 "${line}"
142 # Function for the command line interface
150 description_print
${type} ${name} ${@}
153 description_edit
${type} ${name} ${@}
156 error
"Invalid argument: ${action}"
162 description_check_title
() {
163 # Checks if the title is too long and if so prints a warning
167 local title_length
=40
169 # Have to be shorter then ${title_length}
170 if [ ${#title} -gt ${title_length} ]; then
171 warning
"Title '${title}' is to long. Only titles with ${title_length} or less chracters are allowed"
178 description_check
() {
179 # Check if a description file satisfy our needs
183 local title
=$
(description_title_read
${file})
185 description_check_title
"${title}"