From 8f6ccfba04eb626841697c5cf081a6dfa639c58a Mon Sep 17 00:00:00 2001 From: eleksir Date: Fri, 2 Jan 2026 15:54:23 +0300 Subject: [PATCH] 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 --- export_settings.pl | 148 ++++++++++++++++++++++++++++++++++++++++ extract_chatlist.txt.pl | 28 ++++++++ lib/BotLib/Admin.pm | 49 +++++++++++-- 3 files changed, 219 insertions(+), 6 deletions(-) create mode 100644 export_settings.pl create mode 100644 extract_chatlist.txt.pl diff --git a/export_settings.pl b/export_settings.pl new file mode 100644 index 0000000..d1bcfa4 --- /dev/null +++ b/export_settings.pl @@ -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: diff --git a/extract_chatlist.txt.pl b/extract_chatlist.txt.pl new file mode 100644 index 0000000..591b354 --- /dev/null +++ b/extract_chatlist.txt.pl @@ -0,0 +1,28 @@ +#!/usr/bin/perl +# +use strict; +use warnings; + +my %chats; +open my $F, '<', 'debug.log' or die "Unable to open debug.log: $!\n"; ## no critic (ErrorHandling::RequireUseOfExceptions, InputOutput::RequireBriefOpen) + +foreach my $line (<$F>) { ## no critic (InputOutput::ProhibitReadlineInForLoop) + if ($line =~ /.DEBUG. In public chat .* \(\-(\d+)\) .* say:/) { + next unless defined $1; + next if $1 eq ''; + + $chats{$1} = 1; + } +} + +close $F; + +open $F, '>', 'chatlist.txt' or die "Unable to open chatlist.txt: $!\n"; ## no critic (ErrorHandling::RequireUseOfExceptions) + +foreach my $chat (keys %chats) { + print $F "$chat\n"; +} + +close $F; + +# vim: set ft=perl noet ai ts=4 sw=4 sts=4: diff --git a/lib/BotLib/Admin.pm b/lib/BotLib/Admin.pm index b830960..bd811b5 100644 --- a/lib/BotLib/Admin.pm +++ b/lib/BotLib/Admin.pm @@ -15,12 +15,19 @@ use version; our $VERSION = qw (1.1); use Exporter qw (import); # to export array we need @ISA here our @ISA = qw / Exporter /; ## no critic (ClassHierarchies::ProhibitExplicitISA) -our @EXPORT_OK = qw (@ForbiddenMessageTypes @PluginList GetForbiddenTypes AddForbiddenType DelForbiddenType - ListForbidden FortuneToggle FortuneToggleList FortuneStatus PluginToggle PluginStatus - PluginEnabled ChanMsgToggle ChanMsgStatus ChanMsgEnabled GreetMsgToggle GreetMsgStatus - GreetMsgEnabled GoodbyeMsgToggle GoodbyeMsgStatus GoodbyeMsgEnabled MuteByAdminToggle - MuteByAdminStatus MuteByAdminEnabled MigrateSettingsToNewChatID IsCensored GetSlackGreetRememberId - SetSlackGreetRememberId); +our @EXPORT_OK = qw (@ForbiddenMessageTypes @PluginList + GetForbiddenTypes AddForbiddenType DelForbiddenType ListForbidden EnabledForbidden + FortuneToggle FortuneStatus FortuneEnabled FortuneToggleList + ChanMsgToggle ChanMsgStatus ChanMsgEnabled ChanMsgToggleList + GreetMsgToggle GreetMsgStatus GreetMsgEnabled GreetMsgToggleList + GoodbyeMsgToggle GoodbyeMsgStatus GoodbyeMsgEnabled GoodbyeMsgToggleList + PluginToggle PluginStatus PluginEnabled + MuteByAdminToggle MuteByAdminStatus MuteByAdminEnabled MuteByAdminToggleList + MigrateSettingsToNewChatID + IsCensored + GetSlackGreetRememberId + SetSlackGreetRememberId + ); my $c = LoadConf (); my $cachedir = $c->{cachedir}; @@ -406,6 +413,16 @@ sub GreetMsgEnabled ($) { } } +sub GreetMsgToggleList () { + my $cache = CHI->new ( + driver => 'BerkeleyDB', + root_dir => $cachedir, + namespace => __PACKAGE__ . '_' . 'greet_msg', + ); + + return $cache->get_keys (); +} + sub GoodbyeMsgToggle (@) { my $chatid = shift; my $action = shift // undef; @@ -492,6 +509,16 @@ sub GoodbyeMsgEnabled ($) { } } +sub GoodbyeMsgToggleList () { + my $cache = CHI->new ( + driver => 'BerkeleyDB', + root_dir => $cachedir, + namespace => __PACKAGE__ . '_' . 'goodbye_msg', + ); + + return $cache->get_keys (); +} + sub PluginStatus (@) { my $chatid = shift; my $plugin = shift; @@ -663,6 +690,16 @@ sub MuteByAdminEnabled ($) { } } +sub MuteByAdminToggleList () { + my $cache = CHI->new ( + driver => 'BerkeleyDB', + root_dir => $cachedir, + namespace => __PACKAGE__ . '_' . 'admin_mute', + ); + + return $cache->get_keys (); +} + sub MigrateSettingsToNewChatID { my $old_id = shift; my $new_id = shift;