1
0

znc.init 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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.0"
  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_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_service() {
  125. config_load znc
  126. config_foreach znc_global znc
  127. [ "$DISABLED" -eq 0 ] || return 0
  128. if [ "$EXTERNAL_CONFIG" -eq 0 ]
  129. then
  130. config_foreach add_listener listener
  131. config_foreach add_user user
  132. fi
  133. chown -hR ${RUNAS_USER}:${RUNAS_GROUP} ${ZNC_CONFIG_PATH} || {
  134. logger -s -t ZNC -p daemon.err "Invalid UID/GID. Aborting startup"
  135. exit 1
  136. }
  137. procd_open_instance
  138. procd_set_param file /etc/config/znc
  139. [ "$EXTERNAL_CONFIG" -eq 1 ] && procd_set_param file "${ZNC_CONFIG}/configs/znc.conf"
  140. procd_set_param command /usr/bin/znc
  141. procd_append_param command -f -d$ZNC_CONFIG_PATH
  142. procd_set_param user ${RUNAS_USER}
  143. procd_set_param respawn
  144. procd_close_instance
  145. }