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.

194 lines
4.4 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. add_param() {
  12. echo "$1 = $2" >> $ZNC_CONFIG
  13. }
  14. add_chan() {
  15. chan=${1% *}
  16. pass=${1#* }
  17. echo " <Chan $chan>" >> $ZNC_CONFIG
  18. [ "$chan" != "$pass" ] && echo " Key = $pass" >> $ZNC_CONFIG
  19. echo " </Chan>" >> $ZNC_CONFIG
  20. }
  21. add_network() {
  22. local current_user="$1"
  23. local network="$2"
  24. local user
  25. local name
  26. config_get user "$network" user
  27. [ "$user" = "$current_user" ] || return 0
  28. config_get name "$network" name
  29. echo " <Network $name>" >> $ZNC_CONFIG
  30. config_list_foreach "$network" server "add_param \" Server\""
  31. config_list_foreach "$network" channel "add_chan"
  32. }
  33. znc_global() {
  34. local znc="$1"
  35. local anoniplimit
  36. local maxbuffersize
  37. local connectdelay
  38. local serverthrottle
  39. local znc_config_path
  40. local znc_pem_file
  41. config_get_bool DISABLED "$znc" disabled 0
  42. [ "$DISABLED" -eq 0 ] || return 0
  43. config_get znc_config_path "$znc" znc_config_path
  44. config_get RUNAS_USER "$znc" runas_user
  45. config_get RUNAS_GROUP "$znc" runas_group
  46. if [ "${znc_config_path}" ]
  47. then
  48. ZNC_CONFIG_PATH=$znc_config_path
  49. EXTERNAL_CONFIG=1
  50. else
  51. mkdir -p $ZNC_CONFIG_PATH/configs/
  52. [ ! -f "$ZNC_CONFIG" ] || rm "$ZNC_CONFIG"
  53. add_param "Version" "1.0"
  54. config_get anoniplimit "$znc" anoniplimit
  55. config_get maxbuffersize "$znc" maxbuffersize
  56. config_get connectdelay "$znc" connectdelay
  57. config_get serverthrottle "$znc" serverthrottle
  58. config_get znc_pem_file "$znc" znc_ssl_cert
  59. [ -z "$znc_pem_file" ] || ln -sf "$znc_pem_file" $ZNC_CONFIG_PATH/znc.pem
  60. [ -z $anoniplimit ] || echo "AnonIPLimit = $anoniplimit" >> $ZNC_CONFIG
  61. [ -z $maxbuffersize ] || echo "MaxBufferSize = $maxbuffersize" >> $ZNC_CONFIG
  62. [ -z $connectdelay ] || echo "ConnectDelay = $connectdelay" >> $ZNC_CONFIG
  63. [ -z $serverthrottle ] || echo "ServerThrottle = $anoniplimit" >> $ZNC_CONFIG
  64. echo "PidFile = $PID_FILE" >> $ZNC_CONFIG
  65. config_list_foreach "$znc" listener "add_param Listener"
  66. config_list_foreach "$znc" module "add_param LoadModule"
  67. add_param LoadModule "droproot ${RUNAS_USER:-nobody} ${RUNAS_GROUP:-nogroup}"
  68. fi
  69. }
  70. add_user() {
  71. local user="$1"
  72. local password
  73. local nick
  74. local altnick
  75. local ident
  76. local realname
  77. local buffer
  78. local quitmsg
  79. local chanmodes
  80. local vhost
  81. local server
  82. config_get password "$user" password
  83. config_get nick "$user" nick
  84. config_get altnick "$user" altnick
  85. config_get ident "$user" ident
  86. config_get realname "$user" realname
  87. config_get buffer "$user" buffer
  88. config_get quitmsg "$user" quitmsg
  89. config_get chanmodes "$user" chanmodes
  90. config_get vhost "$user" vhost
  91. config_get server "$user" server
  92. echo "<User $user>" >> $ZNC_CONFIG
  93. case "$password" in
  94. "md5#"* | "sha256#"* | "plain#"*)
  95. add_param " Pass" "$password"
  96. ;;
  97. *)
  98. add_param " Pass" "plain#$password"
  99. ;;
  100. esac
  101. add_param " Nick" "$nick"
  102. add_param " AltNick" "${altnick:-$nick"_"}"
  103. add_param " Ident" "${ident:-$nick}"
  104. add_param " RealName" "${realname:-$nick}"
  105. [ -z "$vhost" ] || add_param " VHost" "$vhost"
  106. add_param " Buffer" "${buffer:-50}"
  107. add_param " KeepBuffer" "false"
  108. add_param " ChanModes" "${chanmodes:-"+stn"}"
  109. [ -z "$quitmsg" ] || add_param " QuitMsg" "$quitmsg"
  110. config_list_foreach "$user" module "add_param \" LoadModule\""
  111. # add legacy network
  112. if [ "$server" ]; then
  113. echo " <Network Default>" >> $ZNC_CONFIG
  114. config_list_foreach "$user" server "add_param \" Server\""
  115. config_list_foreach "$user" channel "add_chan"
  116. echo " </Network>" >> $ZNC_CONFIG
  117. fi
  118. config_foreach "add_network \"$user\"" network
  119. echo "</User>" >> $ZNC_CONFIG
  120. }
  121. start() {
  122. config_load znc
  123. config_foreach znc_global znc
  124. if [ "$DISABLED" -eq 1 ]; then
  125. return 0
  126. fi
  127. if [ "$EXTERNAL_CONFIG" -eq 0 ]
  128. then
  129. config_foreach add_listener listener
  130. config_foreach add_user user
  131. chown -hR ${RUNAS_USER:-nobody}:${RUNAS_GROUP:-nogroup} /tmp/etc/znc
  132. fi
  133. if [ "$EXTERNAL_CONFIG" -eq 1 -a "$RUNAS_USER" ]
  134. then
  135. local SU=$(which su)
  136. if [ "$SU" ]
  137. then
  138. $SU -c "/usr/bin/znc -d$ZNC_CONFIG_PATH >/dev/null &" $RUNAS_USER
  139. else
  140. logger -s -t ZNC -p daemon.err "Could not run ZNC as user $RUNAS_USER: su not found."
  141. exit 1
  142. fi
  143. else
  144. /usr/bin/znc -d$ZNC_CONFIG_PATH >/dev/null &
  145. fi
  146. }
  147. stop() {
  148. if [ -f "$PID_FILE" ]
  149. then
  150. kill $(cat "$PID_FILE")
  151. else
  152. killall znc
  153. fi
  154. }