You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

214 lines
5.4 KiB

  1. #!/bin/sh /etc/rc.common
  2. # Copyright (C) 2010 Openwrt.org
  3. START=60
  4. USE_PROCD=1
  5. ZNC_CONFIG_PATH=/tmp/etc/znc
  6. PID_FILE=${ZNC_CONFIG_PATH}/znc.pid
  7. ZNC_CONFIG=${ZNC_CONFIG_PATH}/configs/znc.conf
  8. EXTERNAL_CONFIG=0
  9. DISABLED=
  10. RUNAS_USER=
  11. RUNAS_GROUP=
  12. add_param() {
  13. echo "$1 = $2" >> $ZNC_CONFIG
  14. }
  15. add_chan() {
  16. chan=${1% *}
  17. pass=${1#* }
  18. echo " <Chan $chan>" >> $ZNC_CONFIG
  19. [ "$chan" != "$pass" ] && echo " Key = $pass" >> $ZNC_CONFIG
  20. echo " </Chan>" >> $ZNC_CONFIG
  21. }
  22. add_network() {
  23. local current_user="$1"
  24. local network="$2"
  25. local user
  26. local name
  27. config_get user "$network" user
  28. [ "$user" = "$current_user" ] || return 0
  29. config_get name "$network" name
  30. echo " <Network $name>" >> $ZNC_CONFIG
  31. config_list_foreach "$network" server "add_param \" Server\""
  32. config_list_foreach "$network" fingerprint "add_param \" trustedserverfingerprint\""
  33. config_list_foreach "$network" channel "add_chan"
  34. echo " </Network>" >> $ZNC_CONFIG
  35. }
  36. znc_global() {
  37. local znc="$1"
  38. local anoniplimit
  39. local maxbuffersize
  40. local connectdelay
  41. local serverthrottle
  42. local znc_config_path
  43. local znc_pem_file
  44. config_get_bool DISABLED "$znc" disabled 0
  45. [ "$DISABLED" -eq 0 ] || return 0
  46. config_get znc_config_path "$znc" znc_config_path
  47. config_get RUNAS_USER "$znc" runas_user znc
  48. config_get RUNAS_GROUP "$znc" runas_group znc
  49. if [ "${znc_config_path}" ]
  50. then
  51. ZNC_CONFIG_PATH=$znc_config_path
  52. EXTERNAL_CONFIG=1
  53. else
  54. mkdir -p $ZNC_CONFIG_PATH/configs/
  55. [ ! -f "$ZNC_CONFIG" ] || rm "$ZNC_CONFIG"
  56. add_param "Version" "1.6"
  57. config_get anoniplimit "$znc" anoniplimit
  58. config_get maxbuffersize "$znc" maxbuffersize
  59. config_get connectdelay "$znc" connectdelay
  60. config_get serverthrottle "$znc" serverthrottle
  61. config_get znc_pem_file "$znc" znc_ssl_cert
  62. [ -z "$znc_pem_file" ] || ln -sf "$znc_pem_file" $ZNC_CONFIG_PATH/znc.pem
  63. [ -z $anoniplimit ] || echo "AnonIPLimit = $anoniplimit" >> $ZNC_CONFIG
  64. [ -z $maxbuffersize ] || echo "MaxBufferSize = $maxbuffersize" >> $ZNC_CONFIG
  65. [ -z $connectdelay ] || echo "ConnectDelay = $connectdelay" >> $ZNC_CONFIG
  66. [ -z $serverthrottle ] || echo "ServerThrottle = $anoniplimit" >> $ZNC_CONFIG
  67. echo "PidFile = $PID_FILE" >> $ZNC_CONFIG
  68. config_list_foreach "$znc" listener "add_param Listener"
  69. config_list_foreach "$znc" module "add_param LoadModule"
  70. fi
  71. }
  72. add_listener() {
  73. local listener="$1"
  74. local host
  75. local port
  76. local allowirc
  77. local allowweb
  78. local ipv4
  79. local ipv6
  80. local ssl
  81. config_get host "$listener" host
  82. config_get port "$listener" port
  83. config_get allowirc "$listener" allowirc
  84. config_get allowweb "$listener" allowweb
  85. config_get ipv4 "$listener" ipv4
  86. config_get ipv6 "$listener" ipv6
  87. config_get ssl "$listener" ssl
  88. echo "<Listener $listener>" >> $ZNC_CONFIG
  89. [ -z "$host" ] || add_param " Host" "$host"
  90. [ -z "$port" ] || add_param " Port" "$port"
  91. [ -z "$allowirc" ] || add_param " AllowIRC" "$allowirc"
  92. [ -z "$allowweb" ] || add_param " AllowWeb" "$allowweb"
  93. [ -z "$ipv4" ] || add_param " IPv4" "$ipv4"
  94. [ -z "$ipv6" ] || add_param " IPv6" "$ipv6"
  95. [ -z "$ssl" ] || add_param " SSL" "$ssl"
  96. echo "</Listener>" >> $ZNC_CONFIG
  97. }
  98. add_user() {
  99. local user="$1"
  100. local password
  101. local nick
  102. local altnick
  103. local ident
  104. local realname
  105. local buffer
  106. local quitmsg
  107. local chanmodes
  108. local vhost
  109. local server
  110. config_get password "$user" password
  111. config_get nick "$user" nick
  112. config_get altnick "$user" altnick
  113. config_get ident "$user" ident
  114. config_get realname "$user" realname
  115. config_get buffer "$user" buffer
  116. config_get quitmsg "$user" quitmsg
  117. config_get chanmodes "$user" chanmodes
  118. config_get vhost "$user" vhost
  119. config_get server "$user" server
  120. echo "<User $user>" >> $ZNC_CONFIG
  121. case "$password" in
  122. "md5#"* | "sha256#"* | "plain#"*)
  123. add_param " Pass" "$password"
  124. ;;
  125. *)
  126. add_param " Pass" "plain#$password"
  127. ;;
  128. esac
  129. add_param " Nick" "$nick"
  130. add_param " AltNick" "${altnick:-$nick"_"}"
  131. add_param " Ident" "${ident:-$nick}"
  132. add_param " RealName" "${realname:-$nick}"
  133. [ -z "$vhost" ] || add_param " VHost" "$vhost"
  134. add_param " Buffer" "${buffer:-50}"
  135. add_param " KeepBuffer" "false"
  136. add_param " ChanModes" "${chanmodes:-"+stn"}"
  137. [ -z "$quitmsg" ] || add_param " QuitMsg" "$quitmsg"
  138. config_list_foreach "$user" module "add_param \" LoadModule\""
  139. # add legacy network
  140. if [ "$server" ]; then
  141. echo " <Network Default>" >> $ZNC_CONFIG
  142. config_list_foreach "$user" server "add_param \" Server\""
  143. config_list_foreach "$user" fingerprint "add_param \" trustedserverfingerprint\""
  144. config_list_foreach "$user" channel "add_chan"
  145. echo " </Network>" >> $ZNC_CONFIG
  146. fi
  147. config_foreach "add_network \"$user\"" network
  148. echo "</User>" >> $ZNC_CONFIG
  149. }
  150. start_service() {
  151. config_load znc
  152. config_foreach znc_global znc
  153. [ "$DISABLED" -eq 0 ] || return 0
  154. if [ "$EXTERNAL_CONFIG" -eq 0 ]
  155. then
  156. config_foreach add_listener listener
  157. config_foreach add_user user
  158. fi
  159. chown -hR ${RUNAS_USER}:${RUNAS_GROUP} ${ZNC_CONFIG_PATH} || {
  160. logger -s -t ZNC -p daemon.err "Invalid UID/GID. Aborting startup"
  161. exit 1
  162. }
  163. procd_open_instance
  164. procd_set_param file /etc/config/znc
  165. [ "$EXTERNAL_CONFIG" -eq 1 ] && procd_set_param file "${ZNC_CONFIG}/configs/znc.conf"
  166. procd_set_param command /usr/bin/znc
  167. procd_append_param command -f -d$ZNC_CONFIG_PATH
  168. procd_set_param user ${RUNAS_USER}
  169. procd_set_param respawn
  170. procd_close_instance
  171. }