More support for multiple identities.
This commit is contained in:
parent
edc5e16d8a
commit
e7ec533944
11 changed files with 516 additions and 121 deletions
|
|
@ -3,11 +3,14 @@
|
|||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <pwd.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <weechat/weechat-plugin.h>
|
||||
#include <tox/tox.h>
|
||||
|
||||
#include "tox-weechat.h"
|
||||
#include "tox-weechat-config.h"
|
||||
#include "tox-weechat-chats.h"
|
||||
#include "tox-weechat-tox-callbacks.h"
|
||||
#include "tox-weechat-utils.h"
|
||||
|
|
@ -17,32 +20,27 @@
|
|||
struct t_tox_weechat_identity *tox_weechat_identities = NULL;
|
||||
struct t_tox_weechat_identity *tox_weechat_last_identity = NULL;
|
||||
|
||||
/**
|
||||
* Return the default data file path for an identity name. Must be freed.
|
||||
*/
|
||||
char *
|
||||
tox_weechat_default_data_path(const char *name)
|
||||
tox_weechat_identity_data_file_path(struct t_tox_weechat_identity *identity)
|
||||
{
|
||||
const char *weechat_dir = weechat_info_get("weechat_dir", NULL);
|
||||
const char *tox_dir = "/tox/";
|
||||
// expand path
|
||||
const char *weechat_dir = weechat_info_get ("weechat_dir", NULL);
|
||||
const char *base_path = weechat_config_string(identity->options[TOX_WEECHAT_IDENTITY_OPTION_SAVEFILE]);
|
||||
char *home_expanded = weechat_string_replace(base_path, "%h", weechat_dir);
|
||||
char *full_path = weechat_string_replace(home_expanded, "%n", identity->name);
|
||||
free(home_expanded);
|
||||
|
||||
weechat_mkdir_home("tox", 0755);
|
||||
|
||||
int path_length = strlen(weechat_dir) + strlen(tox_dir) + strlen(name) + 1;
|
||||
char *tox_data_path = malloc(sizeof(*tox_data_path) + path_length);
|
||||
|
||||
strcpy(tox_data_path, weechat_dir);
|
||||
strcat(tox_data_path, tox_dir);
|
||||
strcat(tox_data_path, name);
|
||||
tox_data_path[path_length-1] = 0;
|
||||
|
||||
return tox_data_path;
|
||||
return full_path;
|
||||
}
|
||||
|
||||
int
|
||||
tox_weechat_load_data_file(Tox *tox, char *path)
|
||||
tox_weechat_load_identity_data_file(struct t_tox_weechat_identity *identity)
|
||||
{
|
||||
FILE *file = fopen(path, "r");
|
||||
char *full_path = tox_weechat_identity_data_file_path(identity);
|
||||
|
||||
FILE *file = fopen(full_path, "r");
|
||||
free(full_path);
|
||||
|
||||
if (file)
|
||||
{
|
||||
// get file size
|
||||
|
|
@ -56,8 +54,7 @@ tox_weechat_load_data_file(Tox *tox, char *path)
|
|||
fclose(file);
|
||||
|
||||
// try loading the data
|
||||
int status = tox_load(tox, data, size);
|
||||
|
||||
int status = tox_load(identity->tox, data, size);
|
||||
free(data);
|
||||
|
||||
return status;
|
||||
|
|
@ -67,15 +64,17 @@ tox_weechat_load_data_file(Tox *tox, char *path)
|
|||
}
|
||||
|
||||
int
|
||||
tox_weechat_save_data_file(Tox *tox, char *path)
|
||||
tox_weechat_save_identity_data_file(struct t_tox_weechat_identity *identity)
|
||||
{
|
||||
char *full_path = tox_weechat_identity_data_file_path(identity);
|
||||
|
||||
// save Tox data to a buffer
|
||||
uint32_t size = tox_size(tox);
|
||||
uint32_t size = tox_size(identity->tox);
|
||||
uint8_t *data = malloc(sizeof(*data) * size);
|
||||
tox_save(tox, data);
|
||||
tox_save(identity->tox, data);
|
||||
|
||||
// save buffer to a file
|
||||
FILE *file = fopen(path, "w");
|
||||
FILE *file = fopen(full_path, "w");
|
||||
if (file)
|
||||
{
|
||||
fwrite(data, sizeof(data[0]), size, file);
|
||||
|
|
@ -94,7 +93,7 @@ tox_weechat_identity_buffer_close_callback(void *data,
|
|||
struct t_tox_weechat_identity *identity = data;
|
||||
identity->buffer = NULL;
|
||||
|
||||
tox_weechat_identity_free(data);
|
||||
tox_weechat_identity_disconnect(identity);
|
||||
|
||||
return WEECHAT_RC_OK;
|
||||
}
|
||||
|
|
@ -124,7 +123,6 @@ tox_weechat_identity_new(const char *name)
|
|||
{
|
||||
struct t_tox_weechat_identity *identity = malloc(sizeof(*identity));
|
||||
identity->name = strdup(name);
|
||||
identity->data_file_path = tox_weechat_default_data_path(name);
|
||||
|
||||
// add to identity list
|
||||
identity->prev_identity= tox_weechat_last_identity;
|
||||
|
|
@ -138,31 +136,49 @@ tox_weechat_identity_new(const char *name)
|
|||
tox_weechat_last_identity = identity;
|
||||
|
||||
// set up internal vars
|
||||
identity->tox = NULL;
|
||||
identity->buffer = NULL;
|
||||
identity->tox_do_timer = NULL;
|
||||
identity->chats = identity->last_chat = NULL;
|
||||
|
||||
// TODO: load from disk
|
||||
identity->friend_requests = identity->last_friend_request = NULL;
|
||||
identity->friend_request_count = 0;
|
||||
|
||||
// set up config
|
||||
tox_weechat_config_init_identity(identity);
|
||||
|
||||
return identity;
|
||||
}
|
||||
|
||||
void
|
||||
tox_weechat_identity_connect(struct t_tox_weechat_identity *identity)
|
||||
{
|
||||
if (identity->tox)
|
||||
return;
|
||||
|
||||
// create main buffer
|
||||
identity->buffer = weechat_buffer_new(identity->name,
|
||||
NULL, NULL,
|
||||
tox_weechat_identity_buffer_close_callback, NULL);
|
||||
tox_weechat_identity_buffer_close_callback, identity);
|
||||
|
||||
// create Tox
|
||||
identity->tox = tox_new(NULL);
|
||||
|
||||
// try loading Tox saved data
|
||||
if (tox_weechat_load_data_file(identity->tox, identity->data_file_path) == -1)
|
||||
if (tox_weechat_load_identity_data_file(identity) == -1)
|
||||
{
|
||||
// couldn't load Tox, set a default name
|
||||
// we failed to load - set an initial name
|
||||
char *name;
|
||||
|
||||
struct passwd *user_pwd;
|
||||
if ((user_pwd = getpwuid(geteuid())))
|
||||
name = user_pwd->pw_name;
|
||||
else
|
||||
name = "Tox User";
|
||||
|
||||
tox_set_name(identity->tox,
|
||||
(uint8_t *)"WeeChatter", strlen("WeeChatter"));
|
||||
(uint8_t *)name, strlen(name));
|
||||
}
|
||||
|
||||
// bootstrap DHT
|
||||
|
|
@ -183,6 +199,60 @@ tox_weechat_identity_connect(struct t_tox_weechat_identity *identity)
|
|||
tox_callback_friend_request(identity->tox, tox_weechat_callback_friend_request, identity);
|
||||
}
|
||||
|
||||
void
|
||||
tox_weechat_identity_disconnect(struct t_tox_weechat_identity *identity)
|
||||
{
|
||||
// check that we're not already disconnected
|
||||
if (!identity->tox)
|
||||
return;
|
||||
|
||||
// save and kill tox
|
||||
int result = tox_weechat_save_identity_data_file(identity);
|
||||
tox_kill(identity->tox);
|
||||
identity->tox = NULL;
|
||||
|
||||
if (result == -1)
|
||||
{
|
||||
char *path = tox_weechat_identity_data_file_path(identity);
|
||||
weechat_printf(NULL,
|
||||
"%s%s: Could not save Tox identity %s to file: %s",
|
||||
weechat_prefix("error"),
|
||||
weechat_plugin->name,
|
||||
identity->name,
|
||||
path);
|
||||
free(path);
|
||||
}
|
||||
|
||||
// stop Tox timer
|
||||
weechat_unhook(identity->tox_do_timer);
|
||||
}
|
||||
|
||||
void
|
||||
tox_weechat_identity_autoconnect()
|
||||
{
|
||||
for (struct t_tox_weechat_identity *identity = tox_weechat_identities;
|
||||
identity;
|
||||
identity = identity->next_identity)
|
||||
{
|
||||
if (weechat_config_boolean(identity->options[TOX_WEECHAT_IDENTITY_OPTION_AUTOCONNECT]))
|
||||
tox_weechat_identity_connect(identity);
|
||||
}
|
||||
}
|
||||
|
||||
struct t_tox_weechat_identity *
|
||||
tox_weechat_identity_name_search(const char *name)
|
||||
{
|
||||
for (struct t_tox_weechat_identity *identity = tox_weechat_identities;
|
||||
identity;
|
||||
identity = identity->next_identity)
|
||||
{
|
||||
if (weechat_strcasecmp(identity->name, name) == 0)
|
||||
return identity;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct t_tox_weechat_identity *
|
||||
tox_weechat_identity_for_buffer(struct t_gui_buffer *buffer)
|
||||
{
|
||||
|
|
@ -208,6 +278,9 @@ tox_weechat_identity_for_buffer(struct t_gui_buffer *buffer)
|
|||
void
|
||||
tox_weechat_identity_free(struct t_tox_weechat_identity *identity)
|
||||
{
|
||||
// disconnect
|
||||
tox_weechat_identity_disconnect(identity);
|
||||
|
||||
// remove from list
|
||||
if (identity == tox_weechat_last_identity)
|
||||
tox_weechat_last_identity = identity->prev_identity;
|
||||
|
|
@ -220,24 +293,7 @@ tox_weechat_identity_free(struct t_tox_weechat_identity *identity)
|
|||
if (identity->next_identity)
|
||||
identity->next_identity->prev_identity = identity->prev_identity;
|
||||
|
||||
// save and kill tox
|
||||
int result = tox_weechat_save_data_file(identity->tox,
|
||||
identity->data_file_path);
|
||||
tox_kill(identity->tox);
|
||||
|
||||
if (result == -1)
|
||||
{
|
||||
weechat_printf(NULL,
|
||||
"%sCould not save Tox identity %s to file: %s",
|
||||
weechat_prefix("error"),
|
||||
identity->name,
|
||||
identity->data_file_path);
|
||||
}
|
||||
|
||||
if (identity->buffer)
|
||||
weechat_buffer_close(identity->buffer);
|
||||
|
||||
// TODO: free config, free friend reqs/chats
|
||||
// TODO: free more things
|
||||
|
||||
free(identity->name);
|
||||
free(identity);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue