Files
tg4xmpp/xmpp_tg/utils.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

76 lines
2.9 KiB
Python

"""
Различные полезные функции
"""
import types
from datetime import datetime
def display_tg_name(peer):
if hasattr(peer,'title') and hasattr(peer,'broadcast') and peer.broadcast: # channel
return '[C] ' + peer.title
elif hasattr(peer,'title') and hasattr(peer,'broadcast') and not peer.broadcast: # supergroup
return '[SG] ' + peer.title
elif hasattr(peer,'title'): # normal group
return '[G] ' + peer.title
elif peer.first_name and peer.last_name: # user with first and last name
return '{} {}'.format(peer.first_name, peer.last_name)
elif peer.first_name: # user with firstname only
return peer.first_name
elif peer.last_name: # user with lastname only
return peer.last_name
elif peer.username: # user with username only
return peer.username
else: # no match, unknown contact
return '[Unknown]'
def get_contact_jid(peer, gatejid):
if peer.id and hasattr(peer,'title') and hasattr(peer, 'broadcast') and peer.broadcast: # channel
return 'c' + str(peer.id) + '@' + gatejid
elif peer.id and hasattr(peer,'title') and hasattr(peer,'broadcast') and not peer.broadcast: # supergroup
return 's' + str(peer.id) + '@' + gatejid
elif peer.id and hasattr(peer,'title'): # normal group
return 'g' + str(peer.id) + '@' + gatejid
elif peer.id and not peer.bot: # it is... user?
return 'u' + str(peer.id) + '@' + gatejid
else:
return None
def var_dump(obj, depth=7, l=""):
# fall back to repr
if depth < 0 or type(obj) is datetime:
return repr(obj)
# expand/recurse dict
if isinstance(obj, dict):
name = ""
objdict = obj
else:
# if basic type, or list thereof, just print
canprint = lambda o: isinstance(o, (int, float, str, bool, type(None), types.LambdaType))
try:
if canprint(obj) or sum(not canprint(o) for o in obj) == 0:
return repr(obj)
except TypeError:
pass
# try to iterate as if obj were a list
try:
return "[\n" + "\n".join(l + var_dump(k, depth=depth - 1, l=l + " ") + "," for k in obj) + "\n" + l + "]"
except TypeError as e:
# else, expand/recurse object attribs
name = (hasattr(obj, '__class__') and obj.__class__.__name__ or type(obj).__name__)
objdict = {}
for a in dir(obj):
if a[:2] != "__" and (not hasattr(obj, a) or not hasattr(getattr(obj, a), '__call__')):
try:
objdict[a] = getattr(obj, a)
except Exception as e:
objdict[a] = str(e)
return name + "{\n" + "\n".join(l + repr(k) + ": " + var_dump(v, depth=depth - 1, l=l + " ") + "," for k, v in
objdict.items()) + "\n" + l + "}"