filebot/callbacks.py

126 lines
4.4 KiB
Python
Raw Normal View History

2022-10-06 15:44:35 +02:00
# -*- mode: python; indent-tabs-mode: nil; py-indent-offset: 4; coding: utf-8 -*-
2016-04-21 21:55:31 +02:00
from settings import Settings
from bot import Bot
2022-10-06 15:44:35 +02:00
from wrapper.toxcore_enums_and_consts import *
from wrapper.tox import bin_to_string
2016-04-21 21:55:31 +02:00
2022-10-06 15:44:35 +02:00
global LOG
import logging
LOG = logging.getLogger(__name__)
2016-04-21 21:55:31 +02:00
# -----------------------------------------------------------------------------------------------------------------
# Callbacks - current user
# -----------------------------------------------------------------------------------------------------------------
def self_connection_status():
"""
Current user changed connection status (offline, UDP, TCP)
"""
def wrapped(tox, connection, user_data):
2022-10-06 15:44:35 +02:00
LOG.debug('Connection status: ' + str(connection))
2016-04-21 21:55:31 +02:00
return wrapped
# -----------------------------------------------------------------------------------------------------------------
# Callbacks - friends
# -----------------------------------------------------------------------------------------------------------------
def friend_connection_status(tox, friend_num, new_status, user_data):
"""
Check friend's connection status (offline, udp, tcp)
"""
2022-10-06 15:44:35 +02:00
LOG.info("Friend #{} connected! Friend's status: {}".format(friend_num, new_status))
2016-04-21 21:55:31 +02:00
def friend_message():
"""
New message from friend
"""
def wrapped(tox, friend_number, message_type, message, size, user_data):
2022-10-06 15:44:35 +02:00
LOG.info(message.decode('utf-8'))
2016-04-21 21:55:31 +02:00
Bot.get_instance().new_message(friend_number, message.decode('utf-8'))
# parse message
return wrapped
def friend_request(tox, public_key, message, message_size, user_data):
"""
Called when user get new friend request
"""
profile = Bot.get_instance()
2016-04-26 12:09:39 +02:00
key = ''.join(chr(x) for x in public_key[:TOX_PUBLIC_KEY_SIZE])
tox_id = bin_to_string(key, TOX_PUBLIC_KEY_SIZE)
2016-04-21 21:55:31 +02:00
profile.process_friend_request(tox_id, message.decode('utf-8'))
# -----------------------------------------------------------------------------------------------------------------
# Callbacks - file transfers
# -----------------------------------------------------------------------------------------------------------------
def tox_file_recv(tox_link):
"""
New incoming file
"""
def wrapped(tox, friend_number, file_number, file_type, size, file_name, file_name_size, user_data):
profile = Bot.get_instance()
if file_type == TOX_FILE_KIND['DATA']:
2022-10-06 15:44:35 +02:00
LOG.info('file')
2016-04-21 21:55:31 +02:00
file_name = unicode(file_name[:file_name_size].decode('utf-8'))
profile.incoming_file_transfer(friend_number, file_number, size, file_name)
else: # AVATAR
tox_link.file_control(friend_number, file_number, TOX_FILE_CONTROL['CANCEL'])
return wrapped
def file_recv_chunk(tox, friend_number, file_number, position, chunk, length, user_data):
"""
Incoming chunk
"""
Bot.get_instance().incoming_chunk(
friend_number,
file_number,
position,
chunk[:length] if length else None)
def file_chunk_request(tox, friend_number, file_number, position, size, user_data):
"""
Outgoing chunk
"""
Bot.get_instance().outgoing_chunk(
friend_number,
file_number,
position,
size)
def file_recv_control(tox, friend_number, file_number, file_control, user_data):
"""
Friend cancelled, paused or resumed file transfer
"""
if file_control == TOX_FILE_CONTROL['CANCEL']:
Bot.get_instance().cancel_transfer(friend_number, file_number, True)
# -----------------------------------------------------------------------------------------------------------------
# Callbacks - initialization
# -----------------------------------------------------------------------------------------------------------------
def init_callbacks(tox):
"""
Initialization of all callbacks.
:param tox: tox instance
"""
2022-10-06 15:44:35 +02:00
tox.callback_self_connection_status(self_connection_status())
2016-04-21 21:55:31 +02:00
2022-10-06 15:44:35 +02:00
tox.callback_friend_message(friend_message())
tox.callback_friend_connection_status(friend_connection_status)
tox.callback_friend_request(friend_request)
2016-04-21 21:55:31 +02:00
2022-10-06 15:44:35 +02:00
tox.callback_file_recv(tox_file_recv(tox))
tox.callback_file_recv_chunk(file_recv_chunk)
tox.callback_file_chunk_request(file_chunk_request)
tox.callback_file_recv_control(file_recv_control)