]>
Commit | Line | Data |
---|---|---|
5ccc7299 JS |
1 | #!/bin/bash |
2 | ############################################################################### | |
3 | # # | |
4 | # IPFire.org - A linux based firewall # | |
5 | # Copyright (C) 2017 IPFire Network Development Team # | |
6 | # # | |
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. # | |
11 | # # | |
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. # | |
16 | # # | |
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/>. # | |
19 | # # | |
20 | ############################################################################### | |
21 | ||
22 | description_format_filename() { | |
23 | # Format the filename of a description file for a given zone or port | |
24 | local type=${1} | |
25 | local name=${2} | |
26 | ||
27 | case ${type} in | |
dc5dcfaf JS |
28 | ipsec-connection) |
29 | echo "${NETWORK_IPSEC_CONNS_DIR}/${name}/description" | |
30 | ;; | |
5ccc7299 | 31 | zone) |
04c27fe9 | 32 | echo "${NETWORK_ZONES_DIR}/${name}/description" |
5ccc7299 JS |
33 | ;; |
34 | port) | |
0d994c19 | 35 | echo "${NETWORK_PORTS_DIR}/${name}/description" |
5ccc7299 JS |
36 | ;; |
37 | esac | |
38 | } | |
39 | ||
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 | |
43 | local type=${1} | |
44 | local name=${2} | |
45 | ||
46 | local file=$(description_format_filename ${type} ${name}) | |
47 | ||
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 | |
51 | touch ${file} | |
52 | fi | |
53 | ||
54 | } | |
55 | description_title_read() { | |
56 | # This function reads the title out of a given description file | |
57 | local file=${1} | |
58 | assert isset file | |
59 | ||
60 | [ -r "${file}" ] || return ${EXIT_ERROR} | |
61 | ||
62 | local title | |
63 | read -r title < ${file} | |
64 | echo ${title} | |
65 | } | |
66 | ||
67 | description_edit() { | |
68 | # This function provides a higher level interface special for description files of the editor function. | |
69 | local type=${1} | |
70 | local name=${2} | |
71 | ||
72 | description_touch_file ${type} ${name} | |
73 | ||
74 | local file=$(description_format_filename ${type} ${name}) | |
75 | editor ${file} "description_check" | |
76 | } | |
77 | ||
78 | description_print() { | |
79 | # This function prints a given description file. | |
80 | local type=${1} | |
81 | local name=${2} | |
82 | ||
83 | local file=$(description_format_filename ${type} ${name}) | |
84 | ||
85 | if [ ! -r "${file}" ] || [ ! -f "${file}" ]; then | |
86 | warning "${file} is not readable" | |
87 | return ${EXIT_ERROR} | |
88 | fi | |
89 | ||
90 | local title=$(description_title_read ${file}) | |
91 | ||
92 | cli_headline 1 "Description" | |
93 | cli_space | |
94 | cli_print 2 "${title}" | |
95 | cli_space | |
96 | ||
97 | # True if we are in the first line | |
98 | local first_line=true | |
99 | ||
100 | # True if we get from the second line on, only whitespace or empty lines | |
101 | local front_white=true | |
102 | ||
103 | # How many blank lines did we get | |
104 | local white_counter=0 | |
105 | ||
106 | while read line; do | |
107 | if ${first_line}; then | |
108 | # We are in the first line and pass they so first_line is now false | |
109 | first_line=false | |
110 | continue | |
111 | fi | |
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 | |
115 | continue | |
116 | else | |
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. | |
119 | front_white=false | |
120 | if [[ "${line}" == "" ]] || [[ "${line}" =~ ^[[:space:]]$ ]]; then | |
121 | # If the line is blank or contain only white space we increase the counter. | |
122 | (( white_counter++ )) | |
123 | else | |
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 | |
127 | cli_space | |
128 | done | |
129 | ||
130 | # The counter is now zero, because the lines were printed. | |
131 | white_counter=0 | |
132 | fi | |
133 | cli_print 2 "${line}" | |
134 | fi | |
135 | fi | |
136 | done < ${file} | |
137 | ||
138 | cli_space | |
139 | } | |
140 | ||
141 | description_cli() { | |
142 | # Function for the command line interface | |
143 | local type=${1} | |
144 | local name=${2} | |
145 | local action=${3} | |
146 | shift 3 | |
147 | ||
148 | case ${action} in | |
149 | show) | |
150 | description_print ${type} ${name} ${@} | |
151 | ;; | |
152 | edit) | |
153 | description_edit ${type} ${name} ${@} | |
154 | ;; | |
155 | *) | |
156 | error "Invalid argument: ${action}" | |
157 | ;; | |
158 | esac | |
159 | ||
160 | } | |
161 | ||
162 | description_check_title() { | |
163 | # Checks if the title is too long and if so prints a warning | |
164 | assert [ $# -eq 1 ] | |
165 | ||
166 | local title=${1} | |
167 | local title_length=40 | |
168 | ||
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" | |
172 | return ${EXIT_ERROR} | |
173 | fi | |
174 | ||
175 | return ${EXIT_OK} | |
176 | } | |
177 | ||
178 | description_check() { | |
179 | # Check if a description file satisfy our needs | |
180 | assert [ $# -eq 1 ] | |
181 | ||
182 | local file=${1} | |
183 | local title=$(description_title_read ${file}) | |
184 | ||
185 | description_check_title "${title}" | |
186 | ||
187 | return ${EXIT_OK} | |
188 | } |