126 lines
4.4 KiB
Python
126 lines
4.4 KiB
Python
# -*- mode: python; indent-tabs-mode: nil; py-indent-offset: 4; coding: utf-8 -*-
|
|
from settings import Settings
|
|
from bot import Bot
|
|
from wrapper.toxcore_enums_and_consts import *
|
|
from wrapper.tox import bin_to_string
|
|
|
|
global LOG
|
|
import logging
|
|
LOG = logging.getLogger(__name__)
|
|
|
|
# -----------------------------------------------------------------------------------------------------------------
|
|
# Callbacks - current user
|
|
# -----------------------------------------------------------------------------------------------------------------
|
|
|
|
|
|
def self_connection_status():
|
|
"""
|
|
Current user changed connection status (offline, UDP, TCP)
|
|
"""
|
|
def wrapped(tox, connection, user_data):
|
|
LOG.debug('Connection status: ' + str(connection))
|
|
return wrapped
|
|
|
|
|
|
# -----------------------------------------------------------------------------------------------------------------
|
|
# Callbacks - friends
|
|
# -----------------------------------------------------------------------------------------------------------------
|
|
|
|
|
|
def friend_connection_status(tox, friend_num, new_status, user_data):
|
|
"""
|
|
Check friend's connection status (offline, udp, tcp)
|
|
"""
|
|
LOG.info("Friend #{} connected! Friend's status: {}".format(friend_num, new_status))
|
|
|
|
|
|
def friend_message():
|
|
"""
|
|
New message from friend
|
|
"""
|
|
def wrapped(tox, friend_number, message_type, message, size, user_data):
|
|
LOG.info(message.decode('utf-8'))
|
|
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()
|
|
key = ''.join(chr(x) for x in public_key[:TOX_PUBLIC_KEY_SIZE])
|
|
tox_id = bin_to_string(key, TOX_PUBLIC_KEY_SIZE)
|
|
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']:
|
|
LOG.info('file')
|
|
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
|
|
"""
|
|
tox.callback_self_connection_status(self_connection_status())
|
|
|
|
tox.callback_friend_message(friend_message())
|
|
tox.callback_friend_connection_status(friend_connection_status)
|
|
tox.callback_friend_request(friend_request)
|
|
|
|
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)
|