summary refs log tree commit diff stats
path: root/libchatprivacy/src/libchatprivacy.c
blob: e3cd59f44c2e106d466b0bf11b5b331e65780318 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// LibChatPrivacy - C component of ChatPrivacy
// Copyright (C) 2021  Soni L.
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

#include <stdlib.h>
#include <assert.h>
#include <string.h>

/* libotr headers */
#include <libotr/proto.h>
#include <libotr/message.h>
#include <libotr/privkey.h>

#include "libchatprivacy.h"

/* The protocol ID */
static char const *const PROTOCOL = "minecraft";

static OtrlUserState userstate = NULL;
static int initialized = 0;
static char *userat = NULL;

static OtrlMessageAppOps msgappops = {0};

#define LCP_CB_IMPL(name, nullable) \
	static lcp_cb_##name *cb_##name = NULL; \
	lcp_cb_##name *lcp_setup_cb_##name(lcp_cb_##name *const func) { \
		nullable \
		lcp_cb_##name *const oldfunc = cb_##name; \
		cb_##name = func; \
		return oldfunc; \
	}

#define LCP_CB_IMPL_OPT(name) LCP_CB_IMPL(name, )
#define LCP_CB_IMPL_REQ(name) LCP_CB_IMPL(name, assert(func != NULL);)

LCP_CB_IMPL_REQ(printmsg)
LCP_CB_IMPL_REQ(sendmsg)
LCP_CB_IMPL_REQ(dialogmsg)
LCP_CB_IMPL_OPT(diagmsg)

int lcp_init(char const *const uuid) {
	if (!initialized) {
		initialized = 1;
		OTRL_INIT;
	}

	/* callback checks */
	assert(cb_printmsg != NULL);
	assert(cb_sendmsg != NULL);
	assert(cb_dialogmsg != NULL);

	/* library state checks */
	if (userstate != NULL || userat != NULL) {
		return 0;
	}

	/* arg checks */
	if (uuid == NULL) {
		return 0;
	}

	userstate = otrl_userstate_create();
	assert(userstate != NULL);

	/* read stuff */
	otrl_privkey_read(userstate, "privkey");
	otrl_instag_read(userstate, "instag");
	otrl_privkey_read_fingerprints(userstate, "fingerprints", NULL, NULL);

	/* copy uuid/userat */
	size_t const uuidlen = strlen(uuid);
	size_t const bufsize = uuidlen + 1;
	char *const userat_buf = calloc(bufsize, 1);
	if (userat_buf == NULL) {
		return 0;
	}
	memcpy(userat_buf, uuid, uuidlen);
	userat = userat_buf;

	if (cb_diagmsg != NULL) {
		cb_diagmsg(LCP_DIAG_INFO, "lcp initialized");
	}

	return 1;
}

int lcp_deinit(void) {
	assert(userstate != NULL);
	assert(userat != NULL);
	otrl_userstate_free(userstate);
	userstate = NULL;
	free((void *)userat);
	userat = NULL;
	return 1;
}

void lcp_user_join(char const *const uuid, char const *const name) {
	assert(0);
}

void lcp_user_part(char const *const uuid, char const *const name) {
	assert(0);
}

void lcp_send(char const *const name, char const *const message) {
	assert(0);
}

void lcp_recv(char const *const uuid, char const *const message) {
	assert(0);
}

void lcp_cmd(char const *const cmd) {
	assert(0);
}

void lcp_tick(void) {
	assert(0);
}