2020-06-11 00:54:34 +02:00
|
|
|
import configparser
|
|
|
|
import importlib
|
|
|
|
import os
|
|
|
|
from collections import defaultdict
|
|
|
|
|
2017-11-01 20:51:12 +01:00
|
|
|
from django.conf import settings
|
2023-03-04 01:52:14 +01:00
|
|
|
from django.db.models import F, Sum
|
2017-11-01 20:51:12 +01:00
|
|
|
from django.db.models.functions import Length
|
|
|
|
|
2020-06-11 00:54:34 +02:00
|
|
|
from zerver.models import BotConfigData, UserProfile
|
2018-01-07 19:21:04 +01:00
|
|
|
|
|
|
|
|
2017-11-01 20:51:12 +01:00
|
|
|
class ConfigError(Exception):
|
|
|
|
pass
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2024-07-12 02:30:17 +02:00
|
|
|
def get_bot_config(bot_profile: UserProfile) -> dict[str, str]:
|
2018-01-06 11:01:22 +01:00
|
|
|
entries = BotConfigData.objects.filter(bot_profile=bot_profile)
|
2018-01-07 19:14:21 +01:00
|
|
|
if not entries:
|
|
|
|
raise ConfigError("No config data available.")
|
2017-11-01 20:51:12 +01:00
|
|
|
return {entry.key: entry.value for entry in entries}
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2024-07-12 02:30:17 +02:00
|
|
|
def get_bot_configs(bot_profile_ids: list[int]) -> dict[int, dict[str, str]]:
|
2018-05-04 22:31:28 +02:00
|
|
|
if not bot_profile_ids:
|
2018-04-02 19:52:54 +02:00
|
|
|
return {}
|
2018-05-04 22:31:28 +02:00
|
|
|
entries = BotConfigData.objects.filter(bot_profile_id__in=bot_profile_ids)
|
2024-07-12 02:30:17 +02:00
|
|
|
entries_by_uid: dict[int, dict[str, str]] = defaultdict(dict)
|
2018-04-02 19:52:54 +02:00
|
|
|
for entry in entries:
|
2018-05-04 22:31:28 +02:00
|
|
|
entries_by_uid[entry.bot_profile_id].update({entry.key: entry.value})
|
2018-04-02 19:52:54 +02:00
|
|
|
return entries_by_uid
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2024-07-12 02:30:23 +02:00
|
|
|
def get_bot_config_size(bot_profile: UserProfile, key: str | None = None) -> int:
|
2017-11-01 20:51:12 +01:00
|
|
|
if key is None:
|
2021-02-12 08:19:30 +01:00
|
|
|
return (
|
|
|
|
BotConfigData.objects.filter(bot_profile=bot_profile)
|
2021-02-12 08:20:45 +01:00
|
|
|
.annotate(key_size=Length("key"), value_size=Length("value"))
|
|
|
|
.aggregate(sum=Sum(F("key_size") + F("value_size")))["sum"]
|
2021-02-12 08:19:30 +01:00
|
|
|
or 0
|
|
|
|
)
|
2017-11-01 20:51:12 +01:00
|
|
|
else:
|
|
|
|
try:
|
2018-01-06 11:01:22 +01:00
|
|
|
return len(key) + len(BotConfigData.objects.get(bot_profile=bot_profile, key=key).value)
|
|
|
|
except BotConfigData.DoesNotExist:
|
2017-11-01 20:51:12 +01:00
|
|
|
return 0
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2018-05-10 19:13:36 +02:00
|
|
|
def set_bot_config(bot_profile: UserProfile, key: str, value: str) -> None:
|
2017-11-01 20:51:12 +01:00
|
|
|
config_size_limit = settings.BOT_CONFIG_SIZE_LIMIT
|
|
|
|
old_entry_size = get_bot_config_size(bot_profile, key)
|
|
|
|
new_entry_size = len(key) + len(value)
|
|
|
|
old_config_size = get_bot_config_size(bot_profile)
|
|
|
|
new_config_size = old_config_size + (new_entry_size - old_entry_size)
|
|
|
|
if new_config_size > config_size_limit:
|
2021-02-12 08:19:30 +01:00
|
|
|
raise ConfigError(
|
2023-08-03 00:28:59 +02:00
|
|
|
f"Cannot store configuration. Request would require {new_config_size} characters. "
|
|
|
|
f"The current configuration size limit is {config_size_limit} characters."
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
|
|
|
obj, created = BotConfigData.objects.get_or_create(
|
2021-02-12 08:20:45 +01:00
|
|
|
bot_profile=bot_profile, key=key, defaults={"value": value}
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2017-11-01 20:51:12 +01:00
|
|
|
if not created:
|
|
|
|
obj.value = value
|
|
|
|
obj.save()
|
2018-01-07 19:21:04 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2024-07-12 02:30:17 +02:00
|
|
|
def load_bot_config_template(bot: str) -> dict[str, str]:
|
2021-02-12 08:20:45 +01:00
|
|
|
bot_module_name = f"zulip_bots.bots.{bot}"
|
2018-01-07 19:21:04 +01:00
|
|
|
bot_module = importlib.import_module(bot_module_name)
|
2021-12-23 06:54:56 +01:00
|
|
|
assert bot_module.__file__ is not None
|
2018-01-07 19:21:04 +01:00
|
|
|
bot_module_path = os.path.dirname(bot_module.__file__)
|
2021-02-12 08:20:45 +01:00
|
|
|
config_path = os.path.join(bot_module_path, f"{bot}.conf")
|
2018-01-07 19:21:04 +01:00
|
|
|
if os.path.isfile(config_path):
|
|
|
|
config = configparser.ConfigParser()
|
|
|
|
with open(config_path) as conf:
|
2020-06-04 03:00:25 +02:00
|
|
|
config.read_file(conf)
|
2018-01-07 19:21:04 +01:00
|
|
|
return dict(config.items(bot))
|
|
|
|
else:
|
2020-09-02 08:14:51 +02:00
|
|
|
return {}
|