i18n.c opens catopen("dtchat", NL_CAT_LOCALE) and dc_text(id, fallback) reads set 1; the program calls dc_i18n_init after setlocale. The visible labels of the main window, the account editor, the request/peer/file/pair dialogs and the local status texts use ui_text.h ids; dtchat.msg (English) has 75 entries and programs/localized/de_DE.UTF-8/msg/dtchat.msg the German copy (ASCII transliteration, core font). test_i18n builds both catalogs with gencat and checks File/Datei, Send/Senden and the fallback; the German run under Xvfb shows Datei/Bearbeiten/Ansicht/Konversation/Hilfe, In der Naehe and Senden (screenshot build/mcde-shots/dtchat-t104-de.png) and the smoke stays green. Dynamic formatted statuses are partly still English. The in-tree build is pending: a foreign release chain holds the lock (pid 141722).
916 lines
24 KiB
C
916 lines
24 KiB
C
/*
|
|
* localui.c -- the local peer manager behind the UI (T5.8)
|
|
*
|
|
* License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
|
|
*
|
|
* Starts the listener, one reconnecting link per --peer, the pin store and the
|
|
* transfers. Peers appear in the Nearby section (DcPeers); their messages
|
|
* become conversations of the "local" account; the file hooks use the dialogs
|
|
* of ui_file.c. An unknown pin shows the 6-digit SAS: only an Accept stores
|
|
* the pin, a Reject (or a changed pin) blocks the peer.
|
|
*/
|
|
#define _POSIX_C_SOURCE 200809L
|
|
|
|
#include "localui.h"
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <time.h>
|
|
#include <unistd.h>
|
|
|
|
#include <Xm/Form.h>
|
|
#include <Xm/Label.h>
|
|
#include <Xm/PushB.h>
|
|
#include <Xm/RowColumn.h>
|
|
#include <Xm/Xm.h>
|
|
|
|
#include "ui_file.h"
|
|
#include "ui_text.h"
|
|
|
|
typedef struct Peer Peer;
|
|
struct Peer {
|
|
DtLocal *local;
|
|
char id[128];
|
|
char name[128];
|
|
char pin[128];
|
|
DcLocalConn *conn; /* accepted connection, or the link's conn */
|
|
DcLocalPeer *link;
|
|
DcFileSend *send;
|
|
char send_id[64];
|
|
DcFileRecv *recv;
|
|
char recv_id[64];
|
|
int blocked;
|
|
int paired;
|
|
Peer *next;
|
|
};
|
|
|
|
struct DtLocal {
|
|
DtChatUi *ui;
|
|
DcModel *model;
|
|
DcPeers *peers;
|
|
DcLoop *loop;
|
|
DcIdentity *identity;
|
|
DcPins *pins;
|
|
DcLocalServer *server;
|
|
char *config;
|
|
char *downloads;
|
|
Peer *state;
|
|
XtAppContext app;
|
|
XtIntervalId timer;
|
|
int seq;
|
|
};
|
|
|
|
typedef struct {
|
|
DtLocal *local;
|
|
Peer *peer;
|
|
DcJson *offer;
|
|
char id[64];
|
|
Widget shell;
|
|
} OfferCtx;
|
|
|
|
typedef struct {
|
|
DtLocal *local;
|
|
Peer *peer;
|
|
char id[128];
|
|
char pin[128];
|
|
char name[128];
|
|
Widget shell;
|
|
} PairCtx;
|
|
|
|
static void
|
|
status(DtLocal *local, const char *text)
|
|
{
|
|
dtchat_ui_set_status(local->ui, text);
|
|
}
|
|
|
|
/* A destroyed dialog leaves a hole under Xvfb (no window manager). */
|
|
static void
|
|
repaint_ui(DtLocal *local)
|
|
{
|
|
Widget shell = dtchat_ui_shell(local->ui);
|
|
|
|
if (shell != NULL && XtIsRealized(shell)) {
|
|
XClearArea(XtDisplay(shell), XtWindow(shell), 0, 0, 0, 0,
|
|
True);
|
|
XFlush(XtDisplay(shell));
|
|
}
|
|
}
|
|
|
|
static Peer *
|
|
peer_find_id(DtLocal *local, const char *id)
|
|
{
|
|
Peer *p;
|
|
|
|
for (p = local->state; p != NULL; p = p->next)
|
|
if (strcmp(p->id, id) == 0)
|
|
return p;
|
|
return NULL;
|
|
}
|
|
|
|
static Peer *
|
|
peer_find_conn(DtLocal *local, DcLocalConn *conn)
|
|
{
|
|
Peer *p;
|
|
|
|
for (p = local->state; p != NULL; p = p->next)
|
|
if (p->conn == conn)
|
|
return p;
|
|
return NULL;
|
|
}
|
|
|
|
static Peer *
|
|
peer_get(DtLocal *local, const char *id, const char *pin)
|
|
{
|
|
Peer *p = peer_find_id(local, id);
|
|
|
|
if (p != NULL)
|
|
return p;
|
|
p = calloc(1, sizeof(*p));
|
|
if (p == NULL)
|
|
return NULL;
|
|
p->local = local;
|
|
snprintf(p->id, sizeof(p->id), "%s", id);
|
|
snprintf(p->name, sizeof(p->name), "%.12s", id);
|
|
if (pin != NULL)
|
|
snprintf(p->pin, sizeof(p->pin), "%s", pin);
|
|
p->next = local->state;
|
|
local->state = p;
|
|
return p;
|
|
}
|
|
|
|
static void
|
|
announce(DtLocal *local, Peer *p, int online)
|
|
{
|
|
char txt[512];
|
|
DcPeer *dp;
|
|
|
|
snprintf(txt, sizeof(txt), "v=1;id=%s;name=%s;pin=%s", p->id, p->name,
|
|
p->pin);
|
|
(void)dc_peers_announce(local->peers, txt, "", 0);
|
|
dp = dc_peers_find(local->peers, p->id);
|
|
if (dp != NULL) {
|
|
dc_peer_set_blocked(dp, p->blocked);
|
|
if (!online)
|
|
(void)dc_peers_offline(local->peers, p->id);
|
|
}
|
|
dtchat_ui_set_peers(local->ui, local->peers);
|
|
}
|
|
|
|
static void
|
|
ensure_conv(DtLocal *local, Peer *p)
|
|
{
|
|
(void)dc_model_add_account(local->model, "local", "local",
|
|
dc_text(TX_LOCAL_PEERS, "Local peers"));
|
|
(void)dc_model_add_conv(local->model, "local", p->id, DC_CONV_DIRECT,
|
|
p->name);
|
|
}
|
|
|
|
static void
|
|
add_message(DtLocal *local, Peer *p, const char *from, const char *text,
|
|
int flags)
|
|
{
|
|
DcConv *conv;
|
|
|
|
ensure_conv(local, p);
|
|
conv = dc_model_find_conv(local->model, "local", p->id);
|
|
if (conv == NULL)
|
|
return;
|
|
(void)dc_conv_add_message(conv, from, text, (long long)time(NULL),
|
|
flags);
|
|
dtchat_ui_message_added(local->ui, "local", p->id);
|
|
}
|
|
|
|
/* ------------------------------- pairing -------------------------------- */
|
|
|
|
static void
|
|
pair_accept(PairCtx *ctx)
|
|
{
|
|
Peer *p;
|
|
|
|
if (dc_pins_confirm(ctx->local->pins, ctx->id, ctx->pin) != 0)
|
|
return;
|
|
(void)dc_pins_save(ctx->local->pins, ctx->local->config);
|
|
p = peer_find_id(ctx->local, ctx->id);
|
|
if (p == NULL)
|
|
p = peer_get(ctx->local, ctx->id, ctx->pin);
|
|
if (p != NULL) {
|
|
snprintf(p->name, sizeof(p->name), "%s",
|
|
ctx->name[0] != '\0' ? ctx->name : p->name);
|
|
snprintf(p->pin, sizeof(p->pin), "%s", ctx->pin);
|
|
p->blocked = 0;
|
|
p->paired = 1;
|
|
announce(ctx->local, p, 1);
|
|
}
|
|
status(ctx->local, dc_text(TX_PEER_PAIRED, "peer paired"));
|
|
XtDestroyWidget(ctx->shell);
|
|
repaint_ui(ctx->local);
|
|
free(ctx);
|
|
}
|
|
|
|
static void
|
|
pair_reject(PairCtx *ctx)
|
|
{
|
|
Peer *p;
|
|
|
|
(void)dc_pins_set_blocked(ctx->local->pins, ctx->id, 1);
|
|
(void)dc_pins_save(ctx->local->pins, ctx->local->config);
|
|
p = peer_find_id(ctx->local, ctx->id);
|
|
if (p != NULL) {
|
|
p->blocked = 1;
|
|
if (p->link != NULL) {
|
|
dc_local_peer_stop(p->link);
|
|
p->conn = NULL;
|
|
} else if (p->conn != NULL) {
|
|
dc_local_conn_close(p->conn);
|
|
p->conn = NULL;
|
|
}
|
|
announce(ctx->local, p, 0);
|
|
}
|
|
status(ctx->local, dc_text(TX_PEER_BLOCKED, "peer blocked"));
|
|
XtDestroyWidget(ctx->shell);
|
|
repaint_ui(ctx->local);
|
|
free(ctx);
|
|
}
|
|
|
|
static void
|
|
pair_accept_cb(Widget w, XtPointer client, XtPointer call)
|
|
{
|
|
(void)w;
|
|
(void)call;
|
|
pair_accept(client);
|
|
}
|
|
|
|
static void
|
|
pair_reject_cb(Widget w, XtPointer client, XtPointer call)
|
|
{
|
|
(void)w;
|
|
(void)call;
|
|
pair_reject(client);
|
|
}
|
|
|
|
static void
|
|
pair_dialog(DtLocal *local, const char *name, const char *id,
|
|
const char *pin)
|
|
{
|
|
PairCtx *ctx = calloc(1, sizeof(*ctx));
|
|
Widget form, label, buttons, accept, reject;
|
|
char code_line[128];
|
|
char *code;
|
|
|
|
if (ctx == NULL)
|
|
return;
|
|
ctx->local = local;
|
|
snprintf(ctx->id, sizeof(ctx->id), "%s", id);
|
|
snprintf(ctx->pin, sizeof(ctx->pin), "%s", pin);
|
|
snprintf(ctx->name, sizeof(ctx->name), "%s", name);
|
|
code = dc_pair_sas(dc_identity_pin(local->identity), pin);
|
|
snprintf(code_line, sizeof(code_line), "%s", code != NULL ? code :
|
|
"------");
|
|
free(code);
|
|
form = XmCreateFormDialog(dtchat_ui_shell(local->ui), "pair", NULL, 0);
|
|
if (form == NULL) {
|
|
free(ctx);
|
|
return;
|
|
}
|
|
ctx->shell = XtParent(form);
|
|
XtVaSetValues(ctx->shell, XmNtitle,
|
|
(char *)dc_text(TX_PAIR_PEER, "Pair peer"), XmNwidth, 380,
|
|
XmNheight, 210, XmNdeleteResponse, XmDESTROY, NULL);
|
|
label = XmCreateLabel(form, "who", NULL, 0);
|
|
{
|
|
char line[512];
|
|
XmString x;
|
|
|
|
snprintf(line, sizeof(line),
|
|
dc_text(TX_WANTS_PAIR, "%s wants to pair. Compare this "
|
|
"code on both machines:"), name);
|
|
x = XmStringCreateLocalized(line);
|
|
XtVaSetValues(label, XmNlabelString, x,
|
|
XmNtopAttachment, XmATTACH_FORM,
|
|
XmNleftAttachment, XmATTACH_FORM,
|
|
XmNrightAttachment, XmATTACH_FORM,
|
|
XmNmarginTop, 10, XmNmarginLeft, 8,
|
|
XmNalignment, XmALIGNMENT_BEGINNING, NULL);
|
|
XmStringFree(x);
|
|
}
|
|
XtManageChild(label);
|
|
{
|
|
Widget big = XmCreateLabel(form, "code", NULL, 0);
|
|
XmString x = XmStringCreateLocalized(code_line);
|
|
|
|
XtVaSetValues(big, XmNlabelString, x,
|
|
XmNtopAttachment, XmATTACH_WIDGET, XmNtopWidget, label,
|
|
XmNleftAttachment, XmATTACH_FORM,
|
|
XmNrightAttachment, XmATTACH_FORM,
|
|
XmNtopOffset, 14, XmNmarginTop, 6, NULL);
|
|
XmStringFree(x);
|
|
XtManageChild(big);
|
|
}
|
|
buttons = XtVaCreateManagedWidget("buttons", xmRowColumnWidgetClass,
|
|
form, XmNorientation, XmHORIZONTAL, XmNspacing, 8,
|
|
XmNbottomAttachment, XmATTACH_FORM,
|
|
XmNleftAttachment, XmATTACH_FORM,
|
|
XmNrightAttachment, XmATTACH_FORM,
|
|
XmNmarginHeight, 8, NULL);
|
|
reject = XmCreatePushButton(buttons, "reject", NULL, 0);
|
|
{
|
|
XmString x = XmStringCreateLocalized((char *)dc_text(TX_REJECT,
|
|
"Reject"));
|
|
|
|
XtVaSetValues(reject, XmNlabelString, x, NULL);
|
|
XmStringFree(x);
|
|
}
|
|
XtAddCallback(reject, XmNactivateCallback, pair_reject_cb, ctx);
|
|
XtManageChild(reject);
|
|
accept = XmCreatePushButton(buttons, "accept", NULL, 0);
|
|
{
|
|
XmString x = XmStringCreateLocalized((char *)dc_text(
|
|
TX_CODES_MATCH, "Codes match"));
|
|
|
|
XtVaSetValues(accept, XmNlabelString, x, NULL);
|
|
XmStringFree(x);
|
|
}
|
|
XtAddCallback(accept, XmNactivateCallback, pair_accept_cb, ctx);
|
|
XtManageChild(accept);
|
|
XtManageChild(form);
|
|
XtVaSetValues(ctx->shell, XmNwidth, 380, XmNheight, 210, NULL);
|
|
XtPopup(ctx->shell, XtGrabNone);
|
|
}
|
|
|
|
/* ----------------------------- file receive ----------------------------- */
|
|
|
|
static void
|
|
offer_accept(OfferCtx *ctx)
|
|
{
|
|
char err[256];
|
|
const char *id = dc_json_string(dc_json_get(ctx->offer, "id"));
|
|
DcJson *acc;
|
|
|
|
ctx->peer->recv = dc_file_recv_new(ctx->local->downloads, ctx->offer,
|
|
err, sizeof(err));
|
|
if (ctx->peer->recv == NULL) {
|
|
status(ctx->local, err);
|
|
} else {
|
|
snprintf(ctx->peer->recv_id, sizeof(ctx->peer->recv_id), "%s",
|
|
id != NULL ? id : ctx->id);
|
|
acc = dc_json_new_object();
|
|
(void)dc_json_object_set(acc, "type",
|
|
dc_json_new_string("file-accept"));
|
|
(void)dc_json_object_set(acc, "id",
|
|
dc_json_new_string(ctx->peer->recv_id));
|
|
(void)dc_local_conn_send_json(ctx->peer->conn, acc);
|
|
dc_json_free(acc);
|
|
status(ctx->local, dc_text(TX_RECEIVING, "receiving file"));
|
|
}
|
|
XtDestroyWidget(ctx->shell);
|
|
repaint_ui(ctx->local);
|
|
dc_json_free(ctx->offer);
|
|
free(ctx);
|
|
}
|
|
|
|
static void
|
|
offer_reject(OfferCtx *ctx)
|
|
{
|
|
DcJson *rej = dc_json_new_object();
|
|
const char *id = dc_json_string(dc_json_get(ctx->offer, "id"));
|
|
|
|
(void)dc_json_object_set(rej, "type",
|
|
dc_json_new_string("file-reject"));
|
|
(void)dc_json_object_set(rej, "id",
|
|
dc_json_new_string(id != NULL ? id : ""));
|
|
(void)dc_json_object_set(rej, "reason",
|
|
dc_json_new_string("rejected"));
|
|
if (ctx->peer->conn != NULL)
|
|
(void)dc_local_conn_send_json(ctx->peer->conn, rej);
|
|
dc_json_free(rej);
|
|
XtDestroyWidget(ctx->shell);
|
|
repaint_ui(ctx->local);
|
|
dc_json_free(ctx->offer);
|
|
free(ctx);
|
|
}
|
|
|
|
static void
|
|
offer_accept_cb(Widget w, XtPointer client, XtPointer call)
|
|
{
|
|
(void)w;
|
|
(void)call;
|
|
offer_accept(client);
|
|
}
|
|
|
|
static void
|
|
offer_reject_cb(Widget w, XtPointer client, XtPointer call)
|
|
{
|
|
(void)w;
|
|
(void)call;
|
|
offer_reject(client);
|
|
}
|
|
|
|
static void
|
|
file_offer(DtLocal *local, Peer *peer, const DcJson *json)
|
|
{
|
|
const char *name = dc_json_string(dc_json_get(json, "name"));
|
|
long long size = dc_json_int(dc_json_get(json, "size"));
|
|
OfferCtx *ctx = calloc(1, sizeof(*ctx));
|
|
const char *id = dc_json_string(dc_json_get(json, "id"));
|
|
Widget form, label, buttons, accept, reject;
|
|
char line[1024];
|
|
char human[64];
|
|
|
|
if (ctx == NULL)
|
|
return;
|
|
ctx->local = local;
|
|
ctx->peer = peer;
|
|
/* keep the fields dc_file_recv_new() needs */
|
|
ctx->offer = dc_json_new_object();
|
|
(void)dc_json_object_set(ctx->offer, "type",
|
|
dc_json_new_string("file-offer"));
|
|
(void)dc_json_object_set(ctx->offer, "id",
|
|
dc_json_new_string(id != NULL ? id : ""));
|
|
(void)dc_json_object_set(ctx->offer, "name",
|
|
dc_json_new_string(name != NULL ? name : "file"));
|
|
(void)dc_json_object_set(ctx->offer, "size", dc_json_new_int(size));
|
|
(void)dc_json_object_set(ctx->offer, "sha256",
|
|
dc_json_new_string(dc_json_string(dc_json_get(json, "sha256"))));
|
|
if (ctx->offer == NULL) {
|
|
free(ctx);
|
|
return;
|
|
}
|
|
if (size >= 1024 * 1024)
|
|
snprintf(human, sizeof(human), "%.1f MB",
|
|
(double)size / (1024.0 * 1024));
|
|
else
|
|
snprintf(human, sizeof(human), "%lld bytes", size);
|
|
form = XmCreateFormDialog(dtchat_ui_shell(local->ui), "offer", NULL,
|
|
0);
|
|
if (form == NULL) {
|
|
dc_json_free(ctx->offer);
|
|
free(ctx);
|
|
return;
|
|
}
|
|
ctx->shell = XtParent(form);
|
|
XtVaSetValues(ctx->shell, XmNtitle,
|
|
(char *)dc_text(TX_INCOMING_FILE, "Incoming file"), XmNwidth, 420,
|
|
XmNheight, 180, NULL);
|
|
snprintf(line, sizeof(line),
|
|
dc_text(TX_OFFERS, "%s offers %s (%s)"), peer->name,
|
|
name != NULL ? name : "a file", human);
|
|
label = XmCreateLabel(form, "name", NULL, 0);
|
|
{
|
|
XmString x = XmStringCreateLocalized(line);
|
|
|
|
XtVaSetValues(label, XmNlabelString, x,
|
|
XmNtopAttachment, XmATTACH_FORM,
|
|
XmNleftAttachment, XmATTACH_FORM,
|
|
XmNrightAttachment, XmATTACH_FORM,
|
|
XmNmarginTop, 12, XmNmarginLeft, 8,
|
|
XmNalignment, XmALIGNMENT_BEGINNING, NULL);
|
|
XmStringFree(x);
|
|
}
|
|
XtManageChild(label);
|
|
buttons = XtVaCreateManagedWidget("buttons", xmRowColumnWidgetClass,
|
|
form, XmNorientation, XmHORIZONTAL, XmNspacing, 8,
|
|
XmNbottomAttachment, XmATTACH_FORM,
|
|
XmNleftAttachment, XmATTACH_FORM,
|
|
XmNrightAttachment, XmATTACH_FORM, XmNmarginHeight, 8, NULL);
|
|
reject = XmCreatePushButton(buttons, "reject", NULL, 0);
|
|
{
|
|
XmString x = XmStringCreateLocalized((char *)dc_text(TX_REJECT,
|
|
"Reject"));
|
|
|
|
XtVaSetValues(reject, XmNlabelString, x, NULL);
|
|
XmStringFree(x);
|
|
}
|
|
XtAddCallback(reject, XmNactivateCallback, offer_reject_cb, ctx);
|
|
XtManageChild(reject);
|
|
accept = XmCreatePushButton(buttons, "accept", NULL, 0);
|
|
{
|
|
XmString x = XmStringCreateLocalized((char *)dc_text(TX_ACCEPT,
|
|
"Accept"));
|
|
|
|
XtVaSetValues(accept, XmNlabelString, x, NULL);
|
|
XmStringFree(x);
|
|
}
|
|
XtAddCallback(accept, XmNactivateCallback, offer_accept_cb, ctx);
|
|
XtManageChild(accept);
|
|
XtManageChild(form);
|
|
XtVaSetValues(ctx->shell, XmNwidth, 420, XmNheight, 180, NULL);
|
|
XtPopup(ctx->shell, XtGrabNone);
|
|
}
|
|
|
|
/* ------------------------------ message lines --------------------------- */
|
|
|
|
static void
|
|
handle_line(DtLocal *local, Peer *peer, DcLocalConn *conn, const DcJson *json)
|
|
{
|
|
const char *type = dc_json_string(dc_json_get(json, "type"));
|
|
const char *id = dc_json_string(dc_json_get(json, "id"));
|
|
|
|
if (type == NULL || peer == NULL)
|
|
return;
|
|
if (!peer->paired &&
|
|
(strcmp(type, "file-offer") == 0 ||
|
|
strcmp(type, "file-data") == 0 ||
|
|
strcmp(type, "file-end") == 0 ||
|
|
strcmp(type, "file-cancel") == 0 ||
|
|
strcmp(type, "file-accept") == 0 ||
|
|
strcmp(type, "file-done") == 0)) {
|
|
if (strcmp(type, "file-offer") == 0) {
|
|
DcJson *rej = dc_json_new_object();
|
|
|
|
(void)dc_json_object_set(rej, "type",
|
|
dc_json_new_string("file-reject"));
|
|
(void)dc_json_object_set(rej, "id",
|
|
dc_json_new_string(dc_json_string(
|
|
dc_json_get(json, "id"))));
|
|
(void)dc_json_object_set(rej, "reason",
|
|
dc_json_new_string("pairing required"));
|
|
if (conn != NULL)
|
|
(void)dc_local_conn_send_json(conn, rej);
|
|
dc_json_free(rej);
|
|
status(local, "file offer refused: peer is not paired");
|
|
}
|
|
return;
|
|
}
|
|
if (strcmp(type, "msg") == 0) {
|
|
const char *text = dc_json_string(dc_json_get(json, "text"));
|
|
|
|
add_message(local, peer, peer->name, text != NULL ? text : "", 0);
|
|
if (conn != NULL)
|
|
(void)dc_local_send_msg_ack(conn, dc_json_string(
|
|
dc_json_get(json, "client_id")));
|
|
} else if (strcmp(type, "file-offer") == 0) {
|
|
file_offer(local, peer, json);
|
|
} else if (strcmp(type, "file-data") == 0) {
|
|
if (peer->recv != NULL && id != NULL &&
|
|
strcmp(id, peer->recv_id) == 0)
|
|
(void)dc_file_recv_data(peer->recv,
|
|
dc_json_string(dc_json_get(json, "data")),
|
|
NULL, 0);
|
|
} else if (strcmp(type, "file-end") == 0) {
|
|
if (peer->recv != NULL) {
|
|
char err[256];
|
|
int ok = dc_file_recv_end(peer->recv, err,
|
|
sizeof(err)) == 0;
|
|
DcJson *done = dc_json_new_object();
|
|
char line[512];
|
|
|
|
(void)dc_json_object_set(done, "type",
|
|
dc_json_new_string("file-done"));
|
|
(void)dc_json_object_set(done, "id",
|
|
dc_json_new_string(peer->recv_id));
|
|
(void)dc_json_object_set(done, "ok",
|
|
dc_json_new_bool(ok));
|
|
if (conn != NULL)
|
|
(void)dc_local_conn_send_json(conn, done);
|
|
dc_json_free(done);
|
|
if (ok) {
|
|
snprintf(line, sizeof(line),
|
|
dc_text(TX_SAVED_TO, "saved to %s"),
|
|
dc_file_recv_path(peer->recv));
|
|
status(local, line);
|
|
} else {
|
|
status(local,
|
|
dc_text(TX_FILE_CHECK_FAILED,
|
|
"file check failed"));
|
|
}
|
|
dc_file_recv_cancel(peer->recv);
|
|
peer->recv = NULL;
|
|
}
|
|
} else if (strcmp(type, "file-cancel") == 0) {
|
|
if (peer->recv != NULL) {
|
|
dc_file_recv_cancel(peer->recv);
|
|
peer->recv = NULL;
|
|
}
|
|
if (peer->send != NULL) {
|
|
dc_file_send_free(peer->send);
|
|
peer->send = NULL;
|
|
}
|
|
} else if (strcmp(type, "file-accept") == 0) {
|
|
if (peer->send != NULL && id != NULL &&
|
|
strcmp(id, peer->send_id) == 0)
|
|
dc_file_send_start(peer->send);
|
|
} else if (strcmp(type, "file-done") == 0) {
|
|
if (peer->send != NULL) {
|
|
dc_file_send_free(peer->send);
|
|
peer->send = NULL;
|
|
status(local, dc_text(TX_FILE_SENT, "file sent"));
|
|
}
|
|
} else if (strcmp(type, "typing") == 0) {
|
|
status(local, dc_json_bool(dc_json_get(json, "on")) ?
|
|
"peer is typing" : "peer stopped typing");
|
|
}
|
|
}
|
|
|
|
/* ------------------------------- callbacks ------------------------------ */
|
|
|
|
static void
|
|
server_hello(DcLocalConn *conn, const DcJson *hello, void *arg)
|
|
{
|
|
DtLocal *local = arg;
|
|
const char *id = dc_json_string(dc_json_get(hello, "id"));
|
|
const char *pin = dc_json_string(dc_json_get(hello, "pin"));
|
|
Peer *peer;
|
|
int known;
|
|
|
|
if (id == NULL || pin == NULL)
|
|
return;
|
|
known = dc_pins_check(local->pins, id, pin);
|
|
peer = peer_get(local, id, pin);
|
|
if (peer == NULL)
|
|
return;
|
|
peer->conn = conn;
|
|
snprintf(peer->pin, sizeof(peer->pin), "%s", pin);
|
|
if (known == 1) {
|
|
peer->paired = 1;
|
|
announce(local, peer, 1);
|
|
} else if (known == 0) {
|
|
pair_dialog(local, peer->name, id, pin);
|
|
} else {
|
|
peer->blocked = 1;
|
|
(void)dc_pins_set_blocked(local->pins, id, 1);
|
|
(void)dc_pins_save(local->pins, local->config);
|
|
announce(local, peer, 0);
|
|
dc_local_conn_close(conn);
|
|
peer->conn = NULL;
|
|
}
|
|
}
|
|
|
|
static void
|
|
server_line(DcLocalConn *conn, const DcJson *json, void *arg)
|
|
{
|
|
DtLocal *local = arg;
|
|
|
|
handle_line(local, peer_find_conn(local, conn), conn, json);
|
|
}
|
|
|
|
static void
|
|
server_closed(DcLocalConn *conn, void *arg)
|
|
{
|
|
DtLocal *local = arg;
|
|
Peer *peer = peer_find_conn(local, conn);
|
|
|
|
if (peer != NULL) {
|
|
peer->conn = NULL;
|
|
announce(local, peer, 0);
|
|
}
|
|
}
|
|
|
|
static void
|
|
link_line(DcLocalPeer *link, const DcJson *json, void *arg)
|
|
{
|
|
Peer *peer = arg;
|
|
|
|
handle_line(peer->local, peer, dc_local_peer_conn(link), json);
|
|
}
|
|
|
|
static void
|
|
link_state(DcLocalPeer *link, int online, void *arg)
|
|
{
|
|
Peer *peer = arg;
|
|
DtLocal *local = peer->local;
|
|
DcLocalConn *conn = dc_local_peer_conn(link);
|
|
const char *id = conn != NULL ? dc_local_conn_peer_id(conn) : NULL;
|
|
const char *pin = conn != NULL ? dc_local_conn_peer_pin(conn) : NULL;
|
|
int known;
|
|
|
|
if (!online) {
|
|
peer->conn = NULL;
|
|
announce(local, peer, 0);
|
|
return;
|
|
}
|
|
if (id == NULL || pin == NULL)
|
|
return;
|
|
snprintf(peer->id, sizeof(peer->id), "%s", id);
|
|
snprintf(peer->pin, sizeof(peer->pin), "%s", pin);
|
|
peer->conn = conn;
|
|
known = dc_pins_check(local->pins, id, pin);
|
|
if (known == 1) {
|
|
peer->paired = 1;
|
|
announce(local, peer, 1);
|
|
} else if (known == 0) {
|
|
pair_dialog(local, peer->name, id, pin);
|
|
} else {
|
|
peer->blocked = 1;
|
|
(void)dc_pins_set_blocked(local->pins, id, 1);
|
|
(void)dc_pins_save(local->pins, local->config);
|
|
announce(local, peer, 0);
|
|
dc_local_peer_stop(link);
|
|
peer->conn = NULL;
|
|
}
|
|
}
|
|
|
|
/* ---------------------------- send hooks -------------------------------- */
|
|
|
|
static int
|
|
local_send(void *arg, const char *conv, const char *text)
|
|
{
|
|
DtLocal *local = arg;
|
|
Peer *peer = peer_find_id(local, conv);
|
|
|
|
if (peer == NULL || peer->conn == NULL)
|
|
return -1;
|
|
if (dc_local_send_msg(peer->conn, text, NULL) != 0)
|
|
return -1;
|
|
add_message(local, peer, "me", text, DTCHAT_MSG_SELF);
|
|
return 0;
|
|
}
|
|
|
|
static void
|
|
local_file_send(const char *peer_id, const char *path, void *arg)
|
|
{
|
|
DtLocal *local = arg;
|
|
Peer *peer = peer_find_id(local, peer_id);
|
|
char err[256];
|
|
|
|
if (peer == NULL || peer->conn == NULL) {
|
|
status(local, "peer is offline");
|
|
return;
|
|
}
|
|
if (!peer->paired) {
|
|
status(local, dc_text(TX_PEER_NOT_PAIRED,
|
|
"peer is not paired"));
|
|
return;
|
|
}
|
|
snprintf(peer->send_id, sizeof(peer->send_id), "f%d", ++local->seq);
|
|
peer->send = dc_file_send_new(peer->conn, peer->send_id, path, err,
|
|
sizeof(err));
|
|
if (peer->send == NULL) {
|
|
status(local, err);
|
|
return;
|
|
}
|
|
status(local, "file offered");
|
|
}
|
|
|
|
/* ------------------------------- the timer ------------------------------ */
|
|
|
|
static void
|
|
pump_cb(XtPointer client, XtIntervalId *id)
|
|
{
|
|
DtLocal *local = client;
|
|
Peer *peer;
|
|
|
|
(void)id;
|
|
(void)dc_loop_run(local->loop, 0);
|
|
for (peer = local->state; peer != NULL; peer = peer->next) {
|
|
int rc;
|
|
|
|
if (peer->send == NULL)
|
|
continue;
|
|
rc = dc_file_send_step(peer->send);
|
|
if (rc == 1) {
|
|
dc_file_send_free(peer->send);
|
|
peer->send = NULL;
|
|
status(local, dc_text(TX_FILE_SENT, "file sent"));
|
|
}
|
|
}
|
|
local->timer = XtAppAddTimeOut(local->app, 40, pump_cb, local);
|
|
}
|
|
|
|
/* ------------------------------- start/stop ----------------------------- */
|
|
|
|
static int
|
|
split_host_port(const char *text, char *host, size_t hostlen, int *port)
|
|
{
|
|
const char *colon = strrchr(text, ':');
|
|
char *end = NULL;
|
|
long value;
|
|
|
|
if (colon == NULL) {
|
|
snprintf(host, hostlen, "127.0.0.1");
|
|
value = strtol(text, &end, 10);
|
|
} else {
|
|
size_t len = (size_t)(colon - text);
|
|
|
|
if (len >= hostlen)
|
|
return -1;
|
|
memcpy(host, text, len);
|
|
host[len] = '\0';
|
|
if (host[0] == '\0')
|
|
snprintf(host, hostlen, "127.0.0.1");
|
|
value = strtol(colon + 1, &end, 10);
|
|
}
|
|
if (end == NULL || *end != '\0' || value < 0 || value > 65535)
|
|
return -1;
|
|
*port = (int)value;
|
|
return 0;
|
|
}
|
|
|
|
DtLocal *
|
|
dtchat_local_start(DtChatUi *ui, DcModel *model, DcPeers *peers,
|
|
const char *config, const char *listen_text, const char *const *peer_text,
|
|
int npeers, const char *downloads)
|
|
{
|
|
DtLocal *local;
|
|
char host[128] = "127.0.0.1";
|
|
int port = 0;
|
|
int i;
|
|
|
|
if (ui == NULL || model == NULL || peers == NULL || config == NULL)
|
|
return NULL;
|
|
local = calloc(1, sizeof(*local));
|
|
if (local == NULL)
|
|
return NULL;
|
|
local->ui = ui;
|
|
local->model = model;
|
|
local->peers = peers;
|
|
local->app = XtWidgetToApplicationContext(dtchat_ui_shell(ui));
|
|
local->identity = dc_identity_load_or_create(config);
|
|
local->pins = dc_pins_load(config);
|
|
if (local->identity == NULL || local->pins == NULL) {
|
|
status(local, "local: cannot load the identity");
|
|
dtchat_local_stop(local);
|
|
return NULL;
|
|
}
|
|
local->config = strdup(config);
|
|
local->downloads = strdup(downloads != NULL ? downloads : config);
|
|
if (local->config == NULL || local->downloads == NULL) {
|
|
dtchat_local_stop(local);
|
|
return NULL;
|
|
}
|
|
local->loop = dc_loop_new();
|
|
if (local->loop == NULL) {
|
|
dtchat_local_stop(local);
|
|
return NULL;
|
|
}
|
|
if (listen_text != NULL) {
|
|
if (split_host_port(listen_text, host, sizeof(host), &port) !=
|
|
0) {
|
|
status(local, "local: bad --listen");
|
|
dtchat_local_stop(local);
|
|
return NULL;
|
|
}
|
|
local->server = dc_local_server_new(local->loop,
|
|
local->identity, host, port, NULL, NULL, server_hello,
|
|
server_line, server_closed, local);
|
|
if (local->server == NULL) {
|
|
status(local, "local: cannot listen");
|
|
dtchat_local_stop(local);
|
|
return NULL;
|
|
}
|
|
}
|
|
for (i = 0; i < npeers; i++) {
|
|
Peer *peer;
|
|
char phost[128];
|
|
int pport;
|
|
|
|
if (split_host_port(peer_text[i], phost, sizeof(phost),
|
|
&pport) != 0 || pport <= 0)
|
|
continue;
|
|
peer = peer_get(local, peer_text[i], "");
|
|
if (peer == NULL)
|
|
continue;
|
|
if (peer->link != NULL)
|
|
continue;
|
|
peer->link = dc_local_peer_new(local->loop, local->identity,
|
|
phost, pport, NULL, link_line, link_state, peer);
|
|
if (peer->link != NULL)
|
|
(void)dc_local_peer_start(peer->link);
|
|
}
|
|
dtchat_ui_set_send_hook(ui, local_send, local);
|
|
dtchat_ui_set_file_send(ui, local_file_send, local);
|
|
dtchat_ui_set_peers(ui, peers);
|
|
{
|
|
char line[256];
|
|
|
|
snprintf(line, sizeof(line),
|
|
"local: listening on port %d, %d peer link(s)",
|
|
local->server != NULL ?
|
|
dc_local_server_port(local->server) : 0, npeers);
|
|
status(local, line);
|
|
}
|
|
local->timer = XtAppAddTimeOut(local->app, 40, pump_cb, local);
|
|
return local;
|
|
}
|
|
|
|
void
|
|
dtchat_local_stop(DtLocal *local)
|
|
{
|
|
Peer *peer;
|
|
|
|
if (local == NULL)
|
|
return;
|
|
if (local->timer != 0) {
|
|
(void)XtRemoveTimeOut(local->timer);
|
|
local->timer = 0;
|
|
}
|
|
while ((peer = local->state) != NULL) {
|
|
local->state = peer->next;
|
|
if (peer->send != NULL)
|
|
dc_file_send_free(peer->send);
|
|
if (peer->recv != NULL)
|
|
dc_file_recv_cancel(peer->recv);
|
|
if (peer->link != NULL)
|
|
dc_local_peer_free(peer->link);
|
|
free(peer);
|
|
}
|
|
dc_local_server_free(local->server);
|
|
if (local->loop != NULL)
|
|
dc_loop_free(local->loop);
|
|
dc_pins_free(local->pins);
|
|
dc_identity_free(local->identity);
|
|
free(local->config);
|
|
free(local->downloads);
|
|
free(local);
|
|
}
|