blob: 391037915c2fd910f63fcdb868ac0f7655ef682d (
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
|
#!/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/>. #
# #
###########################################################################
#---------------------------------------------------------------------
## Convert values with units
## @Dependencies This module depends on units
## @Dependencies (http://www.gnu.org/software/units/units.html)
#---------------------------------------------------------------------
module_convert_INIT() {
modinit_API='2'
modinit_HOOKS=''
if ! hash units > /dev/null 2>&1; then
log_error "Couldn't find \"units\" command line tool. The convert module depend on that tool."
return 1
fi
# Is it GNU units?
if units --help > /dev/null 2>&1; then
module_convert_gnu=1
else
module_convert_gnu=0
fi
commands_register "$1" 'convert' || return 1
helpentry_module_convert_description="Convert between different units."
helpentry_convert_convert_syntax='<value> <unit> [to] <unit>'
helpentry_convert_convert_description='Convert the value from one unit to another.'
}
module_convert_UNLOAD() {
return 0
}
module_convert_REHASH() {
return 0
}
module_convert_handler_convert() {
local sender="$1"
local channel="$2"
local sendernick=
parse_hostmask_nick "$sender" 'sendernick'
# If it isn't in a channel send message back to person who send it,
# otherwise send in channel
if ! [[ $2 =~ ^# ]]; then
channel="$sendernick"
fi
local parameters="$3"
# Format: convert <value> <in unit> <out unit>
if [[ "$parameters" =~ ^([-0-9.]+)\ +([a-zA-Z0-9^/*]+)\ +(to\ +)?([a-zA-Z0-9^/*]+) ]]; then
local value="${BASH_REMATCH[1]}"
local inunit="${BASH_REMATCH[2]}"
local outunit="${BASH_REMATCH[@]: -1}"
# Construct expression of value and inunit,
# needed because of temperature
case $inunit in
C|F|K)
# This only work on GNU units.
if [[ $module_convert_gnu = 1 ]]; then
local inexpr="temp${inunit}($value)"
else
local inexpr="$value deg${inunit}"
fi
;;
*)
local inexpr="$value $inunit"
;;
esac
# Out: Temperature
case $outunit in
C|F|K)
# This only work on GNU units
if [[ $module_convert_gnu = 1 ]]; then
local outexpr="temp${outunit}"
else
local outexpr="deg${outunit}"
fi
local outunit="degrees $outunit"
;;
*)
local outexpr="$outunit"
;;
esac
# Need to do the local separately or return code will be messed up.
local myresult
# Force some security guards
# We can't use -t, that doesn't work on *BSD units...
# so we use awk to get interesting lines.
# Then check pipestatus to give nice return code
myresult="$(ulimit -t 4; units -q "$inexpr" "$outexpr" 2>&1 | awk '/^\t[0-9]+/ {print $1} /\*/ {print $2} /[Ee]rror|[Uu]nknown/'; [[ ${PIPESTATUS[0]} -eq 0 ]] || exit 1)"
if [[ $? -eq 0 ]]; then
send_msg "$channel" "${sendernick}: $myresult $outunit"
else
send_msg "$channel" "${sendernick}: Error: $myresult"
fi
else
feedback_bad_syntax "$sendernick" "convert" "<value> <in unit> [to] <out unit>"
fi
}
|