Add export_settings.pl to save chat settings in json
Add missing functions to lib/BotLib/Admin.pm to be able to expoprt chat settings for for particular chats. Add extract_chatlist.txt.pl to extract chat list from debug.log
This commit is contained in:
148
export_settings.pl
Normal file
148
export_settings.pl
Normal file
@@ -0,0 +1,148 @@
|
||||
#!/usr/bin/perl
|
||||
|
||||
# Плагин для сохранения настроек чатов. Экспортирует настройки в json, который можно использовать для импорта в другого
|
||||
# бота.
|
||||
#
|
||||
# Файлик data/chatlist.txt придётся добывать грепаньем дебаг-логов (через скрипт extract_chatlist.txt.pl).
|
||||
# Формат 1 chatid на строку.
|
||||
|
||||
|
||||
use 5.018; ## no critic (ProhibitImplicitImport)
|
||||
use strict;
|
||||
use warnings;
|
||||
use utf8;
|
||||
use open qw (:std :utf8);
|
||||
use English qw ( -no_match_vars );
|
||||
use Data::Dumper qw(Dumper);
|
||||
use JSON::XS ();
|
||||
|
||||
use lib ("./lib", "./vendor_perl", "./vendor_perl/lib/perl5");
|
||||
|
||||
use BotLib::Admin qw(
|
||||
FortuneToggleList FortuneEnabled
|
||||
ChanMsgToggleList ChanMsgEnabled
|
||||
GreetMsgToggleList GreetMsgEnabled
|
||||
GoodbyeMsgToggleList GoodbyeMsgEnabled
|
||||
MuteByAdminToggleList MuteByAdminEnabled
|
||||
PluginEnabled);
|
||||
use BotLib::Conf qw(LoadConf);
|
||||
|
||||
my $settings;
|
||||
|
||||
my $chatlistfile = 'data/chatlist.txt';
|
||||
|
||||
my $c = LoadConf('data/config.json');
|
||||
|
||||
my $admin_dir = $c->{admin}->{dir};
|
||||
my $cachedir = $c->{cachedir};
|
||||
|
||||
# Достаём список чатов, где настроена fortune-ка по утрам.
|
||||
my @fortune_chats = FortuneToggleList();
|
||||
|
||||
foreach my $chatid (@fortune_chats) {
|
||||
$settings->{$chatid}->{fortune} = FortuneEnabled($chatid);
|
||||
}
|
||||
|
||||
# Достаём список чатов, где настроена возможность удаления сообщений, отправленных от имени другого калана.
|
||||
my @chanmsg_chats = ChanMsgToggleList();
|
||||
|
||||
foreach my $chatid (@chanmsg_chats) {
|
||||
$settings->{$chatid}->{chanmsg} = ChanMsgEnabled($chatid);
|
||||
}
|
||||
|
||||
# Достаём список чатов, где настроены приветственные сообщения.
|
||||
my @greetmsg_chats = GreetMsgToggleList();
|
||||
|
||||
foreach my $chatid (@greetmsg_chats) {
|
||||
$settings->{$chatid}->{greetmsg} = GreetMsgEnabled($chatid);
|
||||
}
|
||||
|
||||
# Достаём список чатов, где настроены прощальные сообщения.
|
||||
my @goodbyemsg_chats = GoodbyeMsgToggleList();
|
||||
|
||||
foreach my $chatid (@goodbyemsg_chats) {
|
||||
$settings->{$chatid}->{goodbyemsg} = GoodbyeMsgEnabled($chatid);
|
||||
}
|
||||
|
||||
# Достаём список чатов, где настроена возможность мута администратором.
|
||||
my @adminmute_chats = MuteByAdminToggleList();
|
||||
|
||||
foreach my $chatid (@adminmute_chats) {
|
||||
$settings->{$chatid}->{adminmute} = MuteByAdminEnabled($chatid);
|
||||
}
|
||||
|
||||
open my $F, '<', $chatlistfile or die "Unable to open $chatlistfile\n"; ## no critic (ErrorHandling::RequireUseOfExceptions,InputOutput::RequireBriefOpen)
|
||||
|
||||
foreach my $chatid (<$F>) { ## no critic (InputOutput::ProhibitReadlineInForLoop)
|
||||
chomp $chatid;
|
||||
next if $chatid eq '';
|
||||
|
||||
if (PluginEnabled($chatid, 'oboobs')) {
|
||||
$settings->{$chatid}->{plugins}->{oboobs} = 1;
|
||||
}
|
||||
if (PluginEnabled($chatid, 'obutts')) {
|
||||
$settings->{$chatid}->{plugins}->{obutts} = 1;
|
||||
}
|
||||
}
|
||||
|
||||
close $F;
|
||||
|
||||
my $data;
|
||||
|
||||
foreach my $chatid (keys %$settings) {
|
||||
my $item->{id} = $chatid;
|
||||
|
||||
if ($settings->{$chatid}->{fortune}) {
|
||||
$item->{fortune} = 1;
|
||||
} else {
|
||||
$item->{fortune} = 0;
|
||||
}
|
||||
|
||||
if ($settings->{$chatid}->{chanmsg}) {
|
||||
$item->{chanmsg} = 1;
|
||||
} else {
|
||||
$item->{chanmsg} = 0;
|
||||
}
|
||||
|
||||
if ($settings->{$chatid}->{greetmsg}) {
|
||||
$item->{greetmsg} = 1;
|
||||
} else {
|
||||
$item->{greetmsg} = 0;
|
||||
}
|
||||
|
||||
if ($settings->{$chatid}->{goodbyemsg}) {
|
||||
$item->{goodbyemsg} = 1;
|
||||
} else {
|
||||
$item->{goodbyemsg} = 0;
|
||||
}
|
||||
|
||||
if ($settings->{$chatid}->{adminmute}) {
|
||||
$item->{adminmute} = 1;
|
||||
} else {
|
||||
$item->{adminmute} = 0;
|
||||
}
|
||||
|
||||
if ($settings->{$chatid}->{plugins}->{obutts}) {
|
||||
$item->{plugins}->{obutts} = 1;
|
||||
} else {
|
||||
$item->{plugins}->{obutts} = 0;
|
||||
}
|
||||
|
||||
if ($settings->{$chatid}->{plugins}->{oboobs}) {
|
||||
$item->{plugins}->{oboobs} = 1;
|
||||
} else {
|
||||
$item->{plugins}->{oboobs} = 0;
|
||||
}
|
||||
|
||||
push @{$data}, $item;
|
||||
}
|
||||
|
||||
my $json = eval { JSON::XS->new->utf8(1)->pretty(1)->encode($data) };
|
||||
|
||||
if (defined $json) {
|
||||
print $json . "\n";
|
||||
} else {
|
||||
die "Unable to generate json, something went wrong: $EVAL_ERROR\n"; ## no critic (ErrorHandling::RequireUseOfExceptions)
|
||||
}
|
||||
|
||||
# vim: set ft=perl noet ai ts=4 sw=4 sts=4:
|
||||
Reference in New Issue
Block a user