Compare commits
4 commits
Master
...
improvemen
Author | SHA1 | Date | |
---|---|---|---|
42ab30f6c5 | |||
![]() |
122a727699 | ||
![]() |
8f668b2249 | ||
![]() |
3d5410fbc4 |
9 changed files with 565 additions and 253 deletions
46
README.md
46
README.md
|
@ -1,2 +1,48 @@
|
||||||
# Pandabot
|
# Pandabot
|
||||||
|
|
||||||
|
## Description
|
||||||
|
|
||||||
|
**Pandabot** is a flexible bot that offers various features such as database integration, file forwarding and interactivity with users via Telegram. With a customizable configuration and PostgreSQL support, the bot is ideal for automating file and communication processes.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### Requirements
|
||||||
|
- **PostgreSQL**: Installed and configured.
|
||||||
|
- **Apache Webserver**: For the integration (optional).
|
||||||
|
- **Telegram Account**
|
||||||
|
|
||||||
|
### Directory structure
|
||||||
|
- A download folder: `downloads` (relative to the bot's directory).
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Installation
|
||||||
|
|
||||||
|
1. **Clone repository**:
|
||||||
|
```bash
|
||||||
|
git clone https://git.n64.cc/hx/Pandabot.git
|
||||||
|
cd Pandabot
|
||||||
|
```
|
||||||
|
|
||||||
|
2. set up **database**:
|
||||||
|
- Create a PostgreSQL database with the default values from ``pandabot.conf`` or customize it.
|
||||||
|
|
||||||
|
3. install **dependencies** (if needed):
|
||||||
|
```bash
|
||||||
|
pip install -r requirements.txt
|
||||||
|
```
|
||||||
|
|
||||||
|
4. customize **configuration files**:
|
||||||
|
- Edit `pandabot.conf` and `channels.conf` to customize the settings accordingly (see below).
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Configuration files
|
||||||
|
|
||||||
|
### `pandabot.conf`
|
||||||
|
|
||||||
|
#### Important parameters
|
||||||
|
- **Botcommand**: Command for bot interactions. Default: `paul`.
|
||||||
|
- **DB connection**: Customize the following fields:
|
||||||
|
- `dbhost`: IP address of the database server (default: `127.0.0.1`).
|
||||||
|
- `dbport`: Port of the database (default: `5432`).
|
|
@ -5,6 +5,6 @@
|
||||||
|
|
||||||
date_default_timezone_set('Europe/Berlin');
|
date_default_timezone_set('Europe/Berlin');
|
||||||
$date=gmdate("Y-m-d H:i:s", time()+date("Z"));
|
$date=gmdate("Y-m-d H:i:s", time()+date("Z"));
|
||||||
$sendMsg="<b>madeline as client registered </b>: ".$date."<br>";
|
$sendMsg="<b>Madeline as client has been registered </b>: ".$date."<br>";
|
||||||
$out=made("messages", "sendMessage", array("peer" => $globalsettings["bot"]["pandaownerid"], "message" => $sendMsg, "parse_mode" => "html"));
|
$out=made("messages", "sendMessage", array("peer" => $globalsettings["bot"]["pandaownerid"], "message" => $sendMsg, "parse_mode" => "html"));
|
||||||
// print_r($out);
|
// print_r($out);
|
127
bot.sh
127
bot.sh
|
@ -1,4 +1,129 @@
|
||||||
#!/bin/bash
|
#!/bin#!/bin/bash
|
||||||
|
|
||||||
|
# Get the current timestamp for logging or unique identifier
|
||||||
|
startDate=$(date "+%Y%m%d%H%M%S")
|
||||||
|
|
||||||
|
# Log file for all actions
|
||||||
|
log_file="/var/log/pandabot.log"
|
||||||
|
|
||||||
|
# Function to log messages to both console and log file with timestamp
|
||||||
|
log_message() {
|
||||||
|
local message=$1
|
||||||
|
local timestamp=$(date '+%Y-%m-%d %H:%M:%S')
|
||||||
|
|
||||||
|
# Log to console
|
||||||
|
echo "$timestamp - $message"
|
||||||
|
|
||||||
|
# Log to file
|
||||||
|
echo "$timestamp - $message" >> "$log_file"
|
||||||
|
}
|
||||||
|
|
||||||
|
# Source external bot configuration and utility functions
|
||||||
|
. inc/sh/inc.bot.sh
|
||||||
|
|
||||||
|
# Define various bot service paths and session files
|
||||||
|
bot_root=$(pwd)
|
||||||
|
bot_pid="${bot_root}/log/tmp/pandabot.pid"
|
||||||
|
madeline_session="${bot_root}/session.madeline"
|
||||||
|
madeline_pid=$(ps -ef | grep "${madeline_session}" | grep -v grep | awk '{print $2}')
|
||||||
|
apache_pid="${bot_root}/log/tmp/httpd.pid"
|
||||||
|
fpm_pid="${bot_root}/log/tmp/php-fpm.pid"
|
||||||
|
|
||||||
|
# Configuration file for bot and services
|
||||||
|
pandabotconf="${bot_root}/conf/pandabot.conf"
|
||||||
|
services="bot|web|apache|fpm|madeline|"
|
||||||
|
|
||||||
|
# Check if pandabot.conf exists before attempting to read
|
||||||
|
if [ ! -f "$pandabotconf" ]; then
|
||||||
|
log_message "ERROR: pandabot.conf not found at $pandabotconf"
|
||||||
|
exit 2
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Get the wait timer from the configuration file
|
||||||
|
waittimer=$(getpandabotconf "${pandabotconf}" "botsh_restart_wait_timer")
|
||||||
|
if [ $? -ne 0 ]; then
|
||||||
|
log_message "ERROR: botsh_restart_wait_timer not configured @ ${pandabotconf}"
|
||||||
|
exit 2
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Check if madeline session file exists before trying to use it
|
||||||
|
if [ ! -f "$madeline_session" ]; then
|
||||||
|
log_message "ERROR: Madeline session file not found at $madeline_session"
|
||||||
|
exit 2
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Ensure that at least one argument is provided
|
||||||
|
if [ $# -eq 0 ]; then
|
||||||
|
bot_sh_usage
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Process command line arguments and options
|
||||||
|
OPTIND=2
|
||||||
|
while getopts "s:" arg; do
|
||||||
|
case "${arg}" in
|
||||||
|
s) service=${OPTARG} ;; # Set the service if -s option is used
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
|
||||||
|
# Check if the specified service is valid
|
||||||
|
if [ ! -z "${service}" ]; then
|
||||||
|
out=$(checkservice "${service}" "${services}")
|
||||||
|
if [ $? -ne 0 ]; then
|
||||||
|
log_message "ERROR: Service '${service}' not found in the list: ${services}"
|
||||||
|
bot_sh_usage
|
||||||
|
exit 4
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
service="all" # Default to all services if no specific service is given
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Main logic for managing services based on the command
|
||||||
|
case "$1" in
|
||||||
|
"start")
|
||||||
|
start_service "${service}"
|
||||||
|
log_message "Started service: ${service}"
|
||||||
|
;;
|
||||||
|
|
||||||
|
"stop")
|
||||||
|
stop_service "${service}"
|
||||||
|
log_message "Stopped service: ${service}"
|
||||||
|
;;
|
||||||
|
|
||||||
|
"restart")
|
||||||
|
stop_service "${service}"
|
||||||
|
log_message "Stopped service: ${service}"
|
||||||
|
echo "Wait ${waittimer} seconds"
|
||||||
|
|
||||||
|
# Validate waittimer (ensure it's a positive integer)
|
||||||
|
if ! [[ "${waittimer}" =~ ^[0-9]+$ ]] || [ "${waittimer}" -le 0 ]; then
|
||||||
|
log_message "ERROR: Invalid waittimer value in ${pandabotconf}. Must be a positive integer."
|
||||||
|
exit 3
|
||||||
|
fi
|
||||||
|
sleep "${waittimer}"
|
||||||
|
start_service "${service}"
|
||||||
|
log_message "Restarted service: ${service}"
|
||||||
|
;;
|
||||||
|
|
||||||
|
"setuptg")
|
||||||
|
echo "Setting up TG for service: ${service}"
|
||||||
|
log_message "Setting up TG for service: ${service}"
|
||||||
|
bin/setuptg.php
|
||||||
|
;;
|
||||||
|
|
||||||
|
"status")
|
||||||
|
status_service "${service}"
|
||||||
|
log_message "Checked status of service: ${service}"
|
||||||
|
;;
|
||||||
|
|
||||||
|
*)
|
||||||
|
# Default case: show usage if no valid argument is given
|
||||||
|
bot_sh_usage
|
||||||
|
log_message "ERROR: Invalid argument provided"
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
/bash
|
||||||
startDate=`date "+%Y%m%d%H%M%S"`;
|
startDate=`date "+%Y%m%d%H%M%S"`;
|
||||||
|
|
||||||
. inc/sh/inc.bot.sh
|
. inc/sh/inc.bot.sh
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
# attention: bot rewrite hes own conf file in human readable form
|
# Attention: bot rewrite hes own conf file in human readable form
|
||||||
# all "to:" channels automatical r mode and will be readed
|
# All "to:" channels automatical r mode and will be read.
|
||||||
# all channels without "to:" will be still readed and saved in database
|
# All channels without "to:" will be still read and saved in the database
|
||||||
# from and section names must be unique, otherwise, the last find will be used
|
# From and section names must be unique, otherwise, the last find will be used
|
||||||
# example section config:
|
# Example section config:
|
||||||
# -----------------------
|
# -----------------------
|
||||||
#
|
#
|
||||||
# '<unique name>':
|
# '<unique name>':
|
||||||
|
|
|
@ -1,263 +1,342 @@
|
||||||
|
# Check if process ID is valid
|
||||||
function checkpid() {
|
function checkpid() {
|
||||||
if [ -f ${1} ]; then
|
if [ -f "$1" ]; then
|
||||||
out=`cat ${1}`
|
out=$(cat "$1")
|
||||||
[ -z "`ps -ef | awk -v pid=${out} '{if ($2 == pid) {print $2}}'`" ] && exit 1
|
if [ -z "$(ps -ef | awk -v pid="$out" '{if ($2 == pid) {print $2}}')" ]; then
|
||||||
else
|
exit 1
|
||||||
exit 1
|
fi
|
||||||
fi
|
else
|
||||||
echo "${out}"
|
exit 1
|
||||||
|
fi
|
||||||
|
echo "$out"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Kill a service
|
||||||
function kill_service() {
|
function kill_service() {
|
||||||
if [ ${1} -ne 0 ]; then
|
if [ "$1" -ne 0 ]; then
|
||||||
echo "${2} not running"
|
echo "$2 not running"
|
||||||
else
|
else
|
||||||
kill ${4} ${3} > /dev/null 2>&1
|
kill "$4" "$3" > /dev/null 2>&1
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Remove Apache built-in modules
|
||||||
function removeapachebuildinmodules() {
|
function removeapachebuildinmodules() {
|
||||||
${2} -l | grep -v "Compiled in modules" | sed "s|\.c$|.so|g" | while read -r line; do
|
"$2" -l | grep -v "Compiled in modules" | sed "s|\.c$|.so|g" | while read -r line; do
|
||||||
sed -i "/^LoadModule.*\/${line}$/d" ${1}
|
sed -i "/^LoadModule.*\/${line}$/d" "$1"
|
||||||
done
|
done
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Get PandaBot configuration from YAML
|
||||||
function getpandabotconf() {
|
function getpandabotconf() {
|
||||||
out=`echo "${1}@@${2}" | php -R '$in=explode("@@", $argn); if (array_key_exists($in[1], yaml_parse_file($in[0]))) { echo yaml_parse_file($in[0])[$in[1]];} else exit(1);'`
|
out=$(echo "$1@@$2" | php -R '
|
||||||
[ $? -ne 0 ] && exit 1
|
$in=explode("@@", $argn);
|
||||||
echo "${out}"
|
$config = yaml_parse_file($in[0]);
|
||||||
|
if (array_key_exists($in[1], $config)) {
|
||||||
|
echo $config[$in[1]];
|
||||||
|
} else {
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
')
|
||||||
|
if [ $? -ne 0 ]; then
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
echo "$out"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Get the path of the HTTP daemon (httpd or apache2)
|
||||||
function httpdbin() {
|
function httpdbin() {
|
||||||
out=`which httpd 2> /dev/null`
|
out=$(which httpd 2> /dev/null)
|
||||||
if [ $? -ne 0 ]; then
|
if [ $? -ne 0 ]; then
|
||||||
out=`which apache2 2> /dev/null`
|
out=$(which apache2 2> /dev/null)
|
||||||
if [ $? -ne 0 ]; then
|
if [ $? -ne 0 ]; then
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
echo "${out}"
|
echo "$out"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Find PHP-FPM binary
|
||||||
function phpfpmbin() {
|
function phpfpmbin() {
|
||||||
out=`find /usr/sbin/ -name php-fpm* 2> /dev/null | head -1`
|
out=$(find /usr/sbin/ -name php-fpm* 2> /dev/null | head -1)
|
||||||
[ -z "${out}" ] && exit 1
|
if [ -z "$out" ]; then
|
||||||
echo "${out}"
|
exit 1
|
||||||
|
fi
|
||||||
|
echo "$out"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Find Apache mod_mpm_event.so path
|
||||||
function apachemod() {
|
function apachemod() {
|
||||||
out=`find /usr -name "mod_mpm_event.so" 2> /dev/null | awk -F / '{for (i=1;i<NF;i++) {printf ("%s/",$i)}}'`
|
out=$(find /usr -name "mod_mpm_event.so" 2> /dev/null | awk -F / '{for (i=1;i<NF;i++) {printf ("%s/",$i)}}')
|
||||||
[ -z "${out}" ] && exit 1
|
if [ -z "$out" ]; then
|
||||||
echo "${out}"
|
exit 1
|
||||||
|
fi
|
||||||
|
echo "$out"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Check if the service is running
|
||||||
function checkservice() {
|
function checkservice() {
|
||||||
out=`echo "${2}" | while read -d "|" arg; do
|
out=$(echo "$2" | while read -d "|" arg; do
|
||||||
[ "${arg}" = "${1}" ] && echo "${1}" && break
|
if [ "$arg" = "$1" ]; then
|
||||||
done`
|
echo "$1"
|
||||||
[ -z "${out}" ] && echo "${1}" && exit 1
|
break
|
||||||
echo "${out}"
|
fi
|
||||||
|
done)
|
||||||
|
if [ -z "$out" ]; then
|
||||||
|
echo "$1"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
echo "$out"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Prepare web server environment variables
|
||||||
function prepare_web() {
|
function prepare_web() {
|
||||||
apache_serverroot=${bot_root}
|
apache_serverroot="$bot_root"
|
||||||
apache_start=`getpandabotconf "${pandabotconf}" "apache_start"`
|
apache_start=$(getpandabotconf "$pandabotconf" "apache_start")
|
||||||
[ $? -ne 0 ] && echo "ERR: apache_start not configured @ ${pandabotconf}" && exit 1
|
|
||||||
|
|
||||||
[ "${apache_start}" != "1" ] && echo "apache_start is not set yes @ ${pandabotconf}" && exit 1
|
if [ $? -ne 0 ]; then
|
||||||
|
echo "ERR: apache_start not configured @ $pandabotconf"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
if [ `id -u` -eq 0 ]; then
|
if [ "$apache_start" != "1" ]; then
|
||||||
apache_user="nobody"
|
echo "apache_start is not set yes @ $pandabotconf"
|
||||||
apache_group="nobody"
|
exit 1
|
||||||
else
|
fi
|
||||||
apache_user=`id -un`
|
|
||||||
apache_group=`id -gn`
|
|
||||||
fi # if [ `id -u` -eq 0 ]; then
|
|
||||||
|
|
||||||
apache_port=`getpandabotconf "${pandabotconf}" "apache_port"`
|
if [ "$(id -u)" -eq 0 ]; then
|
||||||
[ $? -ne 0 ] && echo "ERR: apache_port not configured @ ${pandabotconf}" && exit 1
|
apache_user="nobody"
|
||||||
[ ${apache_port} -lt 1025 ] && echo "ERR: apache_port must be greater than 1024 @ ${pandabotconf}" && exit 1
|
apache_group="nobody"
|
||||||
|
else
|
||||||
|
apache_user=$(id -un)
|
||||||
|
apache_group=$(id -gn)
|
||||||
|
fi
|
||||||
|
|
||||||
|
apache_port=$(getpandabotconf "$pandabotconf" "apache_port")
|
||||||
|
if [ $? -ne 0 ]; then
|
||||||
|
echo "ERR: apache_port not configured @ $pandabotconf"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ "$apache_port" -lt 1025 ]; then
|
||||||
|
echo "ERR: apache_port must be greater than 1024 @ $pandabotconf"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Start PHP-FPM service
|
||||||
function start_fpm() {
|
function start_fpm() {
|
||||||
prepare_web
|
prepare_web
|
||||||
php_fpm_bin=`phpfpmbin`
|
php_fpm_bin=$(phpfpmbin)
|
||||||
[ $? -ne 0 ] && echo "ERR: php-fpm installation not found" && exit 1
|
|
||||||
|
|
||||||
cat ${apache_serverroot}/conf/templates/template.php-fpm.conf \
|
if [ $? -ne 0 ]; then
|
||||||
| sed "s|@@apache_serverroot@@|${apache_serverroot}|g" \
|
echo "ERR: php-fpm installation not found"
|
||||||
| sed "s|@@apache_user@@|${apache_user}|g" \
|
exit 1
|
||||||
| sed "s|@@apache_group@@|${apache_group}|g" \
|
fi
|
||||||
| sed "s|@@apache_port@@|${apache_port}|g" \
|
|
||||||
> ${apache_serverroot}/conf/php-fpm.conf
|
|
||||||
|
|
||||||
${php_fpm_bin} -y ${apache_serverroot}/conf/php-fpm.conf 2> /dev/null
|
cat "${apache_serverroot}/conf/templates/template.php-fpm.conf" | \
|
||||||
if [ $? -ne 0 ]; then
|
sed "s|@@apache_serverroot@@|$apache_serverroot|g" \
|
||||||
echo "php-fpm already running"
|
| sed "s|@@apache_user@@|$apache_user|g" \
|
||||||
out=`checkpid "${fpm_pid}"`
|
| sed "s|@@apache_group@@|$apache_group|g" \
|
||||||
kill ${out} > /dev/null 2>&1
|
| sed "s|@@apache_port@@|$apache_port|g" \
|
||||||
${php_fpm_bin} -y ${apache_serverroot}/conf/php-fpm.conf 2> /dev/null
|
> "${apache_serverroot}/conf/php-fpm.conf"
|
||||||
echo "php-fpm restarted"
|
|
||||||
else
|
"$php_fpm_bin" -y "${apache_serverroot}/conf/php-fpm.conf" 2> /dev/null
|
||||||
echo "php-fpm started"
|
if [ $? -ne 0 ]; then
|
||||||
fi # if [ $? -ne 0 ]; then
|
echo "php-fpm already running"
|
||||||
|
out=$(checkpid "$fpm_pid")
|
||||||
|
kill "$out" > /dev/null 2>&1
|
||||||
|
"$php_fpm_bin" -y "${apache_serverroot}/conf/php-fpm.conf" 2> /dev/null
|
||||||
|
echo "php-fpm restarted"
|
||||||
|
else
|
||||||
|
echo "php-fpm started"
|
||||||
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Start Apache service
|
||||||
function start_apache() {
|
function start_apache() {
|
||||||
prepare_web
|
prepare_web
|
||||||
apachebin=`httpdbin`
|
apachebin=$(httpdbin)
|
||||||
[ $? -ne 0 ] && echo "ERR: apache installation not found" && exit 1
|
|
||||||
|
|
||||||
apache_modules=`apachemod`
|
if [ $? -ne 0 ]; then
|
||||||
[ $? -ne 0 ] && echo "ERR: no apache modules pfad found" && exit 1
|
echo "ERR: apache installation not found"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
rm -rf ${apache_serverroot}/log/access_log > /dev/null 2>&1 # clear access_log file...
|
apache_modules=$(apachemod)
|
||||||
|
|
||||||
apache_pid=${apache_serverroot}/log/tmp/httpd.pid
|
if [ $? -ne 0 ]; then
|
||||||
apache_documentroot="${apache_serverroot}/pb-web"
|
echo "ERR: no apache modules path found"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
cat ${apache_serverroot}/conf/templates/template.httpd.conf \
|
rm -rf "${apache_serverroot}/log/access_log" > /dev/null 2>&1 # clear access_log file...
|
||||||
| sed "s|@@apache_modules@@|${apache_modules}|g" \
|
|
||||||
| sed "s|@@apache_serverroot@@|${apache_serverroot}|g" \
|
|
||||||
| sed "s|@@apache_user@@|${apache_user}|g" \
|
|
||||||
| sed "s|@@apache_group@@|${apache_group}|g" \
|
|
||||||
| sed "s|@@apache_port@@|${apache_port}|g" \
|
|
||||||
| sed "s|@@apache_documentroot@@|${apache_documentroot}|g" \
|
|
||||||
| sed "s|@@apache_pid@@|${apache_pid}|g" \
|
|
||||||
> ${apache_serverroot}/conf/httpd.conf
|
|
||||||
|
|
||||||
removeapachebuildinmodules "${apache_serverroot}/conf/httpd.conf" "${apachebin}"
|
apache_pid="${apache_serverroot}/log/tmp/httpd.pid"
|
||||||
|
apache_documentroot="${apache_serverroot}/pb-web"
|
||||||
|
|
||||||
chmod -R a+rwx ${apache_documentroot} 2>&1 /dev/null
|
cat "${apache_serverroot}/conf/templates/template.httpd.conf" | \
|
||||||
chmod -R a+rwx ${apache_serverroot}/log 2>&1 /dev/null
|
sed "s|@@apache_modules@@|$apache_modules|g" \
|
||||||
|
| sed "s|@@apache_serverroot@@|$apache_serverroot|g" \
|
||||||
|
| sed "s|@@apache_user@@|$apache_user|g" \
|
||||||
|
| sed "s|@@apache_group@@|$apache_group|g" \
|
||||||
|
| sed "s|@@apache_port@@|$apache_port|g" \
|
||||||
|
| sed "s|@@apache_documentroot@@|$apache_documentroot|g" \
|
||||||
|
| sed "s|@@apache_pid@@|$apache_pid|g" \
|
||||||
|
> "${apache_serverroot}/conf/httpd.conf"
|
||||||
|
|
||||||
${apachebin} -f ${apache_serverroot}/conf/httpd.conf -k restart > /dev/null 2>&1
|
removeapachebuildinmodules "${apache_serverroot}/conf/httpd.conf" "$apachebin"
|
||||||
if [ $? -eq 0 ]; then
|
|
||||||
echo "apache (re)started @ port: ${apache_port}"
|
chmod -R a+rwx "$apache_documentroot" 2>&1 /dev/null
|
||||||
else
|
chmod -R a+rwx "${apache_serverroot}/log" 2>&1 /dev/null
|
||||||
echo "ERR: apache not started, something is wrong..."
|
|
||||||
fi # if [ $? -eq 0 ]; then
|
"$apachebin" -f "${apache_serverroot}/conf/httpd.conf" -k restart > /dev/null 2>&1
|
||||||
|
if [ $? -eq 0 ]; then
|
||||||
|
echo "Apache (re)started @ port: $apache_port"
|
||||||
|
else
|
||||||
|
echo "ERR: Apache not started, something is wrong..."
|
||||||
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Start the bot
|
||||||
function start_bot() {
|
function start_bot() {
|
||||||
out=`checkpid "${bot_pid}"`
|
out=$(checkpid "$bot_pid")
|
||||||
[ ! -z "${out}" ] && echo "bot already running, exit!" && exit 1
|
if [ -n "$out" ]; then
|
||||||
|
echo "Bot already running, exit!"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
cp conf/channels.conf log/conf/${startDate}_channels.conf
|
cp conf/channels.conf "log/conf/${startDate}_channels.conf"
|
||||||
cp conf/pandabot.conf log/conf/${startDate}_pandabot.conf
|
cp conf/pandabot.conf "log/conf/${startDate}_pandabot.conf"
|
||||||
|
|
||||||
ls -r1 log/*_pandabot.app.log | awk 'begin {c=0;} {c++; if (c>=10) {print $0}}' | xargs -n1 rm -f # logrotation? function?
|
# Clean up old log files
|
||||||
ls -r1 log/conf/*pandabot.conf | awk 'begin {c=0;} {c++; if (c>=10) {print $0}}' | xargs -n1 rm -f
|
ls -r1 log/*_pandabot.app.log | awk 'NR >= 10' | xargs -n1 rm -f
|
||||||
ls -r1 log/conf/*channels.conf | awk 'begin {c=0;} {c++; if (c>=10) {print $0}}' | xargs -n1 rm -f
|
ls -r1 log/conf/*pandabot.conf | awk 'NR >= 10' | xargs -n1 rm -f
|
||||||
|
ls -r1 log/conf/*channels.conf | awk 'NR >= 10' | xargs -n1 rm -f
|
||||||
|
|
||||||
if [ `id -u` -eq 0 ]; then
|
if [ "$(id -u)" -eq 0 ]; then
|
||||||
chown -R nobody *
|
chown -R nobody *
|
||||||
su nobody -c "nohup bin/pandabot.php 2>&1 | tee log.pandabot.log 2>&1 | tee log/${startDate}_pandabot.app.log > /dev/null &"
|
su nobody -c "nohup bin/pandabot.php 2>&1 | tee log.pandabot.log 2>&1 | tee log/${startDate}_pandabot.app.log > /dev/null &"
|
||||||
if [ $? -ne 0 ]; then
|
if [ $? -ne 0 ]; then
|
||||||
echo "change user nobody from nologin to bash"
|
echo "Change user nobody from nologin to bash"
|
||||||
usermod -s /usr/bin/bash nobody
|
usermod -s /usr/bin/bash nobody
|
||||||
su nobody -c "nohup bin/pandabot.php 2>&1 | tee log.pandabot.log 2>&1 | tee log/${startDate}_pandabot.app.log > /dev/null &"
|
su nobody -c "nohup bin/pandabot.php 2>&1 | tee log.pandabot.log 2>&1 | tee log/${startDate}_pandabot.app.log > /dev/null &"
|
||||||
[ $? -ne 0 ] && "ERR: change user nobody manually from /sbin/nologin to /bin/bash @ /etc/passwd, or start bot in usercontext" && exit 1
|
if [ $? -ne 0 ]; then
|
||||||
fi # if [ $? -ne 0 ]; then
|
echo "ERR: Change user nobody manually from /sbin/nologin to /bin/bash @ /etc/passwd, or start bot in user context"
|
||||||
else
|
exit 1
|
||||||
nohup bin/pandabot.php 2>&1 | tee log.pandabot.log 2>&1 | tee log/${startDate}_pandabot.app.log > /dev/null &
|
fi
|
||||||
fi # if [ `id -u` -eq 0 ]; then
|
fi
|
||||||
|
else
|
||||||
|
nohup bin/pandabot.php 2>&1 | tee log.pandabot.log 2>&1 | tee log/${startDate}_pandabot.app.log > /dev/null &
|
||||||
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Start a specific service
|
||||||
function start_service() {
|
function start_service() {
|
||||||
echo "start ${1}"
|
echo "Start $1"
|
||||||
case "${1}" in
|
case "$1" in
|
||||||
"bot")
|
"bot") start_bot ;;
|
||||||
start_bot
|
"apache")
|
||||||
;;
|
start_apache
|
||||||
"apache")
|
sleep 2
|
||||||
start_apache
|
status_service "apache"
|
||||||
sleep 2
|
;;
|
||||||
status_service "${1}"
|
"fpm") start_fpm ;;
|
||||||
;;
|
"web")
|
||||||
"fpm")
|
start_service "apache"
|
||||||
start_fpm
|
start_service "fpm"
|
||||||
;;
|
;;
|
||||||
"web")
|
"all")
|
||||||
start_service "apache"
|
start_service "bot"
|
||||||
start_service "fpm"
|
start_service "web"
|
||||||
;;
|
;;
|
||||||
"all")
|
esac
|
||||||
start_service "bot"
|
|
||||||
start_service "web"
|
|
||||||
;;
|
|
||||||
esac
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Stop a specific service
|
||||||
function stop_service() {
|
function stop_service() {
|
||||||
echo "stop ${1}"
|
echo "Stop $1"
|
||||||
case "${1}" in
|
case "$1" in
|
||||||
"bot")
|
"bot")
|
||||||
out=`checkpid "${bot_pid}"`
|
out=$(checkpid "$bot_pid")
|
||||||
kill_service "${?}" "${1}" "${out}" "-9"
|
kill_service "$?" "$1" "$out" "-9"
|
||||||
;;
|
;;
|
||||||
"apache")
|
"apache")
|
||||||
out=`checkpid "${apache_pid}"`
|
out=$(checkpid "$apache_pid")
|
||||||
kill_service "${?}" "${1}" "${out}" ""
|
kill_service "$?" "$1" "$out" ""
|
||||||
;;
|
;;
|
||||||
"fpm")
|
"fpm")
|
||||||
out=`checkpid "${fpm_pid}"`
|
out=$(checkpid "$fpm_pid")
|
||||||
kill_service "${?}" "${1}" "${out}" ""
|
kill_service "$?" "$1" "$out" ""
|
||||||
;;
|
;;
|
||||||
"madeline")
|
"madeline")
|
||||||
kill_service "0" "${1}" "${madeline_pid}" "-9"
|
kill_service "0" "$1" "$madeline_pid" "-9"
|
||||||
;;
|
;;
|
||||||
"web")
|
"web")
|
||||||
stop_service "apache"
|
stop_service "apache"
|
||||||
stop_service "fpm"
|
stop_service "fpm"
|
||||||
;;
|
;;
|
||||||
"all")
|
"all")
|
||||||
stop_service "web"
|
stop_service "web"
|
||||||
stop_service "bot"
|
stop_service "bot"
|
||||||
stop_service "madeline"
|
stop_service "madeline"
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
}
|
}
|
||||||
|
|
||||||
status_service() {
|
# Get the status of a service
|
||||||
case "${1}" in
|
function status_service() {
|
||||||
"bot")
|
case "$1" in
|
||||||
out=`checkpid "${bot_pid}"`
|
"bot")
|
||||||
if [ ${?} -eq 0 ]; then echo "${1} running"; else echo "${1} not running"; fi
|
out=$(checkpid "$bot_pid")
|
||||||
;;
|
if [ $? -eq 0 ]; then
|
||||||
"apache")
|
echo "$1 running"
|
||||||
out=`checkpid "${apache_pid}"`
|
else
|
||||||
if [ ${?} -eq 0 ]; then
|
echo "$1 not running"
|
||||||
apache_ip_running=`ip -br address | grep -iwv lo | awk '{print $3}' | awk -F / '{print $1}'`
|
fi
|
||||||
apache_port=`getpandabotconf "${pandabotconf}" "apache_port"`
|
;;
|
||||||
echo "${1} running on: http://${apache_ip_running}:${apache_port}/";
|
"apache")
|
||||||
else
|
out=$(checkpid "$apache_pid")
|
||||||
echo "${1} not running"
|
if [ $? -eq 0 ]; then
|
||||||
fi
|
apache_ip_running=$(ip -br address | grep -iwv lo | awk '{print $3}' | awk -F / '{print $1}')
|
||||||
;;
|
apache_port=$(getpandabotconf "$pandabotconf" "apache_port")
|
||||||
"fpm")
|
echo "$1 running on: http://$apache_ip_running:$apache_port/"
|
||||||
out=`checkpid "${fpm_pid}"`
|
else
|
||||||
if [ ${?} -eq 0 ]; then echo "${1} running"; else echo "${1} not running"; fi
|
echo "$1 not running"
|
||||||
;;
|
fi
|
||||||
"madeline") ## grummel
|
;;
|
||||||
if [ ${?} -eq 0 ]; then echo "${1} running"; else echo "${1} not running"; fi
|
"fpm")
|
||||||
;;
|
out=$(checkpid "$fpm_pid")
|
||||||
"web")
|
if [ $? -eq 0 ]; then
|
||||||
echo "status ${1}"
|
echo "$1 running"
|
||||||
status_service "apache"
|
else
|
||||||
status_service "fpm"
|
echo "$1 not running"
|
||||||
;;
|
fi
|
||||||
"all")
|
;;
|
||||||
echo "status ${1}"
|
"madeline")
|
||||||
status_service "web"
|
if [ $? -eq 0 ]; then
|
||||||
status_service "bot"
|
echo "$1 running"
|
||||||
status_service "madeline"
|
else
|
||||||
;;
|
echo "$1 not running"
|
||||||
esac
|
fi
|
||||||
|
;;
|
||||||
|
"web")
|
||||||
|
echo "Status $1"
|
||||||
|
status_service "apache"
|
||||||
|
status_service "fpm"
|
||||||
|
;;
|
||||||
|
"all")
|
||||||
|
echo "Status $1"
|
||||||
|
status_service "web"
|
||||||
|
status_service "bot"
|
||||||
|
status_service "madeline"
|
||||||
|
;;
|
||||||
|
esac
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Bot script usage guide
|
||||||
function bot_sh_usage() {
|
function bot_sh_usage() {
|
||||||
echo "usage:
|
echo "Usage:
|
||||||
./bot.sh start
|
./bot.sh start
|
||||||
./bot.sh stop
|
./bot.sh stop
|
||||||
./bot.sh restart
|
./bot.sh restart
|
||||||
|
@ -269,7 +348,7 @@ echo "usage:
|
||||||
./bot.sh stop -s [bot|web]
|
./bot.sh stop -s [bot|web]
|
||||||
./bot.sh restart -s [bot|web]
|
./bot.sh restart -s [bot|web]
|
||||||
./bot.sh status -s [bot|web]
|
./bot.sh status -s [bot|web]
|
||||||
eg.
|
Example:
|
||||||
./bot.sh restart -s bot
|
./bot.sh restart -s bot
|
||||||
./bot.sh restart -s web
|
./bot.sh restart -s web
|
||||||
"
|
"
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<meta charset="UTF-8"/>
|
<meta charset="UTF-8"/>
|
||||||
<title>pandabot</title>
|
<title>Pandabot</title>
|
||||||
<link rel="icon" href="img/pb-favicon.jpg">
|
<link rel="icon" href="img/pb-favicon.jpg">
|
||||||
<link rel="stylesheet" href="css/jquery-ui.css">
|
<link rel="stylesheet" href="css/jquery-ui.css">
|
||||||
<link rel="stylesheet" href="css/pandabot.css">
|
<link rel="stylesheet" href="css/pandabot.css">
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
|
|
||||||
setInterval(readLogFile, 4000);
|
setInterval(readLogFile, 4000);
|
||||||
setInterval(stats, 15000);
|
setInterval(stats, 15000);
|
||||||
setInterval(queuestats, 2000);
|
setInterval(queuestats, 2000);
|
||||||
window.onload = start;
|
window.onload = start;
|
||||||
|
@ -20,7 +20,7 @@ function gettitle() {
|
||||||
|
|
||||||
function start() {
|
function start() {
|
||||||
gettitle();
|
gettitle();
|
||||||
$(".fulllogbuttonhide" ).hide();
|
$(".fulllogbuttonhide" ).hide();
|
||||||
$(".fulllog" ).hide();
|
$(".fulllog" ).hide();
|
||||||
$(".editout" ).hide();
|
$(".editout" ).hide();
|
||||||
$(".botcmdout" ).hide();
|
$(".botcmdout" ).hide();
|
||||||
|
@ -131,12 +131,12 @@ function sortchannels(option, cmd) { // hmmm
|
||||||
});
|
});
|
||||||
|
|
||||||
$(function(){
|
$(function(){
|
||||||
$("#floghide").click(function() {
|
$("#floghide").click(function() {
|
||||||
$( ".fulllogbutton" ).show();
|
$( ".fulllogbutton" ).show();
|
||||||
$( "#botcmdid" ).show();
|
$( "#botcmdid" ).show();
|
||||||
$( ".fulllogbuttonhide" ).hide();
|
$( ".fulllogbuttonhide" ).hide();
|
||||||
$( ".fulllog" ).empty();
|
$( ".fulllog" ).empty();
|
||||||
$( ".viewcontainer" ).show();
|
$( ".viewcontainer" ).show();
|
||||||
$( ".viewborder" ).show();
|
$( ".viewborder" ).show();
|
||||||
$( ".fulllog" ).hide();
|
$( ".fulllog" ).hide();
|
||||||
$(".confmain").show();
|
$(".confmain").show();
|
||||||
|
@ -213,12 +213,12 @@ function botcmd(option, cmd) {
|
||||||
|
|
||||||
function botcmdokay() {
|
function botcmdokay() {
|
||||||
if ($(".mainfade").css("opacity") == 1) {
|
if ($(".mainfade").css("opacity") == 1) {
|
||||||
$(".mainfade").fadeTo(1000, 0.05);
|
$(".mainfade").fadeTo(1000, 0.05);
|
||||||
$( ".botcmdout" ).fadeTo(500, 1);
|
$( ".botcmdout" ).fadeTo(500, 1);
|
||||||
}
|
}
|
||||||
if ($(".mainfade").css("opacity") == 0.05) {
|
if ($(".mainfade").css("opacity") == 0.05) {
|
||||||
$(".mainfade").fadeTo(1000, 1);
|
$(".mainfade").fadeTo(1000, 1);
|
||||||
$( ".botcmdout" ).hide();
|
$( ".botcmdout" ).hide();
|
||||||
}
|
}
|
||||||
$("#botcmdid").attr('disabled', false);
|
$("#botcmdid").attr('disabled', false);
|
||||||
$("#flog").attr('disabled', false);
|
$("#flog").attr('disabled', false);
|
||||||
|
@ -231,7 +231,7 @@ function botcmdokay() {
|
||||||
});
|
});
|
||||||
|
|
||||||
$(function(){
|
$(function(){
|
||||||
$("#editokaybutton").click(function() {
|
$("#editokaybutton").click(function() {
|
||||||
editdone();
|
editdone();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,32 +0,0 @@
|
||||||
[ `id -u` -ne 0 ] && echo "ERR: must be run as: root" && exit 1
|
|
||||||
[ ! -f /etc/centos-release ] && echo "ERR: works only on centos" && exit 1
|
|
||||||
|
|
||||||
dnf install https://dl.fedoraproject.org/pub/epel/epel-release-latest-9.noarch.rpm -y
|
|
||||||
dnf install https://rpms.remirepo.net/enterprise/remi-release-9.rpm -y
|
|
||||||
dnf module install php:remi-8.3 -y
|
|
||||||
|
|
||||||
yum install net-tools postgresql postgresql-server httpd php-fpm -y
|
|
||||||
yum install php-pecl-yaml php-pgsql php-pdo -y
|
|
||||||
|
|
||||||
/usr/bin/postgresql-setup --initdb
|
|
||||||
cd /var/lib/pgsql/data
|
|
||||||
cat pg_hba.conf | sed 's|host all all 127.0.0.1/32 ident|host all all 0.0.0.0/0 trust|g' > pg_hba.conf1
|
|
||||||
cp pg_hba.conf1 pg_hba.conf
|
|
||||||
rm -rf pg_hba.conf1
|
|
||||||
|
|
||||||
systemctl restart postgresql
|
|
||||||
|
|
||||||
su - postgres -c 'echo "CREATE DATABASE pandabot;" | psql'
|
|
||||||
|
|
||||||
printf "\n------------------------- --- -- - -\n"
|
|
||||||
echo "edit conf/pandabot.conf && conf/channels.conf"
|
|
||||||
echo "dbname: pandabot"
|
|
||||||
echo "dbhost: `ip -br address | grep -iwv lo | awk '{print $3}' | awk -F / '{print $1}'`"
|
|
||||||
echo "dbport: 5432"
|
|
||||||
echo "dbuser: postgres"
|
|
||||||
echo "dbpass: false"
|
|
||||||
printf "\n"
|
|
||||||
echo "setup tg session with ./bot.sh setuptg"
|
|
||||||
echo "start bot with ./bot.sh start"
|
|
||||||
echo "stop bot with ./bot.sh stop"
|
|
||||||
echo "------------------------- --- -- - -"
|
|
94
prepare_server/Prepare_RHEL-Debian.sh
Normal file
94
prepare_server/Prepare_RHEL-Debian.sh
Normal file
|
@ -0,0 +1,94 @@
|
||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
# Ensure the script is run as root
|
||||||
|
[ "$(id -u)" -ne 0 ] && echo "ERR: must be run as: root" && exit 1
|
||||||
|
|
||||||
|
# Detect the operating system
|
||||||
|
OS=$(cat /etc/*release | grep -i "centos\|debian" | head -n 1 | awk -F= '{print $2}' | tr -d '[:space:]')
|
||||||
|
|
||||||
|
if [[ "$OS" =~ "CentOS" ]]; then
|
||||||
|
PACKAGE_MANAGER="dnf"
|
||||||
|
REPO_INSTALL_CMD="dnf install"
|
||||||
|
PHP_INSTALL_CMD="dnf module install php:remi-8.3 -y"
|
||||||
|
elif [[ "$OS" =~ "Debian" ]]; then
|
||||||
|
PACKAGE_MANAGER="apt"
|
||||||
|
REPO_INSTALL_CMD="apt-get install"
|
||||||
|
PHP_INSTALL_CMD="apt-get install php php-cli php-fpm php-pgsql php-yaml php-pdo -y"
|
||||||
|
else
|
||||||
|
echo "ERR: Unsupported OS, must be CentOS or Debian."
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Install necessary repositories for CentOS or Debian
|
||||||
|
if [[ "$OS" =~ "CentOS" ]]; then
|
||||||
|
$REPO_INSTALL_CMD https://dl.fedoraproject.org/pub/epel/epel-release-latest-9.noarch.rpm -y
|
||||||
|
$REPO_INSTALL_CMD https://rpms.remirepo.net/enterprise/remi-release-9.rpm -y
|
||||||
|
$PHP_INSTALL_CMD
|
||||||
|
elif [[ "$OS" =~ "Debian" ]]; then
|
||||||
|
$REPO_INSTALL_CMD lsb-release wget curl -y
|
||||||
|
# PHP installation is already included in the above command
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Install required packages (PostgreSQL, Apache, PHP, and utilities)
|
||||||
|
$REPO_INSTALL_CMD net-tools postgresql postgresql-server httpd php-fpm -y
|
||||||
|
$REPO_INSTALL_CMD php-pecl-yaml php-pgsql php-pdo -y
|
||||||
|
|
||||||
|
# Setup PostgreSQL
|
||||||
|
if [[ "$OS" =~ "CentOS" ]]; then
|
||||||
|
/usr/bin/postgresql-setup --initdb
|
||||||
|
systemctl enable postgresql
|
||||||
|
systemctl start postgresql
|
||||||
|
elif [[ "$OS" =~ "Debian" ]]; then
|
||||||
|
service postgresql start
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Configure PostgreSQL to allow external connections securely
|
||||||
|
cd /var/lib/pgsql/data || exit
|
||||||
|
cp pg_hba.conf pg_hba.conf.backup
|
||||||
|
|
||||||
|
# Allow connections from any IP (recommended to restrict IP range later)
|
||||||
|
sed -i 's|host all all 127.0.0.1/32 ident|host all all 0.0.0.0/0 md5|g' pg_hba.conf
|
||||||
|
|
||||||
|
# Restart PostgreSQL to apply changes
|
||||||
|
systemctl restart postgresql
|
||||||
|
|
||||||
|
# Create the Pandabot database
|
||||||
|
su - postgres -c 'psql -c "CREATE DATABASE pandabot;"'
|
||||||
|
|
||||||
|
# Display configuration instructions
|
||||||
|
echo -e "\n------------------------- --- -- - -"
|
||||||
|
echo "Edit conf/pandabot.conf and conf/channels.conf"
|
||||||
|
echo "Database settings:"
|
||||||
|
echo "dbname: pandabot"
|
||||||
|
echo "dbhost: $(hostname -I | awk '{print $1}')"
|
||||||
|
echo "dbport: 5432"
|
||||||
|
echo "dbuser: postgres"
|
||||||
|
echo "dbpass: <set_your_password_here>"
|
||||||
|
|
||||||
|
echo -e "\nTo setup the Telegram session, run: ./bot.sh setuptg"
|
||||||
|
echo "To start the bot, run: ./bot.sh start"
|
||||||
|
echo "To stop the bot, run: ./bot.sh stop"
|
||||||
|
echo -e "------------------------- --- -- - -\n"
|
||||||
|
|
||||||
|
# Check if firewall is enabled and add necessary rules for PostgreSQL and Apache
|
||||||
|
if systemctl is-active --quiet firewalld; then
|
||||||
|
echo "Configuring firewall for PostgreSQL (5432) and HTTP (80/443)"
|
||||||
|
firewall-cmd --zone=public --add-port=5432/tcp --permanent
|
||||||
|
firewall-cmd --zone=public --add-service=http --permanent
|
||||||
|
firewall-cmd --reload
|
||||||
|
elif ufw status | grep -q "active"; then
|
||||||
|
echo "Configuring UFW for PostgreSQL (5432) and HTTP (80/443)"
|
||||||
|
ufw allow 5432/tcp
|
||||||
|
ufw allow http
|
||||||
|
ufw allow https
|
||||||
|
ufw reload
|
||||||
|
fi
|
||||||
|
|
||||||
|
# SELinux setup for CentOS (optional step, recommended to allow postgresql and httpd)
|
||||||
|
if [[ "$OS" =~ "CentOS" ]]; then
|
||||||
|
echo "Setting up SELinux to allow PostgreSQL and Apache to work together"
|
||||||
|
setsebool -P httpd_can_network_connect_db 1
|
||||||
|
setsebool -P httpd_can_network_connect 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "Installation and setup complete."
|
Loading…
Add table
Reference in a new issue