iodine: moved to github
[openwrt.org/packages.git] / net / znc.init
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#!/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=

add_param() {
	echo "$1 = $2" >> $ZNC_CONFIG
}

add_chan() {
	chan=${1% *}
	pass=${1#* }
	echo "		<Chan $chan>" >> $ZNC_CONFIG
	[ "$chan" != "$pass" ] && echo "			Key = $pass" >> $ZNC_CONFIG
	echo "		</Chan>" >> $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 "	<Network $name>" >> $ZNC_CONFIG
	config_list_foreach "$network" server "add_param \"		Server\""
	config_list_foreach "$network" channel "add_chan"
}

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

	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 "<User $user>" >> $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 "	<Network Default>" >> $ZNC_CONFIG
		config_list_foreach "$user" server "add_param \"		Server\""
		config_list_foreach "$user" channel "add_chan"
		echo "	</Network>" >> $ZNC_CONFIG
	fi

	config_foreach "add_network \"$user\"" network

	echo "</User>" >> $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} /tmp/etc/znc
	fi

	if [ "$EXTERNAL_CONFIG" -eq 1 -a "$RUNAS_USER"  ]
	then
		local SU=$(which su)
		if [ "$SU" ]
		then
			$SU -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
}


comments