2016-02-16 20:29:18 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
2016-02-16 19:11:56 +01:00
|
|
|
from ctypes import *
|
|
|
|
from settings import Settings
|
2016-02-16 21:10:34 +01:00
|
|
|
from platform import system
|
2016-02-17 22:40:40 +01:00
|
|
|
from toxcore_enums import *
|
2016-02-16 21:30:43 +01:00
|
|
|
import os
|
2016-02-16 19:11:56 +01:00
|
|
|
|
2016-02-17 22:40:40 +01:00
|
|
|
|
2016-02-16 19:11:56 +01:00
|
|
|
class ToxOptions(Structure):
|
|
|
|
_fields_ = [
|
2016-02-17 22:40:40 +01:00
|
|
|
('ipv6_enabled', c_bool),
|
|
|
|
('udp_enabled', c_bool),
|
|
|
|
('proxy_type', c_int),
|
|
|
|
('proxy_host', c_char_p),
|
|
|
|
('proxy_port', c_uint16),
|
|
|
|
('start_port', c_uint16),
|
|
|
|
('end_port', c_uint16),
|
|
|
|
('tcp_port', c_uint16),
|
|
|
|
('savedata_type', c_int),
|
|
|
|
('savedata_data', c_char_p),
|
|
|
|
('savedata_length', c_size_t)
|
2016-02-16 19:11:56 +01:00
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
class Tox(object):
|
|
|
|
|
2016-02-16 20:29:18 +01:00
|
|
|
def __init__(self, name, path=None):
|
2016-02-17 22:40:40 +01:00
|
|
|
# load toxcore
|
2016-02-16 21:10:34 +01:00
|
|
|
if system() == 'Linux':
|
2016-02-16 21:30:43 +01:00
|
|
|
temp = os.path.dirname(os.path.abspath(__file__)) + '/libs/'
|
|
|
|
os.chdir(temp)
|
|
|
|
self.libtoxcore = CDLL(temp + 'libtoxcore.so')
|
2016-02-16 21:10:34 +01:00
|
|
|
elif system() == 'Windows':
|
2016-02-17 19:19:45 +01:00
|
|
|
self.libtoxcore = CDLL('libs/libtox.dll')
|
2016-02-16 20:29:18 +01:00
|
|
|
self.libtoxcore.tox_options_new.restype = POINTER(ToxOptions)
|
2016-02-17 22:40:40 +01:00
|
|
|
|
|
|
|
# load saved data
|
|
|
|
if path is None:
|
|
|
|
path = Settings.get_default_path()
|
|
|
|
full_path = path + name + '.tox'
|
|
|
|
with open(full_path, 'rb') as fl:
|
|
|
|
savedata = fl.read()
|
|
|
|
settings = Settings()
|
|
|
|
|
|
|
|
# creating tox options struct
|
|
|
|
tox_err = c_int()
|
|
|
|
self.tox_options = self.libtoxcore.tox_options_new(addressof(tox_err))
|
|
|
|
if tox_err == TOX_ERR_OPTIONS_NEW['TOX_ERR_OPTIONS_NEW_MALLOC']:
|
|
|
|
raise MemoryError('The function failed to allocate enough memory for the options struct.')
|
|
|
|
|
|
|
|
# filling tox options struct
|
|
|
|
self.tox_options.contents.ipv6_enabled = settings['ipv6_enabled']
|
|
|
|
self.tox_options.contents.udp_enabled = settings['udp_enabled']
|
|
|
|
self.tox_options.contents.proxy_type = settings['proxy_type']
|
|
|
|
self.tox_options.contents.proxy_host = settings['proxy_host']
|
|
|
|
self.tox_options.contents.proxy_port = settings['proxy_port']
|
|
|
|
self.tox_options.contents.start_port = settings['start_port']
|
|
|
|
self.tox_options.contents.end_port = settings['end_port']
|
|
|
|
self.tox_options.contents.tcp_port = settings['tcp_port']
|
|
|
|
self.tox_options.contents.savedata_type = TOX_SAVEDATA_TYPE['TOX_SAVEDATA_TYPE_TOX_SAVE']
|
|
|
|
self.tox_options.contents.savedata_data = c_char_p(savedata)
|
|
|
|
self.tox_options.contents.savedata_length = len(savedata)
|
|
|
|
|
|
|
|
# creating tox object
|
2016-02-16 20:29:18 +01:00
|
|
|
self.libtoxcore.tox_new.restype = POINTER(c_void_p)
|
2016-02-17 22:40:40 +01:00
|
|
|
self.tox_pointer = self.libtoxcore.tox_new(self.tox_options, None)
|
2016-02-16 20:29:18 +01:00
|
|
|
|
2016-02-16 19:11:56 +01:00
|
|
|
|
2016-02-17 22:40:40 +01:00
|
|
|
if __name__ == '__main__':
|
2016-02-16 19:11:56 +01:00
|
|
|
t = Tox('tox_save')
|