Skip to content

Instantly share code, notes, and snippets.

@KireinaHoro
Last active January 15, 2018 05:06
Show Gist options
  • Save KireinaHoro/075348dcf4bc161aa66dc7ef430ddf84 to your computer and use it in GitHub Desktop.
Save KireinaHoro/075348dcf4bc161aa66dc7ef430ddf84 to your computer and use it in GitHub Desktop.
service file for weechat relay
#!/sbin/openrc-run
# Copyright 1999-2017 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
depend() {
need net localmount
after bootmisc
}
checkconfig() {
if [ "${WEECHAT_USER}" = "" ] ; then
eerror "Please edit /etc/conf.d/weechat-relay"
eerror "You have to specify a user to run weechat as, as we will not run it as root!"
eerror "Modify WEECHAT_USER to your needs (you can also add a group, after a colon)"
return 1
fi
if ! getent passwd "${WEECHAT_USER%:*}" >/dev/null ; then
eerror "Please edit /etc/conf.d/weechat-relay"
eerror "Your user has to exist!"
return 1
fi
if [ "${WEECHAT_USER%:*}" = "${WEECHAT_USER}" ] ; then
return 0
else
if ! getent group "${WEECHAT_USER#*:}" >/dev/null ; then
eerror "Please edit /etc/conf.d/weechat-relay"
eerror "Your group has to exist too!"
return 1
fi
fi
if [ "${WEECHAT_LETSENCRYPT_HOSTNAME}" = "" ] ; then
eerror "Please edit /etc/conf.d/weechat-relay"
eerror "Relay hostname not specified."
eerror "Modify WEECHAT_LETSENCRYPT_HOSTNAME to your needs."
return 1
fi
WEECHAT_CERT_PATH="/etc/letsencrypt/live/${WEECHAT_LETSENCRYPT_HOSTNAME}"
if ! ls "${WEECHAT_CERT_PATH}" 2>&1 >/dev/null ; then
eerror "Please set up the domain specified via certbot, so that fullchain.pem and privkey.pem exist in ${WEECHAT_CERT_PATH}."
return 1
fi
return 0
}
craft_relay_cert() {
mkdir -p "${WEECHAT_HOME}/ssl"
cat "${WEECHAT_CERT_PATH}"/{fullchain,privkey}.pem > "${WEECHAT_HOME}/ssl/relay.pem"
}
destroy_relay_cert() {
rm "${WEECHAT_HOME}/ssl/relay.pem" 2>&1 >/dev/null
}
start() {
checkconfig || return $?
if [ "${WEECHAT_HOME}" = "" ] ; then
WEECHAT_HOME="$(getent passwd "${WEECHAT_USER%:*}" | cut -d ':' -f 6)/.weechat"
fi
craft_relay_cert
ebegin "Starting WeeChat Relay"
TMUX_SOCKET_PATH_BASENAME=/tmp/tmux-$(getent passwd "${WEECHAT_USER%:*}" | cut -d ':' -f 3)
TMUX_SOCKET_PATH=${TMUX_SOCKET_PATH_BASENAME}/weechat
mkdir -p "${TMUX_SOCKET_PATH_BASENAME}"
chmod 700 "${TMUX_SOCKET_PATH_BASENAME}"
chown "${WEECHAT_USER}" "${TMUX_SOCKET_PATH_BASENAME}"
start-stop-daemon --start --user "${WEECHAT_USER%:*}" \
--name weechat-relay-tmux --exec /usr/bin/tmux -e WEECHAT_HOME="${WEECHAT_HOME}" \
-- -S "${TMUX_SOCKET_PATH}" -2 new-session -d -s irc -- /usr/bin/weechat
chown "${WEECHAT_USER}" "${TMUX_SOCKET_PATH}"
RET_STATUS=$?
eend ${RET_STATUS}
}
stop() {
if [ "${WEECHAT_HOME}" = "" ] ; then
WEECHAT_HOME="$(getent passwd "${WEECHAT_USER%:*}" | cut -d ':' -f 6)/.weechat"
fi
ebegin "Stopping Weechat Relay"
destroy_relay_cert
/usr/bin/tmux -S /tmp/tmux-$(getent passwd "${WEECHAT_USER%:*}" | cut -d ':' -f 3)/weechat kill-session -t irc
eend $?
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment