patch 5c25f44f55e071966feef397617ebfeea52dd88c Author: E. Bosch Date: Sun May 7 01:31:34 CEST 2023 * telegram: Add an option to enable and control format of timestamps for history messages Add timezone option to convert timestamps diff -rN -u old-irgramd/README.md new-irgramd/README.md --- old-irgramd/README.md 2024-11-22 20:20:11.522070586 +0100 +++ new-irgramd/README.md 2024-11-22 20:20:11.526070579 +0100 @@ -51,7 +51,7 @@ ## Requirements -- [python] (>= v3.8) +- [python] (>= v3.9) - [telethon] (tested with v1.28.5) - [tornado] (tested with v6.1.0) - [aioconsole] (tested with v0.6.1) diff -rN -u old-irgramd/irgramd new-irgramd/irgramd --- old-irgramd/irgramd 2024-11-22 20:20:11.526070579 +0100 +++ new-irgramd/irgramd 2024-11-22 20:20:11.526070579 +0100 @@ -73,6 +73,7 @@ tornado.options.define('config', default='irgramdrc', metavar='CONFIGFILE', help='Config file absolute or relative to `config_dir` (command line options override it)') tornado.options.define('config_dir', default='~/.config/irgramd', metavar='PATH', help='Configuration directory where telegram session info is saved') tornado.options.define('emoji_ascii', default=False, help='Replace emoji with ASCII emoticons') + tornado.options.define('hist_timestamp_format', metavar='DATETIME_FORMAT', help='Format string for timestamps in history, see https://www.strfti.me') tornado.options.define('irc_address', default='127.0.0.1', metavar='ADDRESS', help='Address to listen on for IRC') tornado.options.define('irc_nicks', type=str, multiple=True, metavar='nick,..', help='List of nicks allowed for IRC, if `pam` and optionally `pam_group` are set, PAM authentication will be used instead') tornado.options.define('irc_password', default='', metavar='PASSWORD', help='Password for IRC authentication, if `pam` is set, PAM authentication will be used instead') @@ -87,6 +88,7 @@ tornado.options.define('test_datacenter', default=2, metavar='DATACENTER_NUMBER', help='Datacenter to connect to Telegram test environment') tornado.options.define('test_host', default=None, metavar='HOST_IP', help='Host to connect to Telegram test environment (default: use a internal table depending on datacenter)') tornado.options.define('test_port', default=443, metavar='PORT', help='Port to connect to Telegram test environment') + tornado.options.define('timezone', default='UTC', metavar='TIMEZONE', help='Timezone to use for dates (timestamps in history, last in dialogs, etc.)') tornado.options.define('tls', default=False, help='Use TLS/SSL encrypted connection for IRC server') tornado.options.define('tls_cert', default=None, metavar='CERTFILE', help='IRC server certificate chain for TLS/SSL, also can contain private key if not defined with `tls_key`') tornado.options.define('tls_key', default=None, metavar='KEYFILE', help='IRC server private key for TLS/SSL') diff -rN -u old-irgramd/service.py new-irgramd/service.py --- old-irgramd/service.py 2024-11-22 20:20:11.526070579 +0100 +++ new-irgramd/service.py 2024-11-22 20:20:11.530070573 +0100 @@ -203,7 +203,7 @@ his = await self.tg.telegram_client.get_messages(peer_id, limit=li) for msg in reversed(his): - await self.tg.handle_telegram_message(event=None, message=msg) + await self.tg.handle_telegram_message(event=None, message=msg, history=True) reply = () return reply diff -rN -u old-irgramd/telegram.py new-irgramd/telegram.py --- old-irgramd/telegram.py 2024-11-22 20:20:11.526070579 +0100 +++ new-irgramd/telegram.py 2024-11-22 20:20:11.530070573 +0100 @@ -22,7 +22,7 @@ from include import CHAN_MAX_LENGHT, NICK_MAX_LENGTH from irc import IRCUser -from utils import sanitize_filename, is_url_equiv, extract_url, get_human_size, get_human_duration, get_highlighted, fix_braces +from utils import sanitize_filename, is_url_equiv, extract_url, get_human_size, get_human_duration, get_highlighted, fix_braces, format_timestamp import emoji2emoticon as e # Test IP table @@ -48,6 +48,8 @@ self.test_port = settings['test_port'] self.ask_code = settings['ask_code'] self.quote_len = settings['quote_length'] + self.hist_fmt = settings['hist_timestamp_format'] + self.timezone = settings['timezone'] if not settings['emoji_ascii']: e.emo = {} self.media_cn = 0 @@ -448,7 +450,7 @@ if message: await self.handle_telegram_message(event=None, message=message, upd_to_webpend=update.webpage) - async def handle_telegram_message(self, event, message=None, upd_to_webpend=None): + async def handle_telegram_message(self, event, message=None, upd_to_webpend=None, history=False): self.logger.debug('Handling Telegram Message: %s', event or message) msg = event.message if event else message @@ -456,13 +458,14 @@ user = self.get_irc_user_from_telegram(msg.sender_id) mid = self.mid.num_to_id_offset(msg.id) text = await self.render_text(msg, mid, upd_to_webpend) - chan = await self.relay_telegram_message(msg, user, text) + text_send = self.set_history_timestamp(text, history, msg.date) + chan = await self.relay_telegram_message(msg, user, text_send) self.to_cache(msg.id, mid, msg.message, text, user, chan, msg.media) self.refwd_me = False - async def render_text(self, message, mid, upd_to_webpend): + async def render_text(self, message, mid, upd_to_webpend, history=False): if upd_to_webpend: text = await self.handle_webpage(upd_to_webpend, message) elif message.media: @@ -481,6 +484,14 @@ final_text = e.replace_mult(final_text, e.emo) return final_text + def set_history_timestamp(self, text, history, date): + if history and self.hist_fmt: + timestamp = format_timestamp(self.hist_fmt, self.timezone, date) + res = '{} {}'.format(timestamp, text) + else: + res = text + return res + async def relay_telegram_message(self, message, user, text, channel=None): private = (message and message.is_private) or (not message and not channel) if private: diff -rN -u old-irgramd/utils.py new-irgramd/utils.py --- old-irgramd/utils.py 2024-11-22 20:20:11.526070579 +0100 +++ new-irgramd/utils.py 2024-11-22 20:20:11.530070573 +0100 @@ -11,6 +11,7 @@ import textwrap import re import datetime +import zoneinfo import difflib # Constants @@ -171,3 +172,7 @@ if not '{}' in subtext: return '{}...'.format(subtext) return text + +def format_timestamp(format, tz, date): + date_local = date.astimezone(zoneinfo.ZoneInfo(tz)) + return date_local.strftime(format)