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.

200 lines
4.7 KiB

  1. #!/bin/sh /etc/rc.common
  2. # Copyright (C) 2010 Openwrt.org
  3. START=60
  4. ZNC_CONFIG_PATH=/tmp/etc/znc
  5. PID_FILE=${ZNC_CONFIG_PATH}/znc.pid
  6. ZNC_CONFIG=${ZNC_CONFIG_PATH}/configs/znc.conf
  7. EXTERNAL_CONFIG=0
  8. DISABLED=
  9. RUNAS_USER=
  10. RUNAS_GROUP=
  11. RUNAS_SHELL=
  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
  48. config_get RUNAS_GROUP "$znc" runas_group
  49. config_get RUNAS_SHELL "$znc" runas_shell
  50. if [ "${znc_config_path}" ]
  51. then
  52. ZNC_CONFIG_PATH=$znc_config_path
  53. EXTERNAL_CONFIG=1
  54. else
  55. mkdir -p $ZNC_CONFIG_PATH/configs/
  56. [ ! -f "$ZNC_CONFIG" ] || rm "$ZNC_CONFIG"
  57. add_param "Version" "1.0"
  58. config_get anoniplimit "$znc" anoniplimit
  59. config_get maxbuffersize "$znc" maxbuffersize
  60. config_get connectdelay "$znc" connectdelay
  61. config_get serverthrottle "$znc" serverthrottle
  62. config_get znc_pem_file "$znc" znc_ssl_cert
  63. [ -z "$znc_pem_file" ] || ln -sf "$znc_pem_file" $ZNC_CONFIG_PATH/znc.pem
  64. [ -z $anoniplimit ] || echo "AnonIPLimit = $anoniplimit" >> $ZNC_CONFIG
  65. [ -z $maxbuffersize ] || echo "MaxBufferSize = $maxbuffersize" >> $ZNC_CONFIG
  66. [ -z $connectdelay ] || echo "ConnectDelay = $connectdelay" >> $ZNC_CONFIG
  67. [ -z $serverthrottle ] || echo "ServerThrottle = $anoniplimit" >> $ZNC_CONFIG
  68. echo "PidFile = $PID_FILE" >> $ZNC_CONFIG
  69. config_list_foreach "$znc" listener "add_param Listener"
  70. config_list_foreach "$znc" module "add_param LoadModule"
  71. add_param LoadModule "droproot ${RUNAS_USER:-nobody} ${RUNAS_GROUP:-nogroup}"
  72. fi
  73. }
  74. add_user() {
  75. local user="$1"
  76. local password
  77. local nick
  78. local altnick
  79. local ident
  80. local realname
  81. local buffer
  82. local quitmsg
  83. local chanmodes
  84. local vhost
  85. local server
  86. config_get password "$user" password
  87. config_get nick "$user" nick
  88. config_get altnick "$user" altnick
  89. config_get ident "$user" ident
  90. config_get realname "$user" realname
  91. config_get buffer "$user" buffer
  92. config_get quitmsg "$user" quitmsg
  93. config_get chanmodes "$user" chanmodes
  94. config_get vhost "$user" vhost
  95. config_get server "$user" server
  96. echo "<User $user>" >> $ZNC_CONFIG
  97. case "$password" in
  98. "md5#"* | "sha256#"* | "plain#"*)
  99. add_param " Pass" "$password"
  100. ;;
  101. *)
  102. add_param " Pass" "plain#$password"
  103. ;;
  104. esac
  105. add_param " Nick" "$nick"
  106. add_param " AltNick" "${altnick:-$nick"_"}"
  107. add_param " Ident" "${ident:-$nick}"
  108. add_param " RealName" "${realname:-$nick}"
  109. [ -z "$vhost" ] || add_param " VHost" "$vhost"
  110. add_param " Buffer" "${buffer:-50}"
  111. add_param " KeepBuffer" "false"
  112. add_param " ChanModes" "${chanmodes:-"+stn"}"
  113. [ -z "$quitmsg" ] || add_param " QuitMsg" "$quitmsg"
  114. config_list_foreach "$user" module "add_param \" LoadModule\""
  115. # add legacy network
  116. if [ "$server" ]; then
  117. echo " <Network Default>" >> $ZNC_CONFIG
  118. config_list_foreach "$user" server "add_param \" Server\""
  119. config_list_foreach "$user" fingerprint "add_param \" trustedserverfingerprint\""
  120. config_list_foreach "$user" channel "add_chan"
  121. echo " </Network>" >> $ZNC_CONFIG
  122. fi
  123. config_foreach "add_network \"$user\"" network
  124. echo "</User>" >> $ZNC_CONFIG
  125. }
  126. start() {
  127. config_load znc
  128. config_foreach znc_global znc
  129. if [ "$DISABLED" -eq 1 ]; then
  130. return 0
  131. fi
  132. if [ "$EXTERNAL_CONFIG" -eq 0 ]
  133. then
  134. config_foreach add_listener listener
  135. config_foreach add_user user
  136. chown -hR ${RUNAS_USER:-nobody}:${RUNAS_GROUP:-nogroup} ${ZNC_CONFIG_PATH}
  137. fi
  138. if [ "$EXTERNAL_CONFIG" -eq 1 -a "$RUNAS_USER" ]
  139. then
  140. local SU=$(which su)
  141. if [ "$SU" ]
  142. then
  143. chown -hR ${RUNAS_USER:-nobody}:${RUNAS_GROUP:-nogroup} ${ZNC_CONFIG_PATH}
  144. $SU ${RUNAS_SHELL:+s $RUNAS_SHELL} -c "/usr/bin/znc -d$ZNC_CONFIG_PATH >/dev/null &" $RUNAS_USER
  145. else
  146. logger -s -t ZNC -p daemon.err "Could not run ZNC as user $RUNAS_USER: su not found."
  147. exit 1
  148. fi
  149. else
  150. /usr/bin/znc -d$ZNC_CONFIG_PATH >/dev/null &
  151. fi
  152. }
  153. stop() {
  154. if [ -f "$PID_FILE" ]
  155. then
  156. kill $(cat "$PID_FILE")
  157. else
  158. killall znc
  159. fi
  160. }