summaryrefslogtreecommitdiff
path: root/lib/parse.sh
blob: 6abb9abb4186a8c8cee325a5e18b7ee6ebcc67c8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/bin/bash
# -*- coding: utf-8 -*-
###########################################################################
#                                                                         #
#  envbot - an IRC bot in bash                                            #
#  Copyright (C) 2007-2008  Arvid Norlander                               #
#                                                                         #
#  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 3 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, see <http://www.gnu.org/licenses/>.  #
#                                                                         #
###########################################################################
#---------------------------------------------------------------------
## Data parsing
#---------------------------------------------------------------------

#---------------------------------------------------------------------
## Get parts of hostmask.
## @Note In most cases you should use one of
## @Note <@function parse_hostmask_nick>, <@function parse_hostmask_ident>
## @Note or <@function parse_hostmask_host>. Only use this function
## @Note if you want all several parts.
## @Type API
## @param n!u@h mask
## @param Variable to return nick in
## @param Variable to return ident in
## @param Variable to return host in
#---------------------------------------------------------------------
parse_hostmask() {
	if [[ $1 =~ ^([^ !]+)!([^ @]+)@([^ ]+) ]]; then
		printf -v "$2" '%s' "${BASH_REMATCH[1]}"
		printf -v "$3" '%s' "${BASH_REMATCH[2]}"
		printf -v "$4" '%s' "${BASH_REMATCH[3]}"
	fi
}

#---------------------------------------------------------------------
## Get nick from hostmask
## @Type API
## @param n!u@h mask
## @param Variable to return result in
#---------------------------------------------------------------------
parse_hostmask_nick() {
	if [[ $1 =~ ^([^ !]+)! ]]; then
		printf -v "$2" '%s' "${BASH_REMATCH[1]}"
	fi
}

#---------------------------------------------------------------------
## Get ident from hostmask
## @Type API
## @param n!u@h mask
## @param Variable to return result in
#---------------------------------------------------------------------
parse_hostmask_ident() {
	if [[ $1 =~ ^[^\ !]+!([^ @]+)@ ]]; then
		printf -v "$2" '%s' "${BASH_REMATCH[1]}"
	fi
}

#---------------------------------------------------------------------
## Get host from hostmask
## @Type API
## @param n!u@h mask
## @param Variable to return result in
#---------------------------------------------------------------------
parse_hostmask_host() {
	if [[ $1 =~ ^[^\ !]+![^\ @]+@([^ ]+) ]]; then
		printf -v "$2" '%s' "${BASH_REMATCH[1]}"
	fi
}

#---------------------------------------------------------------------
## This is used to get data out of 005.
## @Type API
## @param Name of data to get
## @param Variable to return result (if any result) in
## @return 0 If found otherwise 1
## @Note That if the variable doesn't have any data,
## @Note but still exist it will return nothing on STDOUT
## @Note but 0 as error code
#---------------------------------------------------------------------
parse_005() {
	if [[ $server_005 =~ ${1}(=([^ ]+))? ]]; then
		if [[ ${BASH_REMATCH[2]} ]]; then
			printf -v "$2" '%s' "${BASH_REMATCH[2]}"
		fi
		return 0
	fi
	return 1
}