Files
tg4xmpp/start.py
annelin 0f648e6bd4 [SVN] updated version to 0.3.0
[FIX] fixed sending subscription request from groups that are removed or from what you've been left and also fixed double auth request
[UPD] now using telethon version == 0.18
[UPD] code optimized and reworked
[UPD] status updates are moved to separate thread to use timer
[UPD] slightly changed status handling (now Available has "Online" status message, "Last seen recently" now is away, not XA, "Last seen ..." is now XA,  "Last seen long time ago" is now DND, chats are ffc)
[UPD] command "!del" removed and replaced with another, see above
[UPD] configuration options `xmpp_use_roster_exchange` and `xmpp_keep_online` was removed from configuration file
[UPD] [BREAK] database structure was changed; please, remove and re-create db.sqlite
[ADD] [BREAK] new options in config file: `logfile` (please, specify it!), and unneccessarry `tg_server_ip`, `tg_server_port`, `tg_server_dc`
[ADD] per-user configuration, parameters stored in database. configurable params:
      - use_roster_exchange: use XEP-0144 for roster import (default: false) (recommended: true, if your client supports that XEP)
      - keep_online: keep telegram session even if jabber goes offline (default: false) (recommended: true, if you wants to receive all events as offline messages when you will go online)
      - status_update_interval: interval (sec.) in what we will update all contact statuses to prevent presence spamming, because telegram sending status updates every fucking second (default: 60)
      To modify your personal config, please, send !configure to gateway
[ADD] added new commands to gateway:
      !configure (for configuration update)
      !add @contact (to find Telegram contact and try to start conversation; any format accepted (t.me link, @username or maybe phone, I don't know... )
      !join t.me/joinchat/secret (to join Telegram conference via invite link, https://t.me/joinchat/xxxxx accepted)
      !group name @contact (try to create normal group with @contact; you can add more later) [UNTESTED]
      !supergroup name (try to create supergroup) [UNTESTED]
      !channel name (try to create channel) [UNTESTED]
      !name first last (change telegram name)
      !username usernme (change telegram @username)
      !about some about text (change about text)
[ADD] added new commands to dialogs with normal users:
      !help
      !block (blacklists user)
      !unblock (unblacklists user)
[ADD] added new commands to group/channel dialogs:
      !help
      !leave (leave current group or supergroup)
      !invite (invite @user to current group/supergroup)
      !kick (kicks @user to/from group/supergroup)

... and also small fixes and improvements
2018-07-01 09:42:35 +00:00

74 lines
2.4 KiB
Python

import xmpp_tg
import logging
import logging.handlers
import os
import sys
import signal
from config import CONFIG
import telethon
import sleekxmpp
xmpp_logger = logging.getLogger('sleekxmpp')
class StreamToLogger:
"""
Прикидывается файловым объектом. Нужен для перехвата стандартных потоков ввода-вывода.
"""
def __init__(self, logger, level=logging.INFO, old_out=None):
self.logger = logger
self.level = level
self.old_out = old_out
self.linebuf = []
self._buffer = ''
self._prev = None
def write(self, buf):
if self._prev == buf == '\n': # Надо на буфер переделывать
self._prev = buf
buf = ''
else:
self._prev = buf
if buf != '\n':
self.logger.log(self.level, buf)
if self.old_out:
self.old_out.write(buf)
def flush(self):
pass
# Настраиваем логгирование
logging.basicConfig(
level=logging.DEBUG if CONFIG['debug'] else logging.INFO,
format='%(asctime)s :: %(levelname)s:%(name)s :: %(message)s',
datefmt='%m/%d/%Y %I:%M:%S %p',
handlers=[logging.handlers.RotatingFileHandler(filename=CONFIG['logfile']), logging.StreamHandler(sys.stdout)]
)
# Создаем логгеры и перехватчики для STDOUT/STDERR
logger_stdout = logging.getLogger('__stdout')
sys.stdout = StreamToLogger(logger_stdout, logging.INFO)
logger_stderr = logging.getLogger('__stderr')
sys.stderr = StreamToLogger(logger_stderr, logging.ERROR)
logging.getLogger().log(logging.INFO, '~'*81)
logging.getLogger().log(logging.INFO, ' RESTART '*9)
logging.getLogger().log(logging.INFO, '~'*81)
print('----------------------------------------------------------------------')
print('--- Telegram (MTProto) <---> XMPP Gateway ---')
print('----------------------------------------------------------------------')
print()
print('Starting...')
print('Gate version: {}'.format(xmpp_tg.__version__))
print('Process pid: {}'.format(os.getpid()))
print('Using Telethon v{} and SleekXMPP v{}'.format(telethon.TelegramClient.__version__, sleekxmpp.__version__))
print()
gate = xmpp_tg.XMPPTelegram(CONFIG)
signal.signal(signal.SIGINT, gate.handle_interrupt)
gate.connect()
gate.process()