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.

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