#!/bin/sh /etc/rc.common
# Copyright (C) 2010 Openwrt.org
START=60
ZNC_CONFIG_PATH=/tmp/etc/znc
PID_FILE=${ZNC_CONFIG_PATH}/znc.pid
ZNC_CONFIG=${ZNC_CONFIG_PATH}/configs/znc.conf
EXTERNAL_CONFIG=0
DISABLED=
RUNAS_USER=
RUNAS_GROUP=
RUNAS_SHELL=
add_param() {
echo "$1 = $2" >> $ZNC_CONFIG
}
add_chan() {
chan=${1% *}
pass=${1#* }
echo " " >> $ZNC_CONFIG
[ "$chan" != "$pass" ] && echo " Key = $pass" >> $ZNC_CONFIG
echo " " >> $ZNC_CONFIG
}
add_network() {
local current_user="$1"
local network="$2"
local user
local name
config_get user "$network" user
[ "$user" = "$current_user" ] || return 0
config_get name "$network" name
echo " " >> $ZNC_CONFIG
config_list_foreach "$network" server "add_param \" Server\""
config_list_foreach "$network" fingerprint "add_param \" trustedserverfingerprint\""
config_list_foreach "$network" channel "add_chan"
echo " " >> $ZNC_CONFIG
}
znc_global() {
local znc="$1"
local anoniplimit
local maxbuffersize
local connectdelay
local serverthrottle
local znc_config_path
local znc_pem_file
config_get_bool DISABLED "$znc" disabled 0
[ "$DISABLED" -eq 0 ] || return 0
config_get znc_config_path "$znc" znc_config_path
config_get RUNAS_USER "$znc" runas_user
config_get RUNAS_GROUP "$znc" runas_group
config_get RUNAS_SHELL "$znc" runas_shell
if [ "${znc_config_path}" ]
then
ZNC_CONFIG_PATH=$znc_config_path
EXTERNAL_CONFIG=1
else
mkdir -p $ZNC_CONFIG_PATH/configs/
[ ! -f "$ZNC_CONFIG" ] || rm "$ZNC_CONFIG"
add_param "Version" "1.0"
config_get anoniplimit "$znc" anoniplimit
config_get maxbuffersize "$znc" maxbuffersize
config_get connectdelay "$znc" connectdelay
config_get serverthrottle "$znc" serverthrottle
config_get znc_pem_file "$znc" znc_ssl_cert
[ -z "$znc_pem_file" ] || ln -sf "$znc_pem_file" $ZNC_CONFIG_PATH/znc.pem
[ -z $anoniplimit ] || echo "AnonIPLimit = $anoniplimit" >> $ZNC_CONFIG
[ -z $maxbuffersize ] || echo "MaxBufferSize = $maxbuffersize" >> $ZNC_CONFIG
[ -z $connectdelay ] || echo "ConnectDelay = $connectdelay" >> $ZNC_CONFIG
[ -z $serverthrottle ] || echo "ServerThrottle = $anoniplimit" >> $ZNC_CONFIG
echo "PidFile = $PID_FILE" >> $ZNC_CONFIG
config_list_foreach "$znc" listener "add_param Listener"
config_list_foreach "$znc" module "add_param LoadModule"
add_param LoadModule "droproot ${RUNAS_USER:-nobody} ${RUNAS_GROUP:-nogroup}"
fi
}
add_user() {
local user="$1"
local password
local nick
local altnick
local ident
local realname
local buffer
local quitmsg
local chanmodes
local vhost
local server
config_get password "$user" password
config_get nick "$user" nick
config_get altnick "$user" altnick
config_get ident "$user" ident
config_get realname "$user" realname
config_get buffer "$user" buffer
config_get quitmsg "$user" quitmsg
config_get chanmodes "$user" chanmodes
config_get vhost "$user" vhost
config_get server "$user" server
echo "" >> $ZNC_CONFIG
case "$password" in
"md5#"* | "sha256#"* | "plain#"*)
add_param " Pass" "$password"
;;
*)
add_param " Pass" "plain#$password"
;;
esac
add_param " Nick" "$nick"
add_param " AltNick" "${altnick:-$nick"_"}"
add_param " Ident" "${ident:-$nick}"
add_param " RealName" "${realname:-$nick}"
[ -z "$vhost" ] || add_param " VHost" "$vhost"
add_param " Buffer" "${buffer:-50}"
add_param " KeepBuffer" "false"
add_param " ChanModes" "${chanmodes:-"+stn"}"
[ -z "$quitmsg" ] || add_param " QuitMsg" "$quitmsg"
config_list_foreach "$user" module "add_param \" LoadModule\""
# add legacy network
if [ "$server" ]; then
echo " " >> $ZNC_CONFIG
config_list_foreach "$user" server "add_param \" Server\""
config_list_foreach "$user" fingerprint "add_param \" trustedserverfingerprint\""
config_list_foreach "$user" channel "add_chan"
echo " " >> $ZNC_CONFIG
fi
config_foreach "add_network \"$user\"" network
echo "" >> $ZNC_CONFIG
}
start() {
config_load znc
config_foreach znc_global znc
if [ "$DISABLED" -eq 1 ]; then
return 0
fi
if [ "$EXTERNAL_CONFIG" -eq 0 ]
then
config_foreach add_listener listener
config_foreach add_user user
chown -hR ${RUNAS_USER:-nobody}:${RUNAS_GROUP:-nogroup} ${ZNC_CONFIG_PATH}
fi
if [ "$EXTERNAL_CONFIG" -eq 1 -a "$RUNAS_USER" ]
then
local SU=$(which su)
if [ "$SU" ]
then
chown -hR ${RUNAS_USER:-nobody}:${RUNAS_GROUP:-nogroup} ${ZNC_CONFIG_PATH}
$SU ${RUNAS_SHELL:+s $RUNAS_SHELL} -c "/usr/bin/znc -d$ZNC_CONFIG_PATH >/dev/null &" $RUNAS_USER
else
logger -s -t ZNC -p daemon.err "Could not run ZNC as user $RUNAS_USER: su not found."
exit 1
fi
else
/usr/bin/znc -d$ZNC_CONFIG_PATH >/dev/null &
fi
}
stop() {
if [ -f "$PID_FILE" ]
then
kill $(cat "$PID_FILE")
else
killall znc
fi
}