From b4830e97ae51396ccaa9ca2acb469aef80094ae8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1rcio=20Silva?= Date: Fri, 2 Jun 2017 15:44:54 -0300 Subject: Add initial files from envbot v0.1-beta1 --- lib/parse.sh | 100 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 lib/parse.sh (limited to 'lib/parse.sh') diff --git a/lib/parse.sh b/lib/parse.sh new file mode 100644 index 0000000..6abb9ab --- /dev/null +++ b/lib/parse.sh @@ -0,0 +1,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 . # +# # +########################################################################### +#--------------------------------------------------------------------- +## 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 +} -- cgit v1.2.3