summaryrefslogtreecommitdiff
path: root/protocols/Tlen/src
diff options
context:
space:
mode:
authorKirill Volinsky <Mataes2007@gmail.com>2017-11-30 07:57:29 +0300
committerKirill Volinsky <Mataes2007@gmail.com>2017-11-30 07:57:29 +0300
commit6532896923fdf6e3d3b341ce21153b913fcda3dc (patch)
treeaeccfbfbb5d4995d146eb62fd61fa7e4207dfcb1 /protocols/Tlen/src
parentcc3d5628fade322f81abad8715d3d2ab031bc8b6 (diff)
Tlen moved to deprecated (#1050)
Diffstat (limited to 'protocols/Tlen/src')
-rw-r--r--protocols/Tlen/src/codec/gsm.h65
-rw-r--r--protocols/Tlen/src/codec/gsm_codec.c283
-rw-r--r--protocols/Tlen/src/codec/gsm_long.c183
-rw-r--r--protocols/Tlen/src/codec/gsm_lpc.c208
-rw-r--r--protocols/Tlen/src/codec/gsm_preprocess.c103
-rw-r--r--protocols/Tlen/src/codec/gsm_rpe.c248
-rw-r--r--protocols/Tlen/src/codec/gsm_short.c211
-rw-r--r--protocols/Tlen/src/commons.h6
-rw-r--r--protocols/Tlen/src/crypto/aes.c1336
-rw-r--r--protocols/Tlen/src/crypto/bignum.c2097
-rw-r--r--protocols/Tlen/src/crypto/padlock.c162
-rw-r--r--protocols/Tlen/src/crypto/polarssl/aes.h167
-rw-r--r--protocols/Tlen/src/crypto/polarssl/bignum.h587
-rw-r--r--protocols/Tlen/src/crypto/polarssl/bn_mul.h738
-rw-r--r--protocols/Tlen/src/crypto/polarssl/config.h625
-rw-r--r--protocols/Tlen/src/crypto/polarssl/padlock.h100
-rw-r--r--protocols/Tlen/src/resource.h145
-rw-r--r--protocols/Tlen/src/stdafx.cxx18
-rw-r--r--protocols/Tlen/src/stdafx.h493
-rw-r--r--protocols/Tlen/src/tlen.cpp370
-rw-r--r--protocols/Tlen/src/tlen_advsearch.cpp113
-rw-r--r--protocols/Tlen/src/tlen_avatar.cpp434
-rw-r--r--protocols/Tlen/src/tlen_avatar.h34
-rw-r--r--protocols/Tlen/src/tlen_file.cpp682
-rw-r--r--protocols/Tlen/src/tlen_file.h34
-rw-r--r--protocols/Tlen/src/tlen_iq.cpp108
-rw-r--r--protocols/Tlen/src/tlen_iq.h54
-rw-r--r--protocols/Tlen/src/tlen_iqid.cpp625
-rw-r--r--protocols/Tlen/src/tlen_list.cpp292
-rw-r--r--protocols/Tlen/src/tlen_list.h90
-rw-r--r--protocols/Tlen/src/tlen_misc.cpp110
-rw-r--r--protocols/Tlen/src/tlen_muc.cpp237
-rw-r--r--protocols/Tlen/src/tlen_muc.h35
-rw-r--r--protocols/Tlen/src/tlen_opt.cpp697
-rw-r--r--protocols/Tlen/src/tlen_p2p_new.cpp343
-rw-r--r--protocols/Tlen/src/tlen_p2p_old.cpp522
-rw-r--r--protocols/Tlen/src/tlen_p2p_old.h70
-rw-r--r--protocols/Tlen/src/tlen_picture.cpp312
-rw-r--r--protocols/Tlen/src/tlen_picture.h33
-rw-r--r--protocols/Tlen/src/tlen_presence.cpp341
-rw-r--r--protocols/Tlen/src/tlen_presence.h29
-rw-r--r--protocols/Tlen/src/tlen_svc.cpp1208
-rw-r--r--protocols/Tlen/src/tlen_thread.cpp1321
-rw-r--r--protocols/Tlen/src/tlen_userinfo.cpp328
-rw-r--r--protocols/Tlen/src/tlen_util.cpp444
-rw-r--r--protocols/Tlen/src/tlen_voice.cpp1109
-rw-r--r--protocols/Tlen/src/tlen_voice.h68
-rw-r--r--protocols/Tlen/src/tlen_ws.cpp157
-rw-r--r--protocols/Tlen/src/tlen_xml.cpp572
-rw-r--r--protocols/Tlen/src/tlen_xml.h79
-rw-r--r--protocols/Tlen/src/version.h17
51 files changed, 0 insertions, 18643 deletions
diff --git a/protocols/Tlen/src/codec/gsm.h b/protocols/Tlen/src/codec/gsm.h
deleted file mode 100644
index f522739d10..0000000000
--- a/protocols/Tlen/src/codec/gsm.h
+++ /dev/null
@@ -1,65 +0,0 @@
-/*
-
-Tlen Protocol Plugin for Miranda NG
-Copyright (C) 2004 Piotr Piastucki
-
-This program is based on GSM 06.10 source code developed by
-Jutta Degener and Carsten Bormann,
-Copyright 1992, 1993, 1994 by Jutta Degener and Carsten Bormann,
-Technische Universitaet Berlin
-
-This program is free software; you can redistribute it and/or
-modify it under the terms of the GNU General Public License
-as published by the Free Software Foundation; either version 2
-of the License, or (at your option) any later version.
-
-This program 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 General Public License for more details.
-
-You should have received a copy of the GNU General Public License
-along with this program; if not, write to the Free Software
-Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
-
-*/
-#include <windows.h>
-
-#define P_MAX 8 /* order p of LPC analysis, typically 8..14 */
-
-typedef struct {
- int LARpp[2][8]; /* LAR coefficients for previous and current frame */
- int j; /* current frame index in LARpp[j][] */
- int qLARs[8]; /* quantized LARs for current frame */
- int u[8]; /* used by short term analysis filter */
- int v[9]; /* used by short term synthesis filter */
- int subframe; /* subframe index (0..3)*/
- short dp0[ 280 ]; /* previous short-term filter output/prediction, used by long term analysis*/
- int e[160]; /* errors (residuals) */
- int nrp; /* long term synthesis */
- int Nc[4], bc[4]; /* long-term lag and gain for current frame */
- int Mc[4]; /* RPE grid index (0..3) */
- int xMaxc[4]; /* RPE maximum error quantized and encoded (mantissa + exp) */
- int xMc[52]; /* quantized residuals (errors) 4 * 13 */
-
- int z1; /* preprocessing.c, Offset_com. */
- int z2; /* Offset_com. */
- int mp; /* Preemphasis */
- int msr; /* decoder.c, Postprocessing */
-
- unsigned char gsmFrame[33];
-}gsm_state;
-
-extern gsm_state * gsm_create() ;
-extern void gsm_release(gsm_state *ptr);
-extern void gsm_encode(gsm_state *state, short *in);
-extern void gsm_decode(gsm_state *state, short *out);
-
-extern void gsm_preprocessing( gsm_state *state, short *in, short *out);
-extern void shortTermAnalysis(gsm_state *state, short *in);
-extern void longTermAnalysis(gsm_state *state, short *in);
-extern void encodeRPE(gsm_state *state);
-extern void decodeRPE(gsm_state *state);
-extern void longTermSynthesis(gsm_state *state);
-extern void shortTermSynthesis(gsm_state *state, short *in, short *out);
-extern void gsm_postprocessing( gsm_state *state, short *in, short *out);
diff --git a/protocols/Tlen/src/codec/gsm_codec.c b/protocols/Tlen/src/codec/gsm_codec.c
deleted file mode 100644
index afa1ef32de..0000000000
--- a/protocols/Tlen/src/codec/gsm_codec.c
+++ /dev/null
@@ -1,283 +0,0 @@
-/*
-
-Tlen Protocol Plugin for Miranda NG
-Copyright (C) 2004-2007 Piotr Piastucki
-
-This program is based on GSM 06.10 source code developed by
-Jutta Degener and Carsten Bormann,
-Copyright 1992, 1993, 1994 by Jutta Degener and Carsten Bormann,
-Technische Universitaet Berlin
-
-This program is free software; you can redistribute it and/or
-modify it under the terms of the GNU General Public License
-as published by the Free Software Foundation; either version 2
-of the License, or (at your option) any later version.
-
-This program 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 General Public License for more details.
-
-You should have received a copy of the GNU General Public License
-along with this program; if not, write to the Free Software
-Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
-
-*/
-
-#include "gsm.h"
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#define GSM_MAGIC 0xD
-
-gsm_state * gsm_create()
-{
- gsm_state * state = (gsm_state *)malloc(sizeof(gsm_state));
- memset(state, 0, sizeof(gsm_state));
- state->nrp = 40;
- return state;
-}
-
-void gsm_release(gsm_state *ptr)
-{
- free(ptr);
-}
-
-void gsm_encode(gsm_state *state, short *in)
-{ int i, j;
- unsigned char *c = state->gsmFrame;
- short temp_in[160];
-
- gsm_preprocessing(state, in, temp_in);
- shortTermAnalysis(state, temp_in);
- for (i = 0; i < 4; i++) {
- state->subframe = i;
- longTermAnalysis(state, temp_in + 40 * i);
- encodeRPE(state);
- for (j = 0; j < 40; j++) {
- int ii = (int) state->dp0[120 + i * 40 + j] + state->e[i * 40 + j];
- if (ii<-32768) ii = -32768;
- else if (ii>32767) ii=32767;
- state->dp0[120 + i * 40 + j] = ii;
- }
- }
- memcpy(state->dp0, state->dp0 + 160, 120 * sizeof(short));
- state->j ^= 1;
- *c++ = ((GSM_MAGIC & 0xF) << 4)
- | ((state->qLARs[0] >> 2) & 0xF);
- *c++ = ((state->qLARs[0] & 0x3) << 6)
- | (state->qLARs[1] & 0x3F);
- *c++ = ((state->qLARs[2] & 0x1F) << 3)
- | ((state->qLARs[3] >> 2) & 0x7);
- *c++ = ((state->qLARs[3] & 0x3) << 6)
- | ((state->qLARs[4] & 0xF) << 2)
- | ((state->qLARs[5] >> 2) & 0x3);
- *c++ = ((state->qLARs[5] & 0x3) << 6)
- | ((state->qLARs[6] & 0x7) << 3)
- | (state->qLARs[7] & 0x7);
- *c++ = ((state->Nc[0] & 0x7F) << 1)
- | ((state->bc[0] >> 1) & 0x1);
- *c++ = ((state->bc[0] & 0x1) << 7)
- | ((state->Mc[0] & 0x3) << 5)
- | ((state->xMaxc[0] >> 1) & 0x1F);
- *c++ = ((state->xMaxc[0] & 0x1) << 7)
- | ((state->xMc[0] & 0x7) << 4)
- | ((state->xMc[1] & 0x7) << 1)
- | ((state->xMc[2] >> 2) & 0x1);
- *c++ = ((state->xMc[2] & 0x3) << 6)
- | ((state->xMc[3] & 0x7) << 3)
- | (state->xMc[4] & 0x7);
- *c++ = ((state->xMc[5] & 0x7) << 5)
- | ((state->xMc[6] & 0x7) << 2)
- | ((state->xMc[7] >> 1) & 0x3);
- *c++ = ((state->xMc[7] & 0x1) << 7)
- | ((state->xMc[8] & 0x7) << 4)
- | ((state->xMc[9] & 0x7) << 1)
- | ((state->xMc[10] >> 2) & 0x1);
- *c++ = ((state->xMc[10] & 0x3) << 6)
- | ((state->xMc[11] & 0x7) << 3)
- | (state->xMc[12] & 0x7);
- *c++ = ((state->Nc[1] & 0x7F) << 1)
- | ((state->bc[1] >> 1) & 0x1);
- *c++ = ((state->bc[1] & 0x1) << 7)
- | ((state->Mc[1] & 0x3) << 5)
- | ((state->xMaxc[1] >> 1) & 0x1F);
- *c++ = ((state->xMaxc[1] & 0x1) << 7)
- | ((state->xMc[13] & 0x7) << 4)
- | ((state->xMc[14] & 0x7) << 1)
- | ((state->xMc[15] >> 2) & 0x1);
- *c++ = ((state->xMc[15] & 0x3) << 6)
- | ((state->xMc[16] & 0x7) << 3)
- | (state->xMc[17] & 0x7);
- *c++ = ((state->xMc[18] & 0x7) << 5)
- | ((state->xMc[19] & 0x7) << 2)
- | ((state->xMc[20] >> 1) & 0x3);
- *c++ = ((state->xMc[20] & 0x1) << 7)
- | ((state->xMc[21] & 0x7) << 4)
- | ((state->xMc[22] & 0x7) << 1)
- | ((state->xMc[23] >> 2) & 0x1);
- *c++ = ((state->xMc[23] & 0x3) << 6)
- | ((state->xMc[24] & 0x7) << 3)
- | (state->xMc[25] & 0x7);
- *c++ = ((state->Nc[2] & 0x7F) << 1)
- | ((state->bc[2] >> 1) & 0x1);
- *c++ = ((state->bc[2] & 0x1) << 7)
- | ((state->Mc[2] & 0x3) << 5)
- | ((state->xMaxc[2] >> 1) & 0x1F);
- *c++ = ((state->xMaxc[2] & 0x1) << 7)
- | ((state->xMc[26] & 0x7) << 4)
- | ((state->xMc[27] & 0x7) << 1)
- | ((state->xMc[28] >> 2) & 0x1);
- *c++ = ((state->xMc[28] & 0x3) << 6)
- | ((state->xMc[29] & 0x7) << 3)
- | (state->xMc[30] & 0x7);
- *c++ = ((state->xMc[31] & 0x7) << 5)
- | ((state->xMc[32] & 0x7) << 2)
- | ((state->xMc[33] >> 1) & 0x3);
- *c++ = ((state->xMc[33] & 0x1) << 7)
- | ((state->xMc[34] & 0x7) << 4)
- | ((state->xMc[35] & 0x7) << 1)
- | ((state->xMc[36] >> 2) & 0x1);
- *c++ = ((state->xMc[36] & 0x3) << 6)
- | ((state->xMc[37] & 0x7) << 3)
- | (state->xMc[38] & 0x7);
- *c++ = ((state->Nc[3] & 0x7F) << 1)
- | ((state->bc[3] >> 1) & 0x1);
- *c++ = ((state->bc[3] & 0x1) << 7)
- | ((state->Mc[3] & 0x3) << 5)
- | ((state->xMaxc[3] >> 1) & 0x1F);
- *c++ = ((state->xMaxc[3] & 0x1) << 7)
- | ((state->xMc[39] & 0x7) << 4)
- | ((state->xMc[40] & 0x7) << 1)
- | ((state->xMc[41] >> 2) & 0x1);
- *c++ = ((state->xMc[41] & 0x3) << 6)
- | ((state->xMc[42] & 0x7) << 3)
- | (state->xMc[43] & 0x7);
- *c++ = ((state->xMc[44] & 0x7) << 5)
- | ((state->xMc[45] & 0x7) << 2)
- | ((state->xMc[46] >> 1) & 0x3);
- *c++ = ((state->xMc[46] & 0x1) << 7)
- | ((state->xMc[47] & 0x7) << 4)
- | ((state->xMc[48] & 0x7) << 1)
- | ((state->xMc[49] >> 2) & 0x1);
- *c++ = ((state->xMc[49] & 0x3) << 6)
- | ((state->xMc[50] & 0x7) << 3)
- | (state->xMc[51] & 0x7);
-}
-
-void gsm_decode(gsm_state *state, short *out)
-{
- int i;
- unsigned char *c = state->gsmFrame;
-
- if (((*c >> 4) & 0x0F) != GSM_MAGIC) return ;
- state->qLARs[0] = (*c++ & 0xF) << 2;
- state->qLARs[0] |= (*c >> 6) & 0x3;
- state->qLARs[1] = *c++ & 0x3F;
- state->qLARs[2] = (*c >> 3) & 0x1F;
- state->qLARs[3] = (*c++ & 0x7) << 2;
- state->qLARs[3] |= (*c >> 6) & 0x3;
- state->qLARs[4] = (*c >> 2) & 0xF;
- state->qLARs[5] = (*c++ & 0x3) << 2;
- state->qLARs[5] |= (*c >> 6) & 0x3;
- state->qLARs[6] = (*c >> 3) & 0x7;
- state->qLARs[7] = *c++ & 0x7;
- state->Nc[0] = (*c >> 1) & 0x7F;
- state->bc[0] = (*c++ & 0x1) << 1;
- state->bc[0] |= (*c >> 7) & 0x1;
- state->Mc[0] = (*c >> 5) & 0x3;
- state->xMaxc[0] = (*c++ & 0x1F) << 1;
- state->xMaxc[0] |= (*c >> 7) & 0x1;
- state->xMc[0] = (*c >> 4) & 0x7;
- state->xMc[1] = (*c >> 1) & 0x7;
- state->xMc[2] = (*c++ & 0x1) << 2;
- state->xMc[2] |= (*c >> 6) & 0x3;
- state->xMc[3] = (*c >> 3) & 0x7;
- state->xMc[4] = *c++ & 0x7;
- state->xMc[5] = (*c >> 5) & 0x7;
- state->xMc[6] = (*c >> 2) & 0x7;
- state->xMc[7] = (*c++ & 0x3) << 1;
- state->xMc[7] |= (*c >> 7) & 0x1;
- state->xMc[8] = (*c >> 4) & 0x7;
- state->xMc[9] = (*c >> 1) & 0x7;
- state->xMc[10] = (*c++ & 0x1) << 2;
- state->xMc[10] |= (*c >> 6) & 0x3;
- state->xMc[11] = (*c >> 3) & 0x7;
- state->xMc[12] = *c++ & 0x7;
- state->Nc[1] = (*c >> 1) & 0x7F;
- state->bc[1] = (*c++ & 0x1) << 1;
- state->bc[1] |= (*c >> 7) & 0x1;
- state->Mc[1] = (*c >> 5) & 0x3;
- state->xMaxc[1] = (*c++ & 0x1F) << 1;
- state->xMaxc[1] |= (*c >> 7) & 0x1;
- state->xMc[13] = (*c >> 4) & 0x7;
- state->xMc[14] = (*c >> 1) & 0x7;
- state->xMc[15] = (*c++ & 0x1) << 2;
- state->xMc[15] |= (*c >> 6) & 0x3;
- state->xMc[16] = (*c >> 3) & 0x7;
- state->xMc[17] = *c++ & 0x7;
- state->xMc[18] = (*c >> 5) & 0x7;
- state->xMc[19] = (*c >> 2) & 0x7;
- state->xMc[20] = (*c++ & 0x3) << 1;
- state->xMc[20] |= (*c >> 7) & 0x1;
- state->xMc[21] = (*c >> 4) & 0x7;
- state->xMc[22] = (*c >> 1) & 0x7;
- state->xMc[23] = (*c++ & 0x1) << 2;
- state->xMc[23] |= (*c >> 6) & 0x3;
- state->xMc[24] = (*c >> 3) & 0x7;
- state->xMc[25] = *c++ & 0x7;
- state->Nc[2] = (*c >> 1) & 0x7F;
- state->bc[2] = (*c++ & 0x1) << 1;
- state->bc[2] |= (*c >> 7) & 0x1;
- state->Mc[2] = (*c >> 5) & 0x3;
- state->xMaxc[2] = (*c++ & 0x1F) << 1;
- state->xMaxc[2] |= (*c >> 7) & 0x1;
- state->xMc[26] = (*c >> 4) & 0x7;
- state->xMc[27] = (*c >> 1) & 0x7;
- state->xMc[28] = (*c++ & 0x1) << 2;
- state->xMc[28] |= (*c >> 6) & 0x3;
- state->xMc[29] = (*c >> 3) & 0x7;
- state->xMc[30] = *c++ & 0x7;
- state->xMc[31] = (*c >> 5) & 0x7;
- state->xMc[32] = (*c >> 2) & 0x7;
- state->xMc[33] = (*c++ & 0x3) << 1;
- state->xMc[33] |= (*c >> 7) & 0x1;
- state->xMc[34] = (*c >> 4) & 0x7;
- state->xMc[35] = (*c >> 1) & 0x7;
- state->xMc[36] = (*c++ & 0x1) << 2;
- state->xMc[36] |= (*c >> 6) & 0x3;
- state->xMc[37] = (*c >> 3) & 0x7;
- state->xMc[38] = *c++ & 0x7;
- state->Nc[3] = (*c >> 1) & 0x7F;
- state->bc[3] = (*c++ & 0x1) << 1;
- state->bc[3] |= (*c >> 7) & 0x1;
- state->Mc[3] = (*c >> 5) & 0x3;
- state->xMaxc[3] = (*c++ & 0x1F) << 1;
- state->xMaxc[3] |= (*c >> 7) & 0x1;
- state->xMc[39] = (*c >> 4) & 0x7;
- state->xMc[40] = (*c >> 1) & 0x7;
- state->xMc[41] = (*c++ & 0x1) << 2;
- state->xMc[41] |= (*c >> 6) & 0x3;
- state->xMc[42] = (*c >> 3) & 0x7;
- state->xMc[43] = *c++ & 0x7;
- state->xMc[44] = (*c >> 5) & 0x7;
- state->xMc[45] = (*c >> 2) & 0x7;
- state->xMc[46] = (*c++ & 0x3) << 1;
- state->xMc[46] |= (*c >> 7) & 0x1;
- state->xMc[47] = (*c >> 4) & 0x7;
- state->xMc[48] = (*c >> 1) & 0x7;
- state->xMc[49] = (*c++ & 0x1) << 2;
- state->xMc[49] |= (*c >> 6) & 0x3;
- state->xMc[50] = (*c >> 3) & 0x7;
- state->xMc[51] = *c & 0x7;
-
- for (i = 0; i < 4; i++) {
- state->subframe = i;
- decodeRPE(state);
- longTermSynthesis(state);
- }
- memcpy(state->dp0, state->dp0 + 160, 120 * sizeof(short));
- shortTermSynthesis(state, &state->dp0[120], out);
- gsm_postprocessing(state, out, out);
- state->j ^= 1;
-}
diff --git a/protocols/Tlen/src/codec/gsm_long.c b/protocols/Tlen/src/codec/gsm_long.c
deleted file mode 100644
index 56d83f7873..0000000000
--- a/protocols/Tlen/src/codec/gsm_long.c
+++ /dev/null
@@ -1,183 +0,0 @@
-/*
-
-Tlen Protocol Plugin for Miranda NG
-Copyright (C) 2004-2007 Piotr Piastucki
-
-This program is based on GSM 06.10 source code developed by
-Jutta Degener and Carsten Bormann,
-Copyright 1992, 1993, 1994 by Jutta Degener and Carsten Bormann,
-Technische Universitaet Berlin
-
-This program is free software; you can redistribute it and/or
-modify it under the terms of the GNU General Public License
-as published by the Free Software Foundation; either version 2
-of the License, or (at your option) any later version.
-
-This program 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 General Public License for more details.
-
-You should have received a copy of the GNU General Public License
-along with this program; if not, write to the Free Software
-Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
-
-*/
-#include "gsm.h"
-#include <stdio.h>
-#include <math.h>
-#include <stdlib.h>
-
-/* Decision levels */
-const static int gsm_DLB[4] = { 6554, 16384, 26214, 32767 };
-/* Quantization levels */
-const static int gsm_QLB[4] = { 3277, 11469, 21299, 32767 };
-
-/*
- * Long term parameters calculation - lag (Nc) and gain (bc).
- */
-static void longTermLTPCalculation(gsm_state *state, short *in, short *in_1)
-{
- int k, lambda;
- int Nc, bc;
- float in_1_float_base[120], * in_1_float = in_1_float_base + 120;
- float in_float[40];
- float L_result, L_max, L_power;
-
- for (k = -120; k < 0; k++) in_1_float[k] = (float)in_1[k];
- for (k = 0; k < 40; k++) in_float[k] = (float)in[k];
-
- /* Search for the maximum cross-correlation and coding of the LTP lag */
-
- L_max = 0;
- Nc = 40; /* index for the maximum cross-correlation */
-
- for (lambda = 40; lambda < 121; lambda++) {
- for (L_result = 0, L_power = 0, k = 0; k < 40; k++) {
- L_result += in_float[k] * in_1_float[k - lambda];
-// L_power += in_1_float[k - lambda] * in_1_float[k - lambda];
- }
-// L_result = L_result / L_power;
- if (L_result > L_max) {
- Nc = lambda;
- L_max = L_result;
- }
- }
- state->Nc[state->subframe] = Nc;
- if (L_max <= 0.) {
- state->bc[state->subframe] = 0;
- return;
- }
- /* Compute the power of the reconstructed short term residual signal dp[..] */
- in_1_float -= Nc;
- L_power = 0;
- for (k = 0; k < 40; k++) {
- float f = in_1_float[k];
- L_power += f * f;
- }
- if (L_max >= L_power) { //1.0
- state->bc[state->subframe] = 3;
- return;
- }
- /* Coding of the LTP gain
- * Table gsm_DLB must be used to obtain the level DLB[i] for the
- * quantization of the LTP gain b to get the coded version bc.
- */
-// lambda = L_max * 32768.;
- lambda = (int)(L_max * 32768. / L_power);
- for (bc = 0; bc <= 2; ++bc) if (lambda <= gsm_DLB[bc]) break;
- state->bc[state->subframe] = bc;
-}
-
-/*
- * Here we decode the bc parameter and compute samples of the estimate out[0..39].
- * The decoding of bc needs the gsm_QLB table.
- * The long term residual signal e[0..39] is then calculated to be fed to the
- * RPE encoding section.
- */
-static void longTermAnalysisFilter(int Nc, int bc, short *in, short *in_1, short *out, int *e)
-{
- int gain, k, l;
- gain = gsm_QLB[bc];
- for (k = 0; k < 40; k++) {
- l = (gain * (int)in_1[k - Nc] + 16384) >> 15;
- if (l < -32768) l = -32768;
- else if (l > 32767) l = 32767;
- out[k] = l;
- e[k] = in[k] - l;
- }
-}
-
-/*
- * This procedure uses the bcr and Ncr parameter to realize the
- * long term synthesis filtering. The decoding of bcr needs table gsm_QLB.
- */
-static void longTermSynthesisFilter(gsm_state * state, int Ncr, int bcr, int *e, short *out)
-{
- int k;
- int brp, Nr;
-
- /* Check the limits of Nr. */
- Nr = Ncr < 40 || Ncr > 120 ? state->nrp : Ncr;
- state->nrp = Nr;
- /* Decoding of the LTP gain bcr */
- brp = gsm_QLB[ bcr ];
-
- /* Computation of the reconstructed short term residual
- * signal drp[0..39]
- */
- for (k = 0; k < 40; k++) {
- int temp = (brp * (int)out[ k-Nr ] + 16384) >> 15;
- out[k] = e[k] + temp;
- }
-}
-
-/*
- * This procedure performs long term analysis.
- */
-void longTermAnalysis(gsm_state *state, short *in)
-{
- short *in_1 = state->dp0 + 120 + state->subframe * 40;
- short *out = state->dp0 + 120 + state->subframe * 40;
- int *e = state->e + state->subframe * 40;
-/*
-
- int i;
- printf("Long-term in: \n");
- for (i=0;i<40;i++) {
- printf("%7d ", in[i]);
- }
- printf("\n");
- */
- longTermLTPCalculation(state, in, in_1);
- /* printf("Nc: %d, bc: %d \n", state->Nc[state->subframe], state->bc[state->subframe]);
- */
- longTermAnalysisFilter(state->Nc[state->subframe], state->bc[state->subframe], in, in_1, out, e);
-/*
- printf("Long-term out: \n");
- for (i=0;i<40;i++) {
- printf("%7d ", out[i]);
- }
- printf("\n");
- */
-}
-
-/*
- * This procedure performs long term synthesis.
- */
-void longTermSynthesis(gsm_state *state)
-{
- int Nc = state->Nc[state->subframe];
- int bc = state->bc[state->subframe];
- int *e = state->e + state->subframe * 40;
- short *out = state->dp0 + 120 + state->subframe * 40;
- //int i;
- longTermSynthesisFilter(state, Nc, bc, e, out);
- /*
- printf("Long-term reconstructed: \n");
- for (i=0;i<160;i++) {
- printf("%7d ", state->dp0[i]);
- }
- printf("\n");
- */
-}
diff --git a/protocols/Tlen/src/codec/gsm_lpc.c b/protocols/Tlen/src/codec/gsm_lpc.c
deleted file mode 100644
index 538a03ad92..0000000000
--- a/protocols/Tlen/src/codec/gsm_lpc.c
+++ /dev/null
@@ -1,208 +0,0 @@
-/*
-
-Tlen Protocol Plugin for Miranda NG
-Copyright (C) 2004-2007 Piotr Piastucki
-
-This program is based on GSM 06.10 source code developed by
-Jutta Degener and Carsten Bormann,
-Copyright 1992, 1993, 1994 by Jutta Degener and Carsten Bormann,
-Technische Universitaet Berlin
-
-This program is free software; you can redistribute it and/or
-modify it under the terms of the GNU General Public License
-as published by the Free Software Foundation; either version 2
-of the License, or (at your option) any later version.
-
-This program 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 General Public License for more details.
-
-You should have received a copy of the GNU General Public License
-along with this program; if not, write to the Free Software
-Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
-
-*/
-
-/* LPC- and Reflection Coefficients
- * The next two functions calculate linear prediction coefficients
- * and/or the related reflection coefficients from the first P_MAX+1
- * values of the autocorrelation function.
- */
-#include "gsm.h" /* for P_MAX */
-#include <math.h>
-#include <stdio.h>
-#include <stdlib.h>
-
-/* Compute the autocorrelation
- * ,--,
- * ac(l) = > x(i) * x(i-l) for all i
- * `--'
- * for lags l between 0 and lag-1, and x(i) == 0 for i < 0 or i >= n
- * @param n: number of samples
- * @param lax: max lag (order)
- * @param in: [0...n-1] samples x
- * @param out: [0...lag-1] autocorrelation
- */
-static void autocorrelation(int n, float const *x, int lag, float *ac)
-{
- float d;
- int i, j;
- for (j = 0; j < lag; j++) {
- for (i = j, d = 0; i < n; i++) d += x[i] * x[i-j];
- ac[j] = d;
- }
- d = (float)fabs(ac[0]);
- if (d != 0) {
- for (j = 0; j < lag; j++) ac[j] /= d;
- }
-}
-
-/* The Levinson-Durbin algorithm was invented by N. Levinson in 1947
- * and modified by J. Durbin in 1959.
- * @param in: [0...p] autocorrelation values
- * @param out: [0...p-1] reflection coefficients
- * @param lpc: [0...p-1] LPC coefficients
- * @return minimum mean square error
- */
-/*
-static float levinson_durbin (float const *ac, float *ref, float *lpc)
-{
- int i, j;
- float r, error = ac[0];
-
- if (ac[0] == 0) {
- for (i = 0; i < P_MAX; i++) ref[i] = 0;
- return 0;
- }
-
- for (i = 0; i < P_MAX; i++) {
-
- r = -ac[i + 1];
- for (j = 0; j < i; j++) r -= lpc[j] * ac[i - j];
- ref[i] = r /= error;
-
- // Update LPC coefficients and total error.
- lpc[i] = r;
- for (j = 0; j < i / 2; j++) {
- float tmp = lpc[j];
- lpc[j] = r * lpc[i - 1 - j];
- lpc[i - 1 - j] += r * tmp;
- }
- if (i % 2) lpc[j] += lpc[j] * r;
-
- error *= 1 - r * r;
- }
- return error;
-}
-*/
-/* I. Schur's recursion from 1917 is related to the Levinson-Durbin method,
- * but faster on parallel architectures; where Levinson-Durbin would take time
- * proportional to p * log(p), Schur only requires time proportional to p. The
- * GSM coder uses an integer version of the Schur recursion.
- * @param in: [0...p] autocorrelation values
- * @param out: [0...p-1] reflection coefficients
- * @return the minimum mean square error
- */
-static float schur(float const *ac, float *ref)
-{
- int i, m;
- float error=ac[0], r, G[2][P_MAX];
-
- if (ac[0] == 0.0) {
- for (i = 0; i < P_MAX; i++) ref[i] = 0;
- return 0;
- }
-
- /* Initialize the rows of the generator matrix G to ac[1...p]. */
- for (i = 0; i < P_MAX; i++) G[0][i] = G[1][i] = ac[i + 1];
-
- for (i = 0;;) {
- /* Calculate this iteration's reflection coefficient and error. */
- ref[i] = r = -G[1][0] / error;
- error += G[1][0] * r;
-
- if (++i >= P_MAX) return error;
-
- /* Update the generator matrix. Unlike Levinson-Durbin's summing of
- * reflection coefficients, this loop could be executed in parallel
- * by p processors in constant time.
- */
- for (m = 0; m < P_MAX - i; m++) {
- G[1][m] = G[1][m + 1] + r * G[0][m];
- G[0][m] = G[1][m + 1] * r + G[0][m];
- }
- }
-}
-
-
- /* 0..7 IN/OUT */
-static void rToLAR(int *r)
-/*
- * The following scaling for r[..] and LAR[..] has been used:
- *
- * r[..] = integer( real_r[..]*32768. ); -1 <= real_r < 1.
- * LAR[..] = integer( real_LAR[..] * 16384 );
- * with -1.625 <= real_LAR <= 1.625
- */
-{
- int temp;
- int i;
-
- /* Computation of the LAR[0..7] from the r[0..7]
- */
- for (i = 0; i < 8; i++) {
- temp = abs(r[i]); //GSM_ABS(temp);
- if (temp < 22118) {
- temp >>= 1;
- } else if (temp < 31130) {
- temp -= 11059;
- } else {
- temp -= 26112;
- temp <<= 2;
- }
- r[i] = r[i] < 0 ? -temp : temp;
- }
-}
-
-static void quantizeLARs(int *LARs, int *qLARs)
-{ int qA[]={20480, 20480, 20480, 20480, 13964, 15360, 8534, 9036};
- int qB[]={0, 0, 2048, -2560, 94, -1792, -341, -1144};
- int qMin[]={-32, -32, -16, -16, -8, -8, -4, -4};
- int qMax[]={31, 31, 15, 15, 7, 7, 3, 3};
- int i;
- for (i = 0; i < 8; i++) {
- int temp;
- temp = (((LARs[i] * qA[i]) >> 15) + qB[i] + 256) >> 9;
- if (temp < qMin[i]) temp = 0;
- else if (temp > qMax[i]) temp = qMax[i] - qMin[i];
- else temp = temp - qMin[i];
- qLARs[i] = temp;
- }
-}
-
-void lpcAnalysis(gsm_state *state, short *in)
-{ float error;
- float samples[160];
- float ac[9];
- float ref[8];
- int i;
- int * qLARs = state->qLARs;
- for (i = 0; i < 160; i++) {
- samples[i] = in[i];
- }
- autocorrelation(160, samples, 9, ac);
- error=schur(ac, ref);
- /*
- printf("reff: ");
- for (i=0;i<8;i++) {
- printf("%5.5f ", ref[i]);//((float)i_lpc[i])/32768.0f);
- }
- printf("\n");
- */
- for (i = 0; i < 8; i++) {
- qLARs[i] = (int) (32768 * ref[i]);
- }
- rToLAR(qLARs);
- quantizeLARs(qLARs, qLARs);
-}
diff --git a/protocols/Tlen/src/codec/gsm_preprocess.c b/protocols/Tlen/src/codec/gsm_preprocess.c
deleted file mode 100644
index 462b8e6c17..0000000000
--- a/protocols/Tlen/src/codec/gsm_preprocess.c
+++ /dev/null
@@ -1,103 +0,0 @@
-/*
-
-Tlen Protocol Plugin for Miranda NG
-Copyright (C) 2004-2007 Piotr Piastucki
-
-This program is based on GSM 06.10 source code developed by
-Jutta Degener and Carsten Bormann,
-Copyright 1992, 1993, 1994 by Jutta Degener and Carsten Bormann,
-Technische Universitaet Berlin
-
-This program is free software; you can redistribute it and/or
-modify it under the terms of the GNU General Public License
-as published by the Free Software Foundation; either version 2
-of the License, or (at your option) any later version.
-
-This program 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 General Public License for more details.
-
-You should have received a copy of the GNU General Public License
-along with this program; if not, write to the Free Software
-Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
-
-*/
-#include "gsm.h"
-#include <stdio.h>
-
-/* 4.2.0 .. 4.2.3 PREPROCESSING SECTION
- *
- * After A-law to linear conversion (or directly from the
- * Ato D converter) the following scaling is assumed for
- * input to the RPE-LTP algorithm:
- *
- *
- * Where S is the sign bit, v a valid bit, and * a "don't care" bit.
- * The original signal is called sop[..]
- *
- */
-
-
-void gsm_preprocessing( gsm_state *state, short *in, short *out)
-{
- int z1 = state->z1;
- int z2 = state->z2;
- int mp = state->mp;
- int so, s1;
- int msp, lsp;
-
- int k = 160;
-
- while (k--) {
- /* Downscaling of the input signal
- * in: 0.1.....................12
- * S.v.v.v.v.v.v.v.v.v.v.v.v.*.*.*
- * out: 0.1................... 12
- * S.S.v.v.v.v.v.v.v.v.v.v.v.v.0.0
- */
- so = (*in >> 3) << 2;
- in++;
- /* 4.2.2 Offset compensation
- *
- * This part implements a high-pass filter and requires extended
- * arithmetic precision for the recursive part of this filter.
- * The input of this procedure is the array so[0...159] and the
- * output the array sof[ 0...159 ].
- */
- /* Compute the non-recursive part
- */
- s1 = (so - z1) << 15;
- z1 = so;
- msp = z2 >> 15;
- lsp = z2 - (msp << 15);
- s1 += ((lsp * 32735) + 16384) >> 15;
- z2 = msp * 32735 + s1;
- /* 4.2.3 Preemphasis
- */
- msp = (-28180 * mp + 16384) >> 15;
- mp = (z2 + 16384) >> 15;
- *out++ = mp + msp;
- }
- state->z1 = z1;
- state->z2 = z2;
- state->mp = mp;
-/*
- printf("preprocessed: \n");
- for (k=0;k<160;k++) {
- printf("%7d ", out[k]);//((float)i_lpc[i])/32768.0f);
- }
- printf("\n");
- */
-}
-
-void gsm_postprocessing( gsm_state *state, short *in, short *out)
-{
- int k;
- int msr = state->msr;
- for (k = 160; k--; in++, out++) {
- msr = *in + ((msr * 28180 + 16384) >>15); /* Deemphasis */
- *out = (msr + msr ) & 0xFFF8; /* Truncation & Upscaling */
- }
- state->msr = msr;
-}
diff --git a/protocols/Tlen/src/codec/gsm_rpe.c b/protocols/Tlen/src/codec/gsm_rpe.c
deleted file mode 100644
index 21c5419672..0000000000
--- a/protocols/Tlen/src/codec/gsm_rpe.c
+++ /dev/null
@@ -1,248 +0,0 @@
-/*
-
-Tlen Protocol Plugin for Miranda NG
-Copyright (C) 2004-2007 Piotr Piastucki
-
-This program is based on GSM 06.10 source code developed by
-Jutta Degener and Carsten Bormann,
-Copyright 1992, 1993, 1994 by Jutta Degener and Carsten Bormann,
-Technische Universitaet Berlin
-
-This program is free software; you can redistribute it and/or
-modify it under the terms of the GNU General Public License
-as published by the Free Software Foundation; either version 2
-of the License, or (at your option) any later version.
-
-This program 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 General Public License for more details.
-
-You should have received a copy of the GNU General Public License
-along with this program; if not, write to the Free Software
-Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
-
-*/
-#include "gsm.h"
-#include <math.h>
-#include <stdio.h>
-#include <stdlib.h>
-
-static const int gsm_H[11] = {-134, -374, 0, 2054, 5741, 8192, 5741, 2054, 0, -374, -134 };
-static const int gsm_NRFAC[8] = { 29128, 26215, 23832, 21846, 20165, 18725, 17476, 16384 };
-static const int gsm_FAC[8] = { 18431, 20479, 22527, 24575, 26623, 28671, 30719, 32767 };
-/*
- * The coefficients of the weighting filter are stored in the gsm_H table.
- * The following scaling is used:
- *
- * gsm_H[0..10] = integer( real_H[ 0..10] * 8192 );
- */
-static void weightingFilter (int *e, int *x)
-{
- int L_result;
- int k, i;
- for (k = 0; k < 40; k++) {
- L_result = 4096;
- for (i = 0; i < 11; i++) {
- int ix = i + k;
- if (ix>4 && ix<45) L_result += e[ix - 5] * gsm_H[i];
- }
- L_result >>= 13;
- x[k] = (L_result < -32768 ? -32768 : (L_result > 32767 ? 32767 : L_result));
- }
-}
-
-/*
- * The signal x[0..39] is used to select the RPE grid which is
- * represented by Mc.
- */
-static void gridSelection( int *x, int *xM, int *Mc_out)
-{
- int m, i;
- int L_result, L_temp;
- int EM, Mc;
-
- EM = 0;
- Mc = 0;
-
- for (m = 0; m < 4; m++) {
- L_result = 0;
- for (i = 0; i < 13; i++) {
- int temp = x[m + 3*i] >> 2;
- L_temp = temp * temp << 1;
- L_result += L_temp;
- }
- if (L_result > EM) {
- Mc = m;
- EM = L_result;
- }
- }
- /* Down-sampling by a factor 3 to get the selected xM[0..12]
- * RPE sequence.
- */
- for (i = 0; i < 13; i ++) xM[i] = x[Mc + 3*i];
- *Mc_out = Mc;
-}
-
-/*
- * This procedure computes the reconstructed long term residual signal
- * ep[0..39] for the LTP analysis filter. The inputs are the Mc
- * which is the grid position selection and the xMp[0..12] decoded
- * RPE samples which are upsampled by a factor of 3 by inserting zero
- * values.
- */
-static void gridPositioning (int Mc, int *xMp, int *ep)
-{
- int i, k;
- for (k = 0; k < 40; k++) ep[k] = 0;
- for (i = 0; i < 13; i++) {
- ep[ Mc + (3*i) ] = xMp[i];
- }
-}
-
-static void APCMXmaxcToExpMant (int xmaxc, int *exp_out, int *mant_out )
-{
- int exp, mant;
- /* Compute exponent and mantissa of the decoded version of xmaxc
- */
- exp = 0;
- if (xmaxc > 15) exp = (xmaxc >> 3) - 1;
- mant = xmaxc - (exp << 3);
-
- if (mant == 0) {
- exp = -4;
- mant = 7;
- }
- else {
- while (mant < 8) {
- mant = mant << 1 | 1;
- exp--;
- }
- mant -= 8;
- }
-
- *exp_out = exp;
- *mant_out = mant;
-}
-
-static void APCMQuantization (int *xM, int *xMc, int *mant_out, int *exp_out, int *xmaxc_out)
-{
- int i, itest;
- int xmax, xmaxc, temp, temp1, temp2;
- int exp, mant;
-
-
- /* Find the maximum absolute value xmax of xM[0..12].
- */
-
- xmax = 0;
- for (i = 0; i < 13; i++) {
- temp = abs(xM[i]);
- if (temp > xmax) xmax = temp;
- }
- if (xmax > 32767) xmax = 32767;
- /* Qantizing and coding of xmax to get xmaxc.
- */
-
- exp = 0;
- temp = xmax >> 9;
- itest = 0;
-
- for (i = 0; i < 6; i++) {
- if (temp != 0) exp++;
- temp = temp >> 1;
- }
-
- temp = exp + 5;
-
- xmaxc = (xmax >> temp) + (exp << 3);
-
- /* Quantizing and coding of the xM[0..12] RPE sequence
- * to get the xMc[0..12]
- */
- APCMXmaxcToExpMant( xmaxc, &exp, &mant );
-
- /* This computation uses the fact that the decoded version of xmaxc
- * can be calculated by using the exponent and the mantissa part of
- * xmaxc (logarithmic table).
- * So, this method avoids any division and uses only a scaling
- * of the RPE samples by a function of the exponent. A direct
- * multiplication by the inverse of the mantissa (NRFAC[0..7]
- * found in table 4.5) gives the 3 bit coded version xMc[0..12]
- * of the RPE samples.
- */
-
- /* Direct computation of xMc[0..12] using table 4.5
- */
- temp = (mant|8)<<(5+exp);
- temp1 = 6 - exp;
- temp2 = gsm_NRFAC[ mant ];
- for (i = 0; i < 13; i++) {
- xMc[i] = ((xM[i] << temp1) * temp2 >> 27) + 4;
- }
-
- /* NOTE: This equation is used to make all the xMc[i] positive.
- */
-
- *mant_out = mant;
- *exp_out = exp;
- *xmaxc_out = xmaxc;
-}
-
-static void APCMDequantization (int *xMc, int mant, int exp, int *xMp)
-{
- int i, temp1, temp2, temp3;
- temp1 = gsm_FAC[ mant ];
- temp2 = 6 - exp;
- if (temp2 <= 0) {
- temp3 = 1 >> (1 - temp2);
- } else {
- temp3 = 1 << (temp2 - 1);
- }
- for (i = 0; i < 13; i++) {
- xMp[i] = ((((((xMc[i]<<1)-7)<<12)*temp1+16384)>>15)+temp3)>>temp2;
- }
-}
-
-void encodeRPE(gsm_state *state)
-{ int x[40];
- int xM[13], xMp[13];
- int mant, exp;
- int *Mc = state->Mc + state->subframe;
- int *xMaxc = state->xMaxc + state->subframe;
- int *xMc = state->xMc + state->subframe * 13;
- int *e = state->e + state->subframe * 40;
- //int i;
- /*
- printf("RPE in: \n");
- for (i=0;i<40;i++) {
- printf("%7d ", e[i]);
- }
- printf("\n");
- */
- weightingFilter(e, x);
- /*
- printf("RPE weighting filter: \n");
- for (i=0;i<40;i++) {
- printf("%7d ", x[i]);
- }
- printf("\n");
- */
- gridSelection(x, xM, Mc);
- APCMQuantization(xM, xMc, &mant, &exp, xMaxc);
- /* printf("RPE Mc(grid #)=%d xmaxc=%d mant=%d exp=%d \n", *Mc, *xMaxc, mant, exp); */
- APCMDequantization(xMc, mant, exp, xMp);
- gridPositioning(*Mc, xMp, e);
-}
-
-void decodeRPE(gsm_state *state)
-{
- int exp, mant;
- int xMp[ 13 ];
- int *xMc = state->xMc + state->subframe * 13;
- int *e = state->e + state->subframe * 40;
-
- APCMXmaxcToExpMant(state->xMaxc[state->subframe], &exp, &mant);
- APCMDequantization(xMc, mant, exp, xMp);
- gridPositioning(state->Mc[state->subframe], xMp, e);
-}
diff --git a/protocols/Tlen/src/codec/gsm_short.c b/protocols/Tlen/src/codec/gsm_short.c
deleted file mode 100644
index 4b02515a72..0000000000
--- a/protocols/Tlen/src/codec/gsm_short.c
+++ /dev/null
@@ -1,211 +0,0 @@
-/*
-
-Tlen Protocol Plugin for Miranda NG
-Copyright (C) 2004-2007 Piotr Piastucki
-
-This program is based on GSM 06.10 source code developed by
-Jutta Degener and Carsten Bormann,
-Copyright 1992, 1993, 1994 by Jutta Degener and Carsten Bormann,
-Technische Universitaet Berlin
-
-This program is free software; you can redistribute it and/or
-modify it under the terms of the GNU General Public License
-as published by the Free Software Foundation; either version 2
-of the License, or (at your option) any later version.
-
-This program 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 General Public License for more details.
-
-You should have received a copy of the GNU General Public License
-along with this program; if not, write to the Free Software
-Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
-
-*/
-#include "gsm.h"
-#include <math.h>
-#include <stdio.h>
-#include <stdlib.h>
-extern void lpcAnalysis(gsm_state *state, short *in);
-
-/*
- * Dequantize LAR coefficients
- */
-static void dequantizeLARs(int *qLARs, int *LARs)
-{
- int dqA[]={13107, 13107, 13107, 13107, 19223, 17476, 31454, 29708};
- int dqB[]={0, 0, 2048, -2560, 94, -1792, -341, -1144};
- int dqMin[]={-32, -32, -16, -16, -8, -8, -4, -4};
- int i;
- for (i = 0; i < 8; i++) {
- int temp;
- temp = ((((qLARs[i] + dqMin[i]) << 10) - (dqB[i] << 1)) * dqA[i] + 16384) >> 15;
- LARs[i] = temp + temp;
- }
-}
-/*
- * Interpolate LAR coefficients (samples 0..12)
- */
-static void interpolateLARs_0_12 (int *LARpp_j_1, int *LARpp_j, int *LARp)
-{ int i;
- for (i = 0; i < 8; i++) {
- LARp[i] = (LARpp_j_1[i] >> 1) + (LARpp_j_1[i] >> 2) + (LARpp_j[i] >> 2);
- }
-}
-/*
- * Interpolate LAR coefficients (samples 13..26)
- */
-static void interpolateLARs_13_26 (int *LARpp_j_1, int *LARpp_j, int *LARp)
-{ int i;
- for (i = 0; i < 8; i++) {
- LARp[i] = (LARpp_j_1[i] >> 1) + (LARpp_j[i] >> 1);
- }
-}
-/*
- * Interpolate LAR coefficients (samples 27..39)
- */
-static void interpolateLARs_27_39 (int *LARpp_j_1, int *LARpp_j, int *LARp)
-{ int i;
- for (i = 0; i < 8; i++) {
- LARp[i] = (LARpp_j[i] >> 1) + (LARpp_j_1[i] >> 2) + (LARpp_j[i] >> 2);
- }
-}
-/*
- * Interpolate LAR coefficients (samples 40..159)
- */
-static void interpolateLARs_40_159 (int *LARpp_j_1, int *LARpp_j, int *LARp)
-{ int i;
- for (i = 0; i < 8; i++) {
- LARp[i] = LARpp_j[i];
- }
-}
-/*
- * Convert LAR coefficients to reflection coefficients
- */
-static void LARToR( int * LARp)
-{
- int i;
- int temp;
- for (i = 0; i < 8; i++) {
- temp = abs(LARp[i]);
- if (temp < 11059) temp <<= 1;
- else if (temp < 20070) temp += 11059;
- else temp = (temp >> 2) + 26112;
- LARp[i] = LARp[i] < 0 ? -temp : temp;
- }
-}
-
-/*
- * This procedure computes the short term residual signal d[..] to be fed
- * to the RPE-LTP loop from the s[..] signal and from the local rp[..]
- * array (quantized reflection coefficients). As the call of this
- * procedure can be done in many ways (see the interpolation of the LAR
- * coefficient), it is assumed that the computation begins with index
- * k_start (for arrays d[..] and s[..]) and stops with index k_end
- * (k_start and k_end are defined in 4.2.9.1). This procedure also
- * needs to keep the array u[0..7] in memory for each call.
- */
-static void shortTermAnalysisFilter (gsm_state * state, int * rp, int len, short * in)
-{
- int * u = state->u;
- int i;
- int di, ui, sav, rpi;
- for (; len--; in++) {
- di = sav = *in;
- for (i = 0; i < 8; i++) {
- ui = u[i];
- rpi = rp[i];
- u[i] = sav;
- sav = ui + ((rpi * di + 16384) >> 15);
- di = di + ((rpi * ui + 16384) >> 15);
- }
- *in = di;
- }
-}
-
-static void shortTermSynthesisFilter(gsm_state * state, int * rrp, int len, short * in, short *out)
-{
- int * v = state->v;
- int i;
- int sri;
- for (; len--; in++, out++) {
- sri = *in;
- for (i = 8; i--;) {
- sri -= (rrp[i] * v[i] + 16384) >> 15;
- if (sri < -32768) sri = -32768;
- else if (sri > 32767) sri = 32767;
- v[i+1] = v[i] + ((rrp[i] * sri + 16384) >> 15);
- if (v[i+1] < -32768) v[i+1] = -32768;
- else if (v[i+1] > 32767) v[i+1] = 32767;
- }
- *out = v[0] = sri;
- }
-}
-
-void shortTermAnalysis(gsm_state *state, short *in)
-{
- int * qLARs = state->qLARs;
- int * LARpp_j = state->LARpp[state->j];
- int * LARpp_j_1 = state->LARpp[state->j ^1];
- int LARp[8];
-// int i;
-
- lpcAnalysis(state, in);//i_samples);
-/*
- printf("short term in: \n");
- for (i=0;i<160;i++) {
- printf("%7d ", in[i]);//((float)i_lpc[i])/32768.0f);
- }
- printf("\n");
- */
- dequantizeLARs(qLARs, LARpp_j);
- interpolateLARs_0_12(LARpp_j_1, LARpp_j, LARp);
- LARToR(LARp);
- shortTermAnalysisFilter(state, LARp, 13, in);
- interpolateLARs_13_26(LARpp_j_1, LARpp_j, LARp);
- LARToR(LARp);
- shortTermAnalysisFilter(state, LARp, 14, in+13);
- interpolateLARs_27_39(LARpp_j_1, LARpp_j, LARp);
- LARToR(LARp);
- shortTermAnalysisFilter(state, LARp, 13, in+27);
- interpolateLARs_40_159(LARpp_j_1, LARpp_j, LARp);
- LARToR(LARp);
- shortTermAnalysisFilter(state, LARp, 120, in+40);
- /*
- printf("short-term residuals: \n");
- for (i=0;i<160;i++) {
- printf("%7d ", in[i]);//((float)i_lpc[i])/32768.0f);
- }
- printf("\n");
- */
-}
-
-void shortTermSynthesis(gsm_state *state, short *in, short *out)
-{
- int * qLARs = state->qLARs;
- int * LARpp_j = state->LARpp[state->j];
- int * LARpp_j_1 = state->LARpp[state->j ^1];
- int LARp[8];
-// int i;
-
- dequantizeLARs(qLARs, LARpp_j);
- interpolateLARs_0_12(LARpp_j_1, LARpp_j, LARp);
- LARToR(LARp);
- shortTermSynthesisFilter(state, LARp, 13, in, out);
- interpolateLARs_13_26(LARpp_j_1, LARpp_j, LARp);
- LARToR(LARp);
- shortTermSynthesisFilter(state, LARp, 14, in+13, out+13);
- interpolateLARs_27_39(LARpp_j_1, LARpp_j, LARp);
- LARToR(LARp);
- shortTermSynthesisFilter(state, LARp, 13, in+27, out+27);
- interpolateLARs_40_159(LARpp_j_1, LARpp_j, LARp);
- LARToR(LARp);
- shortTermSynthesisFilter(state, LARp, 120, in+40, out+40);
- /*
- printf("samples[reconstructed]: \n");
- for (i=0;i<160;i++) {
- printf("%7d ", out[i]);
- }
- */
-}
diff --git a/protocols/Tlen/src/commons.h b/protocols/Tlen/src/commons.h
deleted file mode 100644
index 37f81198e3..0000000000
--- a/protocols/Tlen/src/commons.h
+++ /dev/null
@@ -1,6 +0,0 @@
-#ifndef _TLENCOMMONS_INC
-#define _TLENCOMMONS_INC
-
-#include "version.h"
-
-#endif //ifndef _TLENCOMMONS_INC
diff --git a/protocols/Tlen/src/crypto/aes.c b/protocols/Tlen/src/crypto/aes.c
deleted file mode 100644
index 7bccdf93a1..0000000000
--- a/protocols/Tlen/src/crypto/aes.c
+++ /dev/null
@@ -1,1336 +0,0 @@
-/*
- * FIPS-197 compliant AES implementation
- *
- * Copyright (C) 2006-2010, Brainspark B.V.
- *
- * This file is part of PolarSSL (http://www.polarssl.org)
- * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
- *
- * All rights reserved.
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program 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 General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
- */
-/*
- * The AES block cipher was designed by Vincent Rijmen and Joan Daemen.
- *
- * http://csrc.nist.gov/encryption/aes/rijndael/Rijndael.pdf
- * http://csrc.nist.gov/publications/fips/fips197/fips-197.pdf
- */
-
-#include "polarssl/config.h"
-
-#if defined(POLARSSL_AES_C)
-
-#include "polarssl/aes.h"
-#include "polarssl/padlock.h"
-
-/*
- * 32-bit integer manipulation macros (little endian)
- */
-#ifndef GET_ULONG_LE
-#define GET_ULONG_LE(n,b,i) \
-{ \
- (n) = ( (unsigned long) (b)[(i) ] ) \
- | ( (unsigned long) (b)[(i) + 1] << 8 ) \
- | ( (unsigned long) (b)[(i) + 2] << 16 ) \
- | ( (unsigned long) (b)[(i) + 3] << 24 ); \
-}
-#endif
-
-#ifndef PUT_ULONG_LE
-#define PUT_ULONG_LE(n,b,i) \
-{ \
- (b)[(i) ] = (unsigned char) ( (n) ); \
- (b)[(i) + 1] = (unsigned char) ( (n) >> 8 ); \
- (b)[(i) + 2] = (unsigned char) ( (n) >> 16 ); \
- (b)[(i) + 3] = (unsigned char) ( (n) >> 24 ); \
-}
-#endif
-
-#if defined(POLARSSL_AES_ROM_TABLES)
-/*
- * Forward S-box
- */
-static const unsigned char FSb[256] =
-{
- 0x63, 0x7C, 0x77, 0x7B, 0xF2, 0x6B, 0x6F, 0xC5,
- 0x30, 0x01, 0x67, 0x2B, 0xFE, 0xD7, 0xAB, 0x76,
- 0xCA, 0x82, 0xC9, 0x7D, 0xFA, 0x59, 0x47, 0xF0,
- 0xAD, 0xD4, 0xA2, 0xAF, 0x9C, 0xA4, 0x72, 0xC0,
- 0xB7, 0xFD, 0x93, 0x26, 0x36, 0x3F, 0xF7, 0xCC,
- 0x34, 0xA5, 0xE5, 0xF1, 0x71, 0xD8, 0x31, 0x15,
- 0x04, 0xC7, 0x23, 0xC3, 0x18, 0x96, 0x05, 0x9A,
- 0x07, 0x12, 0x80, 0xE2, 0xEB, 0x27, 0xB2, 0x75,
- 0x09, 0x83, 0x2C, 0x1A, 0x1B, 0x6E, 0x5A, 0xA0,
- 0x52, 0x3B, 0xD6, 0xB3, 0x29, 0xE3, 0x2F, 0x84,
- 0x53, 0xD1, 0x00, 0xED, 0x20, 0xFC, 0xB1, 0x5B,
- 0x6A, 0xCB, 0xBE, 0x39, 0x4A, 0x4C, 0x58, 0xCF,
- 0xD0, 0xEF, 0xAA, 0xFB, 0x43, 0x4D, 0x33, 0x85,
- 0x45, 0xF9, 0x02, 0x7F, 0x50, 0x3C, 0x9F, 0xA8,
- 0x51, 0xA3, 0x40, 0x8F, 0x92, 0x9D, 0x38, 0xF5,
- 0xBC, 0xB6, 0xDA, 0x21, 0x10, 0xFF, 0xF3, 0xD2,
- 0xCD, 0x0C, 0x13, 0xEC, 0x5F, 0x97, 0x44, 0x17,
- 0xC4, 0xA7, 0x7E, 0x3D, 0x64, 0x5D, 0x19, 0x73,
- 0x60, 0x81, 0x4F, 0xDC, 0x22, 0x2A, 0x90, 0x88,
- 0x46, 0xEE, 0xB8, 0x14, 0xDE, 0x5E, 0x0B, 0xDB,
- 0xE0, 0x32, 0x3A, 0x0A, 0x49, 0x06, 0x24, 0x5C,
- 0xC2, 0xD3, 0xAC, 0x62, 0x91, 0x95, 0xE4, 0x79,
- 0xE7, 0xC8, 0x37, 0x6D, 0x8D, 0xD5, 0x4E, 0xA9,
- 0x6C, 0x56, 0xF4, 0xEA, 0x65, 0x7A, 0xAE, 0x08,
- 0xBA, 0x78, 0x25, 0x2E, 0x1C, 0xA6, 0xB4, 0xC6,
- 0xE8, 0xDD, 0x74, 0x1F, 0x4B, 0xBD, 0x8B, 0x8A,
- 0x70, 0x3E, 0xB5, 0x66, 0x48, 0x03, 0xF6, 0x0E,
- 0x61, 0x35, 0x57, 0xB9, 0x86, 0xC1, 0x1D, 0x9E,
- 0xE1, 0xF8, 0x98, 0x11, 0x69, 0xD9, 0x8E, 0x94,
- 0x9B, 0x1E, 0x87, 0xE9, 0xCE, 0x55, 0x28, 0xDF,
- 0x8C, 0xA1, 0x89, 0x0D, 0xBF, 0xE6, 0x42, 0x68,
- 0x41, 0x99, 0x2D, 0x0F, 0xB0, 0x54, 0xBB, 0x16
-};
-
-/*
- * Forward tables
- */
-#define FT \
-\
- V(A5,63,63,C6), V(84,7C,7C,F8), V(99,77,77,EE), V(8D,7B,7B,F6), \
- V(0D,F2,F2,FF), V(BD,6B,6B,D6), V(B1,6F,6F,DE), V(54,C5,C5,91), \
- V(50,30,30,60), V(03,01,01,02), V(A9,67,67,CE), V(7D,2B,2B,56), \
- V(19,FE,FE,E7), V(62,D7,D7,B5), V(E6,AB,AB,4D), V(9A,76,76,EC), \
- V(45,CA,CA,8F), V(9D,82,82,1F), V(40,C9,C9,89), V(87,7D,7D,FA), \
- V(15,FA,FA,EF), V(EB,59,59,B2), V(C9,47,47,8E), V(0B,F0,F0,FB), \
- V(EC,AD,AD,41), V(67,D4,D4,B3), V(FD,A2,A2,5F), V(EA,AF,AF,45), \
- V(BF,9C,9C,23), V(F7,A4,A4,53), V(96,72,72,E4), V(5B,C0,C0,9B), \
- V(C2,B7,B7,75), V(1C,FD,FD,E1), V(AE,93,93,3D), V(6A,26,26,4C), \
- V(5A,36,36,6C), V(41,3F,3F,7E), V(02,F7,F7,F5), V(4F,CC,CC,83), \
- V(5C,34,34,68), V(F4,A5,A5,51), V(34,E5,E5,D1), V(08,F1,F1,F9), \
- V(93,71,71,E2), V(73,D8,D8,AB), V(53,31,31,62), V(3F,15,15,2A), \
- V(0C,04,04,08), V(52,C7,C7,95), V(65,23,23,46), V(5E,C3,C3,9D), \
- V(28,18,18,30), V(A1,96,96,37), V(0F,05,05,0A), V(B5,9A,9A,2F), \
- V(09,07,07,0E), V(36,12,12,24), V(9B,80,80,1B), V(3D,E2,E2,DF), \
- V(26,EB,EB,CD), V(69,27,27,4E), V(CD,B2,B2,7F), V(9F,75,75,EA), \
- V(1B,09,09,12), V(9E,83,83,1D), V(74,2C,2C,58), V(2E,1A,1A,34), \
- V(2D,1B,1B,36), V(B2,6E,6E,DC), V(EE,5A,5A,B4), V(FB,A0,A0,5B), \
- V(F6,52,52,A4), V(4D,3B,3B,76), V(61,D6,D6,B7), V(CE,B3,B3,7D), \
- V(7B,29,29,52), V(3E,E3,E3,DD), V(71,2F,2F,5E), V(97,84,84,13), \
- V(F5,53,53,A6), V(68,D1,D1,B9), V(00,00,00,00), V(2C,ED,ED,C1), \
- V(60,20,20,40), V(1F,FC,FC,E3), V(C8,B1,B1,79), V(ED,5B,5B,B6), \
- V(BE,6A,6A,D4), V(46,CB,CB,8D), V(D9,BE,BE,67), V(4B,39,39,72), \
- V(DE,4A,4A,94), V(D4,4C,4C,98), V(E8,58,58,B0), V(4A,CF,CF,85), \
- V(6B,D0,D0,BB), V(2A,EF,EF,C5), V(E5,AA,AA,4F), V(16,FB,FB,ED), \
- V(C5,43,43,86), V(D7,4D,4D,9A), V(55,33,33,66), V(94,85,85,11), \
- V(CF,45,45,8A), V(10,F9,F9,E9), V(06,02,02,04), V(81,7F,7F,FE), \
- V(F0,50,50,A0), V(44,3C,3C,78), V(BA,9F,9F,25), V(E3,A8,A8,4B), \
- V(F3,51,51,A2), V(FE,A3,A3,5D), V(C0,40,40,80), V(8A,8F,8F,05), \
- V(AD,92,92,3F), V(BC,9D,9D,21), V(48,38,38,70), V(04,F5,F5,F1), \
- V(DF,BC,BC,63), V(C1,B6,B6,77), V(75,DA,DA,AF), V(63,21,21,42), \
- V(30,10,10,20), V(1A,FF,FF,E5), V(0E,F3,F3,FD), V(6D,D2,D2,BF), \
- V(4C,CD,CD,81), V(14,0C,0C,18), V(35,13,13,26), V(2F,EC,EC,C3), \
- V(E1,5F,5F,BE), V(A2,97,97,35), V(CC,44,44,88), V(39,17,17,2E), \
- V(57,C4,C4,93), V(F2,A7,A7,55), V(82,7E,7E,FC), V(47,3D,3D,7A), \
- V(AC,64,64,C8), V(E7,5D,5D,BA), V(2B,19,19,32), V(95,73,73,E6), \
- V(A0,60,60,C0), V(98,81,81,19), V(D1,4F,4F,9E), V(7F,DC,DC,A3), \
- V(66,22,22,44), V(7E,2A,2A,54), V(AB,90,90,3B), V(83,88,88,0B), \
- V(CA,46,46,8C), V(29,EE,EE,C7), V(D3,B8,B8,6B), V(3C,14,14,28), \
- V(79,DE,DE,A7), V(E2,5E,5E,BC), V(1D,0B,0B,16), V(76,DB,DB,AD), \
- V(3B,E0,E0,DB), V(56,32,32,64), V(4E,3A,3A,74), V(1E,0A,0A,14), \
- V(DB,49,49,92), V(0A,06,06,0C), V(6C,24,24,48), V(E4,5C,5C,B8), \
- V(5D,C2,C2,9F), V(6E,D3,D3,BD), V(EF,AC,AC,43), V(A6,62,62,C4), \
- V(A8,91,91,39), V(A4,95,95,31), V(37,E4,E4,D3), V(8B,79,79,F2), \
- V(32,E7,E7,D5), V(43,C8,C8,8B), V(59,37,37,6E), V(B7,6D,6D,DA), \
- V(8C,8D,8D,01), V(64,D5,D5,B1), V(D2,4E,4E,9C), V(E0,A9,A9,49), \
- V(B4,6C,6C,D8), V(FA,56,56,AC), V(07,F4,F4,F3), V(25,EA,EA,CF), \
- V(AF,65,65,CA), V(8E,7A,7A,F4), V(E9,AE,AE,47), V(18,08,08,10), \
- V(D5,BA,BA,6F), V(88,78,78,F0), V(6F,25,25,4A), V(72,2E,2E,5C), \
- V(24,1C,1C,38), V(F1,A6,A6,57), V(C7,B4,B4,73), V(51,C6,C6,97), \
- V(23,E8,E8,CB), V(7C,DD,DD,A1), V(9C,74,74,E8), V(21,1F,1F,3E), \
- V(DD,4B,4B,96), V(DC,BD,BD,61), V(86,8B,8B,0D), V(85,8A,8A,0F), \
- V(90,70,70,E0), V(42,3E,3E,7C), V(C4,B5,B5,71), V(AA,66,66,CC), \
- V(D8,48,48,90), V(05,03,03,06), V(01,F6,F6,F7), V(12,0E,0E,1C), \
- V(A3,61,61,C2), V(5F,35,35,6A), V(F9,57,57,AE), V(D0,B9,B9,69), \
- V(91,86,86,17), V(58,C1,C1,99), V(27,1D,1D,3A), V(B9,9E,9E,27), \
- V(38,E1,E1,D9), V(13,F8,F8,EB), V(B3,98,98,2B), V(33,11,11,22), \
- V(BB,69,69,D2), V(70,D9,D9,A9), V(89,8E,8E,07), V(A7,94,94,33), \
- V(B6,9B,9B,2D), V(22,1E,1E,3C), V(92,87,87,15), V(20,E9,E9,C9), \
- V(49,CE,CE,87), V(FF,55,55,AA), V(78,28,28,50), V(7A,DF,DF,A5), \
- V(8F,8C,8C,03), V(F8,A1,A1,59), V(80,89,89,09), V(17,0D,0D,1A), \
- V(DA,BF,BF,65), V(31,E6,E6,D7), V(C6,42,42,84), V(B8,68,68,D0), \
- V(C3,41,41,82), V(B0,99,99,29), V(77,2D,2D,5A), V(11,0F,0F,1E), \
- V(CB,B0,B0,7B), V(FC,54,54,A8), V(D6,BB,BB,6D), V(3A,16,16,2C)
-
-#define V(a,b,c,d) 0x##a##b##c##d
-static const unsigned long FT0[256] = { FT };
-#undef V
-
-#define V(a,b,c,d) 0x##b##c##d##a
-static const unsigned long FT1[256] = { FT };
-#undef V
-
-#define V(a,b,c,d) 0x##c##d##a##b
-static const unsigned long FT2[256] = { FT };
-#undef V
-
-#define V(a,b,c,d) 0x##d##a##b##c
-static const unsigned long FT3[256] = { FT };
-#undef V
-
-#undef FT
-
-/*
- * Reverse S-box
- */
-static const unsigned char RSb[256] =
-{
- 0x52, 0x09, 0x6A, 0xD5, 0x30, 0x36, 0xA5, 0x38,
- 0xBF, 0x40, 0xA3, 0x9E, 0x81, 0xF3, 0xD7, 0xFB,
- 0x7C, 0xE3, 0x39, 0x82, 0x9B, 0x2F, 0xFF, 0x87,
- 0x34, 0x8E, 0x43, 0x44, 0xC4, 0xDE, 0xE9, 0xCB,
- 0x54, 0x7B, 0x94, 0x32, 0xA6, 0xC2, 0x23, 0x3D,
- 0xEE, 0x4C, 0x95, 0x0B, 0x42, 0xFA, 0xC3, 0x4E,
- 0x08, 0x2E, 0xA1, 0x66, 0x28, 0xD9, 0x24, 0xB2,
- 0x76, 0x5B, 0xA2, 0x49, 0x6D, 0x8B, 0xD1, 0x25,
- 0x72, 0xF8, 0xF6, 0x64, 0x86, 0x68, 0x98, 0x16,
- 0xD4, 0xA4, 0x5C, 0xCC, 0x5D, 0x65, 0xB6, 0x92,
- 0x6C, 0x70, 0x48, 0x50, 0xFD, 0xED, 0xB9, 0xDA,
- 0x5E, 0x15, 0x46, 0x57, 0xA7, 0x8D, 0x9D, 0x84,
- 0x90, 0xD8, 0xAB, 0x00, 0x8C, 0xBC, 0xD3, 0x0A,
- 0xF7, 0xE4, 0x58, 0x05, 0xB8, 0xB3, 0x45, 0x06,
- 0xD0, 0x2C, 0x1E, 0x8F, 0xCA, 0x3F, 0x0F, 0x02,
- 0xC1, 0xAF, 0xBD, 0x03, 0x01, 0x13, 0x8A, 0x6B,
- 0x3A, 0x91, 0x11, 0x41, 0x4F, 0x67, 0xDC, 0xEA,
- 0x97, 0xF2, 0xCF, 0xCE, 0xF0, 0xB4, 0xE6, 0x73,
- 0x96, 0xAC, 0x74, 0x22, 0xE7, 0xAD, 0x35, 0x85,
- 0xE2, 0xF9, 0x37, 0xE8, 0x1C, 0x75, 0xDF, 0x6E,
- 0x47, 0xF1, 0x1A, 0x71, 0x1D, 0x29, 0xC5, 0x89,
- 0x6F, 0xB7, 0x62, 0x0E, 0xAA, 0x18, 0xBE, 0x1B,
- 0xFC, 0x56, 0x3E, 0x4B, 0xC6, 0xD2, 0x79, 0x20,
- 0x9A, 0xDB, 0xC0, 0xFE, 0x78, 0xCD, 0x5A, 0xF4,
- 0x1F, 0xDD, 0xA8, 0x33, 0x88, 0x07, 0xC7, 0x31,
- 0xB1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xEC, 0x5F,
- 0x60, 0x51, 0x7F, 0xA9, 0x19, 0xB5, 0x4A, 0x0D,
- 0x2D, 0xE5, 0x7A, 0x9F, 0x93, 0xC9, 0x9C, 0xEF,
- 0xA0, 0xE0, 0x3B, 0x4D, 0xAE, 0x2A, 0xF5, 0xB0,
- 0xC8, 0xEB, 0xBB, 0x3C, 0x83, 0x53, 0x99, 0x61,
- 0x17, 0x2B, 0x04, 0x7E, 0xBA, 0x77, 0xD6, 0x26,
- 0xE1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0C, 0x7D
-};
-
-/*
- * Reverse tables
- */
-#define RT \
-\
- V(50,A7,F4,51), V(53,65,41,7E), V(C3,A4,17,1A), V(96,5E,27,3A), \
- V(CB,6B,AB,3B), V(F1,45,9D,1F), V(AB,58,FA,AC), V(93,03,E3,4B), \
- V(55,FA,30,20), V(F6,6D,76,AD), V(91,76,CC,88), V(25,4C,02,F5), \
- V(FC,D7,E5,4F), V(D7,CB,2A,C5), V(80,44,35,26), V(8F,A3,62,B5), \
- V(49,5A,B1,DE), V(67,1B,BA,25), V(98,0E,EA,45), V(E1,C0,FE,5D), \
- V(02,75,2F,C3), V(12,F0,4C,81), V(A3,97,46,8D), V(C6,F9,D3,6B), \
- V(E7,5F,8F,03), V(95,9C,92,15), V(EB,7A,6D,BF), V(DA,59,52,95), \
- V(2D,83,BE,D4), V(D3,21,74,58), V(29,69,E0,49), V(44,C8,C9,8E), \
- V(6A,89,C2,75), V(78,79,8E,F4), V(6B,3E,58,99), V(DD,71,B9,27), \
- V(B6,4F,E1,BE), V(17,AD,88,F0), V(66,AC,20,C9), V(B4,3A,CE,7D), \
- V(18,4A,DF,63), V(82,31,1A,E5), V(60,33,51,97), V(45,7F,53,62), \
- V(E0,77,64,B1), V(84,AE,6B,BB), V(1C,A0,81,FE), V(94,2B,08,F9), \
- V(58,68,48,70), V(19,FD,45,8F), V(87,6C,DE,94), V(B7,F8,7B,52), \
- V(23,D3,73,AB), V(E2,02,4B,72), V(57,8F,1F,E3), V(2A,AB,55,66), \
- V(07,28,EB,B2), V(03,C2,B5,2F), V(9A,7B,C5,86), V(A5,08,37,D3), \
- V(F2,87,28,30), V(B2,A5,BF,23), V(BA,6A,03,02), V(5C,82,16,ED), \
- V(2B,1C,CF,8A), V(92,B4,79,A7), V(F0,F2,07,F3), V(A1,E2,69,4E), \
- V(CD,F4,DA,65), V(D5,BE,05,06), V(1F,62,34,D1), V(8A,FE,A6,C4), \
- V(9D,53,2E,34), V(A0,55,F3,A2), V(32,E1,8A,05), V(75,EB,F6,A4), \
- V(39,EC,83,0B), V(AA,EF,60,40), V(06,9F,71,5E), V(51,10,6E,BD), \
- V(F9,8A,21,3E), V(3D,06,DD,96), V(AE,05,3E,DD), V(46,BD,E6,4D), \
- V(B5,8D,54,91), V(05,5D,C4,71), V(6F,D4,06,04), V(FF,15,50,60), \
- V(24,FB,98,19), V(97,E9,BD,D6), V(CC,43,40,89), V(77,9E,D9,67), \
- V(BD,42,E8,B0), V(88,8B,89,07), V(38,5B,19,E7), V(DB,EE,C8,79), \
- V(47,0A,7C,A1), V(E9,0F,42,7C), V(C9,1E,84,F8), V(00,00,00,00), \
- V(83,86,80,09), V(48,ED,2B,32), V(AC,70,11,1E), V(4E,72,5A,6C), \
- V(FB,FF,0E,FD), V(56,38,85,0F), V(1E,D5,AE,3D), V(27,39,2D,36), \
- V(64,D9,0F,0A), V(21,A6,5C,68), V(D1,54,5B,9B), V(3A,2E,36,24), \
- V(B1,67,0A,0C), V(0F,E7,57,93), V(D2,96,EE,B4), V(9E,91,9B,1B), \
- V(4F,C5,C0,80), V(A2,20,DC,61), V(69,4B,77,5A), V(16,1A,12,1C), \
- V(0A,BA,93,E2), V(E5,2A,A0,C0), V(43,E0,22,3C), V(1D,17,1B,12), \
- V(0B,0D,09,0E), V(AD,C7,8B,F2), V(B9,A8,B6,2D), V(C8,A9,1E,14), \
- V(85,19,F1,57), V(4C,07,75,AF), V(BB,DD,99,EE), V(FD,60,7F,A3), \
- V(9F,26,01,F7), V(BC,F5,72,5C), V(C5,3B,66,44), V(34,7E,FB,5B), \
- V(76,29,43,8B), V(DC,C6,23,CB), V(68,FC,ED,B6), V(63,F1,E4,B8), \
- V(CA,DC,31,D7), V(10,85,63,42), V(40,22,97,13), V(20,11,C6,84), \
- V(7D,24,4A,85), V(F8,3D,BB,D2), V(11,32,F9,AE), V(6D,A1,29,C7), \
- V(4B,2F,9E,1D), V(F3,30,B2,DC), V(EC,52,86,0D), V(D0,E3,C1,77), \
- V(6C,16,B3,2B), V(99,B9,70,A9), V(FA,48,94,11), V(22,64,E9,47), \
- V(C4,8C,FC,A8), V(1A,3F,F0,A0), V(D8,2C,7D,56), V(EF,90,33,22), \
- V(C7,4E,49,87), V(C1,D1,38,D9), V(FE,A2,CA,8C), V(36,0B,D4,98), \
- V(CF,81,F5,A6), V(28,DE,7A,A5), V(26,8E,B7,DA), V(A4,BF,AD,3F), \
- V(E4,9D,3A,2C), V(0D,92,78,50), V(9B,CC,5F,6A), V(62,46,7E,54), \
- V(C2,13,8D,F6), V(E8,B8,D8,90), V(5E,F7,39,2E), V(F5,AF,C3,82), \
- V(BE,80,5D,9F), V(7C,93,D0,69), V(A9,2D,D5,6F), V(B3,12,25,CF), \
- V(3B,99,AC,C8), V(A7,7D,18,10), V(6E,63,9C,E8), V(7B,BB,3B,DB), \
- V(09,78,26,CD), V(F4,18,59,6E), V(01,B7,9A,EC), V(A8,9A,4F,83), \
- V(65,6E,95,E6), V(7E,E6,FF,AA), V(08,CF,BC,21), V(E6,E8,15,EF), \
- V(D9,9B,E7,BA), V(CE,36,6F,4A), V(D4,09,9F,EA), V(D6,7C,B0,29), \
- V(AF,B2,A4,31), V(31,23,3F,2A), V(30,94,A5,C6), V(C0,66,A2,35), \
- V(37,BC,4E,74), V(A6,CA,82,FC), V(B0,D0,90,E0), V(15,D8,A7,33), \
- V(4A,98,04,F1), V(F7,DA,EC,41), V(0E,50,CD,7F), V(2F,F6,91,17), \
- V(8D,D6,4D,76), V(4D,B0,EF,43), V(54,4D,AA,CC), V(DF,04,96,E4), \
- V(E3,B5,D1,9E), V(1B,88,6A,4C), V(B8,1F,2C,C1), V(7F,51,65,46), \
- V(04,EA,5E,9D), V(5D,35,8C,01), V(73,74,87,FA), V(2E,41,0B,FB), \
- V(5A,1D,67,B3), V(52,D2,DB,92), V(33,56,10,E9), V(13,47,D6,6D), \
- V(8C,61,D7,9A), V(7A,0C,A1,37), V(8E,14,F8,59), V(89,3C,13,EB), \
- V(EE,27,A9,CE), V(35,C9,61,B7), V(ED,E5,1C,E1), V(3C,B1,47,7A), \
- V(59,DF,D2,9C), V(3F,73,F2,55), V(79,CE,14,18), V(BF,37,C7,73), \
- V(EA,CD,F7,53), V(5B,AA,FD,5F), V(14,6F,3D,DF), V(86,DB,44,78), \
- V(81,F3,AF,CA), V(3E,C4,68,B9), V(2C,34,24,38), V(5F,40,A3,C2), \
- V(72,C3,1D,16), V(0C,25,E2,BC), V(8B,49,3C,28), V(41,95,0D,FF), \
- V(71,01,A8,39), V(DE,B3,0C,08), V(9C,E4,B4,D8), V(90,C1,56,64), \
- V(61,84,CB,7B), V(70,B6,32,D5), V(74,5C,6C,48), V(42,57,B8,D0)
-
-#define V(a,b,c,d) 0x##a##b##c##d
-static const unsigned long RT0[256] = { RT };
-#undef V
-
-#define V(a,b,c,d) 0x##b##c##d##a
-static const unsigned long RT1[256] = { RT };
-#undef V
-
-#define V(a,b,c,d) 0x##c##d##a##b
-static const unsigned long RT2[256] = { RT };
-#undef V
-
-#define V(a,b,c,d) 0x##d##a##b##c
-static const unsigned long RT3[256] = { RT };
-#undef V
-
-#undef RT
-
-/*
- * Round constants
- */
-static const unsigned long RCON[10] =
-{
- 0x00000001, 0x00000002, 0x00000004, 0x00000008,
- 0x00000010, 0x00000020, 0x00000040, 0x00000080,
- 0x0000001B, 0x00000036
-};
-
-#else
-
-/*
- * Forward S-box & tables
- */
-static unsigned char FSb[256];
-static unsigned long FT0[256];
-static unsigned long FT1[256];
-static unsigned long FT2[256];
-static unsigned long FT3[256];
-
-/*
- * Reverse S-box & tables
- */
-static unsigned char RSb[256];
-static unsigned long RT0[256];
-static unsigned long RT1[256];
-static unsigned long RT2[256];
-static unsigned long RT3[256];
-
-/*
- * Round constants
- */
-static unsigned long RCON[10];
-
-/*
- * Tables generation code
- */
-#define ROTL8(x) ( ( x << 8 ) & 0xFFFFFFFF ) | ( x >> 24 )
-#define XTIME(x) ( ( x << 1 ) ^ ( ( x & 0x80 ) ? 0x1B : 0x00 ) )
-#define MUL(x,y) ( ( x && y ) ? pow[(log[x]+log[y]) % 255] : 0 )
-
-static int aes_init_done = 0;
-
-static void aes_gen_tables( void )
-{
- int i, x, y, z;
- int pow[256];
- int log[256];
-
- /*
- * compute pow and log tables over GF(2^8)
- */
- for( i = 0, x = 1; i < 256; i++ )
- {
- pow[i] = x;
- log[x] = i;
- x = ( x ^ XTIME( x ) ) & 0xFF;
- }
-
- /*
- * calculate the round constants
- */
- for( i = 0, x = 1; i < 10; i++ )
- {
- RCON[i] = (unsigned long) x;
- x = XTIME( x ) & 0xFF;
- }
-
- /*
- * generate the forward and reverse S-boxes
- */
- FSb[0x00] = 0x63;
- RSb[0x63] = 0x00;
-
- for( i = 1; i < 256; i++ )
- {
- x = pow[255 - log[i]];
-
- y = x; y = ( (y << 1) | (y >> 7) ) & 0xFF;
- x ^= y; y = ( (y << 1) | (y >> 7) ) & 0xFF;
- x ^= y; y = ( (y << 1) | (y >> 7) ) & 0xFF;
- x ^= y; y = ( (y << 1) | (y >> 7) ) & 0xFF;
- x ^= y ^ 0x63;
-
- FSb[i] = (unsigned char) x;
- RSb[x] = (unsigned char) i;
- }
-
- /*
- * generate the forward and reverse tables
- */
- for( i = 0; i < 256; i++ )
- {
- x = FSb[i];
- y = XTIME( x ) & 0xFF;
- z = ( y ^ x ) & 0xFF;
-
- FT0[i] = ( (unsigned long) y ) ^
- ( (unsigned long) x << 8 ) ^
- ( (unsigned long) x << 16 ) ^
- ( (unsigned long) z << 24 );
-
- FT1[i] = ROTL8( FT0[i] );
- FT2[i] = ROTL8( FT1[i] );
- FT3[i] = ROTL8( FT2[i] );
-
- x = RSb[i];
-
- RT0[i] = ( (unsigned long) MUL( 0x0E, x ) ) ^
- ( (unsigned long) MUL( 0x09, x ) << 8 ) ^
- ( (unsigned long) MUL( 0x0D, x ) << 16 ) ^
- ( (unsigned long) MUL( 0x0B, x ) << 24 );
-
- RT1[i] = ROTL8( RT0[i] );
- RT2[i] = ROTL8( RT1[i] );
- RT3[i] = ROTL8( RT2[i] );
- }
-}
-
-#endif
-
-/*
- * AES key schedule (encryption)
- */
-int aes_setkey_enc( aes_context *ctx, const unsigned char *key, unsigned int keysize )
-{
- unsigned int i;
- unsigned long *RK;
-
-#if !defined(POLARSSL_AES_ROM_TABLES)
- if( aes_init_done == 0 )
- {
- aes_gen_tables();
- aes_init_done = 1;
- }
-#endif
-
- switch( keysize )
- {
- case 128: ctx->nr = 10; break;
- case 192: ctx->nr = 12; break;
- case 256: ctx->nr = 14; break;
- default : return( POLARSSL_ERR_AES_INVALID_KEY_LENGTH );
- }
-
-#if defined(PADLOCK_ALIGN16)
- ctx->rk = RK = PADLOCK_ALIGN16( ctx->buf );
-#else
- ctx->rk = RK = ctx->buf;
-#endif
-
- for( i = 0; i < (keysize >> 5); i++ )
- {
- GET_ULONG_LE( RK[i], key, i << 2 );
- }
-
- switch( ctx->nr )
- {
- case 10:
-
- for( i = 0; i < 10; i++, RK += 4 )
- {
- RK[4] = RK[0] ^ RCON[i] ^
- ( (unsigned long) FSb[ ( RK[3] >> 8 ) & 0xFF ] ) ^
- ( (unsigned long) FSb[ ( RK[3] >> 16 ) & 0xFF ] << 8 ) ^
- ( (unsigned long) FSb[ ( RK[3] >> 24 ) & 0xFF ] << 16 ) ^
- ( (unsigned long) FSb[ ( RK[3] ) & 0xFF ] << 24 );
-
- RK[5] = RK[1] ^ RK[4];
- RK[6] = RK[2] ^ RK[5];
- RK[7] = RK[3] ^ RK[6];
- }
- break;
-
- case 12:
-
- for( i = 0; i < 8; i++, RK += 6 )
- {
- RK[6] = RK[0] ^ RCON[i] ^
- ( (unsigned long) FSb[ ( RK[5] >> 8 ) & 0xFF ] ) ^
- ( (unsigned long) FSb[ ( RK[5] >> 16 ) & 0xFF ] << 8 ) ^
- ( (unsigned long) FSb[ ( RK[5] >> 24 ) & 0xFF ] << 16 ) ^
- ( (unsigned long) FSb[ ( RK[5] ) & 0xFF ] << 24 );
-
- RK[7] = RK[1] ^ RK[6];
- RK[8] = RK[2] ^ RK[7];
- RK[9] = RK[3] ^ RK[8];
- RK[10] = RK[4] ^ RK[9];
- RK[11] = RK[5] ^ RK[10];
- }
- break;
-
- case 14:
-
- for( i = 0; i < 7; i++, RK += 8 )
- {
- RK[8] = RK[0] ^ RCON[i] ^
- ( (unsigned long) FSb[ ( RK[7] >> 8 ) & 0xFF ] ) ^
- ( (unsigned long) FSb[ ( RK[7] >> 16 ) & 0xFF ] << 8 ) ^
- ( (unsigned long) FSb[ ( RK[7] >> 24 ) & 0xFF ] << 16 ) ^
- ( (unsigned long) FSb[ ( RK[7] ) & 0xFF ] << 24 );
-
- RK[9] = RK[1] ^ RK[8];
- RK[10] = RK[2] ^ RK[9];
- RK[11] = RK[3] ^ RK[10];
-
- RK[12] = RK[4] ^
- ( (unsigned long) FSb[ ( RK[11] ) & 0xFF ] ) ^
- ( (unsigned long) FSb[ ( RK[11] >> 8 ) & 0xFF ] << 8 ) ^
- ( (unsigned long) FSb[ ( RK[11] >> 16 ) & 0xFF ] << 16 ) ^
- ( (unsigned long) FSb[ ( RK[11] >> 24 ) & 0xFF ] << 24 );
-
- RK[13] = RK[5] ^ RK[12];
- RK[14] = RK[6] ^ RK[13];
- RK[15] = RK[7] ^ RK[14];
- }
- break;
-
- default:
-
- break;
- }
-
- return( 0 );
-}
-
-/*
- * AES key schedule (decryption)
- */
-int aes_setkey_dec( aes_context *ctx, const unsigned char *key, unsigned int keysize )
-{
- int i, j;
- aes_context cty;
- unsigned long *RK;
- unsigned long *SK;
- int ret;
-
- switch( keysize )
- {
- case 128: ctx->nr = 10; break;
- case 192: ctx->nr = 12; break;
- case 256: ctx->nr = 14; break;
- default : return( POLARSSL_ERR_AES_INVALID_KEY_LENGTH );
- }
-
-#if defined(PADLOCK_ALIGN16)
- ctx->rk = RK = PADLOCK_ALIGN16( ctx->buf );
-#else
- ctx->rk = RK = ctx->buf;
-#endif
-
- ret = aes_setkey_enc( &cty, key, keysize );
- if( ret != 0 )
- return( ret );
-
- SK = cty.rk + cty.nr * 4;
-
- *RK++ = *SK++;
- *RK++ = *SK++;
- *RK++ = *SK++;
- *RK++ = *SK++;
-
- for( i = ctx->nr - 1, SK -= 8; i > 0; i--, SK -= 8 )
- {
- for( j = 0; j < 4; j++, SK++ )
- {
- *RK++ = RT0[ FSb[ ( *SK ) & 0xFF ] ] ^
- RT1[ FSb[ ( *SK >> 8 ) & 0xFF ] ] ^
- RT2[ FSb[ ( *SK >> 16 ) & 0xFF ] ] ^
- RT3[ FSb[ ( *SK >> 24 ) & 0xFF ] ];
- }
- }
-
- *RK++ = *SK++;
- *RK++ = *SK++;
- *RK++ = *SK++;
- *RK++ = *SK++;
-
- memset( &cty, 0, sizeof( aes_context ) );
-
- return( 0 );
-}
-
-#define AES_FROUND(X0,X1,X2,X3,Y0,Y1,Y2,Y3) \
-{ \
- X0 = *RK++ ^ FT0[ ( Y0 ) & 0xFF ] ^ \
- FT1[ ( Y1 >> 8 ) & 0xFF ] ^ \
- FT2[ ( Y2 >> 16 ) & 0xFF ] ^ \
- FT3[ ( Y3 >> 24 ) & 0xFF ]; \
- \
- X1 = *RK++ ^ FT0[ ( Y1 ) & 0xFF ] ^ \
- FT1[ ( Y2 >> 8 ) & 0xFF ] ^ \
- FT2[ ( Y3 >> 16 ) & 0xFF ] ^ \
- FT3[ ( Y0 >> 24 ) & 0xFF ]; \
- \
- X2 = *RK++ ^ FT0[ ( Y2 ) & 0xFF ] ^ \
- FT1[ ( Y3 >> 8 ) & 0xFF ] ^ \
- FT2[ ( Y0 >> 16 ) & 0xFF ] ^ \
- FT3[ ( Y1 >> 24 ) & 0xFF ]; \
- \
- X3 = *RK++ ^ FT0[ ( Y3 ) & 0xFF ] ^ \
- FT1[ ( Y0 >> 8 ) & 0xFF ] ^ \
- FT2[ ( Y1 >> 16 ) & 0xFF ] ^ \
- FT3[ ( Y2 >> 24 ) & 0xFF ]; \
-}
-
-#define AES_RROUND(X0,X1,X2,X3,Y0,Y1,Y2,Y3) \
-{ \
- X0 = *RK++ ^ RT0[ ( Y0 ) & 0xFF ] ^ \
- RT1[ ( Y3 >> 8 ) & 0xFF ] ^ \
- RT2[ ( Y2 >> 16 ) & 0xFF ] ^ \
- RT3[ ( Y1 >> 24 ) & 0xFF ]; \
- \
- X1 = *RK++ ^ RT0[ ( Y1 ) & 0xFF ] ^ \
- RT1[ ( Y0 >> 8 ) & 0xFF ] ^ \
- RT2[ ( Y3 >> 16 ) & 0xFF ] ^ \
- RT3[ ( Y2 >> 24 ) & 0xFF ]; \
- \
- X2 = *RK++ ^ RT0[ ( Y2 ) & 0xFF ] ^ \
- RT1[ ( Y1 >> 8 ) & 0xFF ] ^ \
- RT2[ ( Y0 >> 16 ) & 0xFF ] ^ \
- RT3[ ( Y3 >> 24 ) & 0xFF ]; \
- \
- X3 = *RK++ ^ RT0[ ( Y3 ) & 0xFF ] ^ \
- RT1[ ( Y2 >> 8 ) & 0xFF ] ^ \
- RT2[ ( Y1 >> 16 ) & 0xFF ] ^ \
- RT3[ ( Y0 >> 24 ) & 0xFF ]; \
-}
-
-/*
- * AES-ECB block encryption/decryption
- */
-int aes_crypt_ecb( aes_context *ctx,
- int mode,
- const unsigned char input[16],
- unsigned char output[16] )
-{
- int i;
- unsigned long *RK, X0, X1, X2, X3, Y0, Y1, Y2, Y3;
-
-#if defined(POLARSSL_PADLOCK_C) && defined(POLARSSL_HAVE_X86)
- if( padlock_supports( PADLOCK_ACE ) )
- {
- if( padlock_xcryptecb( ctx, mode, input, output ) == 0 )
- return( 0 );
-
- // If padlock data misaligned, we just fall back to
- // unaccelerated mode
- //
- }
-#endif
-
- RK = ctx->rk;
-
- GET_ULONG_LE( X0, input, 0 ); X0 ^= *RK++;
- GET_ULONG_LE( X1, input, 4 ); X1 ^= *RK++;
- GET_ULONG_LE( X2, input, 8 ); X2 ^= *RK++;
- GET_ULONG_LE( X3, input, 12 ); X3 ^= *RK++;
-
- if( mode == AES_DECRYPT )
- {
- for( i = (ctx->nr >> 1) - 1; i > 0; i-- )
- {
- AES_RROUND( Y0, Y1, Y2, Y3, X0, X1, X2, X3 );
- AES_RROUND( X0, X1, X2, X3, Y0, Y1, Y2, Y3 );
- }
-
- AES_RROUND( Y0, Y1, Y2, Y3, X0, X1, X2, X3 );
-
- X0 = *RK++ ^ \
- ( (unsigned long) RSb[ ( Y0 ) & 0xFF ] ) ^
- ( (unsigned long) RSb[ ( Y3 >> 8 ) & 0xFF ] << 8 ) ^
- ( (unsigned long) RSb[ ( Y2 >> 16 ) & 0xFF ] << 16 ) ^
- ( (unsigned long) RSb[ ( Y1 >> 24 ) & 0xFF ] << 24 );
-
- X1 = *RK++ ^ \
- ( (unsigned long) RSb[ ( Y1 ) & 0xFF ] ) ^
- ( (unsigned long) RSb[ ( Y0 >> 8 ) & 0xFF ] << 8 ) ^
- ( (unsigned long) RSb[ ( Y3 >> 16 ) & 0xFF ] << 16 ) ^
- ( (unsigned long) RSb[ ( Y2 >> 24 ) & 0xFF ] << 24 );
-
- X2 = *RK++ ^ \
- ( (unsigned long) RSb[ ( Y2 ) & 0xFF ] ) ^
- ( (unsigned long) RSb[ ( Y1 >> 8 ) & 0xFF ] << 8 ) ^
- ( (unsigned long) RSb[ ( Y0 >> 16 ) & 0xFF ] << 16 ) ^
- ( (unsigned long) RSb[ ( Y3 >> 24 ) & 0xFF ] << 24 );
-
- X3 = *RK++ ^ \
- ( (unsigned long) RSb[ ( Y3 ) & 0xFF ] ) ^
- ( (unsigned long) RSb[ ( Y2 >> 8 ) & 0xFF ] << 8 ) ^
- ( (unsigned long) RSb[ ( Y1 >> 16 ) & 0xFF ] << 16 ) ^
- ( (unsigned long) RSb[ ( Y0 >> 24 ) & 0xFF ] << 24 );
- }
- else /* AES_ENCRYPT */
- {
- for( i = (ctx->nr >> 1) - 1; i > 0; i-- )
- {
- AES_FROUND( Y0, Y1, Y2, Y3, X0, X1, X2, X3 );
- AES_FROUND( X0, X1, X2, X3, Y0, Y1, Y2, Y3 );
- }
-
- AES_FROUND( Y0, Y1, Y2, Y3, X0, X1, X2, X3 );
-
- X0 = *RK++ ^ \
- ( (unsigned long) FSb[ ( Y0 ) & 0xFF ] ) ^
- ( (unsigned long) FSb[ ( Y1 >> 8 ) & 0xFF ] << 8 ) ^
- ( (unsigned long) FSb[ ( Y2 >> 16 ) & 0xFF ] << 16 ) ^
- ( (unsigned long) FSb[ ( Y3 >> 24 ) & 0xFF ] << 24 );
-
- X1 = *RK++ ^ \
- ( (unsigned long) FSb[ ( Y1 ) & 0xFF ] ) ^
- ( (unsigned long) FSb[ ( Y2 >> 8 ) & 0xFF ] << 8 ) ^
- ( (unsigned long) FSb[ ( Y3 >> 16 ) & 0xFF ] << 16 ) ^
- ( (unsigned long) FSb[ ( Y0 >> 24 ) & 0xFF ] << 24 );
-
- X2 = *RK++ ^ \
- ( (unsigned long) FSb[ ( Y2 ) & 0xFF ] ) ^
- ( (unsigned long) FSb[ ( Y3 >> 8 ) & 0xFF ] << 8 ) ^
- ( (unsigned long) FSb[ ( Y0 >> 16 ) & 0xFF ] << 16 ) ^
- ( (unsigned long) FSb[ ( Y1 >> 24 ) & 0xFF ] << 24 );
-
- X3 = *RK++ ^ \
- ( (unsigned long) FSb[ ( Y3 ) & 0xFF ] ) ^
- ( (unsigned long) FSb[ ( Y0 >> 8 ) & 0xFF ] << 8 ) ^
- ( (unsigned long) FSb[ ( Y1 >> 16 ) & 0xFF ] << 16 ) ^
- ( (unsigned long) FSb[ ( Y2 >> 24 ) & 0xFF ] << 24 );
- }
-
- PUT_ULONG_LE( X0, output, 0 );
- PUT_ULONG_LE( X1, output, 4 );
- PUT_ULONG_LE( X2, output, 8 );
- PUT_ULONG_LE( X3, output, 12 );
-
- return( 0 );
-}
-
-/*
- * AES-CBC buffer encryption/decryption
- */
-int aes_crypt_cbc( aes_context *ctx,
- int mode,
- size_t length,
- unsigned char iv[16],
- const unsigned char *input,
- unsigned char *output )
-{
- int i;
- unsigned char temp[16];
-
- if( length % 16 )
- return( POLARSSL_ERR_AES_INVALID_INPUT_LENGTH );
-
-#if defined(POLARSSL_PADLOCK_C) && defined(POLARSSL_HAVE_X86)
- if( padlock_supports( PADLOCK_ACE ) )
- {
- if( padlock_xcryptcbc( ctx, mode, length, iv, input, output ) == 0 )
- return( 0 );
-
- // If padlock data misaligned, we just fall back to
- // unaccelerated mode
- //
- }
-#endif
-
- if( mode == AES_DECRYPT )
- {
- while( length > 0 )
- {
- memcpy( temp, input, 16 );
- aes_crypt_ecb( ctx, mode, input, output );
-
- for( i = 0; i < 16; i++ )
- output[i] = (unsigned char)( output[i] ^ iv[i] );
-
- memcpy( iv, temp, 16 );
-
- input += 16;
- output += 16;
- length -= 16;
- }
- }
- else
- {
- while( length > 0 )
- {
- for( i = 0; i < 16; i++ )
- output[i] = (unsigned char)( input[i] ^ iv[i] );
-
- aes_crypt_ecb( ctx, mode, output, output );
- memcpy( iv, output, 16 );
-
- input += 16;
- output += 16;
- length -= 16;
- }
- }
-
- return( 0 );
-}
-
-#if defined(POLARSSL_CIPHER_MODE_CFB)
-/*
- * AES-CFB128 buffer encryption/decryption
- */
-int aes_crypt_cfb128( aes_context *ctx,
- int mode,
- size_t length,
- size_t *iv_off,
- unsigned char iv[16],
- const unsigned char *input,
- unsigned char *output )
-{
- int c;
- size_t n = *iv_off;
-
- if( mode == AES_DECRYPT )
- {
- while( length-- )
- {
- if( n == 0 )
- aes_crypt_ecb( ctx, AES_ENCRYPT, iv, iv );
-
- c = *input++;
- *output++ = (unsigned char)( c ^ iv[n] );
- iv[n] = (unsigned char) c;
-
- n = (n + 1) & 0x0F;
- }
- }
- else
- {
- while( length-- )
- {
- if( n == 0 )
- aes_crypt_ecb( ctx, AES_ENCRYPT, iv, iv );
-
- iv[n] = *output++ = (unsigned char)( iv[n] ^ *input++ );
-
- n = (n + 1) & 0x0F;
- }
- }
-
- *iv_off = n;
-
- return( 0 );
-}
-#endif /*POLARSSL_CIPHER_MODE_CFB */
-
-#if defined(POLARSSL_CIPHER_MODE_CTR)
-/*
- * AES-CTR buffer encryption/decryption
- */
-int aes_crypt_ctr( aes_context *ctx,
- size_t length,
- size_t *nc_off,
- unsigned char nonce_counter[16],
- unsigned char stream_block[16],
- const unsigned char *input,
- unsigned char *output )
-{
- int c, i, cb;
- size_t n = *nc_off;
-
- while( length-- )
- {
- if( n == 0 ) {
- aes_crypt_ecb( ctx, AES_ENCRYPT, nonce_counter, stream_block );
-
- i = 15;
- do {
- nonce_counter[i]++;
- cb = nonce_counter[i] == 0;
- } while( i-- && cb );
-
- }
- c = *input++;
- *output++ = (unsigned char)( c ^ stream_block[n] );
-
- n = (n + 1) & 0x0F;
- }
-
- *nc_off = n;
-
- return( 0 );
-}
-#endif /* POLARSSL_CIPHER_MODE_CTR */
-
-#if defined(POLARSSL_SELF_TEST)
-
-#include <stdio.h>
-
-/*
- * AES test vectors from:
- *
- * http://csrc.nist.gov/archive/aes/rijndael/rijndael-vals.zip
- */
-static const unsigned char aes_test_ecb_dec[3][16] =
-{
- { 0x44, 0x41, 0x6A, 0xC2, 0xD1, 0xF5, 0x3C, 0x58,
- 0x33, 0x03, 0x91, 0x7E, 0x6B, 0xE9, 0xEB, 0xE0 },
- { 0x48, 0xE3, 0x1E, 0x9E, 0x25, 0x67, 0x18, 0xF2,
- 0x92, 0x29, 0x31, 0x9C, 0x19, 0xF1, 0x5B, 0xA4 },
- { 0x05, 0x8C, 0xCF, 0xFD, 0xBB, 0xCB, 0x38, 0x2D,
- 0x1F, 0x6F, 0x56, 0x58, 0x5D, 0x8A, 0x4A, 0xDE }
-};
-
-static const unsigned char aes_test_ecb_enc[3][16] =
-{
- { 0xC3, 0x4C, 0x05, 0x2C, 0xC0, 0xDA, 0x8D, 0x73,
- 0x45, 0x1A, 0xFE, 0x5F, 0x03, 0xBE, 0x29, 0x7F },
- { 0xF3, 0xF6, 0x75, 0x2A, 0xE8, 0xD7, 0x83, 0x11,
- 0x38, 0xF0, 0x41, 0x56, 0x06, 0x31, 0xB1, 0x14 },
- { 0x8B, 0x79, 0xEE, 0xCC, 0x93, 0xA0, 0xEE, 0x5D,
- 0xFF, 0x30, 0xB4, 0xEA, 0x21, 0x63, 0x6D, 0xA4 }
-};
-
-static const unsigned char aes_test_cbc_dec[3][16] =
-{
- { 0xFA, 0xCA, 0x37, 0xE0, 0xB0, 0xC8, 0x53, 0x73,
- 0xDF, 0x70, 0x6E, 0x73, 0xF7, 0xC9, 0xAF, 0x86 },
- { 0x5D, 0xF6, 0x78, 0xDD, 0x17, 0xBA, 0x4E, 0x75,
- 0xB6, 0x17, 0x68, 0xC6, 0xAD, 0xEF, 0x7C, 0x7B },
- { 0x48, 0x04, 0xE1, 0x81, 0x8F, 0xE6, 0x29, 0x75,
- 0x19, 0xA3, 0xE8, 0x8C, 0x57, 0x31, 0x04, 0x13 }
-};
-
-static const unsigned char aes_test_cbc_enc[3][16] =
-{
- { 0x8A, 0x05, 0xFC, 0x5E, 0x09, 0x5A, 0xF4, 0x84,
- 0x8A, 0x08, 0xD3, 0x28, 0xD3, 0x68, 0x8E, 0x3D },
- { 0x7B, 0xD9, 0x66, 0xD5, 0x3A, 0xD8, 0xC1, 0xBB,
- 0x85, 0xD2, 0xAD, 0xFA, 0xE8, 0x7B, 0xB1, 0x04 },
- { 0xFE, 0x3C, 0x53, 0x65, 0x3E, 0x2F, 0x45, 0xB5,
- 0x6F, 0xCD, 0x88, 0xB2, 0xCC, 0x89, 0x8F, 0xF0 }
-};
-
-#if defined(POLARSSL_CIPHER_MODE_CFB)
-/*
- * AES-CFB128 test vectors from:
- *
- * http://csrc.nist.gov/publications/nistpubs/800-38a/sp800-38a.pdf
- */
-static const unsigned char aes_test_cfb128_key[3][32] =
-{
- { 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6,
- 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C },
- { 0x8E, 0x73, 0xB0, 0xF7, 0xDA, 0x0E, 0x64, 0x52,
- 0xC8, 0x10, 0xF3, 0x2B, 0x80, 0x90, 0x79, 0xE5,
- 0x62, 0xF8, 0xEA, 0xD2, 0x52, 0x2C, 0x6B, 0x7B },
- { 0x60, 0x3D, 0xEB, 0x10, 0x15, 0xCA, 0x71, 0xBE,
- 0x2B, 0x73, 0xAE, 0xF0, 0x85, 0x7D, 0x77, 0x81,
- 0x1F, 0x35, 0x2C, 0x07, 0x3B, 0x61, 0x08, 0xD7,
- 0x2D, 0x98, 0x10, 0xA3, 0x09, 0x14, 0xDF, 0xF4 }
-};
-
-static const unsigned char aes_test_cfb128_iv[16] =
-{
- 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
- 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
-};
-
-static const unsigned char aes_test_cfb128_pt[64] =
-{
- 0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96,
- 0xE9, 0x3D, 0x7E, 0x11, 0x73, 0x93, 0x17, 0x2A,
- 0xAE, 0x2D, 0x8A, 0x57, 0x1E, 0x03, 0xAC, 0x9C,
- 0x9E, 0xB7, 0x6F, 0xAC, 0x45, 0xAF, 0x8E, 0x51,
- 0x30, 0xC8, 0x1C, 0x46, 0xA3, 0x5C, 0xE4, 0x11,
- 0xE5, 0xFB, 0xC1, 0x19, 0x1A, 0x0A, 0x52, 0xEF,
- 0xF6, 0x9F, 0x24, 0x45, 0xDF, 0x4F, 0x9B, 0x17,
- 0xAD, 0x2B, 0x41, 0x7B, 0xE6, 0x6C, 0x37, 0x10
-};
-
-static const unsigned char aes_test_cfb128_ct[3][64] =
-{
- { 0x3B, 0x3F, 0xD9, 0x2E, 0xB7, 0x2D, 0xAD, 0x20,
- 0x33, 0x34, 0x49, 0xF8, 0xE8, 0x3C, 0xFB, 0x4A,
- 0xC8, 0xA6, 0x45, 0x37, 0xA0, 0xB3, 0xA9, 0x3F,
- 0xCD, 0xE3, 0xCD, 0xAD, 0x9F, 0x1C, 0xE5, 0x8B,
- 0x26, 0x75, 0x1F, 0x67, 0xA3, 0xCB, 0xB1, 0x40,
- 0xB1, 0x80, 0x8C, 0xF1, 0x87, 0xA4, 0xF4, 0xDF,
- 0xC0, 0x4B, 0x05, 0x35, 0x7C, 0x5D, 0x1C, 0x0E,
- 0xEA, 0xC4, 0xC6, 0x6F, 0x9F, 0xF7, 0xF2, 0xE6 },
- { 0xCD, 0xC8, 0x0D, 0x6F, 0xDD, 0xF1, 0x8C, 0xAB,
- 0x34, 0xC2, 0x59, 0x09, 0xC9, 0x9A, 0x41, 0x74,
- 0x67, 0xCE, 0x7F, 0x7F, 0x81, 0x17, 0x36, 0x21,
- 0x96, 0x1A, 0x2B, 0x70, 0x17, 0x1D, 0x3D, 0x7A,
- 0x2E, 0x1E, 0x8A, 0x1D, 0xD5, 0x9B, 0x88, 0xB1,
- 0xC8, 0xE6, 0x0F, 0xED, 0x1E, 0xFA, 0xC4, 0xC9,
- 0xC0, 0x5F, 0x9F, 0x9C, 0xA9, 0x83, 0x4F, 0xA0,
- 0x42, 0xAE, 0x8F, 0xBA, 0x58, 0x4B, 0x09, 0xFF },
- { 0xDC, 0x7E, 0x84, 0xBF, 0xDA, 0x79, 0x16, 0x4B,
- 0x7E, 0xCD, 0x84, 0x86, 0x98, 0x5D, 0x38, 0x60,
- 0x39, 0xFF, 0xED, 0x14, 0x3B, 0x28, 0xB1, 0xC8,
- 0x32, 0x11, 0x3C, 0x63, 0x31, 0xE5, 0x40, 0x7B,
- 0xDF, 0x10, 0x13, 0x24, 0x15, 0xE5, 0x4B, 0x92,
- 0xA1, 0x3E, 0xD0, 0xA8, 0x26, 0x7A, 0xE2, 0xF9,
- 0x75, 0xA3, 0x85, 0x74, 0x1A, 0xB9, 0xCE, 0xF8,
- 0x20, 0x31, 0x62, 0x3D, 0x55, 0xB1, 0xE4, 0x71 }
-};
-#endif /* POLARSSL_CIPHER_MODE_CFB */
-
-#if defined(POLARSSL_CIPHER_MODE_CTR)
-/*
- * AES-CTR test vectors from:
- *
- * http://www.faqs.org/rfcs/rfc3686.html
- */
-
-static const unsigned char aes_test_ctr_key[3][16] =
-{
- { 0xAE, 0x68, 0x52, 0xF8, 0x12, 0x10, 0x67, 0xCC,
- 0x4B, 0xF7, 0xA5, 0x76, 0x55, 0x77, 0xF3, 0x9E },
- { 0x7E, 0x24, 0x06, 0x78, 0x17, 0xFA, 0xE0, 0xD7,
- 0x43, 0xD6, 0xCE, 0x1F, 0x32, 0x53, 0x91, 0x63 },
- { 0x76, 0x91, 0xBE, 0x03, 0x5E, 0x50, 0x20, 0xA8,
- 0xAC, 0x6E, 0x61, 0x85, 0x29, 0xF9, 0xA0, 0xDC }
-};
-
-static const unsigned char aes_test_ctr_nonce_counter[3][16] =
-{
- { 0x00, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 },
- { 0x00, 0x6C, 0xB6, 0xDB, 0xC0, 0x54, 0x3B, 0x59,
- 0xDA, 0x48, 0xD9, 0x0B, 0x00, 0x00, 0x00, 0x01 },
- { 0x00, 0xE0, 0x01, 0x7B, 0x27, 0x77, 0x7F, 0x3F,
- 0x4A, 0x17, 0x86, 0xF0, 0x00, 0x00, 0x00, 0x01 }
-};
-
-static const unsigned char aes_test_ctr_pt[3][48] =
-{
- { 0x53, 0x69, 0x6E, 0x67, 0x6C, 0x65, 0x20, 0x62,
- 0x6C, 0x6F, 0x63, 0x6B, 0x20, 0x6D, 0x73, 0x67 },
-
- { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
- 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
- 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
- 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F },
-
- { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
- 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
- 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
- 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F,
- 0x20, 0x21, 0x22, 0x23 }
-};
-
-static const unsigned char aes_test_ctr_ct[3][48] =
-{
- { 0xE4, 0x09, 0x5D, 0x4F, 0xB7, 0xA7, 0xB3, 0x79,
- 0x2D, 0x61, 0x75, 0xA3, 0x26, 0x13, 0x11, 0xB8 },
- { 0x51, 0x04, 0xA1, 0x06, 0x16, 0x8A, 0x72, 0xD9,
- 0x79, 0x0D, 0x41, 0xEE, 0x8E, 0xDA, 0xD3, 0x88,
- 0xEB, 0x2E, 0x1E, 0xFC, 0x46, 0xDA, 0x57, 0xC8,
- 0xFC, 0xE6, 0x30, 0xDF, 0x91, 0x41, 0xBE, 0x28 },
- { 0xC1, 0xCF, 0x48, 0xA8, 0x9F, 0x2F, 0xFD, 0xD9,
- 0xCF, 0x46, 0x52, 0xE9, 0xEF, 0xDB, 0x72, 0xD7,
- 0x45, 0x40, 0xA4, 0x2B, 0xDE, 0x6D, 0x78, 0x36,
- 0xD5, 0x9A, 0x5C, 0xEA, 0xAE, 0xF3, 0x10, 0x53,
- 0x25, 0xB2, 0x07, 0x2F }
-};
-
-static const int aes_test_ctr_len[3] =
- { 16, 32, 36 };
-#endif /* POLARSSL_CIPHER_MODE_CTR */
-
-/*
- * Checkup routine
- */
-int aes_self_test( int verbose )
-{
- int i, j, u, v;
- unsigned char key[32];
- unsigned char buf[64];
- unsigned char prv[16];
- unsigned char iv[16];
-#if defined(POLARSSL_CIPHER_MODE_CTR) || defined(POLARSSL_CIPHER_MODE_CFB)
- size_t offset;
-#endif
-#if defined(POLARSSL_CIPHER_MODE_CTR)
- int len;
- unsigned char nonce_counter[16];
- unsigned char stream_block[16];
-#endif
- aes_context ctx;
-
- memset( key, 0, 32 );
-
- /*
- * ECB mode
- */
- for( i = 0; i < 6; i++ )
- {
- u = i >> 1;
- v = i & 1;
-
- if( verbose != 0 )
- printf( " AES-ECB-%3d (%s): ", 128 + u * 64,
- ( v == AES_DECRYPT ) ? "dec" : "enc" );
-
- memset( buf, 0, 16 );
-
- if( v == AES_DECRYPT )
- {
- aes_setkey_dec( &ctx, key, 128 + u * 64 );
-
- for( j = 0; j < 10000; j++ )
- aes_crypt_ecb( &ctx, v, buf, buf );
-
- if( memcmp( buf, aes_test_ecb_dec[u], 16 ) != 0 )
- {
- if( verbose != 0 )
- printf( "failed\n" );
-
- return( 1 );
- }
- }
- else
- {
- aes_setkey_enc( &ctx, key, 128 + u * 64 );
-
- for( j = 0; j < 10000; j++ )
- aes_crypt_ecb( &ctx, v, buf, buf );
-
- if( memcmp( buf, aes_test_ecb_enc[u], 16 ) != 0 )
- {
- if( verbose != 0 )
- printf( "failed\n" );
-
- return( 1 );
- }
- }
-
- if( verbose != 0 )
- printf( "passed\n" );
- }
-
- if( verbose != 0 )
- printf( "\n" );
-
- /*
- * CBC mode
- */
- for( i = 0; i < 6; i++ )
- {
- u = i >> 1;
- v = i & 1;
-
- if( verbose != 0 )
- printf( " AES-CBC-%3d (%s): ", 128 + u * 64,
- ( v == AES_DECRYPT ) ? "dec" : "enc" );
-
- memset( iv , 0, 16 );
- memset( prv, 0, 16 );
- memset( buf, 0, 16 );
-
- if( v == AES_DECRYPT )
- {
- aes_setkey_dec( &ctx, key, 128 + u * 64 );
-
- for( j = 0; j < 10000; j++ )
- aes_crypt_cbc( &ctx, v, 16, iv, buf, buf );
-
- if( memcmp( buf, aes_test_cbc_dec[u], 16 ) != 0 )
- {
- if( verbose != 0 )
- printf( "failed\n" );
-
- return( 1 );
- }
- }
- else
- {
- aes_setkey_enc( &ctx, key, 128 + u * 64 );
-
- for( j = 0; j < 10000; j++ )
- {
- unsigned char tmp[16];
-
- aes_crypt_cbc( &ctx, v, 16, iv, buf, buf );
-
- memcpy( tmp, prv, 16 );
- memcpy( prv, buf, 16 );
- memcpy( buf, tmp, 16 );
- }
-
- if( memcmp( prv, aes_test_cbc_enc[u], 16 ) != 0 )
- {
- if( verbose != 0 )
- printf( "failed\n" );
-
- return( 1 );
- }
- }
-
- if( verbose != 0 )
- printf( "passed\n" );
- }
-
- if( verbose != 0 )
- printf( "\n" );
-
-#if defined(POLARSSL_CIPHER_MODE_CFB)
- /*
- * CFB128 mode
- */
- for( i = 0; i < 6; i++ )
- {
- u = i >> 1;
- v = i & 1;
-
- if( verbose != 0 )
- printf( " AES-CFB128-%3d (%s): ", 128 + u * 64,
- ( v == AES_DECRYPT ) ? "dec" : "enc" );
-
- memcpy( iv, aes_test_cfb128_iv, 16 );
- memcpy( key, aes_test_cfb128_key[u], 16 + u * 8 );
-
- offset = 0;
- aes_setkey_enc( &ctx, key, 128 + u * 64 );
-
- if( v == AES_DECRYPT )
- {
- memcpy( buf, aes_test_cfb128_ct[u], 64 );
- aes_crypt_cfb128( &ctx, v, 64, &offset, iv, buf, buf );
-
- if( memcmp( buf, aes_test_cfb128_pt, 64 ) != 0 )
- {
- if( verbose != 0 )
- printf( "failed\n" );
-
- return( 1 );
- }
- }
- else
- {
- memcpy( buf, aes_test_cfb128_pt, 64 );
- aes_crypt_cfb128( &ctx, v, 64, &offset, iv, buf, buf );
-
- if( memcmp( buf, aes_test_cfb128_ct[u], 64 ) != 0 )
- {
- if( verbose != 0 )
- printf( "failed\n" );
-
- return( 1 );
- }
- }
-
- if( verbose != 0 )
- printf( "passed\n" );
- }
-
- if( verbose != 0 )
- printf( "\n" );
-#endif /* POLARSSL_CIPHER_MODE_CFB */
-
-#if defined(POLARSSL_CIPHER_MODE_CTR)
- /*
- * CTR mode
- */
- for( i = 0; i < 6; i++ )
- {
- u = i >> 1;
- v = i & 1;
-
- if( verbose != 0 )
- printf( " AES-CTR-128 (%s): ",
- ( v == AES_DECRYPT ) ? "dec" : "enc" );
-
- memcpy( nonce_counter, aes_test_ctr_nonce_counter[u], 16 );
- memcpy( key, aes_test_ctr_key[u], 16 );
-
- offset = 0;
- aes_setkey_enc( &ctx, key, 128 );
-
- if( v == AES_DECRYPT )
- {
- len = aes_test_ctr_len[u];
- memcpy( buf, aes_test_ctr_ct[u], len );
-
- aes_crypt_ctr( &ctx, len, &offset, nonce_counter, stream_block, buf, buf );
-
- if( memcmp( buf, aes_test_ctr_pt[u], len ) != 0 )
- {
- if( verbose != 0 )
- printf( "failed\n" );
-
- return( 1 );
- }
- }
- else
- {
- len = aes_test_ctr_len[u];
- memcpy( buf, aes_test_ctr_pt[u], len );
-
- aes_crypt_ctr( &ctx, len, &offset, nonce_counter, stream_block, buf, buf );
-
- if( memcmp( buf, aes_test_ctr_ct[u], len ) != 0 )
- {
- if( verbose != 0 )
- printf( "failed\n" );
-
- return( 1 );
- }
- }
-
- if( verbose != 0 )
- printf( "passed\n" );
- }
-
- if( verbose != 0 )
- printf( "\n" );
-#endif /* POLARSSL_CIPHER_MODE_CTR */
-
- return( 0 );
-}
-
-#endif
-
-#endif
diff --git a/protocols/Tlen/src/crypto/bignum.c b/protocols/Tlen/src/crypto/bignum.c
deleted file mode 100644
index eabfbfe6f9..0000000000
--- a/protocols/Tlen/src/crypto/bignum.c
+++ /dev/null
@@ -1,2097 +0,0 @@
-/*
- * Multi-precision integer library
- *
- * Copyright (C) 2006-2010, Brainspark B.V.
- *
- * This file is part of PolarSSL (http://www.polarssl.org)
- * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
- *
- * All rights reserved.
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program 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 General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
- */
-/*
- * This MPI implementation is based on:
- *
- * http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf
- * http://www.stillhq.com/extracted/gnupg-api/mpi/
- * http://math.libtomcrypt.com/files/tommath.pdf
- */
-
-#include "polarssl/config.h"
-
-#if defined(POLARSSL_BIGNUM_C)
-
-#include "polarssl/bignum.h"
-#include "polarssl/bn_mul.h"
-
-#include <stdlib.h>
-
-#define ciL (sizeof(t_uint)) /* chars in limb */
-#define biL (ciL << 3) /* bits in limb */
-#define biH (ciL << 2) /* half limb size */
-
-/*
- * Convert between bits/chars and number of limbs
- */
-#define BITS_TO_LIMBS(i) (((i) + biL - 1) / biL)
-#define CHARS_TO_LIMBS(i) (((i) + ciL - 1) / ciL)
-
-/*
- * Initialize one MPI
- */
-void mpi_init( mpi *X )
-{
- if( X == NULL )
- return;
-
- X->s = 1;
- X->n = 0;
- X->p = NULL;
-}
-
-/*
- * Unallocate one MPI
- */
-void mpi_free( mpi *X )
-{
- if( X == NULL )
- return;
-
- if( X->p != NULL )
- {
- memset( X->p, 0, X->n * ciL );
- free( X->p );
- }
-
- X->s = 1;
- X->n = 0;
- X->p = NULL;
-}
-
-/*
- * Enlarge to the specified number of limbs
- */
-int mpi_grow( mpi *X, size_t nblimbs )
-{
- t_uint *p;
-
- if( nblimbs > POLARSSL_MPI_MAX_LIMBS )
- return( 1 );
-
- if( X->n < nblimbs )
- {
- if( ( p = (t_uint *) malloc( nblimbs * ciL ) ) == NULL )
- return( 1 );
-
- memset( p, 0, nblimbs * ciL );
-
- if( X->p != NULL )
- {
- memcpy( p, X->p, X->n * ciL );
- memset( X->p, 0, X->n * ciL );
- free( X->p );
- }
-
- X->n = nblimbs;
- X->p = p;
- }
-
- return( 0 );
-}
-
-/*
- * Copy the contents of Y into X
- */
-int mpi_copy( mpi *X, const mpi *Y )
-{
- int ret;
- size_t i;
-
- if( X == Y )
- return( 0 );
-
- for( i = Y->n - 1; i > 0; i-- )
- if( Y->p[i] != 0 )
- break;
- i++;
-
- X->s = Y->s;
-
- MPI_CHK( mpi_grow( X, i ) );
-
- memset( X->p, 0, X->n * ciL );
- memcpy( X->p, Y->p, i * ciL );
-
-cleanup:
-
- return( ret );
-}
-
-/*
- * Swap the contents of X and Y
- */
-void mpi_swap( mpi *X, mpi *Y )
-{
- mpi T;
-
- memcpy( &T, X, sizeof( mpi ) );
- memcpy( X, Y, sizeof( mpi ) );
- memcpy( Y, &T, sizeof( mpi ) );
-}
-
-/*
- * Set value from integer
- */
-int mpi_lset( mpi *X, t_sint z )
-{
- int ret;
-
- MPI_CHK( mpi_grow( X, 1 ) );
- memset( X->p, 0, X->n * ciL );
-
- X->p[0] = ( z < 0 ) ? -z : z;
- X->s = ( z < 0 ) ? -1 : 1;
-
-cleanup:
-
- return( ret );
-}
-
-/*
- * Get a specific bit
- */
-int mpi_get_bit( mpi *X, size_t pos )
-{
- if( X->n * biL <= pos )
- return( 0 );
-
- return ( X->p[pos / biL] >> ( pos % biL ) ) & 0x01;
-}
-
-/*
- * Set a bit to a specific value of 0 or 1
- */
-int mpi_set_bit( mpi *X, size_t pos, unsigned char val )
-{
- int ret = 0;
- size_t off = pos / biL;
- size_t idx = pos % biL;
-
- if( val != 0 && val != 1 )
- return POLARSSL_ERR_MPI_BAD_INPUT_DATA;
-
- if( X->n * biL <= pos )
- {
- if( val == 0 )
- return ( 0 );
-
- MPI_CHK( mpi_grow( X, off + 1 ) );
- }
-
- X->p[off] = ( X->p[off] & ~( 0x01 << idx ) ) | ( val << idx );
-
-cleanup:
-
- return( ret );
-}
-
-/*
- * Return the number of least significant bits
- */
-size_t mpi_lsb( const mpi *X )
-{
- size_t i, j, count = 0;
-
- for( i = 0; i < X->n; i++ )
- for( j = 0; j < biL; j++, count++ )
- if( ( ( X->p[i] >> j ) & 1 ) != 0 )
- return( count );
-
- return( 0 );
-}
-
-/*
- * Return the number of most significant bits
- */
-size_t mpi_msb( const mpi *X )
-{
- size_t i, j;
-
- for( i = X->n - 1; i > 0; i-- )
- if( X->p[i] != 0 )
- break;
-
- for( j = biL; j > 0; j-- )
- if( ( ( X->p[i] >> ( j - 1 ) ) & 1 ) != 0 )
- break;
-
- return( ( i * biL ) + j );
-}
-
-/*
- * Return the total size in bytes
- */
-size_t mpi_size( const mpi *X )
-{
- return( ( mpi_msb( X ) + 7 ) >> 3 );
-}
-
-/*
- * Convert an ASCII character to digit value
- */
-static int mpi_get_digit( t_uint *d, int radix, char c )
-{
- *d = 255;
-
- if( c >= 0x30 && c <= 0x39 ) *d = c - 0x30;
- if( c >= 0x41 && c <= 0x46 ) *d = c - 0x37;
- if( c >= 0x61 && c <= 0x66 ) *d = c - 0x57;
-
- if( *d >= (t_uint) radix )
- return( POLARSSL_ERR_MPI_INVALID_CHARACTER );
-
- return( 0 );
-}
-
-/*
- * Import from an ASCII string
- */
-int mpi_read_string( mpi *X, int radix, const char *s )
-{
- int ret;
- size_t i, j, slen, n;
- t_uint d;
- mpi T;
-
- if( radix < 2 || radix > 16 )
- return( POLARSSL_ERR_MPI_BAD_INPUT_DATA );
-
- mpi_init( &T );
-
- slen = strlen( s );
-
- if( radix == 16 )
- {
- n = BITS_TO_LIMBS( slen << 2 );
-
- MPI_CHK( mpi_grow( X, n ) );
- MPI_CHK( mpi_lset( X, 0 ) );
-
- for( i = slen, j = 0; i > 0; i--, j++ )
- {
- if( i == 1 && s[i - 1] == '-' )
- {
- X->s = -1;
- break;
- }
-
- MPI_CHK( mpi_get_digit( &d, radix, s[i - 1] ) );
- X->p[j / (2 * ciL)] |= d << ( (j % (2 * ciL)) << 2 );
- }
- }
- else
- {
- MPI_CHK( mpi_lset( X, 0 ) );
-
- for( i = 0; i < slen; i++ )
- {
- if( i == 0 && s[i] == '-' )
- {
- X->s = -1;
- continue;
- }
-
- MPI_CHK( mpi_get_digit( &d, radix, s[i] ) );
- MPI_CHK( mpi_mul_int( &T, X, radix ) );
-
- if( X->s == 1 )
- {
- MPI_CHK( mpi_add_int( X, &T, d ) );
- }
- else
- {
- MPI_CHK( mpi_sub_int( X, &T, d ) );
- }
- }
- }
-
-cleanup:
-
- mpi_free( &T );
-
- return( ret );
-}
-
-/*
- * Helper to write the digits high-order first
- */
-static int mpi_write_hlp( mpi *X, int radix, char **p )
-{
- int ret;
- t_uint r;
-
- if( radix < 2 || radix > 16 )
- return( POLARSSL_ERR_MPI_BAD_INPUT_DATA );
-
- MPI_CHK( mpi_mod_int( &r, X, radix ) );
- MPI_CHK( mpi_div_int( X, NULL, X, radix ) );
-
- if( mpi_cmp_int( X, 0 ) != 0 )
- MPI_CHK( mpi_write_hlp( X, radix, p ) );
-
- if( r < 10 )
- *(*p)++ = (char)( r + 0x30 );
- else
- *(*p)++ = (char)( r + 0x37 );
-
-cleanup:
-
- return( ret );
-}
-
-/*
- * Export into an ASCII string
- */
-int mpi_write_string( const mpi *X, int radix, char *s, size_t *slen )
-{
- int ret = 0;
- size_t n;
- char *p;
- mpi T;
-
- if( radix < 2 || radix > 16 )
- return( POLARSSL_ERR_MPI_BAD_INPUT_DATA );
-
- n = mpi_msb( X );
- if( radix >= 4 ) n >>= 1;
- if( radix >= 16 ) n >>= 1;
- n += 3;
-
- if( *slen < n )
- {
- *slen = n;
- return( POLARSSL_ERR_MPI_BUFFER_TOO_SMALL );
- }
-
- p = s;
- mpi_init( &T );
-
- if( X->s == -1 )
- *p++ = '-';
-
- if( radix == 16 )
- {
- int c;
- size_t i, j, k;
-
- for( i = X->n, k = 0; i > 0; i-- )
- {
- for( j = ciL; j > 0; j-- )
- {
- c = ( X->p[i - 1] >> ( ( j - 1 ) << 3) ) & 0xFF;
-
- if( c == 0 && k == 0 && ( i + j + 3 ) != 0 )
- continue;
-
- p += sprintf( p, "%02X", c );
- k = 1;
- }
- }
- }
- else
- {
- MPI_CHK( mpi_copy( &T, X ) );
-
- if( T.s == -1 )
- T.s = 1;
-
- MPI_CHK( mpi_write_hlp( &T, radix, &p ) );
- }
-
- *p++ = '\0';
- *slen = p - s;
-
-cleanup:
-
- mpi_free( &T );
-
- return( ret );
-}
-
-#if defined(POLARSSL_FS_IO)
-/*
- * Read X from an opened file
- */
-int mpi_read_file( mpi *X, int radix, FILE *fin )
-{
- t_uint d;
- size_t slen;
- char *p;
- char s[1024];
-
- memset( s, 0, sizeof( s ) );
- if( fgets( s, sizeof( s ) - 1, fin ) == NULL )
- return( POLARSSL_ERR_MPI_FILE_IO_ERROR );
-
- slen = strlen( s );
- if( s[slen - 1] == '\n' ) { slen--; s[slen] = '\0'; }
- if( s[slen - 1] == '\r' ) { slen--; s[slen] = '\0'; }
-
- p = s + slen;
- while( --p >= s )
- if( mpi_get_digit( &d, radix, *p ) != 0 )
- break;
-
- return( mpi_read_string( X, radix, p + 1 ) );
-}
-
-/*
- * Write X into an opened file (or stdout if fout == NULL)
- */
-int mpi_write_file( const char *p, const mpi *X, int radix, FILE *fout )
-{
- int ret;
- size_t n, slen, plen;
- char s[2048];
-
- n = sizeof( s );
- memset( s, 0, n );
- n -= 2;
-
- MPI_CHK( mpi_write_string( X, radix, s, (size_t *) &n ) );
-
- if( p == NULL ) p = "";
-
- plen = strlen( p );
- slen = strlen( s );
- s[slen++] = '\r';
- s[slen++] = '\n';
-
- if( fout != NULL )
- {
- if( fwrite( p, 1, plen, fout ) != plen ||
- fwrite( s, 1, slen, fout ) != slen )
- return( POLARSSL_ERR_MPI_FILE_IO_ERROR );
- }
- else
- printf( "%s%s", p, s );
-
-cleanup:
-
- return( ret );
-}
-#endif /* POLARSSL_FS_IO */
-
-/*
- * Import X from unsigned binary data, big endian
- */
-int mpi_read_binary( mpi *X, const unsigned char *buf, size_t buflen )
-{
- int ret;
- size_t i, j, n;
-
- for( n = 0; n < buflen; n++ )
- if( buf[n] != 0 )
- break;
-
- MPI_CHK( mpi_grow( X, CHARS_TO_LIMBS( buflen - n ) ) );
- MPI_CHK( mpi_lset( X, 0 ) );
-
- for( i = buflen, j = 0; i > n; i--, j++ )
- X->p[j / ciL] |= ((t_uint) buf[i - 1]) << ((j % ciL) << 3);
-
-cleanup:
-
- return( ret );
-}
-
-/*
- * Export X into unsigned binary data, big endian
- */
-int mpi_write_binary( const mpi *X, unsigned char *buf, size_t buflen )
-{
- size_t i, j, n;
-
- n = mpi_size( X );
-
- if( buflen < n )
- return( POLARSSL_ERR_MPI_BUFFER_TOO_SMALL );
-
- memset( buf, 0, buflen );
-
- for( i = buflen - 1, j = 0; n > 0; i--, j++, n-- )
- buf[i] = (unsigned char)( X->p[j / ciL] >> ((j % ciL) << 3) );
-
- return( 0 );
-}
-
-/*
- * Left-shift: X <<= count
- */
-int mpi_shift_l( mpi *X, size_t count )
-{
- int ret;
- size_t i, v0, t1;
- t_uint r0 = 0, r1;
-
- v0 = count / (biL );
- t1 = count & (biL - 1);
-
- i = mpi_msb( X ) + count;
-
- if( X->n * biL < i )
- MPI_CHK( mpi_grow( X, BITS_TO_LIMBS( i ) ) );
-
- ret = 0;
-
- /*
- * shift by count / limb_size
- */
- if( v0 > 0 )
- {
- for( i = X->n; i > v0; i-- )
- X->p[i - 1] = X->p[i - v0 - 1];
-
- for( ; i > 0; i-- )
- X->p[i - 1] = 0;
- }
-
- /*
- * shift by count % limb_size
- */
- if( t1 > 0 )
- {
- for( i = v0; i < X->n; i++ )
- {
- r1 = X->p[i] >> (biL - t1);
- X->p[i] <<= t1;
- X->p[i] |= r0;
- r0 = r1;
- }
- }
-
-cleanup:
-
- return( ret );
-}
-
-/*
- * Right-shift: X >>= count
- */
-int mpi_shift_r( mpi *X, size_t count )
-{
- size_t i, v0, v1;
- t_uint r0 = 0, r1;
-
- v0 = count / biL;
- v1 = count & (biL - 1);
-
- /*
- * shift by count / limb_size
- */
- if( v0 > 0 )
- {
- for( i = 0; i < X->n - v0; i++ )
- X->p[i] = X->p[i + v0];
-
- for( ; i < X->n; i++ )
- X->p[i] = 0;
- }
-
- /*
- * shift by count % limb_size
- */
- if( v1 > 0 )
- {
- for( i = X->n; i > 0; i-- )
- {
- r1 = X->p[i - 1] << (biL - v1);
- X->p[i - 1] >>= v1;
- X->p[i - 1] |= r0;
- r0 = r1;
- }
- }
-
- return( 0 );
-}
-
-/*
- * Compare unsigned values
- */
-int mpi_cmp_abs( const mpi *X, const mpi *Y )
-{
- size_t i, j;
-
- for( i = X->n; i > 0; i-- )
- if( X->p[i - 1] != 0 )
- break;
-
- for( j = Y->n; j > 0; j-- )
- if( Y->p[j - 1] != 0 )
- break;
-
- if( i == 0 && j == 0 )
- return( 0 );
-
- if( i > j ) return( 1 );
- if( j > i ) return( -1 );
-
- for( ; i > 0; i-- )
- {
- if( X->p[i - 1] > Y->p[i - 1] ) return( 1 );
- if( X->p[i - 1] < Y->p[i - 1] ) return( -1 );
- }
-
- return( 0 );
-}
-
-/*
- * Compare signed values
- */
-int mpi_cmp_mpi( const mpi *X, const mpi *Y )
-{
- size_t i, j;
-
- for( i = X->n; i > 0; i-- )
- if( X->p[i - 1] != 0 )
- break;
-
- for( j = Y->n; j > 0; j-- )
- if( Y->p[j - 1] != 0 )
- break;
-
- if( i == 0 && j == 0 )
- return( 0 );
-
- if( i > j ) return( X->s );
- if( j > i ) return( -X->s );
-
- if( X->s > 0 && Y->s < 0 ) return( 1 );
- if( Y->s > 0 && X->s < 0 ) return( -1 );
-
- for( ; i > 0; i-- )
- {
- if( X->p[i - 1] > Y->p[i - 1] ) return( X->s );
- if( X->p[i - 1] < Y->p[i - 1] ) return( -X->s );
- }
-
- return( 0 );
-}
-
-/*
- * Compare signed values
- */
-int mpi_cmp_int( const mpi *X, t_sint z )
-{
- mpi Y;
- t_uint p[1];
-
- *p = ( z < 0 ) ? -z : z;
- Y.s = ( z < 0 ) ? -1 : 1;
- Y.n = 1;
- Y.p = p;
-
- return( mpi_cmp_mpi( X, &Y ) );
-}
-
-/*
- * Unsigned addition: X = |A| + |B| (HAC 14.7)
- */
-int mpi_add_abs( mpi *X, const mpi *A, const mpi *B )
-{
- int ret;
- size_t i, j;
- t_uint *o, *p, c;
-
- if( X == B )
- {
- const mpi *T = A; A = X; B = T;
- }
-
- if( X != A )
- MPI_CHK( mpi_copy( X, A ) );
-
- /*
- * X should always be positive as a result of unsigned additions.
- */
- X->s = 1;
-
- for( j = B->n; j > 0; j-- )
- if( B->p[j - 1] != 0 )
- break;
-
- MPI_CHK( mpi_grow( X, j ) );
-
- o = B->p; p = X->p; c = 0;
-
- for( i = 0; i < j; i++, o++, p++ )
- {
- *p += c; c = ( *p < c );
- *p += *o; c += ( *p < *o );
- }
-
- while( c != 0 )
- {
- if( i >= X->n )
- {
- MPI_CHK( mpi_grow( X, i + 1 ) );
- p = X->p + i;
- }
-
- *p += c; c = ( *p < c ); i++;
- }
-
-cleanup:
-
- return( ret );
-}
-
-/*
- * Helper for mpi substraction
- */
-static void mpi_sub_hlp( size_t n, t_uint *s, t_uint *d )
-{
- size_t i;
- t_uint c, z;
-
- for( i = c = 0; i < n; i++, s++, d++ )
- {
- z = ( *d < c ); *d -= c;
- c = ( *d < *s ) + z; *d -= *s;
- }
-
- while( c != 0 )
- {
- z = ( *d < c ); *d -= c;
- c = z; i++; d++;
- }
-}
-
-/*
- * Unsigned substraction: X = |A| - |B| (HAC 14.9)
- */
-int mpi_sub_abs( mpi *X, const mpi *A, const mpi *B )
-{
- mpi TB;
- int ret;
- size_t n;
-
- if( mpi_cmp_abs( A, B ) < 0 )
- return( POLARSSL_ERR_MPI_NEGATIVE_VALUE );
-
- mpi_init( &TB );
-
- if( X == B )
- {
- MPI_CHK( mpi_copy( &TB, B ) );
- B = &TB;
- }
-
- if( X != A )
- MPI_CHK( mpi_copy( X, A ) );
-
- /*
- * X should always be positive as a result of unsigned substractions.
- */
- X->s = 1;
-
- ret = 0;
-
- for( n = B->n; n > 0; n-- )
- if( B->p[n - 1] != 0 )
- break;
-
- mpi_sub_hlp( n, B->p, X->p );
-
-cleanup:
-
- mpi_free( &TB );
-
- return( ret );
-}
-
-/*
- * Signed addition: X = A + B
- */
-int mpi_add_mpi( mpi *X, const mpi *A, const mpi *B )
-{
- int ret, s = A->s;
-
- if( A->s * B->s < 0 )
- {
- if( mpi_cmp_abs( A, B ) >= 0 )
- {
- MPI_CHK( mpi_sub_abs( X, A, B ) );
- X->s = s;
- }
- else
- {
- MPI_CHK( mpi_sub_abs( X, B, A ) );
- X->s = -s;
- }
- }
- else
- {
- MPI_CHK( mpi_add_abs( X, A, B ) );
- X->s = s;
- }
-
-cleanup:
-
- return( ret );
-}
-
-/*
- * Signed substraction: X = A - B
- */
-int mpi_sub_mpi( mpi *X, const mpi *A, const mpi *B )
-{
- int ret, s = A->s;
-
- if( A->s * B->s > 0 )
- {
- if( mpi_cmp_abs( A, B ) >= 0 )
- {
- MPI_CHK( mpi_sub_abs( X, A, B ) );
- X->s = s;
- }
- else
- {
- MPI_CHK( mpi_sub_abs( X, B, A ) );
- X->s = -s;
- }
- }
- else
- {
- MPI_CHK( mpi_add_abs( X, A, B ) );
- X->s = s;
- }
-
-cleanup:
-
- return( ret );
-}
-
-/*
- * Signed addition: X = A + b
- */
-int mpi_add_int( mpi *X, const mpi *A, t_sint b )
-{
- mpi _B;
- t_uint p[1];
-
- p[0] = ( b < 0 ) ? -b : b;
- _B.s = ( b < 0 ) ? -1 : 1;
- _B.n = 1;
- _B.p = p;
-
- return( mpi_add_mpi( X, A, &_B ) );
-}
-
-/*
- * Signed substraction: X = A - b
- */
-int mpi_sub_int( mpi *X, const mpi *A, t_sint b )
-{
- mpi _B;
- t_uint p[1];
-
- p[0] = ( b < 0 ) ? -b : b;
- _B.s = ( b < 0 ) ? -1 : 1;
- _B.n = 1;
- _B.p = p;
-
- return( mpi_sub_mpi( X, A, &_B ) );
-}
-
-/*
- * Helper for mpi multiplication
- */
-static void mpi_mul_hlp( size_t i, t_uint *s, t_uint *d, t_uint b )
-{
- t_uint c = 0, t = 0;
-
-#if defined(MULADDC_HUIT)
- for( ; i >= 8; i -= 8 )
- {
- MULADDC_INIT
- MULADDC_HUIT
- MULADDC_STOP
- }
-
- for( ; i > 0; i-- )
- {
- MULADDC_INIT
- MULADDC_CORE
- MULADDC_STOP
- }
-#else
- for( ; i >= 16; i -= 16 )
- {
- MULADDC_INIT
- MULADDC_CORE MULADDC_CORE
- MULADDC_CORE MULADDC_CORE
- MULADDC_CORE MULADDC_CORE
- MULADDC_CORE MULADDC_CORE
-
- MULADDC_CORE MULADDC_CORE
- MULADDC_CORE MULADDC_CORE
- MULADDC_CORE MULADDC_CORE
- MULADDC_CORE MULADDC_CORE
- MULADDC_STOP
- }
-
- for( ; i >= 8; i -= 8 )
- {
- MULADDC_INIT
- MULADDC_CORE MULADDC_CORE
- MULADDC_CORE MULADDC_CORE
-
- MULADDC_CORE MULADDC_CORE
- MULADDC_CORE MULADDC_CORE
- MULADDC_STOP
- }
-
- for( ; i > 0; i-- )
- {
- MULADDC_INIT
- MULADDC_CORE
- MULADDC_STOP
- }
-#endif
-
- t++;
-
- do {
- *d += c; c = ( *d < c ); d++;
- }
- while( c != 0 );
-}
-
-/*
- * Baseline multiplication: X = A * B (HAC 14.12)
- */
-int mpi_mul_mpi( mpi *X, const mpi *A, const mpi *B )
-{
- int ret;
- size_t i, j;
- mpi TA, TB;
-
- mpi_init( &TA ); mpi_init( &TB );
-
- if( X == A ) { MPI_CHK( mpi_copy( &TA, A ) ); A = &TA; }
- if( X == B ) { MPI_CHK( mpi_copy( &TB, B ) ); B = &TB; }
-
- for( i = A->n; i > 0; i-- )
- if( A->p[i - 1] != 0 )
- break;
-
- for( j = B->n; j > 0; j-- )
- if( B->p[j - 1] != 0 )
- break;
-
- MPI_CHK( mpi_grow( X, i + j ) );
- MPI_CHK( mpi_lset( X, 0 ) );
-
- for( i++; j > 0; j-- )
- mpi_mul_hlp( i - 1, A->p, X->p + j - 1, B->p[j - 1] );
-
- X->s = A->s * B->s;
-
-cleanup:
-
- mpi_free( &TB ); mpi_free( &TA );
-
- return( ret );
-}
-
-/*
- * Baseline multiplication: X = A * b
- */
-int mpi_mul_int( mpi *X, const mpi *A, t_sint b )
-{
- mpi _B;
- t_uint p[1];
-
- _B.s = 1;
- _B.n = 1;
- _B.p = p;
- p[0] = b;
-
- return( mpi_mul_mpi( X, A, &_B ) );
-}
-
-/*
- * Division by mpi: A = Q * B + R (HAC 14.20)
- */
-int mpi_div_mpi( mpi *Q, mpi *R, const mpi *A, const mpi *B )
-{
- int ret;
- size_t i, n, t, k;
- mpi X, Y, Z, T1, T2;
-
- if( mpi_cmp_int( B, 0 ) == 0 )
- return( POLARSSL_ERR_MPI_DIVISION_BY_ZERO );
-
- mpi_init( &X ); mpi_init( &Y ); mpi_init( &Z );
- mpi_init( &T1 ); mpi_init( &T2 );
-
- if( mpi_cmp_abs( A, B ) < 0 )
- {
- if( Q != NULL ) MPI_CHK( mpi_lset( Q, 0 ) );
- if( R != NULL ) MPI_CHK( mpi_copy( R, A ) );
- return( 0 );
- }
-
- MPI_CHK( mpi_copy( &X, A ) );
- MPI_CHK( mpi_copy( &Y, B ) );
- X.s = Y.s = 1;
-
- MPI_CHK( mpi_grow( &Z, A->n + 2 ) );
- MPI_CHK( mpi_lset( &Z, 0 ) );
- MPI_CHK( mpi_grow( &T1, 2 ) );
- MPI_CHK( mpi_grow( &T2, 3 ) );
-
- k = mpi_msb( &Y ) % biL;
- if( k < biL - 1 )
- {
- k = biL - 1 - k;
- MPI_CHK( mpi_shift_l( &X, k ) );
- MPI_CHK( mpi_shift_l( &Y, k ) );
- }
- else k = 0;
-
- n = X.n - 1;
- t = Y.n - 1;
- mpi_shift_l( &Y, biL * (n - t) );
-
- while( mpi_cmp_mpi( &X, &Y ) >= 0 )
- {
- Z.p[n - t]++;
- mpi_sub_mpi( &X, &X, &Y );
- }
- mpi_shift_r( &Y, biL * (n - t) );
-
- for( i = n; i > t ; i-- )
- {
- if( X.p[i] >= Y.p[t] )
- Z.p[i - t - 1] = ~0;
- else
- {
-#if defined(POLARSSL_HAVE_LONGLONG)
- t_dbl r;
-
- r = (t_dbl) X.p[i] << biL;
- r |= (t_dbl) X.p[i - 1];
- r /= Y.p[t];
- if( r > ((t_dbl) 1 << biL) - 1)
- r = ((t_dbl) 1 << biL) - 1;
-
- Z.p[i - t - 1] = (t_uint) r;
-#else
- /*
- * __udiv_qrnnd_c, from gmp/longlong.h
- */
- t_uint q0, q1, r0, r1;
- t_uint d0, d1, d, m;
-
- d = Y.p[t];
- d0 = ( d << biH ) >> biH;
- d1 = ( d >> biH );
-
- q1 = X.p[i] / d1;
- r1 = X.p[i] - d1 * q1;
- r1 <<= biH;
- r1 |= ( X.p[i - 1] >> biH );
-
- m = q1 * d0;
- if( r1 < m )
- {
- q1--, r1 += d;
- while( r1 >= d && r1 < m )
- q1--, r1 += d;
- }
- r1 -= m;
-
- q0 = r1 / d1;
- r0 = r1 - d1 * q0;
- r0 <<= biH;
- r0 |= ( X.p[i - 1] << biH ) >> biH;
-
- m = q0 * d0;
- if( r0 < m )
- {
- q0--, r0 += d;
- while( r0 >= d && r0 < m )
- q0--, r0 += d;
- }
- r0 -= m;
-
- Z.p[i - t - 1] = ( q1 << biH ) | q0;
-#endif
- }
-
- Z.p[i - t - 1]++;
- do
- {
- Z.p[i - t - 1]--;
-
- MPI_CHK( mpi_lset( &T1, 0 ) );
- T1.p[0] = (t < 1) ? 0 : Y.p[t - 1];
- T1.p[1] = Y.p[t];
- MPI_CHK( mpi_mul_int( &T1, &T1, Z.p[i - t - 1] ) );
-
- MPI_CHK( mpi_lset( &T2, 0 ) );
- T2.p[0] = (i < 2) ? 0 : X.p[i - 2];
- T2.p[1] = (i < 1) ? 0 : X.p[i - 1];
- T2.p[2] = X.p[i];
- }
- while( mpi_cmp_mpi( &T1, &T2 ) > 0 );
-
- MPI_CHK( mpi_mul_int( &T1, &Y, Z.p[i - t - 1] ) );
- MPI_CHK( mpi_shift_l( &T1, biL * (i - t - 1) ) );
- MPI_CHK( mpi_sub_mpi( &X, &X, &T1 ) );
-
- if( mpi_cmp_int( &X, 0 ) < 0 )
- {
- MPI_CHK( mpi_copy( &T1, &Y ) );
- MPI_CHK( mpi_shift_l( &T1, biL * (i - t - 1) ) );
- MPI_CHK( mpi_add_mpi( &X, &X, &T1 ) );
- Z.p[i - t - 1]--;
- }
- }
-
- if( Q != NULL )
- {
- mpi_copy( Q, &Z );
- Q->s = A->s * B->s;
- }
-
- if( R != NULL )
- {
- mpi_shift_r( &X, k );
- mpi_copy( R, &X );
-
- R->s = A->s;
- if( mpi_cmp_int( R, 0 ) == 0 )
- R->s = 1;
- }
-
-cleanup:
-
- mpi_free( &X ); mpi_free( &Y ); mpi_free( &Z );
- mpi_free( &T1 ); mpi_free( &T2 );
-
- return( ret );
-}
-
-/*
- * Division by int: A = Q * b + R
- *
- * Returns 0 if successful
- * 1 if memory allocation failed
- * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if b == 0
- */
-int mpi_div_int( mpi *Q, mpi *R, const mpi *A, t_sint b )
-{
- mpi _B;
- t_uint p[1];
-
- p[0] = ( b < 0 ) ? -b : b;
- _B.s = ( b < 0 ) ? -1 : 1;
- _B.n = 1;
- _B.p = p;
-
- return( mpi_div_mpi( Q, R, A, &_B ) );
-}
-
-/*
- * Modulo: R = A mod B
- */
-int mpi_mod_mpi( mpi *R, const mpi *A, const mpi *B )
-{
- int ret;
-
- if( mpi_cmp_int( B, 0 ) < 0 )
- return POLARSSL_ERR_MPI_NEGATIVE_VALUE;
-
- MPI_CHK( mpi_div_mpi( NULL, R, A, B ) );
-
- while( mpi_cmp_int( R, 0 ) < 0 )
- MPI_CHK( mpi_add_mpi( R, R, B ) );
-
- while( mpi_cmp_mpi( R, B ) >= 0 )
- MPI_CHK( mpi_sub_mpi( R, R, B ) );
-
-cleanup:
-
- return( ret );
-}
-
-/*
- * Modulo: r = A mod b
- */
-int mpi_mod_int( t_uint *r, const mpi *A, t_sint b )
-{
- size_t i;
- t_uint x, y, z;
-
- if( b == 0 )
- return( POLARSSL_ERR_MPI_DIVISION_BY_ZERO );
-
- if( b < 0 )
- return POLARSSL_ERR_MPI_NEGATIVE_VALUE;
-
- /*
- * handle trivial cases
- */
- if( b == 1 )
- {
- *r = 0;
- return( 0 );
- }
-
- if( b == 2 )
- {
- *r = A->p[0] & 1;
- return( 0 );
- }
-
- /*
- * general case
- */
- for( i = A->n, y = 0; i > 0; i-- )
- {
- x = A->p[i - 1];
- y = ( y << biH ) | ( x >> biH );
- z = y / b;
- y -= z * b;
-
- x <<= biH;
- y = ( y << biH ) | ( x >> biH );
- z = y / b;
- y -= z * b;
- }
-
- /*
- * If A is negative, then the current y represents a negative value.
- * Flipping it to the positive side.
- */
- if( A->s < 0 && y != 0 )
- y = b - y;
-
- *r = y;
-
- return( 0 );
-}
-
-/*
- * Fast Montgomery initialization (thanks to Tom St Denis)
- */
-static void mpi_montg_init( t_uint *mm, const mpi *N )
-{
- t_uint x, m0 = N->p[0];
-
- x = m0;
- x += ( ( m0 + 2 ) & 4 ) << 1;
- x *= ( 2 - ( m0 * x ) );
-
- if( biL >= 16 ) x *= ( 2 - ( m0 * x ) );
- if( biL >= 32 ) x *= ( 2 - ( m0 * x ) );
- if( biL >= 64 ) x *= ( 2 - ( m0 * x ) );
-
- *mm = ~x + 1;
-}
-
-/*
- * Montgomery multiplication: A = A * B * R^-1 mod N (HAC 14.36)
- */
-static void mpi_montmul( mpi *A, const mpi *B, const mpi *N, t_uint mm, const mpi *T )
-{
- size_t i, n, m;
- t_uint u0, u1, *d;
-
- memset( T->p, 0, T->n * ciL );
-
- d = T->p;
- n = N->n;
- m = ( B->n < n ) ? B->n : n;
-
- for( i = 0; i < n; i++ )
- {
- /*
- * T = (T + u0*B + u1*N) / 2^biL
- */
- u0 = A->p[i];
- u1 = ( d[0] + u0 * B->p[0] ) * mm;
-
- mpi_mul_hlp( m, B->p, d, u0 );
- mpi_mul_hlp( n, N->p, d, u1 );
-
- *d++ = u0; d[n + 1] = 0;
- }
-
- memcpy( A->p, d, (n + 1) * ciL );
-
- if( mpi_cmp_abs( A, N ) >= 0 )
- mpi_sub_hlp( n, N->p, A->p );
- else
- /* prevent timing attacks */
- mpi_sub_hlp( n, A->p, T->p );
-}
-
-/*
- * Montgomery reduction: A = A * R^-1 mod N
- */
-static void mpi_montred( mpi *A, const mpi *N, t_uint mm, const mpi *T )
-{
- t_uint z = 1;
- mpi U;
-
- U.n = U.s = z;
- U.p = &z;
-
- mpi_montmul( A, &U, N, mm, T );
-}
-
-/*
- * Sliding-window exponentiation: X = A^E mod N (HAC 14.85)
- */
-int mpi_exp_mod( mpi *X, const mpi *A, const mpi *E, const mpi *N, mpi *_RR )
-{
- int ret;
- size_t wbits, wsize, one = 1;
- size_t i, j, nblimbs;
- size_t bufsize, nbits;
- t_uint ei, mm, state;
- mpi RR, T, W[64];
-
- if( mpi_cmp_int( N, 0 ) < 0 || ( N->p[0] & 1 ) == 0 )
- return( POLARSSL_ERR_MPI_BAD_INPUT_DATA );
-
- /*
- * Init temps and window size
- */
- mpi_montg_init( &mm, N );
- mpi_init( &RR ); mpi_init( &T );
- memset( W, 0, sizeof( W ) );
-
- i = mpi_msb( E );
-
- wsize = ( i > 671 ) ? 6 : ( i > 239 ) ? 5 :
- ( i > 79 ) ? 4 : ( i > 23 ) ? 3 : 1;
-
- j = N->n + 1;
- MPI_CHK( mpi_grow( X, j ) );
- MPI_CHK( mpi_grow( &W[1], j ) );
- MPI_CHK( mpi_grow( &T, j * 2 ) );
-
- /*
- * If 1st call, pre-compute R^2 mod N
- */
- if( _RR == NULL || _RR->p == NULL )
- {
- MPI_CHK( mpi_lset( &RR, 1 ) );
- MPI_CHK( mpi_shift_l( &RR, N->n * 2 * biL ) );
- MPI_CHK( mpi_mod_mpi( &RR, &RR, N ) );
-
- if( _RR != NULL )
- memcpy( _RR, &RR, sizeof( mpi ) );
- }
- else
- memcpy( &RR, _RR, sizeof( mpi ) );
-
- /*
- * W[1] = A * R^2 * R^-1 mod N = A * R mod N
- */
- if( mpi_cmp_mpi( A, N ) >= 0 )
- mpi_mod_mpi( &W[1], A, N );
- else mpi_copy( &W[1], A );
-
- mpi_montmul( &W[1], &RR, N, mm, &T );
-
- /*
- * X = R^2 * R^-1 mod N = R mod N
- */
- MPI_CHK( mpi_copy( X, &RR ) );
- mpi_montred( X, N, mm, &T );
-
- if( wsize > 1 )
- {
- /*
- * W[1 << (wsize - 1)] = W[1] ^ (wsize - 1)
- */
- j = one << (wsize - 1);
-
- MPI_CHK( mpi_grow( &W[j], N->n + 1 ) );
- MPI_CHK( mpi_copy( &W[j], &W[1] ) );
-
- for( i = 0; i < wsize - 1; i++ )
- mpi_montmul( &W[j], &W[j], N, mm, &T );
-
- /*
- * W[i] = W[i - 1] * W[1]
- */
- for( i = j + 1; i < (one << wsize); i++ )
- {
- MPI_CHK( mpi_grow( &W[i], N->n + 1 ) );
- MPI_CHK( mpi_copy( &W[i], &W[i - 1] ) );
-
- mpi_montmul( &W[i], &W[1], N, mm, &T );
- }
- }
-
- nblimbs = E->n;
- bufsize = 0;
- nbits = 0;
- wbits = 0;
- state = 0;
-
- while( 1 )
- {
- if( bufsize == 0 )
- {
- if( nblimbs-- == 0 )
- break;
-
- bufsize = sizeof( t_uint ) << 3;
- }
-
- bufsize--;
-
- ei = (E->p[nblimbs] >> bufsize) & 1;
-
- /*
- * skip leading 0s
- */
- if( ei == 0 && state == 0 )
- continue;
-
- if( ei == 0 && state == 1 )
- {
- /*
- * out of window, square X
- */
- mpi_montmul( X, X, N, mm, &T );
- continue;
- }
-
- /*
- * add ei to current window
- */
- state = 2;
-
- nbits++;
- wbits |= (ei << (wsize - nbits));
-
- if( nbits == wsize )
- {
- /*
- * X = X^wsize R^-1 mod N
- */
- for( i = 0; i < wsize; i++ )
- mpi_montmul( X, X, N, mm, &T );
-
- /*
- * X = X * W[wbits] R^-1 mod N
- */
- mpi_montmul( X, &W[wbits], N, mm, &T );
-
- state--;
- nbits = 0;
- wbits = 0;
- }
- }
-
- /*
- * process the remaining bits
- */
- for( i = 0; i < nbits; i++ )
- {
- mpi_montmul( X, X, N, mm, &T );
-
- wbits <<= 1;
-
- if( (wbits & (one << wsize)) != 0 )
- mpi_montmul( X, &W[1], N, mm, &T );
- }
-
- /*
- * X = A^E * R * R^-1 mod N = A^E mod N
- */
- mpi_montred( X, N, mm, &T );
-
-cleanup:
-
- for( i = (one << (wsize - 1)); i < (one << wsize); i++ )
- mpi_free( &W[i] );
-
- mpi_free( &W[1] ); mpi_free( &T );
-
- if( _RR == NULL )
- mpi_free( &RR );
-
- return( ret );
-}
-
-/*
- * Greatest common divisor: G = gcd(A, B) (HAC 14.54)
- */
-int mpi_gcd( mpi *G, const mpi *A, const mpi *B )
-{
- int ret;
- size_t lz, lzt;
- mpi TG, TA, TB;
-
- mpi_init( &TG ); mpi_init( &TA ); mpi_init( &TB );
-
- MPI_CHK( mpi_copy( &TA, A ) );
- MPI_CHK( mpi_copy( &TB, B ) );
-
- lz = mpi_lsb( &TA );
- lzt = mpi_lsb( &TB );
-
- if ( lzt < lz )
- lz = lzt;
-
- MPI_CHK( mpi_shift_r( &TA, lz ) );
- MPI_CHK( mpi_shift_r( &TB, lz ) );
-
- TA.s = TB.s = 1;
-
- while( mpi_cmp_int( &TA, 0 ) != 0 )
- {
- MPI_CHK( mpi_shift_r( &TA, mpi_lsb( &TA ) ) );
- MPI_CHK( mpi_shift_r( &TB, mpi_lsb( &TB ) ) );
-
- if( mpi_cmp_mpi( &TA, &TB ) >= 0 )
- {
- MPI_CHK( mpi_sub_abs( &TA, &TA, &TB ) );
- MPI_CHK( mpi_shift_r( &TA, 1 ) );
- }
- else
- {
- MPI_CHK( mpi_sub_abs( &TB, &TB, &TA ) );
- MPI_CHK( mpi_shift_r( &TB, 1 ) );
- }
- }
-
- MPI_CHK( mpi_shift_l( &TB, lz ) );
- MPI_CHK( mpi_copy( G, &TB ) );
-
-cleanup:
-
- mpi_free( &TG ); mpi_free( &TA ); mpi_free( &TB );
-
- return( ret );
-}
-
-int mpi_fill_random( mpi *X, size_t size, int (*f_rng)(void *), void *p_rng )
-{
- int ret;
- size_t k;
- unsigned char *p;
-
- MPI_CHK( mpi_grow( X, size ) );
- MPI_CHK( mpi_lset( X, 0 ) );
-
- p = (unsigned char *) X->p;
- for( k = 0; k < X->n * ciL; k++ )
- *p++ = (unsigned char) f_rng( p_rng );
-
-cleanup:
- return( ret );
-}
-
-#if defined(POLARSSL_GENPRIME)
-
-/*
- * Modular inverse: X = A^-1 mod N (HAC 14.61 / 14.64)
- */
-int mpi_inv_mod( mpi *X, const mpi *A, const mpi *N )
-{
- int ret;
- mpi G, TA, TU, U1, U2, TB, TV, V1, V2;
-
- if( mpi_cmp_int( N, 0 ) <= 0 )
- return( POLARSSL_ERR_MPI_BAD_INPUT_DATA );
-
- mpi_init( &TA ); mpi_init( &TU ); mpi_init( &U1 ); mpi_init( &U2 );
- mpi_init( &G ); mpi_init( &TB ); mpi_init( &TV );
- mpi_init( &V1 ); mpi_init( &V2 );
-
- MPI_CHK( mpi_gcd( &G, A, N ) );
-
- if( mpi_cmp_int( &G, 1 ) != 0 )
- {
- ret = POLARSSL_ERR_MPI_NOT_ACCEPTABLE;
- goto cleanup;
- }
-
- MPI_CHK( mpi_mod_mpi( &TA, A, N ) );
- MPI_CHK( mpi_copy( &TU, &TA ) );
- MPI_CHK( mpi_copy( &TB, N ) );
- MPI_CHK( mpi_copy( &TV, N ) );
-
- MPI_CHK( mpi_lset( &U1, 1 ) );
- MPI_CHK( mpi_lset( &U2, 0 ) );
- MPI_CHK( mpi_lset( &V1, 0 ) );
- MPI_CHK( mpi_lset( &V2, 1 ) );
-
- do
- {
- while( ( TU.p[0] & 1 ) == 0 )
- {
- MPI_CHK( mpi_shift_r( &TU, 1 ) );
-
- if( ( U1.p[0] & 1 ) != 0 || ( U2.p[0] & 1 ) != 0 )
- {
- MPI_CHK( mpi_add_mpi( &U1, &U1, &TB ) );
- MPI_CHK( mpi_sub_mpi( &U2, &U2, &TA ) );
- }
-
- MPI_CHK( mpi_shift_r( &U1, 1 ) );
- MPI_CHK( mpi_shift_r( &U2, 1 ) );
- }
-
- while( ( TV.p[0] & 1 ) == 0 )
- {
- MPI_CHK( mpi_shift_r( &TV, 1 ) );
-
- if( ( V1.p[0] & 1 ) != 0 || ( V2.p[0] & 1 ) != 0 )
- {
- MPI_CHK( mpi_add_mpi( &V1, &V1, &TB ) );
- MPI_CHK( mpi_sub_mpi( &V2, &V2, &TA ) );
- }
-
- MPI_CHK( mpi_shift_r( &V1, 1 ) );
- MPI_CHK( mpi_shift_r( &V2, 1 ) );
- }
-
- if( mpi_cmp_mpi( &TU, &TV ) >= 0 )
- {
- MPI_CHK( mpi_sub_mpi( &TU, &TU, &TV ) );
- MPI_CHK( mpi_sub_mpi( &U1, &U1, &V1 ) );
- MPI_CHK( mpi_sub_mpi( &U2, &U2, &V2 ) );
- }
- else
- {
- MPI_CHK( mpi_sub_mpi( &TV, &TV, &TU ) );
- MPI_CHK( mpi_sub_mpi( &V1, &V1, &U1 ) );
- MPI_CHK( mpi_sub_mpi( &V2, &V2, &U2 ) );
- }
- }
- while( mpi_cmp_int( &TU, 0 ) != 0 );
-
- while( mpi_cmp_int( &V1, 0 ) < 0 )
- MPI_CHK( mpi_add_mpi( &V1, &V1, N ) );
-
- while( mpi_cmp_mpi( &V1, N ) >= 0 )
- MPI_CHK( mpi_sub_mpi( &V1, &V1, N ) );
-
- MPI_CHK( mpi_copy( X, &V1 ) );
-
-cleanup:
-
- mpi_free( &TA ); mpi_free( &TU ); mpi_free( &U1 ); mpi_free( &U2 );
- mpi_free( &G ); mpi_free( &TB ); mpi_free( &TV );
- mpi_free( &V1 ); mpi_free( &V2 );
-
- return( ret );
-}
-
-static const int small_prime[] =
-{
- 3, 5, 7, 11, 13, 17, 19, 23,
- 29, 31, 37, 41, 43, 47, 53, 59,
- 61, 67, 71, 73, 79, 83, 89, 97,
- 101, 103, 107, 109, 113, 127, 131, 137,
- 139, 149, 151, 157, 163, 167, 173, 179,
- 181, 191, 193, 197, 199, 211, 223, 227,
- 229, 233, 239, 241, 251, 257, 263, 269,
- 271, 277, 281, 283, 293, 307, 311, 313,
- 317, 331, 337, 347, 349, 353, 359, 367,
- 373, 379, 383, 389, 397, 401, 409, 419,
- 421, 431, 433, 439, 443, 449, 457, 461,
- 463, 467, 479, 487, 491, 499, 503, 509,
- 521, 523, 541, 547, 557, 563, 569, 571,
- 577, 587, 593, 599, 601, 607, 613, 617,
- 619, 631, 641, 643, 647, 653, 659, 661,
- 673, 677, 683, 691, 701, 709, 719, 727,
- 733, 739, 743, 751, 757, 761, 769, 773,
- 787, 797, 809, 811, 821, 823, 827, 829,
- 839, 853, 857, 859, 863, 877, 881, 883,
- 887, 907, 911, 919, 929, 937, 941, 947,
- 953, 967, 971, 977, 983, 991, 997, -103
-};
-
-/*
- * Miller-Rabin primality test (HAC 4.24)
- */
-int mpi_is_prime( mpi *X, int (*f_rng)(void *), void *p_rng )
-{
- int ret, xs;
- size_t i, j, n, s;
- mpi W, R, T, A, RR;
-
- if( mpi_cmp_int( X, 0 ) == 0 ||
- mpi_cmp_int( X, 1 ) == 0 )
- return( POLARSSL_ERR_MPI_NOT_ACCEPTABLE );
-
- if( mpi_cmp_int( X, 2 ) == 0 )
- return( 0 );
-
- mpi_init( &W ); mpi_init( &R ); mpi_init( &T ); mpi_init( &A );
- mpi_init( &RR );
-
- xs = X->s; X->s = 1;
-
- /*
- * test trivial factors first
- */
- if( ( X->p[0] & 1 ) == 0 )
- return( POLARSSL_ERR_MPI_NOT_ACCEPTABLE );
-
- for( i = 0; small_prime[i] > 0; i++ )
- {
- t_uint r;
-
- if( mpi_cmp_int( X, small_prime[i] ) <= 0 )
- return( 0 );
-
- MPI_CHK( mpi_mod_int( &r, X, small_prime[i] ) );
-
- if( r == 0 )
- return( POLARSSL_ERR_MPI_NOT_ACCEPTABLE );
- }
-
- /*
- * W = |X| - 1
- * R = W >> lsb( W )
- */
- MPI_CHK( mpi_sub_int( &W, X, 1 ) );
- s = mpi_lsb( &W );
- MPI_CHK( mpi_copy( &R, &W ) );
- MPI_CHK( mpi_shift_r( &R, s ) );
-
- i = mpi_msb( X );
- /*
- * HAC, table 4.4
- */
- n = ( ( i >= 1300 ) ? 2 : ( i >= 850 ) ? 3 :
- ( i >= 650 ) ? 4 : ( i >= 350 ) ? 8 :
- ( i >= 250 ) ? 12 : ( i >= 150 ) ? 18 : 27 );
-
- for( i = 0; i < n; i++ )
- {
- /*
- * pick a random A, 1 < A < |X| - 1
- */
- mpi_fill_random( &A, X->n, f_rng, p_rng );
-
- if( mpi_cmp_mpi( &A, &W ) >= 0 )
- {
- j = mpi_msb( &A ) - mpi_msb( &W );
- MPI_CHK( mpi_shift_r( &A, j + 1 ) );
- }
- A.p[0] |= 3;
-
- /*
- * A = A^R mod |X|
- */
- MPI_CHK( mpi_exp_mod( &A, &A, &R, X, &RR ) );
-
- if( mpi_cmp_mpi( &A, &W ) == 0 ||
- mpi_cmp_int( &A, 1 ) == 0 )
- continue;
-
- j = 1;
- while( j < s && mpi_cmp_mpi( &A, &W ) != 0 )
- {
- /*
- * A = A * A mod |X|
- */
- MPI_CHK( mpi_mul_mpi( &T, &A, &A ) );
- MPI_CHK( mpi_mod_mpi( &A, &T, X ) );
-
- if( mpi_cmp_int( &A, 1 ) == 0 )
- break;
-
- j++;
- }
-
- /*
- * not prime if A != |X| - 1 or A == 1
- */
- if( mpi_cmp_mpi( &A, &W ) != 0 ||
- mpi_cmp_int( &A, 1 ) == 0 )
- {
- ret = POLARSSL_ERR_MPI_NOT_ACCEPTABLE;
- break;
- }
- }
-
-cleanup:
-
- X->s = xs;
-
- mpi_free( &W ); mpi_free( &R ); mpi_free( &T ); mpi_free( &A );
- mpi_free( &RR );
-
- return( ret );
-}
-
-/*
- * Prime number generation
- */
-int mpi_gen_prime( mpi *X, size_t nbits, int dh_flag,
- int (*f_rng)(void *), void *p_rng )
-{
- int ret;
- size_t k, n;
- mpi Y;
-
- if( nbits < 3 || nbits > 4096 )
- return( POLARSSL_ERR_MPI_BAD_INPUT_DATA );
-
- mpi_init( &Y );
-
- n = BITS_TO_LIMBS( nbits );
-
- mpi_fill_random( X, n, f_rng, p_rng );
-
- k = mpi_msb( X );
- if( k < nbits ) MPI_CHK( mpi_shift_l( X, nbits - k ) );
- if( k > nbits ) MPI_CHK( mpi_shift_r( X, k - nbits ) );
-
- X->p[0] |= 3;
-
- if( dh_flag == 0 )
- {
- while( ( ret = mpi_is_prime( X, f_rng, p_rng ) ) != 0 )
- {
- if( ret != POLARSSL_ERR_MPI_NOT_ACCEPTABLE )
- goto cleanup;
-
- MPI_CHK( mpi_add_int( X, X, 2 ) );
- }
- }
- else
- {
- MPI_CHK( mpi_sub_int( &Y, X, 1 ) );
- MPI_CHK( mpi_shift_r( &Y, 1 ) );
-
- while( 1 )
- {
- if( ( ret = mpi_is_prime( X, f_rng, p_rng ) ) == 0 )
- {
- if( ( ret = mpi_is_prime( &Y, f_rng, p_rng ) ) == 0 )
- break;
-
- if( ret != POLARSSL_ERR_MPI_NOT_ACCEPTABLE )
- goto cleanup;
- }
-
- if( ret != POLARSSL_ERR_MPI_NOT_ACCEPTABLE )
- goto cleanup;
-
- MPI_CHK( mpi_add_int( &Y, X, 1 ) );
- MPI_CHK( mpi_add_int( X, X, 2 ) );
- MPI_CHK( mpi_shift_r( &Y, 1 ) );
- }
- }
-
-cleanup:
-
- mpi_free( &Y );
-
- return( ret );
-}
-
-#endif
-
-#if defined(POLARSSL_SELF_TEST)
-
-#define GCD_PAIR_COUNT 3
-
-static const int gcd_pairs[GCD_PAIR_COUNT][3] =
-{
- { 693, 609, 21 },
- { 1764, 868, 28 },
- { 768454923, 542167814, 1 }
-};
-
-/*
- * Checkup routine
- */
-int mpi_self_test( int verbose )
-{
- int ret, i;
- mpi A, E, N, X, Y, U, V;
-
- mpi_init( &A ); mpi_init( &E ); mpi_init( &N ); mpi_init( &X );
- mpi_init( &Y ); mpi_init( &U ); mpi_init( &V );
-
- MPI_CHK( mpi_read_string( &A, 16,
- "EFE021C2645FD1DC586E69184AF4A31E" \
- "D5F53E93B5F123FA41680867BA110131" \
- "944FE7952E2517337780CB0DB80E61AA" \
- "E7C8DDC6C5C6AADEB34EB38A2F40D5E6" ) );
-
- MPI_CHK( mpi_read_string( &E, 16,
- "B2E7EFD37075B9F03FF989C7C5051C20" \
- "34D2A323810251127E7BF8625A4F49A5" \
- "F3E27F4DA8BD59C47D6DAABA4C8127BD" \
- "5B5C25763222FEFCCFC38B832366C29E" ) );
-
- MPI_CHK( mpi_read_string( &N, 16,
- "0066A198186C18C10B2F5ED9B522752A" \
- "9830B69916E535C8F047518A889A43A5" \
- "94B6BED27A168D31D4A52F88925AA8F5" ) );
-
- MPI_CHK( mpi_mul_mpi( &X, &A, &N ) );
-
- MPI_CHK( mpi_read_string( &U, 16,
- "602AB7ECA597A3D6B56FF9829A5E8B85" \
- "9E857EA95A03512E2BAE7391688D264A" \
- "A5663B0341DB9CCFD2C4C5F421FEC814" \
- "8001B72E848A38CAE1C65F78E56ABDEF" \
- "E12D3C039B8A02D6BE593F0BBBDA56F1" \
- "ECF677152EF804370C1A305CAF3B5BF1" \
- "30879B56C61DE584A0F53A2447A51E" ) );
-
- if( verbose != 0 )
- printf( " MPI test #1 (mul_mpi): " );
-
- if( mpi_cmp_mpi( &X, &U ) != 0 )
- {
- if( verbose != 0 )
- printf( "failed\n" );
-
- return( 1 );
- }
-
- if( verbose != 0 )
- printf( "passed\n" );
-
- MPI_CHK( mpi_div_mpi( &X, &Y, &A, &N ) );
-
- MPI_CHK( mpi_read_string( &U, 16,
- "256567336059E52CAE22925474705F39A94" ) );
-
- MPI_CHK( mpi_read_string( &V, 16,
- "6613F26162223DF488E9CD48CC132C7A" \
- "0AC93C701B001B092E4E5B9F73BCD27B" \
- "9EE50D0657C77F374E903CDFA4C642" ) );
-
- if( verbose != 0 )
- printf( " MPI test #2 (div_mpi): " );
-
- if( mpi_cmp_mpi( &X, &U ) != 0 ||
- mpi_cmp_mpi( &Y, &V ) != 0 )
- {
- if( verbose != 0 )
- printf( "failed\n" );
-
- return( 1 );
- }
-
- if( verbose != 0 )
- printf( "passed\n" );
-
- MPI_CHK( mpi_exp_mod( &X, &A, &E, &N, NULL ) );
-
- MPI_CHK( mpi_read_string( &U, 16,
- "36E139AEA55215609D2816998ED020BB" \
- "BD96C37890F65171D948E9BC7CBAA4D9" \
- "325D24D6A3C12710F10A09FA08AB87" ) );
-
- if( verbose != 0 )
- printf( " MPI test #3 (exp_mod): " );
-
- if( mpi_cmp_mpi( &X, &U ) != 0 )
- {
- if( verbose != 0 )
- printf( "failed\n" );
-
- return( 1 );
- }
-
- if( verbose != 0 )
- printf( "passed\n" );
-
-#if defined(POLARSSL_GENPRIME)
- MPI_CHK( mpi_inv_mod( &X, &A, &N ) );
-
- MPI_CHK( mpi_read_string( &U, 16,
- "003A0AAEDD7E784FC07D8F9EC6E3BFD5" \
- "C3DBA76456363A10869622EAC2DD84EC" \
- "C5B8A74DAC4D09E03B5E0BE779F2DF61" ) );
-
- if( verbose != 0 )
- printf( " MPI test #4 (inv_mod): " );
-
- if( mpi_cmp_mpi( &X, &U ) != 0 )
- {
- if( verbose != 0 )
- printf( "failed\n" );
-
- return( 1 );
- }
-
- if( verbose != 0 )
- printf( "passed\n" );
-#endif
-
- if( verbose != 0 )
- printf( " MPI test #5 (simple gcd): " );
-
- for ( i = 0; i < GCD_PAIR_COUNT; i++)
- {
- MPI_CHK( mpi_lset( &X, gcd_pairs[i][0] ) );
- MPI_CHK( mpi_lset( &Y, gcd_pairs[i][1] ) );
-
- MPI_CHK( mpi_gcd( &A, &X, &Y ) );
-
- if( mpi_cmp_int( &A, gcd_pairs[i][2] ) != 0 )
- {
- if( verbose != 0 )
- printf( "failed at %d\n", i );
-
- return( 1 );
- }
- }
-
- if( verbose != 0 )
- printf( "passed\n" );
-
-cleanup:
-
- if( ret != 0 && verbose != 0 )
- printf( "Unexpected error, return code = %08X\n", ret );
-
- mpi_free( &A ); mpi_free( &E ); mpi_free( &N ); mpi_free( &X );
- mpi_free( &Y ); mpi_free( &U ); mpi_free( &V );
-
- if( verbose != 0 )
- printf( "\n" );
-
- return( ret );
-}
-
-#endif
-
-#endif
diff --git a/protocols/Tlen/src/crypto/padlock.c b/protocols/Tlen/src/crypto/padlock.c
deleted file mode 100644
index 2e2e4775ff..0000000000
--- a/protocols/Tlen/src/crypto/padlock.c
+++ /dev/null
@@ -1,162 +0,0 @@
-/*
- * VIA PadLock support functions
- *
- * Copyright (C) 2006-2010, Brainspark B.V.
- *
- * This file is part of PolarSSL (http://www.polarssl.org)
- * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
- *
- * All rights reserved.
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program 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 General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
- */
-/*
- * This implementation is based on the VIA PadLock Programming Guide:
- *
- * http://www.via.com.tw/en/downloads/whitepapers/initiatives/padlock/
- * programming_guide.pdf
- */
-
-#include "polarssl/config.h"
-
-#if defined(POLARSSL_PADLOCK_C)
-
-#include "polarssl/padlock.h"
-
-#if defined(POLARSSL_HAVE_X86)
-
-/*
- * PadLock detection routine
- */
-int padlock_supports( int feature )
-{
- static int flags = -1;
- int ebx, edx;
-
- if( flags == -1 )
- {
- asm( "movl %%ebx, %0 \n" \
- "movl $0xC0000000, %%eax \n" \
- "cpuid \n" \
- "cmpl $0xC0000001, %%eax \n" \
- "movl $0, %%edx \n" \
- "jb unsupported \n" \
- "movl $0xC0000001, %%eax \n" \
- "cpuid \n" \
- "unsupported: \n" \
- "movl %%edx, %1 \n" \
- "movl %2, %%ebx \n"
- : "=m" (ebx), "=m" (edx)
- : "m" (ebx)
- : "eax", "ecx", "edx" );
-
- flags = edx;
- }
-
- return( flags & feature );
-}
-
-/*
- * PadLock AES-ECB block en(de)cryption
- */
-int padlock_xcryptecb( aes_context *ctx,
- int mode,
- const unsigned char input[16],
- unsigned char output[16] )
-{
- int ebx;
- unsigned long *rk;
- unsigned long *blk;
- unsigned long *ctrl;
- unsigned char buf[256];
-
- rk = ctx->rk;
- blk = PADLOCK_ALIGN16( buf );
- memcpy( blk, input, 16 );
-
- ctrl = blk + 4;
- *ctrl = 0x80 | ctx->nr | ( ( ctx->nr + ( mode^1 ) - 10 ) << 9 );
-
- asm( "pushfl; popfl \n" \
- "movl %%ebx, %0 \n" \
- "movl $1, %%ecx \n" \
- "movl %2, %%edx \n" \
- "movl %3, %%ebx \n" \
- "movl %4, %%esi \n" \
- "movl %4, %%edi \n" \
- ".byte 0xf3,0x0f,0xa7,0xc8\n" \
- "movl %1, %%ebx \n"
- : "=m" (ebx)
- : "m" (ebx), "m" (ctrl), "m" (rk), "m" (blk)
- : "ecx", "edx", "esi", "edi" );
-
- memcpy( output, blk, 16 );
-
- return( 0 );
-}
-
-/*
- * PadLock AES-CBC buffer en(de)cryption
- */
-int padlock_xcryptcbc( aes_context *ctx,
- int mode,
- size_t length,
- unsigned char iv[16],
- const unsigned char *input,
- unsigned char *output )
-{
- int ebx;
- size_t count;
- unsigned long *rk;
- unsigned long *iw;
- unsigned long *ctrl;
- unsigned char buf[256];
-
- if( ( (long) input & 15 ) != 0 ||
- ( (long) output & 15 ) != 0 )
- return( POLARSSL_ERR_PADLOCK_DATA_MISALIGNED );
-
- rk = ctx->rk;
- iw = PADLOCK_ALIGN16( buf );
- memcpy( iw, iv, 16 );
-
- ctrl = iw + 4;
- *ctrl = 0x80 | ctx->nr | ( ( ctx->nr + (mode^1) - 10 ) << 9 );
-
- count = (length + 15) >> 4;
-
- asm( "pushfl; popfl \n" \
- "movl %%ebx, %0 \n" \
- "movl %2, %%ecx \n" \
- "movl %3, %%edx \n" \
- "movl %4, %%ebx \n" \
- "movl %5, %%esi \n" \
- "movl %6, %%edi \n" \
- "movl %7, %%eax \n" \
- ".byte 0xf3,0x0f,0xa7,0xd0\n" \
- "movl %1, %%ebx \n"
- : "=m" (ebx)
- : "m" (ebx), "m" (count), "m" (ctrl),
- "m" (rk), "m" (input), "m" (output), "m" (iw)
- : "eax", "ecx", "edx", "esi", "edi" );
-
- memcpy( iv, iw, 16 );
-
- return( 0 );
-}
-
-#endif
-
-#endif
diff --git a/protocols/Tlen/src/crypto/polarssl/aes.h b/protocols/Tlen/src/crypto/polarssl/aes.h
deleted file mode 100644
index efc13daf5b..0000000000
--- a/protocols/Tlen/src/crypto/polarssl/aes.h
+++ /dev/null
@@ -1,167 +0,0 @@
-/**
- * \file aes.h
- *
- * \brief AES block cipher
- *
- * Copyright (C) 2006-2010, Brainspark B.V.
- *
- * This file is part of PolarSSL (http://www.polarssl.org)
- * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
- *
- * All rights reserved.
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program 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 General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
- */
-#ifndef POLARSSL_AES_H
-#define POLARSSL_AES_H
-
-#include <string.h>
-
-#define AES_ENCRYPT 1
-#define AES_DECRYPT 0
-
-#define POLARSSL_ERR_AES_INVALID_KEY_LENGTH -0x0020 /**< Invalid key length. */
-#define POLARSSL_ERR_AES_INVALID_INPUT_LENGTH -0x0022 /**< Invalid data input length. */
-
-/**
- * \brief AES context structure
- */
-typedef struct
-{
- int nr; /*!< number of rounds */
- unsigned long *rk; /*!< AES round keys */
- unsigned long buf[68]; /*!< unaligned data */
-}
-aes_context;
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-/**
- * \brief AES key schedule (encryption)
- *
- * \param ctx AES context to be initialized
- * \param key encryption key
- * \param keysize must be 128, 192 or 256
- *
- * \return 0 if successful, or POLARSSL_ERR_AES_INVALID_KEY_LENGTH
- */
-int aes_setkey_enc( aes_context *ctx, const unsigned char *key, unsigned int keysize );
-
-/**
- * \brief AES key schedule (decryption)
- *
- * \param ctx AES context to be initialized
- * \param key decryption key
- * \param keysize must be 128, 192 or 256
- *
- * \return 0 if successful, or POLARSSL_ERR_AES_INVALID_KEY_LENGTH
- */
-int aes_setkey_dec( aes_context *ctx, const unsigned char *key, unsigned int keysize );
-
-/**
- * \brief AES-ECB block encryption/decryption
- *
- * \param ctx AES context
- * \param mode AES_ENCRYPT or AES_DECRYPT
- * \param input 16-byte input block
- * \param output 16-byte output block
- *
- * \return 0 if successful
- */
-int aes_crypt_ecb( aes_context *ctx,
- int mode,
- const unsigned char input[16],
- unsigned char output[16] );
-
-/**
- * \brief AES-CBC buffer encryption/decryption
- * Length should be a multiple of the block
- * size (16 bytes)
- *
- * \param ctx AES context
- * \param mode AES_ENCRYPT or AES_DECRYPT
- * \param length length of the input data
- * \param iv initialization vector (updated after use)
- * \param input buffer holding the input data
- * \param output buffer holding the output data
- *
- * \return 0 if successful, or POLARSSL_ERR_AES_INVALID_INPUT_LENGTH
- */
-int aes_crypt_cbc( aes_context *ctx,
- int mode,
- size_t length,
- unsigned char iv[16],
- const unsigned char *input,
- unsigned char *output );
-
-/**
- * \brief AES-CFB128 buffer encryption/decryption.
- *
- * \param ctx AES context
- * \param mode AES_ENCRYPT or AES_DECRYPT
- * \param length length of the input data
- * \param iv_off offset in IV (updated after use)
- * \param iv initialization vector (updated after use)
- * \param input buffer holding the input data
- * \param output buffer holding the output data
- *
- * \return 0 if successful
- */
-int aes_crypt_cfb128( aes_context *ctx,
- int mode,
- size_t length,
- size_t *iv_off,
- unsigned char iv[16],
- const unsigned char *input,
- unsigned char *output );
-
-/*
- * \brief AES-CTR buffer encryption/decryption
- *
- * Warning: You have to keep the maximum use of your counter in mind!
- *
- * \param length The length of the data
- * \param nc_off The offset in the current stream_block (for resuming
- * within current cipher stream). The offset pointer to
- * should be 0 at the start of a stream.
- * \param nonce_counter The 128-bit nonce and counter.
- * \param stream_block The saved stream-block for resuming. Is overwritten
- * by the function.
- * \param input The input data stream
- * \param output The output data stream
- *
- * \return 0 if successful
- */
-int aes_crypt_ctr( aes_context *ctx,
- size_t length,
- size_t *nc_off,
- unsigned char nonce_counter[16],
- unsigned char stream_block[16],
- const unsigned char *input,
- unsigned char *output );
-/**
- * \brief Checkup routine
- *
- * \return 0 if successful, or 1 if the test failed
- */
-int aes_self_test( int verbose );
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif /* aes.h */
diff --git a/protocols/Tlen/src/crypto/polarssl/bignum.h b/protocols/Tlen/src/crypto/polarssl/bignum.h
deleted file mode 100644
index 7938406708..0000000000
--- a/protocols/Tlen/src/crypto/polarssl/bignum.h
+++ /dev/null
@@ -1,587 +0,0 @@
-/**
- * \file bignum.h
- *
- * \brief Multi-precision integer library
- *
- * Copyright (C) 2006-2010, Brainspark B.V.
- *
- * This file is part of PolarSSL (http://www.polarssl.org)
- * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
- *
- * All rights reserved.
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program 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 General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
- */
-#ifndef POLARSSL_BIGNUM_H
-#define POLARSSL_BIGNUM_H
-
-#include <stdio.h>
-#include <string.h>
-
-#define POLARSSL_ERR_MPI_FILE_IO_ERROR -0x0002 /**< An error occurred while reading from or writing to a file. */
-#define POLARSSL_ERR_MPI_BAD_INPUT_DATA -0x0004 /**< Bad input parameters to function. */
-#define POLARSSL_ERR_MPI_INVALID_CHARACTER -0x0006 /**< There is an invalid character in the digit string. */
-#define POLARSSL_ERR_MPI_BUFFER_TOO_SMALL -0x0008 /**< The output buffer is too small to write too. */
-#define POLARSSL_ERR_MPI_NEGATIVE_VALUE -0x000A /**< The input arguments are negative or result in illegal output. */
-#define POLARSSL_ERR_MPI_DIVISION_BY_ZERO -0x000C /**< The input argument for division is zero, which is not allowed. */
-#define POLARSSL_ERR_MPI_NOT_ACCEPTABLE -0x000E /**< The input arguments are not acceptable. */
-
-#define MPI_CHK(f) if( ( ret = f ) != 0 ) goto cleanup
-
-/*
- * Maximum size MPIs are allowed to grow to in number of limbs.
- */
-#define POLARSSL_MPI_MAX_LIMBS 10000
-
-/*
- * Define the base integer type, architecture-wise
- */
-#if defined(POLARSSL_HAVE_INT8)
-typedef signed char t_sint;
-typedef unsigned char t_uint;
-typedef unsigned short t_udbl;
-#else
-#if defined(POLARSSL_HAVE_INT16)
-typedef signed short t_sint;
-typedef unsigned short t_uint;
-typedef unsigned long t_udbl;
-#else
- typedef signed long t_sint;
- typedef unsigned long t_uint;
- #if defined(_MSC_VER) && defined(_M_IX86)
- typedef unsigned __int64 t_udbl;
- #else
- #if defined(__amd64__) || defined(__x86_64__) || \
- defined(__ppc64__) || defined(__powerpc64__) || \
- defined(__ia64__) || defined(__alpha__)
- typedef unsigned int t_udbl __attribute__((mode(TI)));
- #else
- #if defined(POLARSSL_HAVE_LONGLONG)
- typedef unsigned long long t_udbl;
- #endif
- #endif
- #endif
-#endif
-#endif
-
-/**
- * \brief MPI structure
- */
-typedef struct
-{
- int s; /*!< integer sign */
- size_t n; /*!< total # of limbs */
- t_uint *p; /*!< pointer to limbs */
-}
-mpi;
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-/**
- * \brief Initialize one MPI
- *
- * \param X One MPI to initialize.
- */
-void mpi_init( mpi *X );
-
-/**
- * \brief Unallocate one MPI
- *
- * \param X One MPI to unallocate.
- */
-void mpi_free( mpi *X );
-
-/**
- * \brief Enlarge to the specified number of limbs
- *
- * \param X MPI to grow
- * \param nblimbs The target number of limbs
- *
- * \return 0 if successful,
- * 1 if memory allocation failed
- */
-int mpi_grow( mpi *X, size_t nblimbs );
-
-/**
- * \brief Copy the contents of Y into X
- *
- * \param X Destination MPI
- * \param Y Source MPI
- *
- * \return 0 if successful,
- * 1 if memory allocation failed
- */
-int mpi_copy( mpi *X, const mpi *Y );
-
-/**
- * \brief Swap the contents of X and Y
- *
- * \param X First MPI value
- * \param Y Second MPI value
- */
-void mpi_swap( mpi *X, mpi *Y );
-
-/**
- * \brief Set value from integer
- *
- * \param X MPI to set
- * \param z Value to use
- *
- * \return 0 if successful,
- * 1 if memory allocation failed
- */
-int mpi_lset( mpi *X, t_sint z );
-
-/*
- * \brief Get a specific bit from X
- *
- * \param X MPI to use
- * \param pos Zero-based index of the bit in X
- *
- * \return Either a 0 or a 1
- */
-int mpi_get_bit( mpi *X, size_t pos );
-
-/*
- * \brief Set a bit of X to a specific value of 0 or 1
- *
- * \note Will grow X if necessary to set a bit to 1 in a not yet
- * existing limb. Will not grow if bit should be set to 0
- *
- * \param X MPI to use
- * \param pos Zero-based index of the bit in X
- * \param val The value to set the bit to (0 or 1)
- *
- * \return 0 if successful,
- * 1 if memory allocation failed,
- * POLARSSL_ERR_MPI_BAD_INPUT_DATA if val is not 0 or 1
- */
-int mpi_set_bit( mpi *X, size_t pos, unsigned char val );
-
-/**
- * \brief Return the number of least significant bits
- *
- * \param X MPI to use
- */
-size_t mpi_lsb( const mpi *X );
-
-/**
- * \brief Return the number of most significant bits
- *
- * \param X MPI to use
- */
-size_t mpi_msb( const mpi *X );
-
-/**
- * \brief Return the total size in bytes
- *
- * \param X MPI to use
- */
-size_t mpi_size( const mpi *X );
-
-/**
- * \brief Import from an ASCII string
- *
- * \param X Destination MPI
- * \param radix Input numeric base
- * \param s Null-terminated string buffer
- *
- * \return 0 if successful, or an POLARSSL_ERR_MPI_XXX error code
- */
-int mpi_read_string( mpi *X, int radix, const char *s );
-
-/**
- * \brief Export into an ASCII string
- *
- * \param X Source MPI
- * \param radix Output numeric base
- * \param s String buffer
- * \param slen String buffer size
- *
- * \return 0 if successful, or an POLARSSL_ERR_MPI_XXX error code.
- * *slen is always updated to reflect the amount
- * of data that has (or would have) been written.
- *
- * \note Call this function with *slen = 0 to obtain the
- * minimum required buffer size in *slen.
- */
-int mpi_write_string( const mpi *X, int radix, char *s, size_t *slen );
-
-/**
- * \brief Read X from an opened file
- *
- * \param X Destination MPI
- * \param radix Input numeric base
- * \param fin Input file handle
- *
- * \return 0 if successful, or an POLARSSL_ERR_MPI_XXX error code
- */
-int mpi_read_file( mpi *X, int radix, FILE *fin );
-
-/**
- * \brief Write X into an opened file, or stdout if fout is NULL
- *
- * \param p Prefix, can be NULL
- * \param X Source MPI
- * \param radix Output numeric base
- * \param fout Output file handle (can be NULL)
- *
- * \return 0 if successful, or an POLARSSL_ERR_MPI_XXX error code
- *
- * \note Set fout == NULL to print X on the console.
- */
-int mpi_write_file( const char *p, const mpi *X, int radix, FILE *fout );
-
-/**
- * \brief Import X from unsigned binary data, big endian
- *
- * \param X Destination MPI
- * \param buf Input buffer
- * \param buflen Input buffer size
- *
- * \return 0 if successful,
- * 1 if memory allocation failed
- */
-int mpi_read_binary( mpi *X, const unsigned char *buf, size_t buflen );
-
-/**
- * \brief Export X into unsigned binary data, big endian
- *
- * \param X Source MPI
- * \param buf Output buffer
- * \param buflen Output buffer size
- *
- * \return 0 if successful,
- * POLARSSL_ERR_MPI_BUFFER_TOO_SMALL if buf isn't large enough
- */
-int mpi_write_binary( const mpi *X, unsigned char *buf, size_t buflen );
-
-/**
- * \brief Left-shift: X <<= count
- *
- * \param X MPI to shift
- * \param count Amount to shift
- *
- * \return 0 if successful,
- * 1 if memory allocation failed
- */
-int mpi_shift_l( mpi *X, size_t count );
-
-/**
- * \brief Right-shift: X >>= count
- *
- * \param X MPI to shift
- * \param count Amount to shift
- *
- * \return 0 if successful,
- * 1 if memory allocation failed
- */
-int mpi_shift_r( mpi *X, size_t count );
-
-/**
- * \brief Compare unsigned values
- *
- * \param X Left-hand MPI
- * \param Y Right-hand MPI
- *
- * \return 1 if |X| is greater than |Y|,
- * -1 if |X| is lesser than |Y| or
- * 0 if |X| is equal to |Y|
- */
-int mpi_cmp_abs( const mpi *X, const mpi *Y );
-
-/**
- * \brief Compare signed values
- *
- * \param X Left-hand MPI
- * \param Y Right-hand MPI
- *
- * \return 1 if X is greater than Y,
- * -1 if X is lesser than Y or
- * 0 if X is equal to Y
- */
-int mpi_cmp_mpi( const mpi *X, const mpi *Y );
-
-/**
- * \brief Compare signed values
- *
- * \param X Left-hand MPI
- * \param z The integer value to compare to
- *
- * \return 1 if X is greater than z,
- * -1 if X is lesser than z or
- * 0 if X is equal to z
- */
-int mpi_cmp_int( const mpi *X, t_sint z );
-
-/**
- * \brief Unsigned addition: X = |A| + |B|
- *
- * \param X Destination MPI
- * \param A Left-hand MPI
- * \param B Right-hand MPI
- *
- * \return 0 if successful,
- * 1 if memory allocation failed
- */
-int mpi_add_abs( mpi *X, const mpi *A, const mpi *B );
-
-/**
- * \brief Unsigned substraction: X = |A| - |B|
- *
- * \param X Destination MPI
- * \param A Left-hand MPI
- * \param B Right-hand MPI
- *
- * \return 0 if successful,
- * POLARSSL_ERR_MPI_NEGATIVE_VALUE if B is greater than A
- */
-int mpi_sub_abs( mpi *X, const mpi *A, const mpi *B );
-
-/**
- * \brief Signed addition: X = A + B
- *
- * \param X Destination MPI
- * \param A Left-hand MPI
- * \param B Right-hand MPI
- *
- * \return 0 if successful,
- * 1 if memory allocation failed
- */
-int mpi_add_mpi( mpi *X, const mpi *A, const mpi *B );
-
-/**
- * \brief Signed substraction: X = A - B
- *
- * \param X Destination MPI
- * \param A Left-hand MPI
- * \param B Right-hand MPI
- *
- * \return 0 if successful,
- * 1 if memory allocation failed
- */
-int mpi_sub_mpi( mpi *X, const mpi *A, const mpi *B );
-
-/**
- * \brief Signed addition: X = A + b
- *
- * \param X Destination MPI
- * \param A Left-hand MPI
- * \param b The integer value to add
- *
- * \return 0 if successful,
- * 1 if memory allocation failed
- */
-int mpi_add_int( mpi *X, const mpi *A, t_sint b );
-
-/**
- * \brief Signed substraction: X = A - b
- *
- * \param X Destination MPI
- * \param A Left-hand MPI
- * \param b The integer value to subtract
- *
- * \return 0 if successful,
- * 1 if memory allocation failed
- */
-int mpi_sub_int( mpi *X, const mpi *A, t_sint b );
-
-/**
- * \brief Baseline multiplication: X = A * B
- *
- * \param X Destination MPI
- * \param A Left-hand MPI
- * \param B Right-hand MPI
- *
- * \return 0 if successful,
- * 1 if memory allocation failed
- */
-int mpi_mul_mpi( mpi *X, const mpi *A, const mpi *B );
-
-/**
- * \brief Baseline multiplication: X = A * b
- * Note: b is an unsigned integer type, thus
- * Negative values of b are ignored.
- *
- * \param X Destination MPI
- * \param A Left-hand MPI
- * \param b The integer value to multiply with
- *
- * \return 0 if successful,
- * 1 if memory allocation failed
- */
-int mpi_mul_int( mpi *X, const mpi *A, t_sint b );
-
-/**
- * \brief Division by mpi: A = Q * B + R
- *
- * \param Q Destination MPI for the quotient
- * \param R Destination MPI for the rest value
- * \param A Left-hand MPI
- * \param B Right-hand MPI
- *
- * \return 0 if successful,
- * 1 if memory allocation failed,
- * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if B == 0
- *
- * \note Either Q or R can be NULL.
- */
-int mpi_div_mpi( mpi *Q, mpi *R, const mpi *A, const mpi *B );
-
-/**
- * \brief Division by int: A = Q * b + R
- *
- * \param Q Destination MPI for the quotient
- * \param R Destination MPI for the rest value
- * \param A Left-hand MPI
- * \param b Integer to divide by
- *
- * \return 0 if successful,
- * 1 if memory allocation failed,
- * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if b == 0
- *
- * \note Either Q or R can be NULL.
- */
-int mpi_div_int( mpi *Q, mpi *R, const mpi *A, t_sint b );
-
-/**
- * \brief Modulo: R = A mod B
- *
- * \param R Destination MPI for the rest value
- * \param A Left-hand MPI
- * \param B Right-hand MPI
- *
- * \return 0 if successful,
- * 1 if memory allocation failed,
- * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if B == 0,
- * POLARSSL_ERR_MPI_NEGATIVE_VALUE if B < 0
- */
-int mpi_mod_mpi( mpi *R, const mpi *A, const mpi *B );
-
-/**
- * \brief Modulo: r = A mod b
- *
- * \param r Destination t_uint
- * \param A Left-hand MPI
- * \param b Integer to divide by
- *
- * \return 0 if successful,
- * 1 if memory allocation failed,
- * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if b == 0,
- * POLARSSL_ERR_MPI_NEGATIVE_VALUE if b < 0
- */
-int mpi_mod_int( t_uint *r, const mpi *A, t_sint b );
-
-/**
- * \brief Sliding-window exponentiation: X = A^E mod N
- *
- * \param X Destination MPI
- * \param A Left-hand MPI
- * \param E Exponent MPI
- * \param N Modular MPI
- * \param _RR Speed-up MPI used for recalculations
- *
- * \return 0 if successful,
- * 1 if memory allocation failed,
- * POLARSSL_ERR_MPI_BAD_INPUT_DATA if N is negative or even
- *
- * \note _RR is used to avoid re-computing R*R mod N across
- * multiple calls, which speeds up things a bit. It can
- * be set to NULL if the extra performance is unneeded.
- */
-int mpi_exp_mod( mpi *X, const mpi *A, const mpi *E, const mpi *N, mpi *_RR );
-
-/**
- * \brief Fill an MPI X with size bytes of random
- *
- * \param X Destination MPI
- * \param size Size in bytes
- * \param f_rng RNG function
- * \param p_rng RNG parameter
- *
- * \return 0 if successful,
- * 1 if memory allocation failed
- */
-int mpi_fill_random( mpi *X, size_t size, int (*f_rng)(void *), void *p_rng );
-
-/**
- * \brief Greatest common divisor: G = gcd(A, B)
- *
- * \param G Destination MPI
- * \param A Left-hand MPI
- * \param B Right-hand MPI
- *
- * \return 0 if successful,
- * 1 if memory allocation failed
- */
-int mpi_gcd( mpi *G, const mpi *A, const mpi *B );
-
-/**
- * \brief Modular inverse: X = A^-1 mod N
- *
- * \param X Destination MPI
- * \param A Left-hand MPI
- * \param N Right-hand MPI
- *
- * \return 0 if successful,
- * 1 if memory allocation failed,
- * POLARSSL_ERR_MPI_BAD_INPUT_DATA if N is negative or nil
- POLARSSL_ERR_MPI_NOT_ACCEPTABLE if A has no inverse mod N
- */
-int mpi_inv_mod( mpi *X, const mpi *A, const mpi *N );
-
-/**
- * \brief Miller-Rabin primality test
- *
- * \param X MPI to check
- * \param f_rng RNG function
- * \param p_rng RNG parameter
- *
- * \return 0 if successful (probably prime),
- * 1 if memory allocation failed,
- * POLARSSL_ERR_MPI_NOT_ACCEPTABLE if X is not prime
- */
-int mpi_is_prime( mpi *X, int (*f_rng)(void *), void *p_rng );
-
-/**
- * \brief Prime number generation
- *
- * \param X Destination MPI
- * \param nbits Required size of X in bits ( 3 <= nbits <= 4096 )
- * \param dh_flag If 1, then (X-1)/2 will be prime too
- * \param f_rng RNG function
- * \param p_rng RNG parameter
- *
- * \return 0 if successful (probably prime),
- * 1 if memory allocation failed,
- * POLARSSL_ERR_MPI_BAD_INPUT_DATA if nbits is < 3
- */
-int mpi_gen_prime( mpi *X, size_t nbits, int dh_flag,
- int (*f_rng)(void *), void *p_rng );
-
-/**
- * \brief Checkup routine
- *
- * \return 0 if successful, or 1 if the test failed
- */
-int mpi_self_test( int verbose );
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif /* bignum.h */
diff --git a/protocols/Tlen/src/crypto/polarssl/bn_mul.h b/protocols/Tlen/src/crypto/polarssl/bn_mul.h
deleted file mode 100644
index 59a32857cb..0000000000
--- a/protocols/Tlen/src/crypto/polarssl/bn_mul.h
+++ /dev/null
@@ -1,738 +0,0 @@
-/**
- * \file bn_mul.h
- *
- * \brief Multi-precision integer library
- *
- * Copyright (C) 2006-2010, Brainspark B.V.
- *
- * This file is part of PolarSSL (http://www.polarssl.org)
- * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
- *
- * All rights reserved.
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program 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 General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
- */
-/*
- * Multiply source vector [s] with b, add result
- * to destination vector [d] and set carry c.
- *
- * Currently supports:
- *
- * . IA-32 (386+) . AMD64 / EM64T
- * . IA-32 (SSE2) . Motorola 68000
- * . PowerPC, 32-bit . MicroBlaze
- * . PowerPC, 64-bit . TriCore
- * . SPARC v8 . ARM v3+
- * . Alpha . MIPS32
- * . C, longlong . C, generic
- */
-#ifndef POLARSSL_BN_MUL_H
-#define POLARSSL_BN_MUL_H
-
-#include "polarssl/config.h"
-
-#if defined(POLARSSL_HAVE_ASM)
-
-#if defined(__GNUC__)
-#if defined(__i386__)
-
-#define MULADDC_INIT \
- asm( " \
- movl %%ebx, %0; \
- movl %5, %%esi; \
- movl %6, %%edi; \
- movl %7, %%ecx; \
- movl %8, %%ebx; \
- "
-
-#define MULADDC_CORE \
- " \
- lodsl; \
- mull %%ebx; \
- addl %%ecx, %%eax; \
- adcl $0, %%edx; \
- addl (%%edi), %%eax; \
- adcl $0, %%edx; \
- movl %%edx, %%ecx; \
- stosl; \
- "
-
-#if defined(POLARSSL_HAVE_SSE2)
-
-#define MULADDC_HUIT \
- " \
- movd %%ecx, %%mm1; \
- movd %%ebx, %%mm0; \
- movd (%%edi), %%mm3; \
- paddq %%mm3, %%mm1; \
- movd (%%esi), %%mm2; \
- pmuludq %%mm0, %%mm2; \
- movd 4(%%esi), %%mm4; \
- pmuludq %%mm0, %%mm4; \
- movd 8(%%esi), %%mm6; \
- pmuludq %%mm0, %%mm6; \
- movd 12(%%esi), %%mm7; \
- pmuludq %%mm0, %%mm7; \
- paddq %%mm2, %%mm1; \
- movd 4(%%edi), %%mm3; \
- paddq %%mm4, %%mm3; \
- movd 8(%%edi), %%mm5; \
- paddq %%mm6, %%mm5; \
- movd 12(%%edi), %%mm4; \
- paddq %%mm4, %%mm7; \
- movd %%mm1, (%%edi); \
- movd 16(%%esi), %%mm2; \
- pmuludq %%mm0, %%mm2; \
- psrlq $32, %%mm1; \
- movd 20(%%esi), %%mm4; \
- pmuludq %%mm0, %%mm4; \
- paddq %%mm3, %%mm1; \
- movd 24(%%esi), %%mm6; \
- pmuludq %%mm0, %%mm6; \
- movd %%mm1, 4(%%edi); \
- psrlq $32, %%mm1; \
- movd 28(%%esi), %%mm3; \
- pmuludq %%mm0, %%mm3; \
- paddq %%mm5, %%mm1; \
- movd 16(%%edi), %%mm5; \
- paddq %%mm5, %%mm2; \
- movd %%mm1, 8(%%edi); \
- psrlq $32, %%mm1; \
- paddq %%mm7, %%mm1; \
- movd 20(%%edi), %%mm5; \
- paddq %%mm5, %%mm4; \
- movd %%mm1, 12(%%edi); \
- psrlq $32, %%mm1; \
- paddq %%mm2, %%mm1; \
- movd 24(%%edi), %%mm5; \
- paddq %%mm5, %%mm6; \
- movd %%mm1, 16(%%edi); \
- psrlq $32, %%mm1; \
- paddq %%mm4, %%mm1; \
- movd 28(%%edi), %%mm5; \
- paddq %%mm5, %%mm3; \
- movd %%mm1, 20(%%edi); \
- psrlq $32, %%mm1; \
- paddq %%mm6, %%mm1; \
- movd %%mm1, 24(%%edi); \
- psrlq $32, %%mm1; \
- paddq %%mm3, %%mm1; \
- movd %%mm1, 28(%%edi); \
- addl $32, %%edi; \
- addl $32, %%esi; \
- psrlq $32, %%mm1; \
- movd %%mm1, %%ecx; \
- "
-
-#define MULADDC_STOP \
- " \
- emms; \
- movl %4, %%ebx; \
- movl %%ecx, %1; \
- movl %%edi, %2; \
- movl %%esi, %3; \
- " \
- : "=m" (t), "=m" (c), "=m" (d), "=m" (s) \
- : "m" (t), "m" (s), "m" (d), "m" (c), "m" (b) \
- : "eax", "ecx", "edx", "esi", "edi" \
- );
-
-#else
-
-#define MULADDC_STOP \
- " \
- movl %4, %%ebx; \
- movl %%ecx, %1; \
- movl %%edi, %2; \
- movl %%esi, %3; \
- " \
- : "=m" (t), "=m" (c), "=m" (d), "=m" (s) \
- : "m" (t), "m" (s), "m" (d), "m" (c), "m" (b) \
- : "eax", "ecx", "edx", "esi", "edi" \
- );
-#endif /* SSE2 */
-#endif /* i386 */
-
-#if defined(__amd64__) || defined (__x86_64__)
-
-#define MULADDC_INIT \
- asm( "movq %0, %%rsi " :: "m" (s)); \
- asm( "movq %0, %%rdi " :: "m" (d)); \
- asm( "movq %0, %%rcx " :: "m" (c)); \
- asm( "movq %0, %%rbx " :: "m" (b)); \
- asm( "xorq %r8, %r8 " );
-
-#define MULADDC_CORE \
- asm( "movq (%rsi),%rax " ); \
- asm( "mulq %rbx " ); \
- asm( "addq $8, %rsi " ); \
- asm( "addq %rcx, %rax " ); \
- asm( "movq %r8, %rcx " ); \
- asm( "adcq $0, %rdx " ); \
- asm( "nop " ); \
- asm( "addq %rax, (%rdi) " ); \
- asm( "adcq %rdx, %rcx " ); \
- asm( "addq $8, %rdi " );
-
-#define MULADDC_STOP \
- asm( "movq %%rcx, %0 " : "=m" (c)); \
- asm( "movq %%rdi, %0 " : "=m" (d)); \
- asm( "movq %%rsi, %0 " : "=m" (s) :: \
- "rax", "rcx", "rdx", "rbx", "rsi", "rdi", "r8" );
-
-#endif /* AMD64 */
-
-#if defined(__mc68020__) || defined(__mcpu32__)
-
-#define MULADDC_INIT \
- asm( "movl %0, %%a2 " :: "m" (s)); \
- asm( "movl %0, %%a3 " :: "m" (d)); \
- asm( "movl %0, %%d3 " :: "m" (c)); \
- asm( "movl %0, %%d2 " :: "m" (b)); \
- asm( "moveq #0, %d0 " );
-
-#define MULADDC_CORE \
- asm( "movel %a2@+, %d1 " ); \
- asm( "mulul %d2, %d4:%d1 " ); \
- asm( "addl %d3, %d1 " ); \
- asm( "addxl %d0, %d4 " ); \
- asm( "moveq #0, %d3 " ); \
- asm( "addl %d1, %a3@+ " ); \
- asm( "addxl %d4, %d3 " );
-
-#define MULADDC_STOP \
- asm( "movl %%d3, %0 " : "=m" (c)); \
- asm( "movl %%a3, %0 " : "=m" (d)); \
- asm( "movl %%a2, %0 " : "=m" (s) :: \
- "d0", "d1", "d2", "d3", "d4", "a2", "a3" );
-
-#define MULADDC_HUIT \
- asm( "movel %a2@+, %d1 " ); \
- asm( "mulul %d2, %d4:%d1 " ); \
- asm( "addxl %d3, %d1 " ); \
- asm( "addxl %d0, %d4 " ); \
- asm( "addl %d1, %a3@+ " ); \
- asm( "movel %a2@+, %d1 " ); \
- asm( "mulul %d2, %d3:%d1 " ); \
- asm( "addxl %d4, %d1 " ); \
- asm( "addxl %d0, %d3 " ); \
- asm( "addl %d1, %a3@+ " ); \
- asm( "movel %a2@+, %d1 " ); \
- asm( "mulul %d2, %d4:%d1 " ); \
- asm( "addxl %d3, %d1 " ); \
- asm( "addxl %d0, %d4 " ); \
- asm( "addl %d1, %a3@+ " ); \
- asm( "movel %a2@+, %d1 " ); \
- asm( "mulul %d2, %d3:%d1 " ); \
- asm( "addxl %d4, %d1 " ); \
- asm( "addxl %d0, %d3 " ); \
- asm( "addl %d1, %a3@+ " ); \
- asm( "movel %a2@+, %d1 " ); \
- asm( "mulul %d2, %d4:%d1 " ); \
- asm( "addxl %d3, %d1 " ); \
- asm( "addxl %d0, %d4 " ); \
- asm( "addl %d1, %a3@+ " ); \
- asm( "movel %a2@+, %d1 " ); \
- asm( "mulul %d2, %d3:%d1 " ); \
- asm( "addxl %d4, %d1 " ); \
- asm( "addxl %d0, %d3 " ); \
- asm( "addl %d1, %a3@+ " ); \
- asm( "movel %a2@+, %d1 " ); \
- asm( "mulul %d2, %d4:%d1 " ); \
- asm( "addxl %d3, %d1 " ); \
- asm( "addxl %d0, %d4 " ); \
- asm( "addl %d1, %a3@+ " ); \
- asm( "movel %a2@+, %d1 " ); \
- asm( "mulul %d2, %d3:%d1 " ); \
- asm( "addxl %d4, %d1 " ); \
- asm( "addxl %d0, %d3 " ); \
- asm( "addl %d1, %a3@+ " ); \
- asm( "addxl %d0, %d3 " );
-
-#endif /* MC68000 */
-
-#if defined(__powerpc__) || defined(__ppc__)
-#if defined(__powerpc64__) || defined(__ppc64__)
-
-#if defined(__MACH__) && defined(__APPLE__)
-
-#define MULADDC_INIT \
- asm( "ld r3, %0 " :: "m" (s)); \
- asm( "ld r4, %0 " :: "m" (d)); \
- asm( "ld r5, %0 " :: "m" (c)); \
- asm( "ld r6, %0 " :: "m" (b)); \
- asm( "addi r3, r3, -8 " ); \
- asm( "addi r4, r4, -8 " ); \
- asm( "addic r5, r5, 0 " );
-
-#define MULADDC_CORE \
- asm( "ldu r7, 8(r3) " ); \
- asm( "mulld r8, r7, r6 " ); \
- asm( "mulhdu r9, r7, r6 " ); \
- asm( "adde r8, r8, r5 " ); \
- asm( "ld r7, 8(r4) " ); \
- asm( "addze r5, r9 " ); \
- asm( "addc r8, r8, r7 " ); \
- asm( "stdu r8, 8(r4) " );
-
-#define MULADDC_STOP \
- asm( "addze r5, r5 " ); \
- asm( "addi r4, r4, 8 " ); \
- asm( "addi r3, r3, 8 " ); \
- asm( "std r5, %0 " : "=m" (c)); \
- asm( "std r4, %0 " : "=m" (d)); \
- asm( "std r3, %0 " : "=m" (s) :: \
- "r3", "r4", "r5", "r6", "r7", "r8", "r9" );
-
-#else
-
-#define MULADDC_INIT \
- asm( "ld %%r3, %0 " :: "m" (s)); \
- asm( "ld %%r4, %0 " :: "m" (d)); \
- asm( "ld %%r5, %0 " :: "m" (c)); \
- asm( "ld %%r6, %0 " :: "m" (b)); \
- asm( "addi %r3, %r3, -8 " ); \
- asm( "addi %r4, %r4, -8 " ); \
- asm( "addic %r5, %r5, 0 " );
-
-#define MULADDC_CORE \
- asm( "ldu %r7, 8(%r3) " ); \
- asm( "mulld %r8, %r7, %r6 " ); \
- asm( "mulhdu %r9, %r7, %r6 " ); \
- asm( "adde %r8, %r8, %r5 " ); \
- asm( "ld %r7, 8(%r4) " ); \
- asm( "addze %r5, %r9 " ); \
- asm( "addc %r8, %r8, %r7 " ); \
- asm( "stdu %r8, 8(%r4) " );
-
-#define MULADDC_STOP \
- asm( "addze %r5, %r5 " ); \
- asm( "addi %r4, %r4, 8 " ); \
- asm( "addi %r3, %r3, 8 " ); \
- asm( "std %%r5, %0 " : "=m" (c)); \
- asm( "std %%r4, %0 " : "=m" (d)); \
- asm( "std %%r3, %0 " : "=m" (s) :: \
- "r3", "r4", "r5", "r6", "r7", "r8", "r9" );
-
-#endif
-
-#else /* PPC32 */
-
-#if defined(__MACH__) && defined(__APPLE__)
-
-#define MULADDC_INIT \
- asm( "lwz r3, %0 " :: "m" (s)); \
- asm( "lwz r4, %0 " :: "m" (d)); \
- asm( "lwz r5, %0 " :: "m" (c)); \
- asm( "lwz r6, %0 " :: "m" (b)); \
- asm( "addi r3, r3, -4 " ); \
- asm( "addi r4, r4, -4 " ); \
- asm( "addic r5, r5, 0 " );
-
-#define MULADDC_CORE \
- asm( "lwzu r7, 4(r3) " ); \
- asm( "mullw r8, r7, r6 " ); \
- asm( "mulhwu r9, r7, r6 " ); \
- asm( "adde r8, r8, r5 " ); \
- asm( "lwz r7, 4(r4) " ); \
- asm( "addze r5, r9 " ); \
- asm( "addc r8, r8, r7 " ); \
- asm( "stwu r8, 4(r4) " );
-
-#define MULADDC_STOP \
- asm( "addze r5, r5 " ); \
- asm( "addi r4, r4, 4 " ); \
- asm( "addi r3, r3, 4 " ); \
- asm( "stw r5, %0 " : "=m" (c)); \
- asm( "stw r4, %0 " : "=m" (d)); \
- asm( "stw r3, %0 " : "=m" (s) :: \
- "r3", "r4", "r5", "r6", "r7", "r8", "r9" );
-
-#else
-
-#define MULADDC_INIT \
- asm( "lwz %%r3, %0 " :: "m" (s)); \
- asm( "lwz %%r4, %0 " :: "m" (d)); \
- asm( "lwz %%r5, %0 " :: "m" (c)); \
- asm( "lwz %%r6, %0 " :: "m" (b)); \
- asm( "addi %r3, %r3, -4 " ); \
- asm( "addi %r4, %r4, -4 " ); \
- asm( "addic %r5, %r5, 0 " );
-
-#define MULADDC_CORE \
- asm( "lwzu %r7, 4(%r3) " ); \
- asm( "mullw %r8, %r7, %r6 " ); \
- asm( "mulhwu %r9, %r7, %r6 " ); \
- asm( "adde %r8, %r8, %r5 " ); \
- asm( "lwz %r7, 4(%r4) " ); \
- asm( "addze %r5, %r9 " ); \
- asm( "addc %r8, %r8, %r7 " ); \
- asm( "stwu %r8, 4(%r4) " );
-
-#define MULADDC_STOP \
- asm( "addze %r5, %r5 " ); \
- asm( "addi %r4, %r4, 4 " ); \
- asm( "addi %r3, %r3, 4 " ); \
- asm( "stw %%r5, %0 " : "=m" (c)); \
- asm( "stw %%r4, %0 " : "=m" (d)); \
- asm( "stw %%r3, %0 " : "=m" (s) :: \
- "r3", "r4", "r5", "r6", "r7", "r8", "r9" );
-
-#endif
-
-#endif /* PPC32 */
-#endif /* PPC64 */
-
-#if defined(__sparc__)
-
-#define MULADDC_INIT \
- asm( "ld %0, %%o0 " :: "m" (s)); \
- asm( "ld %0, %%o1 " :: "m" (d)); \
- asm( "ld %0, %%o2 " :: "m" (c)); \
- asm( "ld %0, %%o3 " :: "m" (b));
-
-#define MULADDC_CORE \
- asm( "ld [%o0], %o4 " ); \
- asm( "inc 4, %o0 " ); \
- asm( "ld [%o1], %o5 " ); \
- asm( "umul %o3, %o4, %o4 " ); \
- asm( "addcc %o4, %o2, %o4 " ); \
- asm( "rd %y, %g1 " ); \
- asm( "addx %g1, 0, %g1 " ); \
- asm( "addcc %o4, %o5, %o4 " ); \
- asm( "st %o4, [%o1] " ); \
- asm( "addx %g1, 0, %o2 " ); \
- asm( "inc 4, %o1 " );
-
-#define MULADDC_STOP \
- asm( "st %%o2, %0 " : "=m" (c)); \
- asm( "st %%o1, %0 " : "=m" (d)); \
- asm( "st %%o0, %0 " : "=m" (s) :: \
- "g1", "o0", "o1", "o2", "o3", "o4", "o5" );
-
-#endif /* SPARCv8 */
-
-#if defined(__microblaze__) || defined(microblaze)
-
-#define MULADDC_INIT \
- asm( "lwi r3, %0 " :: "m" (s)); \
- asm( "lwi r4, %0 " :: "m" (d)); \
- asm( "lwi r5, %0 " :: "m" (c)); \
- asm( "lwi r6, %0 " :: "m" (b)); \
- asm( "andi r7, r6, 0xffff" ); \
- asm( "bsrli r6, r6, 16 " );
-
-#define MULADDC_CORE \
- asm( "lhui r8, r3, 0 " ); \
- asm( "addi r3, r3, 2 " ); \
- asm( "lhui r9, r3, 0 " ); \
- asm( "addi r3, r3, 2 " ); \
- asm( "mul r10, r9, r6 " ); \
- asm( "mul r11, r8, r7 " ); \
- asm( "mul r12, r9, r7 " ); \
- asm( "mul r13, r8, r6 " ); \
- asm( "bsrli r8, r10, 16 " ); \
- asm( "bsrli r9, r11, 16 " ); \
- asm( "add r13, r13, r8 " ); \
- asm( "add r13, r13, r9 " ); \
- asm( "bslli r10, r10, 16 " ); \
- asm( "bslli r11, r11, 16 " ); \
- asm( "add r12, r12, r10 " ); \
- asm( "addc r13, r13, r0 " ); \
- asm( "add r12, r12, r11 " ); \
- asm( "addc r13, r13, r0 " ); \
- asm( "lwi r10, r4, 0 " ); \
- asm( "add r12, r12, r10 " ); \
- asm( "addc r13, r13, r0 " ); \
- asm( "add r12, r12, r5 " ); \
- asm( "addc r5, r13, r0 " ); \
- asm( "swi r12, r4, 0 " ); \
- asm( "addi r4, r4, 4 " );
-
-#define MULADDC_STOP \
- asm( "swi r5, %0 " : "=m" (c)); \
- asm( "swi r4, %0 " : "=m" (d)); \
- asm( "swi r3, %0 " : "=m" (s) :: \
- "r3", "r4" , "r5" , "r6" , "r7" , "r8" , \
- "r9", "r10", "r11", "r12", "r13" );
-
-#endif /* MicroBlaze */
-
-#if defined(__tricore__)
-
-#define MULADDC_INIT \
- asm( "ld.a %%a2, %0 " :: "m" (s)); \
- asm( "ld.a %%a3, %0 " :: "m" (d)); \
- asm( "ld.w %%d4, %0 " :: "m" (c)); \
- asm( "ld.w %%d1, %0 " :: "m" (b)); \
- asm( "xor %d5, %d5 " );
-
-#define MULADDC_CORE \
- asm( "ld.w %d0, [%a2+] " ); \
- asm( "madd.u %e2, %e4, %d0, %d1 " ); \
- asm( "ld.w %d0, [%a3] " ); \
- asm( "addx %d2, %d2, %d0 " ); \
- asm( "addc %d3, %d3, 0 " ); \
- asm( "mov %d4, %d3 " ); \
- asm( "st.w [%a3+], %d2 " );
-
-#define MULADDC_STOP \
- asm( "st.w %0, %%d4 " : "=m" (c)); \
- asm( "st.a %0, %%a3 " : "=m" (d)); \
- asm( "st.a %0, %%a2 " : "=m" (s) :: \
- "d0", "d1", "e2", "d4", "a2", "a3" );
-
-#endif /* TriCore */
-
-#if defined(__arm__)
-
-#define MULADDC_INIT \
- asm( "ldr r0, %0 " :: "m" (s)); \
- asm( "ldr r1, %0 " :: "m" (d)); \
- asm( "ldr r2, %0 " :: "m" (c)); \
- asm( "ldr r3, %0 " :: "m" (b));
-
-#define MULADDC_CORE \
- asm( "ldr r4, [r0], #4 " ); \
- asm( "mov r5, #0 " ); \
- asm( "ldr r6, [r1] " ); \
- asm( "umlal r2, r5, r3, r4 " ); \
- asm( "adds r7, r6, r2 " ); \
- asm( "adc r2, r5, #0 " ); \
- asm( "str r7, [r1], #4 " );
-
-#define MULADDC_STOP \
- asm( "str r2, %0 " : "=m" (c)); \
- asm( "str r1, %0 " : "=m" (d)); \
- asm( "str r0, %0 " : "=m" (s) :: \
- "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7" );
-
-#endif /* ARMv3 */
-
-#if defined(__alpha__)
-
-#define MULADDC_INIT \
- asm( "ldq $1, %0 " :: "m" (s)); \
- asm( "ldq $2, %0 " :: "m" (d)); \
- asm( "ldq $3, %0 " :: "m" (c)); \
- asm( "ldq $4, %0 " :: "m" (b));
-
-#define MULADDC_CORE \
- asm( "ldq $6, 0($1) " ); \
- asm( "addq $1, 8, $1 " ); \
- asm( "mulq $6, $4, $7 " ); \
- asm( "umulh $6, $4, $6 " ); \
- asm( "addq $7, $3, $7 " ); \
- asm( "cmpult $7, $3, $3 " ); \
- asm( "ldq $5, 0($2) " ); \
- asm( "addq $7, $5, $7 " ); \
- asm( "cmpult $7, $5, $5 " ); \
- asm( "stq $7, 0($2) " ); \
- asm( "addq $2, 8, $2 " ); \
- asm( "addq $6, $3, $3 " ); \
- asm( "addq $5, $3, $3 " );
-
-#define MULADDC_STOP \
- asm( "stq $3, %0 " : "=m" (c)); \
- asm( "stq $2, %0 " : "=m" (d)); \
- asm( "stq $1, %0 " : "=m" (s) :: \
- "$1", "$2", "$3", "$4", "$5", "$6", "$7" );
-
-#endif /* Alpha */
-
-#if defined(__mips__)
-
-#define MULADDC_INIT \
- asm( "lw $10, %0 " :: "m" (s)); \
- asm( "lw $11, %0 " :: "m" (d)); \
- asm( "lw $12, %0 " :: "m" (c)); \
- asm( "lw $13, %0 " :: "m" (b));
-
-#define MULADDC_CORE \
- asm( "lw $14, 0($10) " ); \
- asm( "multu $13, $14 " ); \
- asm( "addi $10, $10, 4 " ); \
- asm( "mflo $14 " ); \
- asm( "mfhi $9 " ); \
- asm( "addu $14, $12, $14 " ); \
- asm( "lw $15, 0($11) " ); \
- asm( "sltu $12, $14, $12 " ); \
- asm( "addu $15, $14, $15 " ); \
- asm( "sltu $14, $15, $14 " ); \
- asm( "addu $12, $12, $9 " ); \
- asm( "sw $15, 0($11) " ); \
- asm( "addu $12, $12, $14 " ); \
- asm( "addi $11, $11, 4 " );
-
-#define MULADDC_STOP \
- asm( "sw $12, %0 " : "=m" (c)); \
- asm( "sw $11, %0 " : "=m" (d)); \
- asm( "sw $10, %0 " : "=m" (s) :: \
- "$9", "$10", "$11", "$12", "$13", "$14", "$15" );
-
-#endif /* MIPS */
-#endif /* GNUC */
-
-#if (defined(_MSC_VER) && defined(_M_IX86)) || defined(__WATCOMC__)
-
-#define MULADDC_INIT \
- __asm mov esi, s \
- __asm mov edi, d \
- __asm mov ecx, c \
- __asm mov ebx, b
-
-#define MULADDC_CORE \
- __asm lodsd \
- __asm mul ebx \
- __asm add eax, ecx \
- __asm adc edx, 0 \
- __asm add eax, [edi] \
- __asm adc edx, 0 \
- __asm mov ecx, edx \
- __asm stosd
-
-#if defined(POLARSSL_HAVE_SSE2)
-
-#define EMIT __asm _emit
-
-#define MULADDC_HUIT \
- EMIT 0x0F EMIT 0x6E EMIT 0xC9 \
- EMIT 0x0F EMIT 0x6E EMIT 0xC3 \
- EMIT 0x0F EMIT 0x6E EMIT 0x1F \
- EMIT 0x0F EMIT 0xD4 EMIT 0xCB \
- EMIT 0x0F EMIT 0x6E EMIT 0x16 \
- EMIT 0x0F EMIT 0xF4 EMIT 0xD0 \
- EMIT 0x0F EMIT 0x6E EMIT 0x66 EMIT 0x04 \
- EMIT 0x0F EMIT 0xF4 EMIT 0xE0 \
- EMIT 0x0F EMIT 0x6E EMIT 0x76 EMIT 0x08 \
- EMIT 0x0F EMIT 0xF4 EMIT 0xF0 \
- EMIT 0x0F EMIT 0x6E EMIT 0x7E EMIT 0x0C \
- EMIT 0x0F EMIT 0xF4 EMIT 0xF8 \
- EMIT 0x0F EMIT 0xD4 EMIT 0xCA \
- EMIT 0x0F EMIT 0x6E EMIT 0x5F EMIT 0x04 \
- EMIT 0x0F EMIT 0xD4 EMIT 0xDC \
- EMIT 0x0F EMIT 0x6E EMIT 0x6F EMIT 0x08 \
- EMIT 0x0F EMIT 0xD4 EMIT 0xEE \
- EMIT 0x0F EMIT 0x6E EMIT 0x67 EMIT 0x0C \
- EMIT 0x0F EMIT 0xD4 EMIT 0xFC \
- EMIT 0x0F EMIT 0x7E EMIT 0x0F \
- EMIT 0x0F EMIT 0x6E EMIT 0x56 EMIT 0x10 \
- EMIT 0x0F EMIT 0xF4 EMIT 0xD0 \
- EMIT 0x0F EMIT 0x73 EMIT 0xD1 EMIT 0x20 \
- EMIT 0x0F EMIT 0x6E EMIT 0x66 EMIT 0x14 \
- EMIT 0x0F EMIT 0xF4 EMIT 0xE0 \
- EMIT 0x0F EMIT 0xD4 EMIT 0xCB \
- EMIT 0x0F EMIT 0x6E EMIT 0x76 EMIT 0x18 \
- EMIT 0x0F EMIT 0xF4 EMIT 0xF0 \
- EMIT 0x0F EMIT 0x7E EMIT 0x4F EMIT 0x04 \
- EMIT 0x0F EMIT 0x73 EMIT 0xD1 EMIT 0x20 \
- EMIT 0x0F EMIT 0x6E EMIT 0x5E EMIT 0x1C \
- EMIT 0x0F EMIT 0xF4 EMIT 0xD8 \
- EMIT 0x0F EMIT 0xD4 EMIT 0xCD \
- EMIT 0x0F EMIT 0x6E EMIT 0x6F EMIT 0x10 \
- EMIT 0x0F EMIT 0xD4 EMIT 0xD5 \
- EMIT 0x0F EMIT 0x7E EMIT 0x4F EMIT 0x08 \
- EMIT 0x0F EMIT 0x73 EMIT 0xD1 EMIT 0x20 \
- EMIT 0x0F EMIT 0xD4 EMIT 0xCF \
- EMIT 0x0F EMIT 0x6E EMIT 0x6F EMIT 0x14 \
- EMIT 0x0F EMIT 0xD4 EMIT 0xE5 \
- EMIT 0x0F EMIT 0x7E EMIT 0x4F EMIT 0x0C \
- EMIT 0x0F EMIT 0x73 EMIT 0xD1 EMIT 0x20 \
- EMIT 0x0F EMIT 0xD4 EMIT 0xCA \
- EMIT 0x0F EMIT 0x6E EMIT 0x6F EMIT 0x18 \
- EMIT 0x0F EMIT 0xD4 EMIT 0xF5 \
- EMIT 0x0F EMIT 0x7E EMIT 0x4F EMIT 0x10 \
- EMIT 0x0F EMIT 0x73 EMIT 0xD1 EMIT 0x20 \
- EMIT 0x0F EMIT 0xD4 EMIT 0xCC \
- EMIT 0x0F EMIT 0x6E EMIT 0x6F EMIT 0x1C \
- EMIT 0x0F EMIT 0xD4 EMIT 0xDD \
- EMIT 0x0F EMIT 0x7E EMIT 0x4F EMIT 0x14 \
- EMIT 0x0F EMIT 0x73 EMIT 0xD1 EMIT 0x20 \
- EMIT 0x0F EMIT 0xD4 EMIT 0xCE \
- EMIT 0x0F EMIT 0x7E EMIT 0x4F EMIT 0x18 \
- EMIT 0x0F EMIT 0x73 EMIT 0xD1 EMIT 0x20 \
- EMIT 0x0F EMIT 0xD4 EMIT 0xCB \
- EMIT 0x0F EMIT 0x7E EMIT 0x4F EMIT 0x1C \
- EMIT 0x83 EMIT 0xC7 EMIT 0x20 \
- EMIT 0x83 EMIT 0xC6 EMIT 0x20 \
- EMIT 0x0F EMIT 0x73 EMIT 0xD1 EMIT 0x20 \
- EMIT 0x0F EMIT 0x7E EMIT 0xC9
-
-#define MULADDC_STOP \
- EMIT 0x0F EMIT 0x77 \
- __asm mov c, ecx \
- __asm mov d, edi \
- __asm mov s, esi \
-
-#else
-
-#define MULADDC_STOP \
- __asm mov c, ecx \
- __asm mov d, edi \
- __asm mov s, esi \
-
-#endif /* SSE2 */
-#endif /* MSVC */
-
-#endif /* POLARSSL_HAVE_ASM */
-
-#if !defined(MULADDC_CORE)
-#if defined(POLARSSL_HAVE_LONGLONG)
-
-#define MULADDC_INIT \
-{ \
- t_udbl r; \
- t_uint r0, r1;
-
-#define MULADDC_CORE \
- r = *(s++) * (t_udbl) b; \
- r0 = r; \
- r1 = r >> biL; \
- r0 += c; r1 += (r0 < c); \
- r0 += *d; r1 += (r0 < *d); \
- c = r1; *(d++) = r0;
-
-#define MULADDC_STOP \
-}
-
-#else
-#define MULADDC_INIT \
-{ \
- t_uint s0, s1, b0, b1; \
- t_uint r0, r1, rx, ry; \
- b0 = ( b << biH ) >> biH; \
- b1 = ( b >> biH );
-
-#define MULADDC_CORE \
- s0 = ( *s << biH ) >> biH; \
- s1 = ( *s >> biH ); s++; \
- rx = s0 * b1; r0 = s0 * b0; \
- ry = s1 * b0; r1 = s1 * b1; \
- r1 += ( rx >> biH ); \
- r1 += ( ry >> biH ); \
- rx <<= biH; ry <<= biH; \
- r0 += rx; r1 += (r0 < rx); \
- r0 += ry; r1 += (r0 < ry); \
- r0 += c; r1 += (r0 < c); \
- r0 += *d; r1 += (r0 < *d); \
- c = r1; *(d++) = r0;
-
-#define MULADDC_STOP \
-}
-
-#endif /* C (generic) */
-#endif /* C (longlong) */
-
-#endif /* bn_mul.h */
diff --git a/protocols/Tlen/src/crypto/polarssl/config.h b/protocols/Tlen/src/crypto/polarssl/config.h
deleted file mode 100644
index 61500a75ee..0000000000
--- a/protocols/Tlen/src/crypto/polarssl/config.h
+++ /dev/null
@@ -1,625 +0,0 @@
-/**
- * \file config.h
- *
- * \brief Configuration options (set of defines)
- *
- * Copyright (C) 2006-2010, Brainspark B.V.
- *
- * This file is part of PolarSSL (http://www.polarssl.org)
- * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
- *
- * All rights reserved.
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program 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 General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
- *
- * This set of compile-time options may be used to enable
- * or disable features selectively, and reduce the global
- * memory footprint.
- */
-#ifndef POLARSSL_CONFIG_H
-#define POLARSSL_CONFIG_H
-
-#ifndef _CRT_SECURE_NO_DEPRECATE
-#define _CRT_SECURE_NO_DEPRECATE 1
-#endif
-
-/**
- * \name SECTION: System support
- *
- * This section sets system specific settings.
- * \{
- */
-
-/**
- * \def POLARSSL_HAVE_INT8
- *
- * The system uses 8-bit wide native integers.
- *
- * Uncomment if native integers are 8-bit wide.
-#define POLARSSL_HAVE_INT8
- */
-
-/**
- * \def POLARSSL_HAVE_INT16
- *
- * The system uses 16-bit wide native integers.
- *
- * Uncomment if native integers are 16-bit wide.
-#define POLARSSL_HAVE_INT16
- */
-
-/**
- * \def POLARSSL_HAVE_LONGLONG
- *
- * The compiler supports the use of long long.
- *
- * Uncomment if the compiler supports long long.
-#define POLARSSL_HAVE_LONGLONG
- */
-
-/**
- * \def POLARSSL_HAVE_ASM
- *
- * The compiler has support for asm()
- *
- * Uncomment to enable the use of assembly code.
- *
- * Requires support for asm() in compiler.
- *
- * Used in:
- * library/timing.c
- * library/padlock.c
- * include/polarssl/bn_mul.h
- *
- */
-#define POLARSSL_HAVE_ASM
-
-/**
- * \def POLARSSL_HAVE_SSE2
- *
- * CPI supports SSE2 instruction set.
- *
- * Uncomment if the CPU supports SSE2 (IA-32 specific).
- *
-#define POLARSSL_HAVE_SSE2
- */
-/* \} name */
-
-/**
- * \name SECTION: PolarSSL feature support
- *
- * This section sets support for features that are or are not needed
- * within the modules that are enabled.
- * \{
- */
-
-/**
- * \def POLARSSL_AES_ROM_TABLES
- *
- * Store the AES tables in ROM.
- *
- * Uncomment this macro to store the AES tables in ROM.
- *
-#define POLARSSL_AES_ROM_TABLES
- */
-
-/**
- * \def POLARSSL_CIPHER_MODE_CFB
- *
- * Enable Cipher Feedback mode (CFB) for symmetric ciphers.
- */
-#define POLARSSL_CIPHER_MODE_CFB
-
-/**
- * \def POLARSSL_CIPHER_MODE_CTR
- *
- * Enable Counter Block Cipher mode (CTR) for symmetric ciphers.
- */
-#define POLARSSL_CIPHER_MODE_CTR
-
-/**
- * \def POLARSSL_DEBUG_MSG
- *
- * Requires: POLARSSL_DEBUG_C
- *
- * Enable all SSL/TLS debugging messages.
- */
-#define POLARSSL_DEBUG_MSG
-
-/**
- * \def POLARSSL_GENPRIME
- *
- * Requires: POLARSSL_BIGNUM_C, POLARSSL_RSA_C
- *
- * Enable the RSA prime-number generation code.
- */
-#define POLARSSL_GENPRIME
-
-/**
- * \def POLARSSL_FS_IO
- *
- * Enable functions that use the filesystem.
- */
-#define POLARSSL_FS_IO
-
-/**
- * \def POLARSSL_PKCS1_V21
- *
- * Requires: POLARSSL_MD_C, POLARSSL_RSA_C
- *
- * Enable support for PKCS#1 v2.1 encoding.
- * This enables support for RSAES-OAEP and RSASSA-PSS operations.
- */
-#define POLARSSL_PKCS1_V21
-
-/**
- * \def POLARSSL_RSA_NO_CRT
- *
- * Do not use the Chinese Remainder Theorem for the RSA private operation.
- *
- * Uncomment this macro to disable the use of CRT in RSA.
- *
-#define POLARSSL_RSA_NO_CRT
- */
-
-/**
- * \def POLARSSL_SELF_TEST
- *
- * Enable the checkup functions (*_self_test).
- */
-#define POLARSSL_SELF_TEST
-
-/**
- * \def POLARSSL_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION
- *
- * If set, the X509 parser will not break-off when parsing an X509 certificate
- * and encountering an unknown critical extension.
- *
- * Uncomment to prevent an error.
- *
-#define POLARSSL_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION
- */
-/* \} name */
-
-/**
- * \name SECTION: PolarSSL modules
- *
- * This section enables or disables entire modules in PolarSSL
- * \{
- */
-
-/**
- * \def POLARSSL_AES_C
- *
- * Enable the AES block cipher.
- *
- * Module: library/aes.c
- * Caller: library/ssl_tls.c
- * library/pem.c
- *
- * This module enables the following ciphersuites:
- * SSL_RSA_AES_128_SHA
- * SSL_RSA_AES_256_SHA
- * SSL_EDH_RSA_AES_256_SHA
- */
-#define POLARSSL_AES_C
-
-/**
- * \def POLARSSL_ARC4_C
- *
- * Enable the ARCFOUR stream cipher.
- *
- * Module: library/arc4.c
- * Caller: library/ssl_tls.c
- *
- * This module enables the following ciphersuites:
- * SSL_RSA_RC4_128_MD5
- * SSL_RSA_RC4_128_SHA
- */
-#define POLARSSL_ARC4_C
-
-/**
- * \def POLARSSL_BASE64_C
- *
- * Enable the Base64 module.
- *
- * Module: library/base64.c
- * Caller: library/pem.c
- *
- * This module is required for PEM support (required by X.509).
- */
-#define POLARSSL_BASE64_C
-
-/**
- * \def POLARSSL_BIGNUM_C
- *
- * Enable the multo-precision integer library.
- *
- * Module: library/bignum.c
- * Caller: library/dhm.c
- * library/rsa.c
- * library/ssl_tls.c
- * library/x509parse.c
- *
- * This module is required for RSA and DHM support.
- */
-#define POLARSSL_BIGNUM_C
-
-/**
- * \def POLARSSL_CAMELLIA_C
- *
- * Enable the Camellia block cipher.
- *
- * Module: library/camellia.c
- * Caller: library/ssl_tls.c
- *
- * This module enabled the following cipher suites:
- * SSL_RSA_CAMELLIA_128_SHA
- * SSL_RSA_CAMELLIA_256_SHA
- * SSL_EDH_RSA_CAMELLIA_256_SHA
- */
-#define POLARSSL_CAMELLIA_C
-
-/**
- * \def POLARSSL_CERTS_C
- *
- * Enable the test certificates.
- *
- * Module: library/certs.c
- * Caller:
- *
- * This module is used for testing (ssl_client/server).
- */
-#define POLARSSL_CERTS_C
-
-/**
- * \def POLARSSL_CIPHER_C
- *
- * Enable the generic cipher layer.
- *
- * Module: library/cipher.c
- * Caller:
- *
- * Uncomment to enable generic cipher wrappers.
- */
-#define POLARSSL_CIPHER_C
-
-/**
- * \def POLARSSL_DEBUG_C
- *
- * Enable the debug functions.
- *
- * Module: library/debug.c
- * Caller: library/ssl_cli.c
- * library/ssl_srv.c
- * library/ssl_tls.c
- *
- * This module provides debugging functions.
- */
-#define POLARSSL_DEBUG_C
-
-/**
- * \def POLARSSL_DES_C
- *
- * Enable the DES block cipher.
- *
- * Module: library/des.c
- * Caller: library/ssl_tls.c
- *
- * This module enables the following ciphersuites:
- * SSL_RSA_DES_168_SHA
- * SSL_EDH_RSA_DES_168_SHA
- */
-#define POLARSSL_DES_C
-
-/**
- * \def POLARSSL_DHM_C
- *
- * Enable the Diffie-Hellman-Merkle key exchange.
- *
- * Module: library/dhm.c
- * Caller: library/ssl_cli.c
- * library/ssl_srv.c
- *
- * This module enables the following ciphersuites:
- * SSL_EDH_RSA_DES_168_SHA
- * SSL_EDH_RSA_AES_256_SHA
- * SSL_EDH_RSA_CAMELLIA_256_SHA
- */
-#define POLARSSL_DHM_C
-
-/**
- * \def POLARSSL_ERROR_C
- *
- * Enable error code to error string conversion.
- *
- * Module: library/error.c
- * Caller:
- *
- * This module enables err_strerror().
- */
-#define POLARSSL_ERROR_C
-
-/**
- * \def POLARSSL_HAVEGE_C
- *
- * Enable the HAVEGE random generator.
- *
- * Module: library/havege.c
- * Caller:
- *
- * Requires: POLARSSL_TIMING_C
- *
- * This module enables the HAVEGE random number generator.
- */
-#define POLARSSL_HAVEGE_C
-
-/**
- * \def POLARSSL_MD_C
- *
- * Enable the generic message digest layer.
- *
- * Module: library/md.c
- * Caller:
- *
- * Uncomment to enable generic message digest wrappers.
- */
-#define POLARSSL_MD_C
-
-/**
- * \def POLARSSL_MD2_C
- *
- * Enable the MD2 hash algorithm
- *
- * Module: library/md2.c
- * Caller: library/x509parse.c
- *
- * Uncomment to enable support for (rare) MD2-signed X.509 certs.
- *
-#define POLARSSL_MD2_C
- */
-
-/**
- * \def POLARSSL_MD4_C
- *
- * Enable the MD4 hash algorithm
- *
- * Module: library/md4.c
- * Caller: library/x509parse.c
- *
- * Uncomment to enable support for (rare) MD4-signed X.509 certs.
- *
-#define POLARSSL_MD4_C
- */
-
-/**
- * \def POLARSSL_MD5_C
- *
- * Enable the MD5 hash algorithm
- *
- * Module: library/md5.c
- * Caller: library/ssl_tls.c
- * library/x509parse.c
- *
- * This module is required for SSL/TLS and X.509.
- */
-#define POLARSSL_MD5_C
-
-/**
- * \def POLARSSL_NET_C
- *
- * Enable the TCP/IP networking routines.
- *
- * Module: library/net.c
- * Caller:
- *
- * This module provides TCP/IP networking routines.
- */
-#define POLARSSL_NET_C
-
-/**
- * \def POLARSSL_PADLOCK_C
- *
- * Enable VIA Padlock support on x86.
- *
- * Module: library/padlock.c
- * Caller: library/aes.c
- *
- * This modules adds support for the VIA PadLock on x86.
- */
-#define POLARSSL_PADLOCK_C
-
-/**
- * \def POLARSSL_PEM_C
- *
- * Enable PEM decoding
- *
- * Module: library/pem.c
- * Caller: library/x509parse.c
- *
- * Requires: POLARSSL_BASE64_C
- *
- * This modules adds support for decoding PEM files.
- */
-#define POLARSSL_PEM_C
-
-/**
- * \def POLARSSL_PKCS11_C
- *
- * Enable support for PKCS#11 smartcard support.
- *
- * Module: library/ssl_srv.c
- * Caller: library/ssl_cli.c
- * library/ssl_srv.c
- *
- * Requires: POLARSSL_SSL_TLS_C
- *
- * This module is required for SSL/TLS PKCS #11 smartcard support.
- * Requires the presence of the PKCS#11 helper library (libpkcs11-helper)
-#define POLARSSL_PKCS11_C
- */
-
-/**
- * \def POLARSSL_RSA_C
- *
- * Enable the RSA public-key cryptosystem.
- *
- * Module: library/rsa.c
- * Caller: library/ssl_cli.c
- * library/ssl_srv.c
- * library/ssl_tls.c
- * library/x509.c
- *
- * Requires: POLARSSL_BIGNUM_C
- *
- * This module is required for SSL/TLS and MD5-signed certificates.
- */
-#define POLARSSL_RSA_C
-
-/**
- * \def POLARSSL_SHA1_C
- *
- * Enable the SHA1 cryptographic hash algorithm.
- *
- * Module: library/sha1.c
- * Caller: library/ssl_cli.c
- * library/ssl_srv.c
- * library/ssl_tls.c
- * library/x509parse.c
- *
- * This module is required for SSL/TLS and SHA1-signed certificates.
- */
-#define POLARSSL_SHA1_C
-
-/**
- * \def POLARSSL_SHA2_C
- *
- * Enable the SHA-224 and SHA-256 cryptographic hash algorithms.
- *
- * Module: library/sha2.c
- * Caller: library/md_wrap.c
- * library/x509parse.c
- *
- * This module adds support for SHA-224 and SHA-256.
- */
-#define POLARSSL_SHA2_C
-
-/**
- * \def POLARSSL_SHA4_C
- *
- * Enable the SHA-384 and SHA-512 cryptographic hash algorithms.
- *
- * Module: library/sha4.c
- * Caller: library/md_wrap.c
- * library/x509parse.c
- *
- * This module adds support for SHA-384 and SHA-512.
- */
-#define POLARSSL_SHA4_C
-
-/**
- * \def POLARSSL_SSL_CLI_C
- *
- * Enable the SSL/TLS client code.
- *
- * Module: library/ssl_cli.c
- * Caller:
- *
- * Requires: POLARSSL_SSL_TLS_C
- *
- * This module is required for SSL/TLS client support.
- */
-#define POLARSSL_SSL_CLI_C
-
-/*
- * \def POLARSSL_SSL_SRV_C
- *
- * Enable the SSL/TLS server code.
- *
- * Module: library/ssl_srv.c
- * Caller:
- *
- * Requires: POLARSSL_SSL_TLS_C
- *
- * This module is required for SSL/TLS server support.
- */
-#define POLARSSL_SSL_SRV_C
-
-/**
- * \def POLARSSL_SSL_TLS_C
- *
- * Enable the generic SSL/TLS code.
- *
- * Module: library/ssl_tls.c
- * Caller: library/ssl_cli.c
- * library/ssl_srv.c
- *
- * Requires: POLARSSL_MD5_C, POLARSSL_SHA1_C, POLARSSL_X509_PARSE_C
- *
- * This module is required for SSL/TLS.
- */
-#define POLARSSL_SSL_TLS_C
-
-/**
- * \def POLARSSL_TIMING_C
- *
- * Enable the portable timing interface.
- *
- * Module: library/timing.c
- * Caller: library/havege.c
- *
- * This module is used by the HAVEGE random number generator.
- */
-#define POLARSSL_TIMING_C
-
-/**
- * \def POLARSSL_VERSION_C
- *
- * Enable run-time version information.
- *
- * Module: library/version.c
- *
- * This module provides run-time version information.
- */
-#define POLARSSL_VERSION_C
-
-/**
- * \def POLARSSL_X509_PARSE_C
- *
- * Enable X.509 certificate parsing.
- *
- * Module: library/x509parse.c
- * Caller: library/ssl_cli.c
- * library/ssl_srv.c
- * library/ssl_tls.c
- *
- * Requires: POLARSSL_BIGNUM_C, POLARSSL_RSA_C
- *
- * This module is required for X.509 certificate parsing.
- */
-#define POLARSSL_X509_PARSE_C
-
-/**
- * \def POLARSSL_XTEA_C
- *
- * Enable the XTEA block cipher.
- *
- * Module: library/xtea.c
- * Caller:
- */
-#define POLARSSL_XTEA_C
-/* \} name */
-
-#endif /* config.h */
diff --git a/protocols/Tlen/src/crypto/polarssl/padlock.h b/protocols/Tlen/src/crypto/polarssl/padlock.h
deleted file mode 100644
index 08fbe825c9..0000000000
--- a/protocols/Tlen/src/crypto/polarssl/padlock.h
+++ /dev/null
@@ -1,100 +0,0 @@
-/**
- * \file padlock.h
- *
- * \brief VIA PadLock ACE for HW encryption/decryption supported by some processors
- *
- * Copyright (C) 2006-2010, Brainspark B.V.
- *
- * This file is part of PolarSSL (http://www.polarssl.org)
- * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
- *
- * All rights reserved.
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program 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 General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
- */
-#ifndef POLARSSL_PADLOCK_H
-#define POLARSSL_PADLOCK_H
-
-#include "polarssl/aes.h"
-
-#define POLARSSL_ERR_PADLOCK_DATA_MISALIGNED -0x0030 /**< Input data should be aligned. */
-
-#if defined(POLARSSL_HAVE_ASM) && defined(__GNUC__) && defined(__i386__)
-
-#ifndef POLARSSL_HAVE_X86
-#define POLARSSL_HAVE_X86
-#endif
-
-#define PADLOCK_RNG 0x000C
-#define PADLOCK_ACE 0x00C0
-#define PADLOCK_PHE 0x0C00
-#define PADLOCK_PMM 0x3000
-
-#define PADLOCK_ALIGN16(x) (unsigned long *) (16 + ((long) x & ~15))
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-/**
- * \brief PadLock detection routine
- *
- * \param The feature to detect
- *
- * \return 1 if CPU has support for the feature, 0 otherwise
- */
-int padlock_supports( int feature );
-
-/**
- * \brief PadLock AES-ECB block en(de)cryption
- *
- * \param ctx AES context
- * \param mode AES_ENCRYPT or AES_DECRYPT
- * \param input 16-byte input block
- * \param output 16-byte output block
- *
- * \return 0 if success, 1 if operation failed
- */
-int padlock_xcryptecb( aes_context *ctx,
- int mode,
- const unsigned char input[16],
- unsigned char output[16] );
-
-/**
- * \brief PadLock AES-CBC buffer en(de)cryption
- *
- * \param ctx AES context
- * \param mode AES_ENCRYPT or AES_DECRYPT
- * \param length length of the input data
- * \param iv initialization vector (updated after use)
- * \param input buffer holding the input data
- * \param output buffer holding the output data
- *
- * \return 0 if success, 1 if operation failed
- */
-int padlock_xcryptcbc( aes_context *ctx,
- int mode,
- size_t length,
- unsigned char iv[16],
- const unsigned char *input,
- unsigned char *output );
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif /* HAVE_X86 */
-
-#endif /* padlock.h */
diff --git a/protocols/Tlen/src/resource.h b/protocols/Tlen/src/resource.h
deleted file mode 100644
index 4ce70c58b2..0000000000
--- a/protocols/Tlen/src/resource.h
+++ /dev/null
@@ -1,145 +0,0 @@
-//{{NO_DEPENDENCIES}}
-// Microsoft Developer Studio generated include file.
-// Used by Tlen.rc
-//
-
-#define IDI_TLEN 100
-#define IDI_MAIL 101
-#define IDI_MUC 102
-#define IDI_CHATS 103
-#define IDI_VOICE 106
-#define IDI_MICROPHONE 107
-#define IDI_SPEAKER 108
-#define IDI_IMAGE 109
-
-#define IDD_OPTIONS_BASIC 151
-#define IDD_OPTIONS_VOICE 152
-#define IDD_OPTIONS_POPUPS 153
-#define IDD_OPTIONS_ADVANCED 154
-#define IDD_USER_INFO 155
-#define IDD_USER_VCARD 156
-#define IDD_PASSWORD 157
-#define IDD_ADVSEARCH 158
-#define IDD_VOICE 159
-#define IDD_ACCEPT_VOICE 160
-#define IDD_USER_CHANGEAVATAR 161
-#define IDD_ACCMGRUI 162
-
-#define IDC_TABS 1000
-#define IDC_EDIT_USERNAME 1001
-#define IDC_SAVE 1002
-#define IDC_EDIT_PASSWORD 1003
-#define IDC_EDIT_LOGIN_SERVER 1004
-#define IDC_INFO_JID 1007
-#define IDC_INFO_RESOURCE 1008
-#define IDC_SHOW_OFFLINE 1012
-#define IDC_OFFLINE_MESSAGE 1013
-#define IDC_OFFLINE_MESSAGE_OPTION 1014
-#define IDC_VISIBILITY_SUPPORT 1015
-#define IDC_NUDGE_SUPPORT 1016
-#define IDC_LOG_ALERTS 1017
-
-#define IDC_SUBSCRIPTION 1039
-#define IDC_REGISTERACCOUNT 1040
-#define IDC_SIMPLE 1041
-#define IDC_KEEPALIVE 1042
-#define IDC_HOST 1043
-#define IDC_HOSTPORT 1044
-#define IDC_USE_SSL 1045
-#define IDC_SAVEPASSWORD 1048
-#define IDC_ROSTER_ALERTS 1049
-#define IDC_PASSWORD 1050
-#define IDC_RECONNECT 1050
-#define IDC_JID 1051
-#define IDC_ROSTER_SYNC 1052
-#define IDC_ALERT_POLICY 1053
-#define IDC_MUC_POLICY 1054
-#define IDC_VOICE_POLICY 1055
-#define IDC_IGNORE_ADVERTISEMENTS 1056
-#define IDC_AVATARS 1057
-#define IDC_VERSIONINFO 1058
-#define IDC_IMAGE_POLICY 1059
-#define IDC_CITY 1060
-#define IDC_FULLNAME 1061
-#define IDC_NICKNAME 1062
-#define IDC_FIRSTNAME 1063
-#define IDC_LASTNAME 1064
-#define IDC_BIRTH 1065
-#define IDC_AGE 1065
-#define IDC_OCCUPATION 1066
-#define IDC_EMAIL 1073
-#define IDC_GENDER 1096
-#define IDC_VOICECONVERSATIONS 1097
-#define IDC_PUBLICSTATUS 1098
-#define IDC_VCQUALITY 1100
-#define IDC_VUMETERIN 1101
-#define IDC_VUMETEROUT 1102
-#define IDC_BYTESIN 1103
-#define IDC_BYTESOUT 1104
-#define IDC_MICROPHONE 1105
-#define IDC_SPEAKER 1106
-#define IDC_ACCEPT 1107
-
-#define IDC_FILE_PROXY_TYPE_LABEL 1108
-#define IDC_FILE_PROXY_TYPE 1109
-#define IDC_FILE_PROXY_HOST_LABEL 1110
-#define IDC_FILE_PROXY_HOST 1111
-#define IDC_FILE_PROXY_PORT_LABEL 1112
-#define IDC_FILE_PROXY_PORT 1113
-#define IDC_FILE_PROXY_USE_AUTH 1114
-#define IDC_FILE_PROXY_USER 1115
-#define IDC_FILE_PROXY_USER_LABEL 1116
-#define IDC_FILE_PROXY_PASSWORD_LABEL 1117
-#define IDC_FILE_PROXY_PASSWORD 1118
-#define IDC_FILE_USE_PROXY 1119
-#define IDC_VOICE_DEVICE_IN 1120
-#define IDC_VOICE_DEVICE_OUT 1121
-
-#define IDC_MSG_ACK 1124
-#define IDC_SERVER 1125
-#define IDC_NICK 1129
-#define IDC_LIST 1133
-#define IDC_ENTER 1144
-#define IDC_AGEFROM 1152
-#define IDC_AGETO 1153
-#define IDC_LOOKFOR 1154
-#define IDC_SCHOOL 1155
-#define IDC_FROM 1175
-#define IDC_DELAY 1180
-#define IDC_COLORBKG 1181
-#define IDC_COLORTXT 1182
-#define IDC_PREVIEW 1183
-#define IDC_ENABLEPOPUP 1184
-#define IDC_DELAY_PERMANENT 1185
-#define IDC_DELAY_CUSTOM 1186
-#define IDC_DELAY_POPUP 1187
-#define IDC_TLEN 1190
-#define IDC_GENDER_TEXT 1195
-#define IDC_OCCUPATION_TEXT 1199
-#define IDC_LOOKFOR_TEXT 1200
-#define IDC_GROUP 1203
-#define IDC_SOFTWARE 1204
-#define IDC_VERSION 1205
-#define IDC_SYSTEM 1206
-#define IDC_AVATAR 1210
-#define IDC_SETAVATAR 1211
-#define IDC_DELETEAVATAR 1212
-#define IDC_AVATAR_LIST 1213
-#define IDC_OLD_AVATAR 1214
-#define IDC_BROWSEAVATAR 1215
-#define IDC_PUBLICAVATAR 1216
-
-#define IDC_STATUS 1414
-#define IDC_PLAN 1415
-#define IDC_PERSONALGROUP 1434
-#define IDC_EXTRAGROUP 1435
-// Next default values for new objects
-//
-#ifdef APSTUDIO_INVOKED
-#ifndef APSTUDIO_READONLY_SYMBOLS
-#define _APS_NEXT_RESOURCE_VALUE 190
-#define _APS_NEXT_COMMAND_VALUE 40017
-#define _APS_NEXT_CONTROL_VALUE 1204
-#define _APS_NEXT_SYMED_VALUE 101
-#endif
-#endif
diff --git a/protocols/Tlen/src/stdafx.cxx b/protocols/Tlen/src/stdafx.cxx
deleted file mode 100644
index b05ed73bc5..0000000000
--- a/protocols/Tlen/src/stdafx.cxx
+++ /dev/null
@@ -1,18 +0,0 @@
-/*
-Copyright (C) 2012-17 Miranda NG project (https://miranda-ng.org)
-
-This program is free software; you can redistribute it and/or
-modify it under the terms of the GNU General Public License
-as published by the Free Software Foundation version 2
-of the License.
-
-This program 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 General Public License for more details.
-
-You should have received a copy of the GNU General Public License
-along with this program. If not, see <http://www.gnu.org/licenses/>.
-*/
-
-#include "stdafx.h" \ No newline at end of file
diff --git a/protocols/Tlen/src/stdafx.h b/protocols/Tlen/src/stdafx.h
deleted file mode 100644
index 079d700eb8..0000000000
--- a/protocols/Tlen/src/stdafx.h
+++ /dev/null
@@ -1,493 +0,0 @@
-/*
-
-Jabber Protocol Plugin for Miranda IM
-Tlen Protocol Plugin for Miranda NG
-Copyright (C) 2002-2004 Santithorn Bunchua
-Copyright (C) 2004-2007 Piotr Piastucki
-
-This program is free software; you can redistribute it and/or
-modify it under the terms of the GNU General Public License
-as published by the Free Software Foundation; either version 2
-of the License, or (at your option) any later version.
-
-This program 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 General Public License for more details.
-
-You should have received a copy of the GNU General Public License
-along with this program; if not, write to the Free Software
-Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
-
-*/
-
-#ifndef _TLEN_H_
-#define _TLEN_H_
-
-#define __try
-#define __except(x) if (0)
-#define __finally
-
-#define _try __try
-#define _except __except
-#define _finally __finally
-
-#ifdef _DEBUG
-#define _CRTDBG_MAP_ALLOC
-#include <stdlib.h>
-#include <crtdbg.h>
-#endif
-#define ENABLE_LOGGING
-
-/*******************************************************************
- * Global compilation flags
- *******************************************************************/
-
-/*******************************************************************
- * Global header files
- *******************************************************************/
-
-#include <windows.h>
-#include <process.h>
-#include <stdio.h>
-#include <stdarg.h>
-#include <time.h>
-#include <limits.h>
-#include <win2k.h>
-
-#include <newpluginapi.h>
-#include <m_system.h>
-#include <m_netlib.h>
-#include <m_protosvc.h>
-#include <m_protoint.h>
-#include <m_contacts.h>
-#include <m_clist.h>
-#include <m_options.h>
-#include <m_icolib.h>
-#include <m_userinfo.h>
-#include <m_database.h>
-#include <m_langpack.h>
-#include <m_utils.h>
-#include <m_message.h>
-#include <m_skin.h>
-#include <m_popup.h>
-#include <m_avatars.h>
-#include <m_nudge.h>
-
-#include "tlen_xml.h"
-#include "crypto/polarssl/aes.h"
-#include "crypto/polarssl/bignum.h"
-
-/*******************************************************************
- * Global constants
- *******************************************************************/
-#define TLEN_DEFAULT_PORT 443
-#define TLEN_IQID "mim_"
-#define TLEN_REGISTER "http://reg.tlen.pl/"
-#define TLEN_MAX_SEARCH_RESULTS_PER_PAGE 20
-
-// User-defined message
-#define WM_TLEN_REFRESH (WM_USER + 100)
-// Error code
-#define TLEN_ERROR_REDIRECT 302
-#define TLEN_ERROR_BAD_REQUEST 400
-#define TLEN_ERROR_UNAUTHORIZED 401
-#define TLEN_ERROR_PAYMENT_REQUIRED 402
-#define TLEN_ERROR_FORBIDDEN 403
-#define TLEN_ERROR_NOT_FOUND 404
-#define TLEN_ERROR_NOT_ALLOWED 405
-#define TLEN_ERROR_NOT_ACCEPTABLE 406
-#define TLEN_ERROR_REGISTRATION_REQUIRED 407
-#define TLEN_ERROR_REQUEST_TIMEOUT 408
-#define TLEN_ERROR_CONFLICT 409
-#define TLEN_ERROR_INTERNAL_SERVER_ERROR 500
-#define TLEN_ERROR_NOT_IMPLEMENTED 501
-#define TLEN_ERROR_REMOTE_SERVER_ERROR 502
-#define TLEN_ERROR_SERVICE_UNAVAILABLE 503
-#define TLEN_ERROR_REMOTE_SERVER_TIMEOUT 504
-
-#define TLEN_ALERTS_ACCEPT_ALL 0
-#define TLEN_ALERTS_IGNORE_NIR 1
-#define TLEN_ALERTS_IGNORE_ALL 2
-
-#define TLEN_IMAGES_ACCEPT_ALL 0
-#define TLEN_IMAGES_IGNORE_NIR 1
-#define TLEN_IMAGES_IGNORE_ALL 2
-
-#define TLEN_MUC_ASK 0
-#define TLEN_MUC_ACCEPT_IR 1
-#define TLEN_MUC_ACCEPT_ALL 2
-#define TLEN_MUC_IGNORE_NIR 3
-#define TLEN_MUC_IGNORE_ALL 4
-
-#define IDC_STATIC (-1)
-
-/*******************************************************************
- * Global data structures and data type definitions
- *******************************************************************/
-
-typedef enum {
- LIST_ROSTER, // Roster list
- LIST_CHATROOM, // Groupchat room currently joined
- LIST_FILE, // Current file transfer session
- LIST_INVITATIONS,// Invitations to be sent
- LIST_SEARCH, // Rooms names being searched
- LIST_VOICE,
- LIST_PICTURE
-} TLEN_LIST;
-
-typedef enum {
- IQ_PROC_NONE,
- IQ_PROC_GETSEARCH
-} TLEN_IQ_PROCID;
-
-typedef enum {
- SUB_NONE,
- SUB_TO,
- SUB_FROM,
- SUB_BOTH
-} TLEN_SUBSCRIPTION;
-
-typedef struct {
- char *szOnline;
- char *szAway;
- char *szNa;
- char *szDnd;
- char *szFreechat;
- char *szInvisible;
-} TLEN_MODEMSGS;
-
-typedef struct {
- char mailBase[256];
- char mailMsg[256];
- int mailMsgMthd;
- char mailIndex[256];
- int mailIndexMthd;
- char mailLogin[256];
- int mailLoginMthd;
- char mailCompose[256];
- int mailComposeMthd;
- char avatarGet[256];
- int avatarGetMthd;
- char avatarUpload[256];
- int avatarUploadMthd;
- char avatarRemove[256];
- int avatarRemoveMthd;
-} TlenConfiguration;
-
-typedef struct {
- BOOL useEncryption;
- BOOL reconnect;
- BOOL rosterSync;
- BOOL offlineAsInvisible;
- BOOL leaveOfflineMessage;
- int offlineMessageOption;
- BOOL ignoreAdvertisements;
- int alertPolicy;
- int groupChatPolicy;
- int voiceChatPolicy;
- int imagePolicy;
- BOOL enableAvatars;
- BOOL enableVersion;
- BOOL useNudge;
- BOOL logAlerts;
- BOOL useNewP2P;
- BOOL sendKeepAlive;
- BOOL savePassword;
-} TlenOptions;
-
-
-struct TLEN_IQ_FUNC_STRUCT;
-struct TLEN_LIST_ITEM_STRUCT;
-struct TLEN_VOICE_CONTROL_STRUCT;
-
-struct TlenProtocol : public PROTO<TlenProtocol>
-{
- TlenProtocol( const char*, const wchar_t* );
- ~TlenProtocol();
-
- //====================================================================================
- // PROTO_INTERFACE
- //====================================================================================
-
- virtual MCONTACT __cdecl AddToList(int flags, PROTOSEARCHRESULT* psr);
- virtual MCONTACT __cdecl AddToListByEvent(int flags, int iContact, MEVENT hDbEvent);
-
- virtual int __cdecl Authorize(MEVENT hDbEvent);
- virtual int __cdecl AuthDeny(MEVENT hDbEvent, const wchar_t* szReason);
-
- virtual HANDLE __cdecl FileAllow(MCONTACT hContact, HANDLE hTransfer, const wchar_t* szPath);
- virtual int __cdecl FileCancel(MCONTACT hContact, HANDLE hTransfer);
- virtual int __cdecl FileDeny(MCONTACT hContact, HANDLE hTransfer, const wchar_t* szReason);
- virtual int __cdecl FileResume(HANDLE hTransfer, int* action, const wchar_t** szFilename);
-
- virtual DWORD_PTR __cdecl GetCaps(int type, MCONTACT hContact = NULL);
- virtual int __cdecl GetInfo(MCONTACT hContact, int infoType);
-
- virtual HANDLE __cdecl SearchBasic(const wchar_t* id);
- virtual HANDLE __cdecl SearchByEmail(const wchar_t* email);
- virtual HANDLE __cdecl SearchByName(const wchar_t* nick, const wchar_t* firstName, const wchar_t* lastName);
- virtual HWND __cdecl SearchAdvanced(HWND owner);
- virtual HWND __cdecl CreateExtendedSearchUI(HWND owner);
-
- virtual HANDLE __cdecl SendFile(MCONTACT hContact, const wchar_t* szDescription, wchar_t** ppszFiles);
- virtual int __cdecl SendMsg(MCONTACT hContact, int flags, const char* msg);
-
- virtual int __cdecl SetApparentMode(MCONTACT hContact, int mode);
- virtual int __cdecl SetStatus(int iNewStatus);
-
- virtual HANDLE __cdecl GetAwayMsg(MCONTACT hContact);
- virtual int __cdecl RecvAwayMsg(MCONTACT hContact, int mode, PROTORECVEVENT* evt);
- virtual int __cdecl SetAwayMsg(int iStatus, const wchar_t* msg);
-
- virtual int __cdecl UserIsTyping(MCONTACT hContact, int type);
-
- virtual int __cdecl OnEvent(PROTOEVENTTYPE iEventType, WPARAM wParam, LPARAM lParam);
-
- //====================================================================================
- // Services
-
- INT_PTR __cdecl GetName(WPARAM wParam, LPARAM lParam);
- INT_PTR __cdecl GetAvatarInfo(WPARAM wParam, LPARAM lParam);
- INT_PTR __cdecl SendAlert(WPARAM wParam, LPARAM lParam);
- INT_PTR __cdecl GetAvatarCaps(WPARAM wParam, LPARAM lParam);
- INT_PTR __cdecl SetMyAvatar(WPARAM wParam, LPARAM lParam);
- INT_PTR __cdecl GetMyAvatar(WPARAM wParam, LPARAM lParam);
- INT_PTR __cdecl GetStatus(WPARAM wParam, LPARAM lParam);
- INT_PTR __cdecl AccMgrUI(WPARAM wParam, LPARAM lParam);
-
- INT_PTR __cdecl MUCMenuHandleChats(WPARAM wParam, LPARAM lParam);
- INT_PTR __cdecl MUCMenuHandleMUC(WPARAM wParam, LPARAM lParam);
- INT_PTR __cdecl MenuHandleInbox(WPARAM wParam, LPARAM lParam);
- INT_PTR __cdecl ContactMenuHandleSendPicture(WPARAM wParam, LPARAM lParam);
- INT_PTR __cdecl MUCContactMenuHandleMUC(WPARAM wParam, LPARAM lParam);
- INT_PTR __cdecl VoiceContactMenuHandleVoice(WPARAM wParam, LPARAM lParam);
- INT_PTR __cdecl ContactMenuHandleRequestAuth(WPARAM wParam, LPARAM lParam);
- INT_PTR __cdecl ContactMenuHandleGrantAuth(WPARAM wParam, LPARAM lParam);
-
- //====================================================================================
- // Events
-
- int __cdecl OnModulesLoaded(WPARAM wParam, LPARAM lParam);
- int __cdecl OptionsInit(WPARAM wParam, LPARAM lParam);
- int __cdecl TlenDbSettingChanged(WPARAM wParam, LPARAM lParam);
- int __cdecl TlenContactDeleted(WPARAM wParam, LPARAM lParam);
- int __cdecl PrebuildContactMenu(WPARAM wParam, LPARAM lParam);
- int __cdecl PreShutdown(WPARAM wParam, LPARAM lParam);
-
- int __cdecl UserInfoInit(WPARAM wParam, LPARAM lParam);
-
- int __cdecl MUCHandleEvent(WPARAM wParam, LPARAM lParam);
-
- //====================================================================================
- HNETLIBUSER hFileNetlibUser;
-
- TLEN_MODEMSGS modeMsgs;
-
- struct ThreadDataStruct *threadData;
- HANDLE hTlenNudge;
- HGENMENU hMenuMUC;
- HGENMENU hMenuChats;
- HGENMENU hMenuInbox;
- HGENMENU hMenuContactMUC;
- HGENMENU hMenuContactVoice;
- HGENMENU hMenuContactGrantAuth;
- HGENMENU hMenuContactRequestAuth;
- HGENMENU hMenuPicture;
-
- int listsCount;
- struct TLEN_LIST_ITEM_STRUCT *lists;
- mir_cs csLists;
-
- int iqCount;
- int iqAlloced;
- struct TLEN_IQ_FUNC_STRUCT *iqList;
- mir_cs csIqList;
-
- mir_cs csSerial;
- unsigned int serial;
- BOOL isOnline;
- BOOL isConnected;
-
- mir_cs modeMsgMutex;
-
- void initMenuItems();
- HGENMENU hMenuRoot;
-
- char *searchJID;
- int searchID;
- int searchIndex;
- char *searchQuery;
- int searchQueryLen;
-
- mir_cs csSend;
-
- HWND voiceDlgHWND;
- struct TLEN_VOICE_CONTROL_STRUCT *playbackControl;
- struct TLEN_VOICE_CONTROL_STRUCT *recordingControl;
- int framesAvailableForPlayback;
- int availOverrunValue;
-
- TlenOptions tlenOptions;
-
-};
-
-
-
-typedef struct ThreadDataStruct{
- HANDLE hThread;
- char *streamId;
- char username[128];
- char password[128];
- char server[128];
- char manualHost[128];
- char avatarToken[128];
- char avatarHash[64];
- int avatarFormat;
- WORD port;
- BOOL useEncryption;
-
- HNETLIBCONN s;
- aes_context aes_in_context;
- aes_context aes_out_context;
- unsigned char aes_in_iv[16];
- unsigned char aes_out_iv[16];
-
- BOOL useAES;
- TlenConfiguration tlenConfig;
- TlenProtocol *proto;
-} ThreadData;
-
-
-typedef enum { FT_CONNECTING, FT_INITIALIZING, FT_RECEIVING, FT_DONE, FT_ERROR, FT_DENIED, FT_SWITCH } TLEN_FILE_STATE;
-typedef enum { FT_RECV, FT_SEND} TLEN_FILE_MODE;
-typedef struct TLEN_FILE_TRANSFER_STRUCT{
- MCONTACT hContact;
- HNETLIBCONN s;
- NETLIBNEWCONNECTIONPROC_V2 pfnNewConnectionV2;
- TLEN_FILE_STATE state;
- char *jid;
- int fileId;
- char *iqId;
- int mode;
-
- // Used by file receiving only
- char *hostName;
- WORD wPort;
- char *localName;
- WORD wLocalPort;
- char *szSavePath;
- long fileReceivedBytes;
- long fileTotalSize;
-
- // Used by file sending only
- HANDLE hFileEvent;
- int fileCount;
- char **files;
- long *filesSize;
- //long fileTotalSize; // Size of the current file (file being sent)
- long allFileTotalSize;
- long allFileReceivedBytes;
- char *szDescription;
- int currentFile;
-
- // New p2p
- BOOL newP2P;
- char *id2;
- SOCKET udps;
- aes_context aes_context;
- unsigned char aes_iv[16];
- TlenProtocol *proto;
-
-} TLEN_FILE_TRANSFER;
-
-typedef struct {
- PROTOSEARCHRESULT hdr;
- char jid[256];
-} TLEN_SEARCH_RESULT;
-
-typedef struct {
- char *iqId;
- PROTOSEARCHRESULT hdr;
- char jid[256];
-} TLEN_CONFERENCE;
-
-
-typedef struct {
- int id;
- wchar_t *name;
-} TLEN_FIELD_MAP;
-
-
-/*******************************************************************
- * Global variables
- *******************************************************************/
-extern HINSTANCE hInst;
-extern HANDLE hMainThread;
-
-/*******************************************************************
- * Function declarations
- *******************************************************************/
-HICON GetIcolibIcon(int iconId);
-HANDLE GetIconHandle(int iconId);
-void ReleaseIcolibIcon(HICON hIcon);
-
-void __cdecl TlenServerThread(ThreadData *info);
-// tlen_ws.cpp
-BOOL TlenWsInit(TlenProtocol *proto);
-void TlenWsUninit(TlenProtocol *proto);
-HNETLIBCONN TlenWsConnect(TlenProtocol *proto, char *host, WORD port);
-int TlenWsSend(TlenProtocol *proto, HNETLIBCONN s, char *data, int datalen);
-int TlenWsRecv(TlenProtocol *proto, HNETLIBCONN s, char *data, long datalen);
-int TlenWsSendAES(TlenProtocol *proto, char *data, int datalen, aes_context *aes_ctx, unsigned char *aes_iv);
-int TlenWsRecvAES(TlenProtocol *proto, char *data, long datalen, aes_context *aes_ctx, unsigned char *aes_iv);
-
-// tlen_util.cpp
-void TlenSerialInit(TlenProtocol *proto);
-unsigned int TlenSerialNext(TlenProtocol *proto);
-int TlenSend(TlenProtocol *proto, const char *fmt, ...);
-MCONTACT TlenHContactFromJID(TlenProtocol *proto, const char *jid);
-char *TlenJIDFromHContact(TlenProtocol *proto, MCONTACT hContact);
-char *TlenLoginFromJID(const char *jid);
-char *TlenResourceFromJID(const char *jid);
-char *TlenNickFromJID(const char *jid);
-char *TlenLocalNickFromJID(const char *jid);
-char *TlenGroupEncode(const char *str);
-char *TlenGroupDecode(const char *str);
-char *TlenSha1(char *str);
-char *TlenSha1(char *str, int len);
-char *TlenPasswordHash(const char *str);
-void TlenUrlDecode(char *str);
-char *TlenUrlEncode(const char *str);
-char *TlenTextEncode(const char *str);
-char *TlenTextDecode(const char *str);
-void TlenLogMessage(TlenProtocol *proto, MCONTACT hContact, DWORD flags, const char *message);
-BOOL IsAuthorized(TlenProtocol *proto, const char *jid);
-//char *TlenGetVersionText();
-time_t TlenIsoToUnixTime(char *stamp);
-time_t TlenTimeToUTC(time_t time);
-void TlenSendPresence(TlenProtocol *proto,int status);
-void TlenStringAppend(char **str, int *sizeAlloced, const char *fmt, ...);
-//char *TlenGetClientJID(char *jid);
-// tlen_misc.cpp
-void TlenDBAddEvent(TlenProtocol *proto, MCONTACT hContact, int eventType, DWORD flags, PBYTE pBlob, DWORD cbBlob);
-void TlenDBAddAuthRequest(TlenProtocol *proto, char *jid, char *nick);
-MCONTACT TlenDBCreateContact(TlenProtocol *proto, char *jid, char *nick, BOOL temporary);
-// tlen_svc.cpp
-int TlenRunSearch(TlenProtocol *proto);
-// tlen_opt.cpp
-void TlenLoadOptions(TlenProtocol *proto);
-// tlen_voice.cpp
-int TlenVoiceCancelAll(TlenProtocol *proto);
-// tlen_advsearch.cpp
-extern TLEN_FIELD_MAP tlenFieldGender[];
-extern TLEN_FIELD_MAP tlenFieldLookfor[];
-extern TLEN_FIELD_MAP tlenFieldStatus[];
-extern TLEN_FIELD_MAP tlenFieldOccupation[];
-extern TLEN_FIELD_MAP tlenFieldPlan[];
-// tlen_advsearch.cpp
-INT_PTR CALLBACK TlenAdvSearchDlgProc(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam);
-char *TlenAdvSearchCreateQuery(HWND hwndDlg, int iqId);
-
-
-#endif
diff --git a/protocols/Tlen/src/tlen.cpp b/protocols/Tlen/src/tlen.cpp
deleted file mode 100644
index 452a641a0a..0000000000
--- a/protocols/Tlen/src/tlen.cpp
+++ /dev/null
@@ -1,370 +0,0 @@
-/*
-
-Tlen Protocol Plugin for Miranda NG
-Copyright (C) 2002-2004 Santithorn Bunchua
-Copyright (C) 2004-2007 Piotr Piastucki
-
-This program is free software; you can redistribute it and/or
-modify it under the terms of the GNU General Public License
-as published by the Free Software Foundation; either version 2
-of the License, or (at your option) any later version.
-
-This program 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 General Public License for more details.
-
-You should have received a copy of the GNU General Public License
-along with this program; if not, write to the Free Software
-Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
-
-*/
-
-#include "stdafx.h"
-#include "commons.h"
-#include "tlen_muc.h"
-#include "tlen_file.h"
-#include "tlen_voice.h"
-#include "tlen_list.h"
-#include "tlen_iq.h"
-#include "resource.h"
-#include "tlen_picture.h"
-#include <richedit.h>
-#include <ctype.h>
-#include <m_icolib.h>
-#include <m_genmenu.h>
-
-CLIST_INTERFACE *pcli;
-int hLangpack;
-HINSTANCE hInst;
-HANDLE hMainThread;
-
-PLUGININFOEX pluginInfo = {
- sizeof(PLUGININFOEX),
- __PLUGIN_NAME,
- PLUGIN_MAKE_VERSION(__MAJOR_VERSION, __MINOR_VERSION, __RELEASE_NUM, __BUILD_NUM),
- __DESCRIPTION,
- __AUTHOR,
- __AUTHOREMAIL,
- __COPYRIGHT,
- __AUTHORWEB,
- UNICODE_AWARE,
- // {748F8934-781A-528D-5208-001265404AB3}
- {0x748f8934, 0x781a, 0x528d, {0x52, 0x08, 0x00, 0x12, 0x65, 0x40, 0x4a, 0xb3}}
-};
-
-// Main tlen server connection thread global variables
-
-BOOL WINAPI DllMain(HINSTANCE hModule, DWORD, LPVOID)
-{
- hInst = hModule;
- return TRUE;
-}
-
-extern "C" __declspec(dllexport) PLUGININFOEX *MirandaPluginInfoEx(DWORD)
-{
- return &pluginInfo;
-}
-
-extern "C" __declspec(dllexport) const MUUID MirandaInterfaces[] = { MIID_PROTOCOL, MIID_LAST };
-
-static IconItem iconList[] =
-{
- { LPGEN("Protocol icon"), "PROTO", IDI_TLEN },
- { LPGEN("Tlen inbox"), "MAIL", IDI_MAIL },
- { LPGEN("Group chats"), "MUC", IDI_MUC },
- { LPGEN("Tlen chats"), "CHATS", IDI_CHATS },
- { LPGEN("Voice chat"), "VOICE", IDI_VOICE },
- { LPGEN("Microphone"), "MICROPHONE", IDI_MICROPHONE },
- { LPGEN("Speaker"), "SPEAKER", IDI_SPEAKER },
- { LPGEN("Send image"), "IMAGE", IDI_IMAGE }
-};
-
-HANDLE GetIconHandle(int iconId)
-{
- for (int i = 0; i < _countof(iconList); i++)
- if (iconList[i].defIconID == iconId)
- return iconList[i].hIcolib;
-
- return nullptr;
-}
-
-HICON GetIcolibIcon(int iconId)
-{
- HANDLE handle = GetIconHandle(iconId);
- if (handle != nullptr)
- return IcoLib_GetIconByHandle(handle);
-
- return nullptr;
-}
-
-void ReleaseIcolibIcon(HICON hIcon)
-{
- IcoLib_ReleaseIcon(hIcon);
-}
-
-static void TlenRegisterIcons()
-{
- Icon_Register(hInst, "Protocols/Tlen", iconList, _countof(iconList), "TLEN");
-}
-
-int TlenProtocol::PrebuildContactMenu(WPARAM hContact, LPARAM)
-{
- if (hContact != NULL && isOnline) {
- DBVARIANT dbv;
- if (!db_get(hContact, m_szModuleName, "jid", &dbv)) {
- TLEN_LIST_ITEM *item = TlenListGetItemPtr(this, LIST_ROSTER, dbv.pszVal);
- db_free(&dbv);
- if (item != nullptr) {
- Menu_ShowItem(hMenuContactRequestAuth, item->subscription == SUB_NONE || item->subscription == SUB_FROM);
- Menu_ShowItem(hMenuContactGrantAuth, item->subscription == SUB_NONE || item->subscription == SUB_TO);
- Menu_ShowItem(hMenuContactMUC, item->status != ID_STATUS_OFFLINE);
- Menu_ShowItem(hMenuContactVoice, item->status != ID_STATUS_OFFLINE && !TlenVoiceIsInUse(this));
- Menu_ShowItem(hMenuPicture, item->status != ID_STATUS_OFFLINE);
- return 0;
- }
- }
- }
-
- Menu_ShowItem(hMenuContactMUC, false);
- Menu_ShowItem(hMenuContactVoice, false);
- Menu_ShowItem(hMenuContactRequestAuth, false);
- Menu_ShowItem(hMenuContactGrantAuth, false);
- return 0;
-}
-
-INT_PTR TlenProtocol::ContactMenuHandleRequestAuth(WPARAM hContact, LPARAM)
-{
- if (hContact != NULL && isOnline) {
- DBVARIANT dbv;
- if (!db_get(hContact, m_szModuleName, "jid", &dbv)) {
- TlenSend(this, "<presence to='%s' type='subscribe'/>", dbv.pszVal);
- db_free(&dbv);
- }
- }
- return 0;
-}
-
-INT_PTR TlenProtocol::ContactMenuHandleGrantAuth(WPARAM hContact, LPARAM)
-{
- if (hContact != NULL && isOnline) {
- DBVARIANT dbv;
- if (!db_get(hContact, m_szModuleName, "jid", &dbv)) {
- TlenSend(this, "<presence to='%s' type='subscribed'/>", dbv.pszVal);
- db_free(&dbv);
- }
- }
- return 0;
-}
-
-INT_PTR TlenProtocol::ContactMenuHandleSendPicture(WPARAM hContact, LPARAM)
-{
- if (hContact != NULL && isOnline)
- SendPicture(this, hContact);
- return 0;
-}
-
-INT_PTR TlenProtocol::MenuHandleInbox(WPARAM, LPARAM)
-{
- char szFileName[MAX_PATH];
- DBVARIANT dbv;
- NETLIBHTTPREQUEST req;
- NETLIBHTTPHEADER headers[2];
- NETLIBHTTPREQUEST *resp;
- char *login = nullptr, *password = nullptr;
- char form[1024];
- char cookie[1024];
- if (!db_get(NULL, m_szModuleName, "LoginName", &dbv)) {
- login = mir_strdup(dbv.pszVal);
- db_free(&dbv);
- }
-
- if (db_get_b(NULL, m_szModuleName, "SavePassword", TRUE) == TRUE)
- password = db_get_sa(NULL, m_szModuleName, "Password");
- else if (threadData != nullptr && mir_strlen(threadData->password) > 0)
- password = mir_strdup(threadData->password);
-
- memset(&cookie, 0, sizeof(cookie));
- if (login != nullptr && password != nullptr) {
- mir_snprintf(form, "username=%s&password=%s", login, password);
- headers[0].szName = "Content-Type";
- headers[0].szValue = "application/x-www-form-urlencoded";
- memset(&req, 0, sizeof(req));
- req.cbSize = sizeof(req);
- req.requestType = REQUEST_POST;
- req.flags = 0;
- req.headersCount = 1;
- req.headers = headers;
- req.pData = form;
- req.dataLength = (int)mir_strlen(form);
- req.szUrl = "http://poczta.o2.pl/login.html";
- resp = Netlib_HttpTransaction(m_hNetlibUser, &req);
- if (resp != nullptr) {
- if (resp->resultCode / 100 == 2 || resp->resultCode == 302) {
- int i;
- for (i = 0; i < resp->headersCount; i++) {
- if (strcmpi(resp->headers[i].szName, "Set-Cookie") == 0) {
- char *start = strstr(resp->headers[i].szValue, "ssid=");
- if (start != nullptr) {
- char *end = strstr(resp->headers[i].szValue, ";");
- start = start + 5;
- if (end == nullptr) {
- end = resp->headers[i].szValue + mir_strlen(resp->headers[i].szValue);
- }
- strncpy(cookie, start, (end - start));
- break;
- }
- }
- }
- }
- Netlib_FreeHttpRequest(resp);
- }
- }
- mir_free(login);
- mir_free(password);
-
- mir_snprintf(szFileName, "http://poczta.o2.pl/login.html?sid=%s", cookie);
- Utils_OpenUrl(szFileName);
- return 0;
-}
-
-int TlenProtocol::OnModulesLoaded(WPARAM, LPARAM)
-{
- /* Set all contacts to offline */
- for (MCONTACT hContact = db_find_first(m_szModuleName); hContact; hContact = db_find_next(hContact, m_szModuleName))
- if (db_get_w(hContact, m_szModuleName, "Status", ID_STATUS_OFFLINE) != ID_STATUS_OFFLINE)
- db_set_w(hContact, m_szModuleName, "Status", ID_STATUS_OFFLINE);
-
- Skin_AddSound("TlenMailNotify", m_tszUserName, LPGENW("Incoming mail"));
- Skin_AddSound("TlenAlertNotify", m_tszUserName, LPGENW("Alert"));
- Skin_AddSound("TlenVoiceNotify", m_tszUserName, LPGENW("Voice chat"));
-
- HookProtoEvent(ME_USERINFO_INITIALISE, &TlenProtocol::UserInfoInit);
- return 0;
-}
-
-
-int TlenProtocol::PreShutdown(WPARAM, LPARAM)
-{
- debugLogA("TLEN TlenPreShutdown");
- return 0;
-}
-
-void TlenProtocol::initMenuItems()
-{
- char text[MAX_PATH];
- strncpy_s(text, m_szModuleName, _TRUNCATE);
- char *pSvcName = text + mir_strlen(text);
-
- CMenuItem mi;
- mi.root = hMenuRoot = Menu_CreateRoot(MO_MAIN, m_tszUserName, -1999901009, GetIconHandle(IDI_TLEN));
- mi.pszService = text;
-
- hMenuChats = nullptr;
-
- // "Multi-User Conference"
- SET_UID(mi,0x4984828, 0x2066, 0x43da, 0x87, 0x9e, 0x71, 0x23, 0xc6, 0xe2, 0x46, 0xd5);
- mir_strcpy(pSvcName, "/MainMenuMUC");
- CreateProtoService(pSvcName, &TlenProtocol::MUCMenuHandleMUC);
- mi.name.a = LPGEN("Multi-User Conference");
- mi.position = 2000050002;
- mi.hIcolibItem = GetIconHandle(IDI_MUC);
- mi.pszService = text;
- hMenuMUC = Menu_AddMainMenuItem(&mi);
-
- SET_UID(mi, 0x594133f1, 0xc5b1, 0x4825, 0xa3, 0x8d, 0x33, 0x0, 0x33, 0x74, 0x3a, 0x29);
- mir_strcpy(pSvcName, "/MainMenuInbox");
- CreateProtoService(pSvcName, &TlenProtocol::MenuHandleInbox);
- mi.name.a = LPGEN("Tlen Mail");
- mi.position = 2000050003;
- mi.hIcolibItem = GetIconHandle(IDI_MAIL);
- mi.pszService = text;
- hMenuInbox = Menu_AddMainMenuItem(&mi);
-
- // contact menu items
- mi.root = nullptr;
-
- // "Send picture"
- SET_UID(mi, 0x12c66fb1, 0x6e57, 0x4acd, 0x90, 0x16, 0xc, 0x83, 0xae, 0x16, 0xe3, 0x12);
- mi.pszService = "/SendPicture";
- CreateProtoService(mi.pszService, &TlenProtocol::ContactMenuHandleSendPicture);
- mi.name.a = LPGEN("Send picture");
- mi.position = -2000019030;
- mi.hIcolibItem = GetIconHandle(IDI_IMAGE);
- hMenuPicture = Menu_AddContactMenuItem(&mi, m_szModuleName);
-
- // "Invite to MUC"
- SET_UID(mi, 0x4728c863, 0xe95f, 0x4203, 0x9b, 0xe8, 0x1, 0xbf, 0x5e, 0x57, 0x88, 0x43);
- mi.pszService = "/ContactMenuMUC";
- CreateProtoService(mi.pszService, &TlenProtocol::MUCContactMenuHandleMUC);
- mi.name.a = LPGEN("Multi-User Conference");
- mi.position = -2000019020;
- mi.hIcolibItem = GetIconHandle(IDI_MUC);
- hMenuContactMUC = Menu_AddContactMenuItem(&mi, m_szModuleName);
-
- // "Invite to voice chat"
- SET_UID(mi, 0x53f919b6, 0x313c, 0x4ed4, 0x96, 0xbc, 0xf6, 0xa2, 0x67, 0x10, 0x7b, 0xa2);
- mi.pszService = "/ContactMenuVoice";
- CreateProtoService(mi.pszService, &TlenProtocol::VoiceContactMenuHandleVoice);
- mi.name.a = LPGEN("Voice Chat");
- mi.position = -2000019010;
- mi.hIcolibItem = GetIconHandle(IDI_VOICE);
- hMenuContactVoice = Menu_AddContactMenuItem(&mi, m_szModuleName);
-
- // "Request authorization"
- SET_UID(mi, 0x36375a1f, 0xc142, 0x4d6e, 0xa6, 0x57, 0xe4, 0x76, 0x5d, 0xbc, 0x59, 0x8e);
- mi.pszService = "/RequestAuth";
- CreateProtoService(mi.pszService, &TlenProtocol::ContactMenuHandleRequestAuth);
- mi.name.a = LPGEN("Request authorization");
- mi.position = -2000001001;
- mi.hIcolibItem = Skin_GetIconHandle(SKINICON_AUTH_REQUEST);
- hMenuContactRequestAuth = Menu_AddContactMenuItem(&mi, m_szModuleName);
-
- // "Grant authorization"
- SET_UID(mi, 0x4c90452a, 0x869a, 0x4a81, 0xaf, 0xa8, 0x28, 0x34, 0xaf, 0x2b, 0x6b, 0x30);
- mi.pszService = "/GrantAuth";
- CreateProtoService(mi.pszService, &TlenProtocol::ContactMenuHandleGrantAuth);
- mi.name.a = LPGEN("Grant authorization");
- mi.position = -2000001000;
- mi.hIcolibItem = Skin_GetIconHandle(SKINICON_AUTH_GRANT);
- hMenuContactGrantAuth = Menu_AddContactMenuItem(&mi, m_szModuleName);
-}
-
-TlenProtocol* tlenProtoInit(const char* pszProtoName, const wchar_t* tszUserName)
-{
- TlenProtocol* ppro = new TlenProtocol(pszProtoName, tszUserName);
- return ppro;
-}
-
-static int tlenProtoUninit(TlenProtocol* ppro)
-{
- delete ppro;
- return 0;
-}
-
-extern "C" int __declspec(dllexport) Load(void)
-{
- mir_getLP(&pluginInfo);
- pcli = Clist_GetInterface();
-
- DuplicateHandle(GetCurrentProcess(), GetCurrentThread(), GetCurrentProcess(), &hMainThread, THREAD_SET_CONTEXT, FALSE, 0);
-
- srand((unsigned)time(nullptr));
-
- TlenRegisterIcons();
-
- // Register protocol module
- PROTOCOLDESCRIPTOR pd = { 0 };
- pd.cbSize = sizeof(pd);
- pd.szName = "TLEN";
- pd.fnInit = (pfnInitProto)tlenProtoInit;
- pd.fnUninit = (pfnUninitProto)tlenProtoUninit;
- pd.type = PROTOTYPE_PROTOCOL;
- Proto_RegisterModule(&pd);
- return 0;
-}
-
-extern "C" int __declspec(dllexport) Unload(void)
-{
- return 0;
-}
diff --git a/protocols/Tlen/src/tlen_advsearch.cpp b/protocols/Tlen/src/tlen_advsearch.cpp
deleted file mode 100644
index a8fe15d789..0000000000
--- a/protocols/Tlen/src/tlen_advsearch.cpp
+++ /dev/null
@@ -1,113 +0,0 @@
-/*
-
-Jabber Protocol Plugin for Miranda IM
-Tlen Protocol Plugin for Miranda NG
-Copyright (C) 2002-2004 Santithorn Bunchua
-Copyright (C) 2004-2007 Piotr Piastucki
-
-This program is free software; you can redistribute it and/or
-modify it under the terms of the GNU General Public License
-as published by the Free Software Foundation; either version 2
-of the License, or (at your option) any later version.
-
-This program 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 General Public License for more details.
-
-You should have received a copy of the GNU General Public License
-along with this program; if not, write to the Free Software
-Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
-
-*/
-
-#include "stdafx.h"
-#include "resource.h"
-
-static void InitComboBox(HWND hwndCombo, TLEN_FIELD_MAP *fieldMap)
-{
- int i, n;
-
- n = SendMessage(hwndCombo, CB_ADDSTRING, 0, (LPARAM)L"");
- SendMessage(hwndCombo, CB_SETITEMDATA, n, 0);
- SendMessage(hwndCombo, CB_SETCURSEL, n, 0);
- for (i=0;;i++) {
- if (fieldMap[i].name == nullptr)
- break;
- n = SendMessage(hwndCombo, CB_ADDSTRING, 0, (LPARAM) TranslateW(fieldMap[i].name));
- SendMessage(hwndCombo, CB_SETITEMDATA, n, fieldMap[i].id);
- }
-}
-
-INT_PTR CALLBACK TlenAdvSearchDlgProc(HWND hwndDlg, UINT msg, WPARAM, LPARAM)
-{
- switch(msg) {
- case WM_INITDIALOG:
- TranslateDialogDefault(hwndDlg);
- InitComboBox(GetDlgItem(hwndDlg, IDC_GENDER), tlenFieldGender);
- InitComboBox(GetDlgItem(hwndDlg, IDC_LOOKFOR), tlenFieldLookfor);
- InitComboBox(GetDlgItem(hwndDlg, IDC_STATUS), tlenFieldStatus);
- InitComboBox(GetDlgItem(hwndDlg, IDC_OCCUPATION), tlenFieldOccupation);
- InitComboBox(GetDlgItem(hwndDlg, IDC_PLAN), tlenFieldPlan);
- return TRUE;
- }
- return FALSE;
-}
-
-static void FetchField(HWND hwndDlg, UINT idCtrl, char *fieldName, char **str, int *strSize)
-{
- char text[512];
- char *localFieldName, *localText;
-
- if (hwndDlg == nullptr || fieldName == nullptr || str == nullptr || strSize == nullptr)
- return;
- GetDlgItemTextA(hwndDlg, idCtrl, text, _countof(text));
- if (text[0]) {
- if ((localFieldName=TlenTextEncode(fieldName)) != nullptr) {
- if ((localText=TlenTextEncode(text)) != nullptr) {
- TlenStringAppend(str, strSize, "<%s>%s</%s>", localFieldName, localText, localFieldName);
- mir_free(localText);
- }
- mir_free(localFieldName);
- }
- }
-}
-
-static void FetchCombo(HWND hwndDlg, UINT idCtrl, char *fieldName, char **str, int *strSize)
-{
- int value;
- char *localFieldName;
-
- if (hwndDlg == nullptr || fieldName == nullptr || str == nullptr || strSize == nullptr)
- return;
- value = (int) SendDlgItemMessage(hwndDlg, idCtrl, CB_GETITEMDATA, SendDlgItemMessage(hwndDlg, idCtrl, CB_GETCURSEL, 0, 0), 0);
- if (value > 0) {
- if ((localFieldName=TlenTextEncode(fieldName)) != nullptr) {
- TlenStringAppend(str, strSize, "<%s>%d</%s>", localFieldName, value, localFieldName);
- mir_free(localFieldName);
- }
- }
-}
-
-char *TlenAdvSearchCreateQuery(HWND hwndDlg, int)
-{
- char *str;
- int strSize;
-
- if (hwndDlg == nullptr) return nullptr;
- str = nullptr;
- FetchField(hwndDlg, IDC_FIRSTNAME, "first", &str, &strSize);
- FetchField(hwndDlg, IDC_LASTNAME, "last", &str, &strSize);
- FetchField(hwndDlg, IDC_NICK, "nick", &str, &strSize);
- FetchField(hwndDlg, IDC_EMAIL, "email", &str, &strSize);
- FetchCombo(hwndDlg, IDC_GENDER, "s", &str, &strSize);
- FetchField(hwndDlg, IDC_AGEFROM, "d", &str, &strSize);
- FetchField(hwndDlg, IDC_AGETO, "u", &str, &strSize);
- FetchField(hwndDlg, IDC_CITY, "c", &str, &strSize);
- FetchCombo(hwndDlg, IDC_OCCUPATION, "j", &str, &strSize);
- FetchField(hwndDlg, IDC_SCHOOL, "e", &str, &strSize);
- FetchCombo(hwndDlg, IDC_STATUS, "m", &str, &strSize);
- FetchCombo(hwndDlg, IDC_LOOKFOR, "r", &str, &strSize);
- FetchCombo(hwndDlg, IDC_PLAN, "p", &str, &strSize);
- return str;
-}
diff --git a/protocols/Tlen/src/tlen_avatar.cpp b/protocols/Tlen/src/tlen_avatar.cpp
deleted file mode 100644
index 998265a326..0000000000
--- a/protocols/Tlen/src/tlen_avatar.cpp
+++ /dev/null
@@ -1,434 +0,0 @@
-/*
-
-Tlen Protocol Plugin for Miranda NG
-Copyright (C) 2004-2007 Piotr Piastucki
-
-This program is free software; you can redistribute it and/or
-modify it under the terms of the GNU General Public License
-as published by the Free Software Foundation; either version 2
-of the License, or (at your option) any later version.
-
-This program 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 General Public License for more details.
-
-You should have received a copy of the GNU General Public License
-along with this program; if not, write to the Free Software
-Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
-
-*/
-
-
-#include "stdafx.h"
-#include "tlen_list.h"
-#include "tlen_avatar.h"
-#include <stdio.h>
-#include <io.h>
-#include <fcntl.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-
-/* TlenGetAvatarFileName() - gets a file name for the avatar image */
-
-void TlenGetAvatarFileName(TlenProtocol *proto, TLEN_LIST_ITEM *item, wchar_t* ptszDest, int cbLen)
-{
- int tPathLen = mir_snwprintf(ptszDest, cbLen, TEXT("%s\\%S"), VARSW(TEXT("%miranda_avatarcache%")), proto->m_szModuleName);
- if (_waccess(ptszDest, 0)) {
- int ret = CreateDirectoryTreeW(ptszDest);
- if (ret == 0)
- proto->debugLogW(L"getAvatarFilename(): Created new directory for avatar cache: %s.", ptszDest);
- else {
- proto->debugLogW(L"getAvatarFilename(): Can not create directory for avatar cache: %s. errno=%d: %s", ptszDest, errno, strerror(errno));
- wchar_t buffer[512];
- mir_snwprintf(buffer, TranslateT("Cannot create avatars cache directory. ERROR: %d: %s\n%s"), errno, _wcserror(errno), ptszDest);
- PUShowMessageT(buffer, SM_WARNING);
- }
- }
-
- int format = PA_FORMAT_PNG;
-
- ptszDest[tPathLen++] = '\\';
- if (item != nullptr)
- format = item->avatarFormat;
- else if (proto->threadData != nullptr)
- format = proto->threadData->avatarFormat;
- else
- format = db_get_dw(NULL, proto->m_szModuleName, "AvatarFormat", PA_FORMAT_UNKNOWN);
-
- const wchar_t *tszFileType = ProtoGetAvatarExtension(format);
- if (item != nullptr)
- mir_snwprintf(ptszDest + tPathLen, MAX_PATH - tPathLen, TEXT("%S%s"), ptrA(TlenSha1(item->jid)), tszFileType);
- else
- mir_snwprintf(ptszDest + tPathLen, MAX_PATH - tPathLen, TEXT("%S_avatar%s"), proto->m_szModuleName, tszFileType);
-}
-
-static void RemoveAvatar(TlenProtocol *proto, MCONTACT hContact)
-{
- wchar_t tFileName[MAX_PATH];
- if (hContact == NULL)
- proto->threadData->avatarHash[0] = '\0';
-
- TlenGetAvatarFileName(proto, nullptr, tFileName, _countof(tFileName) - 1);
- DeleteFile(tFileName);
- db_unset(hContact, "ContactPhoto", "File");
- db_unset(hContact, proto->m_szModuleName, "AvatarHash");
- db_unset(hContact, proto->m_szModuleName, "AvatarFormat");
- ProtoBroadcastAck(proto->m_szModuleName, NULL, ACKTYPE_AVATAR, ACKRESULT_STATUS, nullptr, 0);
-}
-
-static void SetAvatar(TlenProtocol *proto, MCONTACT hContact, TLEN_LIST_ITEM *item, char *data, int len, DWORD format)
-{
- wchar_t filename[MAX_PATH];
- char md5[33];
- mir_md5_state_t ctx;
- DWORD digest[4];
-
- if (format == PA_FORMAT_UNKNOWN && len > 4)
- format = ProtoGetBufferFormat(data);
-
- mir_md5_init(&ctx);
- mir_md5_append(&ctx, (BYTE*)data, len);
- mir_md5_finish(&ctx, (BYTE*)digest);
-
- sprintf(md5, "%08x%08x%08x%08x", (int)htonl(digest[0]), (int)htonl(digest[1]), (int)htonl(digest[2]), (int)htonl(digest[3])); //!!!!!!!!!!!!!!
- if (item != nullptr) {
- char *hash = item->avatarHash;
- item->avatarFormat = format;
- item->avatarHash = mir_strdup(md5);
- mir_free(hash);
- }
- else {
- proto->threadData->avatarFormat = format;
- mir_strcpy(proto->threadData->avatarHash, md5);
- }
- TlenGetAvatarFileName(proto, item, filename, _countof(filename) - 1);
- DeleteFile(filename);
- FILE *out = _wfopen(filename, TEXT("wb"));
- if (out != nullptr) {
- fwrite(data, len, 1, out);
- fclose(out);
- db_set_ws(hContact, "ContactPhoto", "File", filename);
- db_set_s(hContact, proto->m_szModuleName, "AvatarHash", md5);
- db_set_dw(hContact, proto->m_szModuleName, "AvatarFormat", format);
- }
- else {
- wchar_t buffer[128];
- mir_snwprintf(buffer, TranslateT("Cannot save new avatar file \"%s\" Error:\n\t%s (Error: %d)"), filename, _wcserror(errno), errno);
- PUShowMessageT(buffer, SM_WARNING);
- proto->debugLogW(buffer);
- return;
- }
- ProtoBroadcastAck(proto->m_szModuleName, hContact, ACKTYPE_AVATAR, ACKRESULT_STATUS, nullptr, 0);
-}
-
-int TlenProcessAvatarNode(TlenProtocol *proto, XmlNode *avatarNode, TLEN_LIST_ITEM *item)
-{
- XmlNode *aNode;
- char *oldHash = nullptr;
- char *md5 = nullptr, *type = nullptr;
- MCONTACT hContact = NULL;
- if (item != nullptr)
- if ((hContact = TlenHContactFromJID(proto, item->jid)) == NULL)
- return 0;
-
- if (item == nullptr)
- oldHash = proto->threadData->avatarHash;
- else
- oldHash = item->avatarHash;
-
- if (avatarNode != nullptr) {
- aNode = TlenXmlGetChild(avatarNode, "a");
- if (aNode != nullptr) {
- type = TlenXmlGetAttrValue(aNode, "type");
- md5 = TlenXmlGetAttrValue(aNode, "md5");
- }
- }
-
- if (md5 != nullptr) {
- /* check contact's avatar hash - md5 */
- if (oldHash == nullptr || mir_strcmp(oldHash, md5)) {
- if (item != nullptr) {
- item->newAvatarDownloading = TRUE;
- }
- TlenGetAvatar(proto, hContact);
- return 1;
- }
- }
- else {
- /* remove avatar */
- if (oldHash != nullptr) {
- if (item != nullptr) {
- item->avatarHash = nullptr;
- mir_free(oldHash);
- item->newAvatarDownloading = FALSE;
- }
- RemoveAvatar(proto, hContact);
- return 1;
- }
- }
- return 0;
-}
-
-void TlenProcessPresenceAvatar(TlenProtocol *proto, XmlNode *node, TLEN_LIST_ITEM *item)
-{
- MCONTACT hContact=TlenHContactFromJID(proto, item->jid);
- if (hContact != NULL)
- TlenProcessAvatarNode(proto, TlenXmlGetChild(node, "avatar"), item);
-}
-
-static char *replaceTokens(const char *base, const char *uri, const char *login, const char* token, int type, int access)
-{
- char *result;
- int i, l, size;
- l = (int)mir_strlen(uri);
- size = (int)mir_strlen(base);
- for (i = 0; i < l; ) {
- if (!strncmp(uri + i, "^login^", 7)) {
- size += (int)mir_strlen(login);
- i += 7;
- } else if (!strncmp(uri + i, "^type^", 6)) {
- size++;
- i += 6;
- } else if (!strncmp(uri + i, "^token^", 7)) {
- size += (int)mir_strlen(token);
- i += 7;
- } else if (!strncmp(uri + i, "^access^", 8)) {
- size++;
- i += 8;
- } else {
- size++;
- i++;
- }
- }
- result = (char *)mir_alloc(size +1);
- mir_strcpy(result, base);
- size = (int)mir_strlen(base);
- for (i = 0; i < l; ) {
- if (!strncmp(uri + i, "^login^", 7)) {
- mir_strcpy(result + size, login);
- size += (int)mir_strlen(login);
- i += 7;
- } else if (!strncmp(uri + i, "^type^", 6)) {
- result[size++] = '0' + type;
- i += 6;
- } else if (!strncmp(uri + i, "^token^", 7)) {
- mir_strcpy(result + size, token);
- size += (int)mir_strlen(token);
- i += 7;
- } else if (!strncmp(uri + i, "^access^", 8)) {
- result[size++] = '0' + access;
- i += 8;
- } else {
- result[size++] = uri[i++];
- }
- }
- result[size] = '\0';
- return result;
-}
-
-static int getAvatarMutex = 0;
-
-struct TLENGETAVATARTHREADDATA
-{
- TlenProtocol *proto;
- MCONTACT hContact;
-};
-
-static void TlenGetAvatarThread(void *ptr)
-{
- TLEN_LIST_ITEM *item = nullptr;
- TLENGETAVATARTHREADDATA *data = (TLENGETAVATARTHREADDATA *)ptr;
- MCONTACT hContact = data->hContact;
- char *login = nullptr;
- if (hContact != NULL) {
- char *jid = TlenJIDFromHContact(data->proto, hContact);
- login = TlenNickFromJID(jid);
- item = TlenListGetItemPtr(data->proto, LIST_ROSTER, jid);
- mir_free(jid);
- }
- else {
- if (data->proto->threadData != nullptr)
- login = mir_strdup(data->proto->threadData->username);
- }
-
- if ((data->proto->threadData != nullptr && hContact == NULL) || item != nullptr) {
- DWORD format = PA_FORMAT_UNKNOWN;
- if (item != nullptr)
- item->newAvatarDownloading = TRUE;
-
- char *request = replaceTokens(data->proto->threadData->tlenConfig.mailBase, data->proto->threadData->tlenConfig.avatarGet, login, data->proto->threadData->avatarToken, 0, 0);
- NETLIBHTTPREQUEST req;
- memset(&req, 0, sizeof(req));
- req.cbSize = sizeof(req);
- req.requestType = data->proto->threadData->tlenConfig.avatarGetMthd;
- req.flags = 0;
- req.headersCount = 0;
- req.headers = nullptr;
- req.dataLength = 0;
- req.szUrl = request;
- NETLIBHTTPREQUEST *resp = Netlib_HttpTransaction(data->proto->m_hNetlibUser, &req);
- if (item != nullptr)
- item->newAvatarDownloading = FALSE;
-
- if (resp != nullptr) {
- if (resp->resultCode/100 == 2) {
- if (resp->dataLength > 0) {
- for (int i=0; i<resp->headersCount; i++ ) {
- if (!strcmpi(resp->headers[i].szName, "Content-Type")) {
- format = ProtoGetAvatarFormatByMimeType(_A2T(resp->headers[i].szValue));
- break;
- }
- }
- SetAvatar(data->proto, hContact, item, resp->pData, resp->dataLength, format);
- }
- else RemoveAvatar(data->proto, hContact);
- }
- Netlib_FreeHttpRequest(resp);
- }
- mir_free(request);
- mir_free(login);
- }
-
- if (hContact == NULL)
- getAvatarMutex = 0;
-
- mir_free(login);
- mir_free(data);
-}
-
-void TlenGetAvatar(TlenProtocol *proto, MCONTACT hContact)
-{
- if (hContact == NULL) {
- if (getAvatarMutex != 0)
- return;
-
- getAvatarMutex = 1;
- }
-
- TLENGETAVATARTHREADDATA *data = (TLENGETAVATARTHREADDATA *)mir_alloc(sizeof(TLENGETAVATARTHREADDATA));
- data->proto = proto;
- data->hContact = hContact;
- mir_forkthread(TlenGetAvatarThread, data);
-}
-
-struct TLENREMOVEAVATARTHREADDATA
-{
- TlenProtocol *proto;
- NETLIBHTTPREQUEST *req;
-};
-
-static void TlenRemoveAvatarRequestThread(void *ptr) {
- TLENREMOVEAVATARTHREADDATA *data = (TLENREMOVEAVATARTHREADDATA*)ptr;
- NETLIBHTTPREQUEST *req = (NETLIBHTTPREQUEST *)data->req;
- NETLIBHTTPREQUEST *resp = Netlib_HttpTransaction(data->proto->m_hNetlibUser, req);
- mir_free(req->szUrl);
- mir_free(req->headers);
- mir_free(req->pData);
- mir_free(req);
- if (resp != nullptr) {
- Netlib_FreeHttpRequest(resp);
- RemoveAvatar(data->proto, NULL);
- }
- mir_free(data);
-
-}
-
-struct TLENUPLOADAVATARTHREADDATA
-{
- TlenProtocol *proto;
- NETLIBHTTPREQUEST *req;
- char *data;
- int length;
-};
-
-boolean checkUploadAvatarResponse(TlenProtocol *proto, NETLIBHTTPREQUEST *resp)
-{
- if (resp == nullptr) {
- proto->debugLogA("Error while setting avatar on Tlen account (no response)");
- PUShowMessageT(TranslateT("Error while setting avatar on Tlen account (no response)"), SM_WARNING);
- return false;
- }
- if (resp->resultCode != 200 || !resp->dataLength || !resp->pData) {
- proto->debugLogA("Error while setting avatar on Tlen account (invalid response) resultCode=%d, dataLength=%d", resp->resultCode, resp->dataLength);
- PUShowMessageT(TranslateT("Error while setting avatar on Tlen account (invalid response)"), SM_WARNING);
- return false;
- }
- if (strncmp(resp->pData, "<ok", 3)) {
- proto->debugLogA("Error while setting avatar on Tlen account: %s", resp->pData);
- PUShowMessageT(TranslateT("Error while setting avatar on Tlen account"), SM_WARNING);
- return false;
- }
- return true;
-}
-
-static void TlenUploadAvatarRequestThread(void *ptr)
-{
- TLENUPLOADAVATARTHREADDATA *data = (TLENUPLOADAVATARTHREADDATA *) ptr;
- NETLIBHTTPREQUEST *req = data->req;
- NETLIBHTTPREQUEST *resp = Netlib_HttpTransaction(data->proto->m_hNetlibUser, req);
- if (checkUploadAvatarResponse(data->proto, resp)) {
- Netlib_FreeHttpRequest(resp);
- SetAvatar(data->proto, NULL, nullptr, data->data, data->length, PA_FORMAT_PNG);
- }
- mir_free(req->szUrl);
- mir_free(req->headers);
- mir_free(req->pData);
- mir_free(req);
- mir_free(data->data);
- mir_free(data);
-}
-
-void TlenRemoveAvatar(TlenProtocol *proto)
-{
- if (proto->threadData != nullptr) {
- TLENREMOVEAVATARTHREADDATA *data = (TLENREMOVEAVATARTHREADDATA *)mir_alloc(sizeof(TLENREMOVEAVATARTHREADDATA));
- NETLIBHTTPREQUEST *req = (NETLIBHTTPREQUEST *)mir_alloc(sizeof(NETLIBHTTPREQUEST));
- data->proto = proto;
- data->req = req;
- char *request = replaceTokens(proto->threadData->tlenConfig.mailBase, proto->threadData->tlenConfig.avatarRemove, "", proto->threadData->avatarToken, 0, 0);
- memset(req, 0, sizeof(NETLIBHTTPREQUEST));
- req->cbSize = sizeof(NETLIBHTTPREQUEST);
- req->requestType = proto->threadData->tlenConfig.avatarGetMthd;
- req->szUrl = request;
- mir_forkthread(TlenRemoveAvatarRequestThread, data);
- }
-}
-
-void TlenUploadAvatar(TlenProtocol *proto, unsigned char *data, int dataLen, int access)
-{
- NETLIBHTTPHEADER *headers;
- unsigned char *buffer;
- if (proto->threadData != nullptr && dataLen > 0 && data != nullptr) {
- char *mpartHead = "--AaB03x\r\nContent-Disposition: form-data; name=\"filename\"; filename=\"plik.png\"\r\nContent-Type: image/png\r\n\r\n";
- char *mpartTail = "\r\n--AaB03x--\r\n";
- int size, sizeHead = (int)mir_strlen(mpartHead), sizeTail = (int)mir_strlen(mpartTail);
- char *request = replaceTokens(proto->threadData->tlenConfig.mailBase, proto->threadData->tlenConfig.avatarUpload, "", proto->threadData->avatarToken, 0, access);
- TLENUPLOADAVATARTHREADDATA *threadData = (TLENUPLOADAVATARTHREADDATA *)mir_alloc(sizeof(TLENUPLOADAVATARTHREADDATA));
- threadData->proto = proto;
- NETLIBHTTPREQUEST *req = (NETLIBHTTPREQUEST *)mir_alloc(sizeof(NETLIBHTTPREQUEST));
- headers = (NETLIBHTTPHEADER *)mir_alloc(sizeof(NETLIBHTTPHEADER));
- memset(req, 0, sizeof(NETLIBHTTPREQUEST));
- req->cbSize = sizeof(NETLIBHTTPREQUEST);
- req->requestType = proto->threadData->tlenConfig.avatarUploadMthd;
- req->szUrl = request;
- req->flags = 0;
- headers[0].szName = "Content-Type";
- headers[0].szValue = "multipart/form-data; boundary=AaB03x";
- req->headersCount = 1;
- req->headers = headers;
- size = dataLen + sizeHead + sizeTail;
- buffer = (unsigned char *)mir_alloc(size);
- memcpy(buffer, mpartHead, sizeHead);
- memcpy(buffer + sizeHead, data, dataLen);
- memcpy(buffer + sizeHead + dataLen, mpartTail, sizeTail);
- req->dataLength = size;
- req->pData = (char*)buffer;
- threadData->req = req;
- threadData->data = (char*)mir_alloc(dataLen);
- memcpy(threadData->data, data, dataLen);
- threadData->length = dataLen;
- mir_forkthread(TlenUploadAvatarRequestThread, threadData);
- }
-}
-
diff --git a/protocols/Tlen/src/tlen_avatar.h b/protocols/Tlen/src/tlen_avatar.h
deleted file mode 100644
index d994713a54..0000000000
--- a/protocols/Tlen/src/tlen_avatar.h
+++ /dev/null
@@ -1,34 +0,0 @@
-/*
-
-Jabber Protocol Plugin for Miranda IM
-Tlen Protocol Plugin for Miranda NG
-Copyright (C) 2002-2004 Santithorn Bunchua
-Copyright (C) 2004-2007 Piotr Piastucki
-
-This program is free software; you can redistribute it and/or
-modify it under the terms of the GNU General Public License
-as published by the Free Software Foundation; either version 2
-of the License, or (at your option) any later version.
-
-This program 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 General Public License for more details.
-
-You should have received a copy of the GNU General Public License
-along with this program; if not, write to the Free Software
-Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
-
-*/
-
-#ifndef _TLEN_AVATAR_H_
-#define _TLEN_AVATAR_H_
-
-void TlenProcessPresenceAvatar(TlenProtocol *proto, XmlNode *node, TLEN_LIST_ITEM *item);
-int TlenProcessAvatarNode(TlenProtocol *proto, XmlNode *avatarNode, TLEN_LIST_ITEM *item);
-void TlenGetAvatarFileName(TlenProtocol *proto, TLEN_LIST_ITEM *item, wchar_t* psztDest, int cbLen);
-void TlenGetAvatar(TlenProtocol *proto, MCONTACT hContact);
-void TlenUploadAvatar(TlenProtocol *proto, unsigned char *data, int dataLen, int access);
-void TlenRemoveAvatar(TlenProtocol *proto);
-
-#endif // _TLEN_AVATAR_H_
diff --git a/protocols/Tlen/src/tlen_file.cpp b/protocols/Tlen/src/tlen_file.cpp
deleted file mode 100644
index d80baba3e6..0000000000
--- a/protocols/Tlen/src/tlen_file.cpp
+++ /dev/null
@@ -1,682 +0,0 @@
-/*
-
-Tlen Protocol Plugin for Miranda NG
-Copyright (C) 2004-2007 Piotr Piastucki
-
-This program is free software; you can redistribute it and/or
-modify it under the terms of the GNU General Public License
-as published by the Free Software Foundation; either version 2
-of the License, or (at your option) any later version.
-
-This program 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 General Public License for more details.
-
-You should have received a copy of the GNU General Public License
-along with this program; if not, write to the Free Software
-Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
-
-*/
-
-#include "stdafx.h"
-#include <io.h>
-#include <fcntl.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include "tlen_list.h"
-#include "tlen_p2p_old.h"
-
-static void TlenFileReceiveParse(TLEN_FILE_TRANSFER *ft)
-{
- int i;
- char *p;
- TLEN_FILE_PACKET *rpacket = nullptr, *packet;
- if (ft->state == FT_CONNECTING) {
- rpacket = TlenP2PPacketReceive(ft->s);
- if (rpacket != nullptr) {
- p = rpacket->packet;
- if (rpacket->type == TLEN_FILE_PACKET_FILE_LIST) { // list of files (length & name)
- ft->fileCount = (int)(*((DWORD*)p));
- ft->files = (char **)mir_alloc(sizeof(char *) * ft->fileCount);
- ft->filesSize = (long *)mir_alloc(sizeof(long) * ft->fileCount);
- ft->currentFile = 0;
- ft->allFileTotalSize = 0;
- ft->allFileReceivedBytes = 0;
- p += sizeof(DWORD);
- for (i = 0; i < ft->fileCount; i++) {
- ft->filesSize[i] = (long)(*((DWORD*)p));
- ft->allFileTotalSize += ft->filesSize[i];
- p += sizeof(DWORD);
- ft->files[i] = (char *)mir_alloc(256);
- memcpy(ft->files[i], p, 256);
- p += 256;
- }
-
- if ((packet = TlenP2PPacketCreate(3 * sizeof(DWORD))) == nullptr)
- ft->state = FT_ERROR;
- else {
- TlenP2PPacketSetType(packet, TLEN_FILE_PACKET_FILE_LIST_ACK);
- TlenP2PPacketSend(ft->s, packet);
- TlenP2PPacketFree(packet);
- ft->state = FT_INITIALIZING;
- ft->proto->debugLogA("Change to FT_INITIALIZING");
- }
- }
- TlenP2PPacketFree(rpacket);
- }
- else ft->state = FT_ERROR;
- }
- else if (ft->state == FT_INITIALIZING) {
- char *fullFileName;
- if ((packet = TlenP2PPacketCreate(3 * sizeof(DWORD))) != nullptr) {
- TlenP2PPacketSetType(packet, TLEN_FILE_PACKET_FILE_REQUEST); // file request
- TlenP2PPacketPackDword(packet, ft->currentFile);
- TlenP2PPacketPackDword(packet, 0);
- TlenP2PPacketPackDword(packet, 0);
- TlenP2PPacketSend(ft->s, packet);
- TlenP2PPacketFree(packet);
-
- fullFileName = (char *)mir_alloc(mir_strlen(ft->szSavePath) + mir_strlen(ft->files[ft->currentFile]) + 2);
- mir_strcpy(fullFileName, ft->szSavePath);
- if (fullFileName[mir_strlen(fullFileName) - 1] != '\\')
- mir_strcat(fullFileName, "\\");
- mir_strcat(fullFileName, ft->files[ft->currentFile]);
- ft->fileId = _open(fullFileName, _O_BINARY | _O_WRONLY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
- ft->fileReceivedBytes = 0;
- ft->fileTotalSize = ft->filesSize[ft->currentFile];
- ft->proto->debugLogA("Saving to [%s] [%d]", fullFileName, ft->filesSize[ft->currentFile]);
- mir_free(fullFileName);
- ft->state = FT_RECEIVING;
- ft->proto->debugLogA("Change to FT_RECEIVING");
- }
- else ft->state = FT_ERROR;
- }
- else if (ft->state == FT_RECEIVING) {
- PROTOFILETRANSFERSTATUS pfts;
- memset(&pfts, 0, sizeof(PROTOFILETRANSFERSTATUS));
- pfts.cbSize = sizeof(PROTOFILETRANSFERSTATUS);
- pfts.hContact = ft->hContact;
- pfts.pszFiles = ft->files;
- pfts.totalFiles = ft->fileCount;
- pfts.currentFileNumber = ft->currentFile;
- pfts.totalBytes = ft->allFileTotalSize;
- pfts.szWorkingDir = nullptr;
- pfts.szCurrentFile = ft->files[ft->currentFile];
- pfts.currentFileSize = ft->filesSize[ft->currentFile];
- pfts.currentFileTime = 0;
- ft->proto->debugLogA("Receiving data...");
- while (ft->state == FT_RECEIVING) {
- rpacket = TlenP2PPacketReceive(ft->s);
- if (rpacket != nullptr) {
- p = rpacket->packet;
- if (rpacket->type == TLEN_FILE_PACKET_FILE_DATA) { // file data
- int writeSize;
- writeSize = rpacket->len - 2 * sizeof(DWORD); // skip file offset
- if (_write(ft->fileId, p + 2 * sizeof(DWORD), writeSize) != writeSize) {
- ft->state = FT_ERROR;
- }
- else {
- ft->fileReceivedBytes += writeSize;
- ft->allFileReceivedBytes += writeSize;
- pfts.totalProgress = ft->allFileReceivedBytes;
- pfts.currentFileProgress = ft->fileReceivedBytes;
- ProtoBroadcastAck(ft->proto->m_szModuleName, ft->hContact, ACKTYPE_FILE, ACKRESULT_DATA, ft, (LPARAM)&pfts);
- }
- }
- else if (rpacket->type == TLEN_FILE_PACKET_END_OF_FILE) { // end of file
- _close(ft->fileId);
- ft->proto->debugLogA("Finishing this file...");
- if (ft->currentFile >= ft->fileCount - 1) {
- ft->state = FT_DONE;
- }
- else {
- ft->currentFile++;
- ft->state = FT_INITIALIZING;
- ft->proto->debugLogA("File received, advancing to the next file...");
- ProtoBroadcastAck(ft->proto->m_szModuleName, ft->hContact, ACKTYPE_FILE, ACKRESULT_NEXTFILE, ft, 0);
- }
- }
- TlenP2PPacketFree(rpacket);
- }
- else ft->state = FT_ERROR;
- }
- }
-}
-
-static void TlenFileReceivingConnection(HNETLIBCONN hConnection, DWORD, void * pExtra)
-{
- TlenProtocol *proto = (TlenProtocol *)pExtra;
- TLEN_FILE_TRANSFER *ft = TlenP2PEstablishIncomingConnection(proto, hConnection, LIST_FILE, TRUE);
- if (ft != nullptr) {
- HNETLIBCONN slisten = ft->s;
- ft->s = hConnection;
- ft->proto->debugLogA("Set ft->s to %d (saving %d)", hConnection, slisten);
- ft->proto->debugLogA("Entering send loop for this file connection... (ft->s is hConnection)");
- while (ft->state != FT_DONE && ft->state != FT_ERROR)
- TlenFileReceiveParse(ft);
-
- if (ft->state == FT_DONE)
- ProtoBroadcastAck(ft->proto->m_szModuleName, ft->hContact, ACKTYPE_FILE, ACKRESULT_SUCCESS, ft, 0);
- else
- ProtoBroadcastAck(ft->proto->m_szModuleName, ft->hContact, ACKTYPE_FILE, ACKRESULT_FAILED, ft, 0);
- ft->proto->debugLogA("Closing connection for this file transfer... (ft->s is now hBind)");
- ft->s = slisten;
- ft->proto->debugLogA("ft->s is restored to %d", ft->s);
-
- if (ft->s != hConnection)
- Netlib_CloseHandle(hConnection);
- if (ft->hFileEvent != nullptr)
- SetEvent(ft->hFileEvent);
- }
- else Netlib_CloseHandle(hConnection);
-}
-
-static void __cdecl TlenFileReceiveThread(void *arg)
-{
- TLEN_FILE_TRANSFER *ft = (TLEN_FILE_TRANSFER *)arg;
- ft->proto->debugLogA("Thread started: type=file_receive server='%s' port='%d'", ft->hostName, ft->wPort);
- ft->mode = FT_RECV;
-
- NETLIBOPENCONNECTION nloc = { sizeof(nloc) };
- nloc.szHost = ft->hostName;
- nloc.wPort = ft->wPort;
- ProtoBroadcastAck(ft->proto->m_szModuleName, ft->hContact, ACKTYPE_FILE, ACKRESULT_CONNECTING, ft, 0);
- HNETLIBCONN s = Netlib_OpenConnection(ft->proto->m_hNetlibUser, &nloc);
- if (s != nullptr) {
- ft->s = s;
- ft->proto->debugLogA("Entering file receive loop");
- TlenP2PEstablishOutgoingConnection(ft, TRUE);
- while (ft->state != FT_DONE && ft->state != FT_ERROR)
- TlenFileReceiveParse(ft);
-
- if (ft->s) {
- Netlib_CloseHandle(s);
- ft->s = nullptr;
- }
- }
- else {
- ft->pfnNewConnectionV2 = TlenFileReceivingConnection;
- ft->proto->debugLogA("Connection failed - receiving as server");
- s = TlenP2PListen(ft);
- if (s != nullptr) {
- ft->s = s;
- HANDLE hEvent = CreateEvent(nullptr, FALSE, FALSE, nullptr);
- ft->hFileEvent = hEvent;
- ft->currentFile = 0;
- ft->state = FT_CONNECTING;
- char *nick = TlenNickFromJID(ft->jid);
- TlenSend(ft->proto, "<f t='%s' i='%s' e='7' a='%s' p='%d'/>", nick, ft->iqId, ft->localName, ft->wLocalPort);
- mir_free(nick);
- ft->proto->debugLogA("Waiting for the file to be received...");
- WaitForSingleObject(hEvent, INFINITE);
- ft->hFileEvent = nullptr;
- CloseHandle(hEvent);
- ft->proto->debugLogA("Finish all files");
- Netlib_CloseHandle(s);
- }
- else {
- ft->state = FT_ERROR;
- }
- }
- TlenListRemove(ft->proto, LIST_FILE, ft->iqId);
- if (ft->state == FT_DONE)
- ProtoBroadcastAck(ft->proto->m_szModuleName, ft->hContact, ACKTYPE_FILE, ACKRESULT_SUCCESS, ft, 0);
- else {
- char *nick = TlenNickFromJID(ft->jid);
- TlenSend(ft->proto, "<f t='%s' i='%s' e='8'/>", nick, ft->iqId);
- mir_free(nick);
- ProtoBroadcastAck(ft->proto->m_szModuleName, ft->hContact, ACKTYPE_FILE, ACKRESULT_FAILED, ft, 0);
- }
-
- ft->proto->debugLogA("Thread ended: type=file_receive server='%s'", ft->hostName);
- TlenP2PFreeFileTransfer(ft);
-}
-
-
-
-static void TlenFileSendParse(TLEN_FILE_TRANSFER *ft)
-{
- int i;
- char *p, *t;
- int currentFile, numRead;
- char *fileBuffer;
- TLEN_FILE_PACKET *packet;
-
- if (ft->state == FT_CONNECTING) {
- char filename[256]; // Must be 256 (0x100)
- if ((packet = TlenP2PPacketCreate(sizeof(DWORD) + (ft->fileCount*(sizeof(filename) + sizeof(DWORD))))) != nullptr) {
- // Must pause a bit, sending these two packets back to back
- // will break the session because the receiver cannot take it :)
- SleepEx(1000, TRUE);
- TlenP2PPacketSetLen(packet, 0); // Reuse packet
- TlenP2PPacketSetType(packet, TLEN_FILE_PACKET_FILE_LIST);
- TlenP2PPacketPackDword(packet, (DWORD)ft->fileCount);
- for (i = 0; i < ft->fileCount; i++) {
- // struct _stat statbuf;
- // _stat(ft->files[i], &statbuf);
- // TlenP2PPacketPackDword(packet, statbuf.st_size);
- TlenP2PPacketPackDword(packet, ft->filesSize[i]);
- memset(filename, 0, sizeof(filename));
- if ((t = strrchr(ft->files[i], '\\')) != nullptr)
- t++;
- else
- t = ft->files[i];
- strncpy_s(filename, t, _TRUNCATE);
- TlenP2PPacketPackBuffer(packet, filename, sizeof(filename));
- }
- TlenP2PPacketSend(ft->s, packet);
- TlenP2PPacketFree(packet);
-
- ft->allFileReceivedBytes = 0;
- ft->state = FT_INITIALIZING;
- ft->proto->debugLogA("Change to FT_INITIALIZING");
- }
- else ft->state = FT_ERROR;
- }
- else if (ft->state == FT_INITIALIZING) { // FT_INITIALIZING
- TLEN_FILE_PACKET *rpacket = TlenP2PPacketReceive(ft->s);
- ft->proto->debugLogA("FT_INITIALIZING: recv %d", rpacket);
- if (rpacket == nullptr) {
- ft->state = FT_ERROR;
- return;
- }
- ft->proto->debugLogA("FT_INITIALIZING: recv type %d", rpacket->type);
- p = rpacket->packet;
- // TYPE: TLEN_FILE_PACKET_FILE_LIST_ACK will be ignored
- // LEN: 0
- /*if (rpacket->type == TLEN_FILE_PACKET_FILE_LIST_ACK) {
-
- }
- // Then the receiver will request each file
- // TYPE: TLEN_FILE_PACKET_REQUEST
- // LEN:
- // (DWORD) file number
- // (DWORD) 0
- // (DWORD) 0
- else */if (rpacket->type == TLEN_FILE_PACKET_FILE_REQUEST) {
- PROTOFILETRANSFERSTATUS pfts;
- //struct _stat statbuf;
-
- currentFile = *((DWORD*)p);
- if (currentFile != ft->currentFile) {
- ft->proto->debugLogA("Requested file (#%d) is invalid (must be %d)", currentFile, ft->currentFile);
- ft->state = FT_ERROR;
- }
- else {
- // _stat(ft->files[currentFile], &statbuf); // file size in statbuf.st_size
- ft->proto->debugLogA("Sending [%s] [%d]", ft->files[currentFile], ft->filesSize[currentFile]);
- if ((ft->fileId = _open(ft->files[currentFile], _O_BINARY | _O_RDONLY)) < 0) {
- ft->proto->debugLogA("File cannot be opened");
- ft->state = FT_ERROR;
- }
- else {
- memset(&pfts, 0, sizeof(PROTOFILETRANSFERSTATUS));
- pfts.cbSize = sizeof(PROTOFILETRANSFERSTATUS);
- pfts.hContact = ft->hContact;
- pfts.flags = PFTS_SENDING;
- pfts.pszFiles = ft->files;
- pfts.totalFiles = ft->fileCount;
- pfts.currentFileNumber = ft->currentFile;
- pfts.totalBytes = ft->allFileTotalSize;
- pfts.szWorkingDir = nullptr;
- pfts.szCurrentFile = ft->files[ft->currentFile];
- pfts.currentFileSize = ft->filesSize[ft->currentFile]; //statbuf.st_size;
- pfts.currentFileTime = 0;
- ft->fileReceivedBytes = 0;
- if ((packet = TlenP2PPacketCreate(2 * sizeof(DWORD) + 2048)) == nullptr) {
- ft->state = FT_ERROR;
- }
- else {
- TlenP2PPacketSetType(packet, TLEN_FILE_PACKET_FILE_DATA);
- fileBuffer = (char *)mir_alloc(2048);
- ft->proto->debugLogA("Sending file data...");
- while ((numRead = _read(ft->fileId, fileBuffer, 2048)) > 0) {
- TlenP2PPacketSetLen(packet, 0); // Reuse packet
- TlenP2PPacketPackDword(packet, (DWORD)ft->fileReceivedBytes);
- TlenP2PPacketPackDword(packet, 0);
- TlenP2PPacketPackBuffer(packet, fileBuffer, numRead);
- if (TlenP2PPacketSend(ft->s, packet)) {
- ft->fileReceivedBytes += numRead;
- ft->allFileReceivedBytes += numRead;
- pfts.totalProgress = ft->allFileReceivedBytes;
- pfts.currentFileProgress = ft->fileReceivedBytes;
- ProtoBroadcastAck(ft->proto->m_szModuleName, ft->hContact, ACKTYPE_FILE, ACKRESULT_DATA, ft, (LPARAM)&pfts);
- }
- else {
- ft->state = FT_ERROR;
- break;
- }
- }
- mir_free(fileBuffer);
- _close(ft->fileId);
- if (ft->state != FT_ERROR) {
- if (ft->currentFile >= ft->fileCount - 1)
- ft->state = FT_DONE;
- else {
- ft->currentFile++;
- ft->state = FT_INITIALIZING;
- ft->proto->debugLogA("File sent, advancing to the next file...");
- ProtoBroadcastAck(ft->proto->m_szModuleName, ft->hContact, ACKTYPE_FILE, ACKRESULT_NEXTFILE, ft, 0);
- }
- }
- ft->proto->debugLogA("Finishing this file...");
- TlenP2PPacketSetLen(packet, 0); // Reuse packet
- TlenP2PPacketSetType(packet, TLEN_FILE_PACKET_END_OF_FILE);
- TlenP2PPacketPackDword(packet, currentFile);
- TlenP2PPacketSend(ft->s, packet);
- TlenP2PPacketFree(packet);
- }
- }
- }
- TlenP2PPacketFree(rpacket);
- }
- else {
- TlenP2PPacketFree(rpacket);
- ft->state = FT_ERROR;
- }
- }
-}
-
-static void TlenFileSendingConnection(HNETLIBCONN hConnection, DWORD, void * pExtra)
-{
- HNETLIBCONN slisten;
- TlenProtocol *proto = (TlenProtocol *)pExtra;
-
- TLEN_FILE_TRANSFER *ft = TlenP2PEstablishIncomingConnection(proto, hConnection, LIST_FILE, TRUE);
- if (ft != nullptr) {
- slisten = ft->s;
- ft->s = hConnection;
- ft->proto->debugLogA("Set ft->s to %d (saving %d)", hConnection, slisten);
-
- ft->proto->debugLogA("Entering send loop for this file connection... (ft->s is hConnection)");
- while (ft->state != FT_DONE && ft->state != FT_ERROR) {
- TlenFileSendParse(ft);
- }
- if (ft->state == FT_DONE)
- ProtoBroadcastAck(ft->proto->m_szModuleName, ft->hContact, ACKTYPE_FILE, ACKRESULT_SUCCESS, ft, 0);
- else
- ProtoBroadcastAck(ft->proto->m_szModuleName, ft->hContact, ACKTYPE_FILE, ACKRESULT_FAILED, ft, 0);
- ft->proto->debugLogA("Closing connection for this file transfer... (ft->s is now hBind)");
- ft->s = slisten;
- ft->proto->debugLogA("ft->s is restored to %d", ft->s);
-
- if (ft->s != hConnection)
- Netlib_CloseHandle(hConnection);
- if (ft->hFileEvent != nullptr)
- SetEvent(ft->hFileEvent);
- }
- else Netlib_CloseHandle(hConnection);
-}
-
-int TlenFileCancelAll(TlenProtocol *proto)
-{
- HANDLE hEvent;
- int i = 0;
-
- while ((i = TlenListFindNext(proto, LIST_FILE, 0)) >= 0) {
- TLEN_LIST_ITEM *item = TlenListGetItemPtrFromIndex(proto, i);
- if (item != nullptr) {
- TLEN_FILE_TRANSFER *ft = item->ft;
- TlenListRemoveByIndex(proto, i);
- if (ft != nullptr) {
- if (ft->s) {
- //ProtoBroadcastAck(m_szModuleName, ft->hContact, ACKTYPE_FILE, ACKRESULT_FAILED, ft, 0);
- ft->proto->debugLogA("Closing ft->s = %d", ft->s);
- ft->state = FT_ERROR;
- Netlib_CloseHandle(ft->s);
- ft->s = nullptr;
- if (ft->hFileEvent != nullptr) {
- hEvent = ft->hFileEvent;
- ft->hFileEvent = nullptr;
- SetEvent(hEvent);
- }
- }
- else {
- ft->proto->debugLogA("freeing ft struct");
- TlenP2PFreeFileTransfer(ft);
- }
- }
- }
- }
- return 0;
-}
-
-static void __cdecl TlenFileSendingThread(void *arg)
-{
- TLEN_FILE_TRANSFER *ft = (TLEN_FILE_TRANSFER *)arg;
- char *nick;
-
- ft->proto->debugLogA("Thread started: type=tlen_file_send");
- ft->mode = FT_SEND;
- ft->pfnNewConnectionV2 = TlenFileSendingConnection;
- HNETLIBCONN s = TlenP2PListen(ft);
- if (s != nullptr) {
- ProtoBroadcastAck(ft->proto->m_szModuleName, ft->hContact, ACKTYPE_FILE, ACKRESULT_CONNECTING, ft, 0);
- ft->s = s;
- //TlenLog("ft->s = %d", s);
- //TlenLog("fileCount = %d", ft->fileCount);
-
- HANDLE hEvent = CreateEvent(nullptr, FALSE, FALSE, nullptr);
- ft->hFileEvent = hEvent;
- ft->currentFile = 0;
- ft->state = FT_CONNECTING;
-
- nick = TlenNickFromJID(ft->jid);
- TlenSend(ft->proto, "<f t='%s' i='%s' e='6' a='%s' p='%d'/>", nick, ft->iqId, ft->localName, ft->wLocalPort);
- mir_free(nick);
- ft->proto->debugLogA("Waiting for the file to be sent...");
- WaitForSingleObject(hEvent, INFINITE);
- ft->hFileEvent = nullptr;
- CloseHandle(hEvent);
- ft->proto->debugLogA("Finish all files");
- Netlib_CloseHandle(s);
- ft->s = nullptr;
- ft->proto->debugLogA("ft->s is NULL");
-
- if (ft->state == FT_SWITCH) {
- ft->proto->debugLogA("Sending as client...");
- ft->state = FT_CONNECTING;
-
- NETLIBOPENCONNECTION nloc = { sizeof(nloc) };
- nloc.szHost = ft->hostName;
- nloc.wPort = ft->wPort;
- HNETLIBCONN hConn = Netlib_OpenConnection(ft->proto->m_hNetlibUser, &nloc);
- if (hConn != nullptr) {
- ProtoBroadcastAck(ft->proto->m_szModuleName, ft->hContact, ACKTYPE_FILE, ACKRESULT_CONNECTING, ft, 0);
- ft->s = hConn;
- TlenP2PEstablishOutgoingConnection(ft, TRUE);
- ft->proto->debugLogA("Entering send loop for this file connection...");
- while (ft->state != FT_DONE && ft->state != FT_ERROR)
- TlenFileSendParse(ft);
-
- if (ft->state == FT_DONE)
- ProtoBroadcastAck(ft->proto->m_szModuleName, ft->hContact, ACKTYPE_FILE, ACKRESULT_SUCCESS, ft, 0);
- else
- ProtoBroadcastAck(ft->proto->m_szModuleName, ft->hContact, ACKTYPE_FILE, ACKRESULT_FAILED, ft, 0);
- ft->proto->debugLogA("Closing connection for this file transfer... ");
- Netlib_CloseHandle(hConn);
- }
- else ft->state = FT_ERROR;
- }
- }
- else {
- ft->proto->debugLogA("Cannot allocate port to bind for file server thread, thread ended.");
- ft->state = FT_ERROR;
- }
- TlenListRemove(ft->proto, LIST_FILE, ft->iqId);
-
- switch (ft->state) {
- case FT_DONE:
- ft->proto->debugLogA("Finish successfully");
- ProtoBroadcastAck(ft->proto->m_szModuleName, ft->hContact, ACKTYPE_FILE, ACKRESULT_SUCCESS, ft, 0);
- break;
- case FT_DENIED:
- ProtoBroadcastAck(ft->proto->m_szModuleName, ft->hContact, ACKTYPE_FILE, ACKRESULT_DENIED, ft, 0);
- break;
- default: // FT_ERROR:
- nick = TlenNickFromJID(ft->jid);
- TlenSend(ft->proto, "<f t='%s' i='%s' e='8'/>", nick, ft->iqId);
- mir_free(nick);
- ft->proto->debugLogA("Finish with errors");
- ProtoBroadcastAck(ft->proto->m_szModuleName, ft->hContact, ACKTYPE_FILE, ACKRESULT_FAILED, ft, 0);
- break;
- }
- ft->proto->debugLogA("Thread ended: type=file_send");
- TlenP2PFreeFileTransfer(ft);
-}
-
-
-TLEN_FILE_TRANSFER *TlenFileCreateFT(TlenProtocol *proto, const char *jid)
-{
- TLEN_FILE_TRANSFER *ft = (TLEN_FILE_TRANSFER *)mir_alloc(sizeof(TLEN_FILE_TRANSFER));
- memset(ft, 0, sizeof(TLEN_FILE_TRANSFER));
- ft->proto = proto;
- ft->jid = mir_strdup(jid);
- return ft;
-}
-
-
-/*
- * File transfer
- */
-void TlenProcessF(XmlNode *node, ThreadData *info)
-{
- char *p;
- char jid[128], szFilename[MAX_PATH];
- int numFiles;
- TLEN_LIST_ITEM *item;
-
- // if (!node->name || mir_strcmp(node->name, "f")) return;
- if (info == nullptr) return;
-
- char *from = TlenXmlGetAttrValue(node, "f");
- if (from != nullptr) {
- if (strchr(from, '@') == nullptr)
- mir_snprintf(jid, "%s@%s", from, info->server);
- else
- strncpy_s(jid, from, _TRUNCATE);
-
- char *e = TlenXmlGetAttrValue(node, "e");
- if (e != nullptr) {
- if (!mir_strcmp(e, "1")) {
- // FILE_RECV : e='1' : File transfer request
- TLEN_FILE_TRANSFER *ft = TlenFileCreateFT(info->proto, jid);
- ft->hContact = TlenHContactFromJID(info->proto, jid);
-
- if ((p = TlenXmlGetAttrValue(node, "i")) != nullptr)
- ft->iqId = mir_strdup(p);
-
- szFilename[0] = '\0';
- if ((p = TlenXmlGetAttrValue(node, "c")) != nullptr) {
- numFiles = atoi(p);
- if (numFiles == 1) {
- if ((p = TlenXmlGetAttrValue(node, "n")) != nullptr) {
- p = TlenTextDecode(p);
- strncpy(szFilename, p, sizeof(szFilename) - 1);
- mir_free(p);
- }
- else mir_strcpy(szFilename, Translate("1 File"));
- }
- else if (numFiles > 1)
- mir_snprintf(szFilename, Translate("%d Files"), numFiles);
- }
-
- if (szFilename[0] != '\0' && ft->iqId != nullptr) {
- wchar_t* filenameT = mir_utf8decodeW((char*)szFilename);
- PROTORECVFILET pre = { 0 };
- pre.dwFlags = PRFF_UNICODE;
- pre.fileCount = 1;
- pre.timestamp = time(nullptr);
- pre.descr.w = filenameT;
- pre.files.w = &filenameT;
- pre.lParam = (LPARAM)ft;
- ft->proto->debugLogA("sending chainrecv");
- ProtoChainRecvFile(ft->hContact, &pre);
- mir_free(filenameT);
- }
- else {
- // malformed <f/> request, reject
- if (ft->iqId)
- TlenSend(ft->proto, "<f i='%s' e='4' t='%s'/>", ft->iqId, from);
- else
- TlenSend(ft->proto, "<f e='4' t='%s'/>", from);
- TlenP2PFreeFileTransfer(ft);
- }
- }
- else if (!mir_strcmp(e, "3")) {
- // FILE_RECV : e='3' : invalid transfer error
- if ((p = TlenXmlGetAttrValue(node, "i")) != nullptr) {
- if ((item = TlenListGetItemPtr(info->proto, LIST_FILE, p)) != nullptr) {
- if (item->ft != nullptr) {
- ProtoBroadcastAck(info->proto->m_szModuleName, item->ft->hContact, ACKTYPE_FILE, ACKRESULT_FAILED, item->ft, 0);
- info->proto->FileCancel(NULL, item->ft);
- }
- TlenListRemove(info->proto, LIST_FILE, p);
- }
- }
- }
- else if (!mir_strcmp(e, "4")) {
- // FILE_SEND : e='4' : File sending request was denied by the remote client
- if ((p = TlenXmlGetAttrValue(node, "i")) != nullptr) {
- if ((item = TlenListGetItemPtr(info->proto, LIST_FILE, p)) != nullptr) {
- if (!mir_strcmp(item->ft->jid, jid)) {
- ProtoBroadcastAck(info->proto->m_szModuleName, item->ft->hContact, ACKTYPE_FILE, ACKRESULT_DENIED, item->ft, 0);
- TlenListRemove(info->proto, LIST_FILE, p);
- }
- }
- }
- }
- else if (!mir_strcmp(e, "5")) {
- // FILE_SEND : e='5' : File sending request was accepted
- if ((p = TlenXmlGetAttrValue(node, "i")) != nullptr)
- if ((item = TlenListGetItemPtr(info->proto, LIST_FILE, p)) != nullptr)
- if (!mir_strcmp(item->ft->jid, jid))
- mir_forkthread(TlenFileSendingThread, item->ft);
- }
- else if (!mir_strcmp(e, "6")) {
- // FILE_RECV : e='6' : IP and port information to connect to get file
- if ((p = TlenXmlGetAttrValue(node, "i")) != nullptr) {
- if ((item = TlenListGetItemPtr(info->proto, LIST_FILE, p)) != nullptr) {
- if ((p = TlenXmlGetAttrValue(node, "a")) != nullptr) {
- item->ft->hostName = mir_strdup(p);
- if ((p = TlenXmlGetAttrValue(node, "p")) != nullptr) {
- item->ft->wPort = atoi(p);
- mir_forkthread(TlenFileReceiveThread, item->ft);
- }
- }
- }
- }
- }
- else if (!mir_strcmp(e, "7")) {
- // FILE_RECV : e='7' : IP and port information to connect to send file
- // in case the conection to the given server was not successful
- if ((p = TlenXmlGetAttrValue(node, "i")) != nullptr) {
- if ((item = TlenListGetItemPtr(info->proto, LIST_FILE, p)) != nullptr) {
- if ((p = TlenXmlGetAttrValue(node, "a")) != nullptr) {
- if (item->ft->hostName != nullptr) mir_free(item->ft->hostName);
- item->ft->hostName = mir_strdup(p);
- if ((p = TlenXmlGetAttrValue(node, "p")) != nullptr) {
- item->ft->wPort = atoi(p);
- item->ft->state = FT_SWITCH;
- SetEvent(item->ft->hFileEvent);
- }
- }
- }
- }
- }
- else if (!mir_strcmp(e, "8")) {
- // FILE_RECV : e='8' : transfer error
- if ((p = TlenXmlGetAttrValue(node, "i")) != nullptr) {
- if ((item = TlenListGetItemPtr(info->proto, LIST_FILE, p)) != nullptr) {
- item->ft->state = FT_ERROR;
- if (item->ft->hFileEvent != nullptr)
- SetEvent(item->ft->hFileEvent);
- else
- ProtoBroadcastAck(info->proto->m_szModuleName, item->ft->hContact, ACKTYPE_FILE, ACKRESULT_FAILED, item->ft, 0);
- }
- }
- }
- }
- }
-}
diff --git a/protocols/Tlen/src/tlen_file.h b/protocols/Tlen/src/tlen_file.h
deleted file mode 100644
index 346ec886ac..0000000000
--- a/protocols/Tlen/src/tlen_file.h
+++ /dev/null
@@ -1,34 +0,0 @@
-/*
-
-Jabber Protocol Plugin for Miranda IM
-Tlen Protocol Plugin for Miranda NG
-Copyright (C) 2002-2004 Santithorn Bunchua
-Copyright (C) 2004-2007 Piotr Piastucki
-
-This program is free software; you can redistribute it and/or
-modify it under the terms of the GNU General Public License
-as published by the Free Software Foundation; either version 2
-of the License, or (at your option) any later version.
-
-This program 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 General Public License for more details.
-
-You should have received a copy of the GNU General Public License
-along with this program; if not, write to the Free Software
-Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
-
-*/
-
-#ifndef _TLEN_FILE_H_
-#define _TLEN_FILE_H_
-
-#include <windows.h>
-#include "stdafx.h"
-
-extern int TlenFileCancelAll(TlenProtocol *proto);
-extern void TlenProcessF(XmlNode *node, ThreadData *userdata);
-extern TLEN_FILE_TRANSFER *TlenFileCreateFT(TlenProtocol *proto, const char *jid);
-#endif
-
diff --git a/protocols/Tlen/src/tlen_iq.cpp b/protocols/Tlen/src/tlen_iq.cpp
deleted file mode 100644
index 82923570b3..0000000000
--- a/protocols/Tlen/src/tlen_iq.cpp
+++ /dev/null
@@ -1,108 +0,0 @@
-/*
-
-Jabber Protocol Plugin for Miranda IM
-Tlen Protocol Plugin for Miranda NG
-Copyright (C) 2002-2004 Santithorn Bunchua
-
-This program is free software; you can redistribute it and/or
-modify it under the terms of the GNU General Public License
-as published by the Free Software Foundation; either version 2
-of the License, or (at your option) any later version.
-
-This program 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 General Public License for more details.
-
-You should have received a copy of the GNU General Public License
-along with this program; if not, write to the Free Software
-Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
-
-*/
-
-#include "stdafx.h"
-#include "tlen_iq.h"
-
-
-void TlenIqInit(TlenProtocol *proto)
-{
- proto->iqList = nullptr;
- proto->iqCount = 0;
- proto->iqAlloced = 0;
-}
-
-void TlenIqUninit(TlenProtocol *proto)
-{
- if (proto->iqList) mir_free(proto->iqList);
- proto->iqList = nullptr;
- proto->iqCount = 0;
- proto->iqAlloced = 0;
-}
-
-static void TlenIqRemove(TlenProtocol *proto, int index)
-{
- mir_cslock lck(proto->csIqList);
- if (index >= 0 && index<proto->iqCount) {
- memmove(proto->iqList+index, proto->iqList+index+1, sizeof(TLEN_IQ_FUNC)*(proto->iqCount-index-1));
- proto->iqCount--;
- }
-}
-
-static void TlenIqExpire(TlenProtocol *proto)
-{
- int i;
- time_t expire;
-
- mir_cslock lck(proto->csIqList);
- expire = time(nullptr) - 120; // 2 minute
- i = 0;
- while (i < proto->iqCount) {
- if (proto->iqList[i].requestTime < expire)
- TlenIqRemove(proto, i);
- else
- i++;
- }
-}
-
-TLEN_IQ_PFUNC TlenIqFetchFunc(TlenProtocol *proto, int iqId)
-{
- int i;
- TLEN_IQ_PFUNC res;
-
- mir_cslock lck(proto->csIqList);
- TlenIqExpire(proto);
- for (i=0; i<proto->iqCount && proto->iqList[i].iqId != iqId; i++);
- if (i < proto->iqCount) {
- res = proto->iqList[i].func;
- TlenIqRemove(proto, i);
- }
- else {
- res = (TLEN_IQ_PFUNC) nullptr;
- }
- return res;
-}
-
-void TlenIqAdd(TlenProtocol *proto, unsigned int iqId, TLEN_IQ_PROCID procId, TLEN_IQ_PFUNC func)
-{
- int i;
-
- mir_cslock lck(proto->csIqList);
- if (procId == IQ_PROC_NONE)
- i = proto->iqCount;
- else
- for (i=0; i<proto->iqCount && proto->iqList[i].procId != procId; i++);
-
- if (i >= proto->iqCount && proto->iqCount >= proto->iqAlloced) {
- proto->iqAlloced = proto->iqCount + 8;
- proto->iqList = (TLEN_IQ_FUNC*)mir_realloc(proto->iqList, sizeof(TLEN_IQ_FUNC)*proto->iqAlloced);
- }
-
- if (proto->iqList != nullptr) {
- proto->iqList[i].iqId = iqId;
- proto->iqList[i].procId = procId;
- proto->iqList[i].func = func;
- proto->iqList[i].requestTime = time(nullptr);
- if (i == proto->iqCount) proto->iqCount++;
- }
-}
-
diff --git a/protocols/Tlen/src/tlen_iq.h b/protocols/Tlen/src/tlen_iq.h
deleted file mode 100644
index 6966de74fb..0000000000
--- a/protocols/Tlen/src/tlen_iq.h
+++ /dev/null
@@ -1,54 +0,0 @@
-/*
-
-Jabber Protocol Plugin for Miranda IM
-Tlen Protocol Plugin for Miranda NG
-Copyright (C) 2002-2004 Santithorn Bunchua
-Copyright (C) 2004-2007 Piotr Piastucki
-
-This program is free software; you can redistribute it and/or
-modify it under the terms of the GNU General Public License
-as published by the Free Software Foundation; either version 2
-of the License, or (at your option) any later version.
-
-This program 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 General Public License for more details.
-
-You should have received a copy of the GNU General Public License
-along with this program; if not, write to the Free Software
-Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
-
-*/
-#ifndef _TLEN_IQ_H_
-#define _TLEN_IQ_H_
-
-#include "tlen_xml.h"
-#include "stdafx.h"
-
-typedef void (*TLEN_IQ_PFUNC)(TlenProtocol *proto, XmlNode *iqNode);
-
-typedef struct TLEN_IQ_FUNC_STRUCT {
- int iqId; // id to match IQ get/set with IQ result
- TLEN_IQ_PROCID procId; // must be unique in the list, except for IQ_PROC_NONE which can have multiple entries
- TLEN_IQ_PFUNC func; // callback function
- time_t requestTime; // time the request was sent, used to remove relinquent entries
-} TLEN_IQ_FUNC;
-
-void TlenIqInit(TlenProtocol *proto);
-void TlenIqUninit(TlenProtocol *proto);
-TLEN_IQ_PFUNC TlenIqFetchFunc(TlenProtocol *proto, int iqId);
-void TlenIqAdd(TlenProtocol *proto, unsigned int iqId, TLEN_IQ_PROCID procId, TLEN_IQ_PFUNC func);
-
-void TlenIqResultAuth(TlenProtocol *proto, XmlNode *iqNode);
-void TlenIqResultRoster(TlenProtocol *proto, XmlNode *iqNode);
-void TlenIqResultVcard(TlenProtocol *proto, XmlNode *iqNode);
-void TlenIqResultSearch(TlenProtocol *proto, XmlNode *iqNode);
-void TlenIqResultVersion(TlenProtocol *proto, XmlNode *iqNode);
-void TlenIqResultInfo(TlenProtocol *proto, XmlNode *iqNode);
-void TlenIqResultTcfg(TlenProtocol *proto, XmlNode *iqNode);
-
-//void TlenIqResultSetPassword(XmlNode *iqNode, void *userdata);
-
-#endif
-
diff --git a/protocols/Tlen/src/tlen_iqid.cpp b/protocols/Tlen/src/tlen_iqid.cpp
deleted file mode 100644
index 39024a544a..0000000000
--- a/protocols/Tlen/src/tlen_iqid.cpp
+++ /dev/null
@@ -1,625 +0,0 @@
-/*
-
-Jabber Protocol Plugin for Miranda IM
-Tlen Protocol Plugin for Miranda NG
-Copyright (C) 2002-2004 Santithorn Bunchua
-Copyright (C) 2004-2007 Piotr Piastucki
-
-This program is free software; you can redistribute it and/or
-modify it under the terms of the GNU General Public License
-as published by the Free Software Foundation; either version 2
-of the License, or (at your option) any later version.
-
-This program 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 General Public License for more details.
-
-You should have received a copy of the GNU General Public License
-along with this program; if not, write to the Free Software
-Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
-
-*/
-
-#include "stdafx.h"
-#include "resource.h"
-#include "tlen_list.h"
-#include "tlen_iq.h"
-#include "tlen_muc.h"
-
-// RECVED: authentication result
-// ACTION: if successfully logged in, continue by requesting roster list and set my initial status
-void TlenIqResultAuth(TlenProtocol *proto, XmlNode *iqNode)
-{
- char *type = TlenXmlGetAttrValue(iqNode, "type");
- if (type == nullptr)
- return;
-
- if (!mir_strcmp(type, "result")) {
- DBVARIANT dbv;
-
- if (db_get(NULL, proto->m_szModuleName, "Nick", &dbv))
- db_set_s(NULL, proto->m_szModuleName, "Nick", proto->threadData->username);
- else
- db_free(&dbv);
- // iqId = TlenSerialNext();
- // TlenIqAdd(iqId, IQ_PROC_NONE, TlenIqResultGetRoster);
- // TlenSend(info, "<iq type='get' id='"TLEN_IQID"%d'><query xmlns='jabber:iq:roster'/></iq>", iqId);
-
- TlenSend(proto, "<iq type='get' id='GetRoster'><query xmlns='jabber:iq:roster'/></iq>");
- TlenSend(proto, "<iq to='tcfg' type='get' id='TcfgGetAfterLoggedIn'></iq>");
- }
- // What to do if password error? etc...
- else if (!mir_strcmp(type, "error")) {
- char text[128];
-
- TlenSend(proto, "</s>");
- mir_snprintf(text, Translate("Authentication failed for %s@%s."), proto->threadData->username, proto->threadData->server);
- MessageBoxA(nullptr, text, Translate("Tlen Authentication"), MB_OK | MB_ICONSTOP | MB_SETFOREGROUND);
- ProtoBroadcastAck(proto->m_szModuleName, NULL, ACKTYPE_LOGIN, ACKRESULT_FAILED, nullptr, LOGINERR_WRONGPASSWORD);
- proto->threadData = nullptr; // To disallow auto reconnect
- }
-}
-
-void TlenResultSetRoster(TlenProtocol *proto, XmlNode *queryNode) {
- DBVARIANT dbv;
- XmlNode *groupNode;
- MCONTACT hContact;
- char *name, *nick;
-
- for (int i = 0; i < queryNode->numChild; i++) {
- XmlNode *itemNode = queryNode->child[i];
- if (!mir_strcmp(itemNode->name, "item")) {
- char *jid = TlenXmlGetAttrValue(itemNode, "jid");
- if (jid != nullptr) {
- char *str = TlenXmlGetAttrValue(itemNode, "subscription");
- if (str == nullptr)
- continue;
- else if (!mir_strcmp(str, "remove")) {
- if ((hContact = TlenHContactFromJID(proto, jid)) != NULL) {
- if (db_get_w(hContact, proto->m_szModuleName, "Status", ID_STATUS_OFFLINE) != ID_STATUS_OFFLINE)
- db_set_w(hContact, proto->m_szModuleName, "Status", ID_STATUS_OFFLINE);
- }
- TlenListRemove(proto, LIST_ROSTER, jid);
- }
- else {
- TLEN_LIST_ITEM *item = TlenListAdd(proto, LIST_ROSTER, jid);
- if (item != nullptr) {
- if (!mir_strcmp(str, "both"))
- item->subscription = SUB_BOTH;
- else if (!mir_strcmp(str, "to"))
- item->subscription = SUB_TO;
- else if (!mir_strcmp(str, "from"))
- item->subscription = SUB_FROM;
- else
- item->subscription = SUB_NONE;
- if ((name = TlenXmlGetAttrValue(itemNode, "name")) != nullptr) {
- nick = TlenTextDecode(name);
- }
- else {
- nick = TlenLocalNickFromJID(jid);
- }
- if (nick != nullptr) {
- if (item->nick) mir_free(item->nick);
- item->nick = nick;
-
- if ((hContact = TlenHContactFromJID(proto, jid)) == NULL) {
- // Received roster has a new JID.
- // Add the jid (with empty resource) to Miranda contact list.
- hContact = TlenDBCreateContact(proto, jid, nick, FALSE);
- }
- db_set_s(hContact, "CList", "MyHandle", nick);
- if (item->group) mir_free(item->group);
- if ((groupNode = TlenXmlGetChild(itemNode, "group")) != nullptr && groupNode->text != nullptr) {
- item->group = TlenGroupDecode(groupNode->text);
- Clist_GroupCreate(0, _A2T(item->group));
- // Don't set group again if already correct, or Miranda may show wrong group count in some case
- if (!db_get(hContact, "CList", "Group", &dbv)) {
- if (mir_strcmp(dbv.pszVal, item->group))
- db_set_s(hContact, "CList", "Group", item->group);
- db_free(&dbv);
- }
- else
- db_set_s(hContact, "CList", "Group", item->group);
- }
- else {
- item->group = nullptr;
- db_unset(hContact, "CList", "Group");
- }
- }
- }
- }
- }
- }
- }
-}
-
-// RECVED: roster information
-// ACTION: populate LIST_ROSTER and create contact for any new rosters
-void TlenIqResultRoster(TlenProtocol *proto, XmlNode *iqNode)
-{
- char *type = TlenXmlGetAttrValue(iqNode, "type");
- if (type == nullptr) return;
- XmlNode *queryNode = TlenXmlGetChild(iqNode, "query");
- if (queryNode == nullptr) return;
-
- if (!mir_strcmp(type, "result")) {
- char *str = TlenXmlGetAttrValue(queryNode, "xmlns");
- if (str != nullptr && !mir_strcmp(str, "jabber:iq:roster")) {
- DBVARIANT dbv;
- XmlNode *itemNode, *groupNode;
- TLEN_SUBSCRIPTION sub;
- TLEN_LIST_ITEM *item;
- char *jid, *name, *nick;
- int i;
-
- for (i = 0; i < queryNode->numChild; i++) {
- itemNode = queryNode->child[i];
- if (!mir_strcmp(itemNode->name, "item")) {
- str = TlenXmlGetAttrValue(itemNode, "subscription");
- if (str == nullptr) sub = SUB_NONE;
- else if (!mir_strcmp(str, "both")) sub = SUB_BOTH;
- else if (!mir_strcmp(str, "to")) sub = SUB_TO;
- else if (!mir_strcmp(str, "from")) sub = SUB_FROM;
- else sub = SUB_NONE;
- //if (str != NULL && (!mir_strcmp(str, "to") || !mir_strcmp(str, "both"))) {
- if ((jid = TlenXmlGetAttrValue(itemNode, "jid")) != nullptr) {
- if ((name = TlenXmlGetAttrValue(itemNode, "name")) != nullptr)
- nick = TlenTextDecode(name);
- else
- nick = TlenLocalNickFromJID(jid);
-
- if (nick != nullptr) {
- MCONTACT hContact;
- item = TlenListAdd(proto, LIST_ROSTER, jid);
- if (item->nick) mir_free(item->nick);
- item->nick = nick;
- item->subscription = sub;
- if ((hContact = TlenHContactFromJID(proto, jid)) == NULL) {
- // Received roster has a new JID.
- // Add the jid (with empty resource) to Miranda contact list.
- hContact = TlenDBCreateContact(proto, jid, nick, FALSE);
- }
- db_set_s(hContact, "CList", "MyHandle", nick);
- if (item->group) mir_free(item->group);
- if ((groupNode = TlenXmlGetChild(itemNode, "group")) != nullptr && groupNode->text != nullptr) {
- item->group = TlenGroupDecode(groupNode->text);
- Clist_GroupCreate(0, _A2T(item->group));
- // Don't set group again if already correct, or Miranda may show wrong group count in some case
- if (!db_get(hContact, "CList", "Group", &dbv)) {
- if (mir_strcmp(dbv.pszVal, item->group))
- db_set_s(hContact, "CList", "Group", item->group);
- db_free(&dbv);
- }
- else db_set_s(hContact, "CList", "Group", item->group);
- }
- else {
- item->group = nullptr;
- db_unset(hContact, "CList", "Group");
- }
-
- ptrA szAvatarHash(proto->getStringA(hContact, "AvatarHash"));
- if (szAvatarHash != NULL) {
- replaceStr(item->avatarHash, szAvatarHash);
- proto->debugLogA("Setting hash [%s] = %s", nick, item->avatarHash);
- }
- item->avatarFormat = db_get_dw(hContact, proto->m_szModuleName, "AvatarFormat", PA_FORMAT_UNKNOWN);
- }
- }
- }
- }
-
- // Delete orphaned contacts (if roster sync is enabled)
- if (db_get_b(NULL, proto->m_szModuleName, "RosterSync", FALSE) == TRUE) {
- for (MCONTACT hContact = db_find_first(proto->m_szModuleName); hContact; ) {
- MCONTACT hNext = hContact = db_find_next(hContact, proto->m_szModuleName);
- ptrA szJid(proto->getStringA(hContact, "szJid"));
- if (szJid != NULL) {
- if (!TlenListExist(proto, LIST_ROSTER, szJid)) {
- proto->debugLogA("Syncing roster: deleting 0x%x", hContact);
- db_delete_contact(hContact);
- }
- }
- hContact = hNext;
- }
- }
-
- }
- }
-}
-
-
-// Tlen actually use jabber:iq:search for other users vCard or jabber:iq:register for own vCard
-void TlenIqResultVcard(TlenProtocol *proto, XmlNode *iqNode)
-{
- char text[128];
- MCONTACT hContact;
- char *nText;
-
- // TlenLog("<iq/> iqIdGetVcard (tlen)");
- char *type = TlenXmlGetAttrValue(iqNode, "type");
- if (type == nullptr) return;
-
- if (!mir_strcmp(type, "result")) {
- DBVARIANT dbv;
-
- XmlNode *queryNode = TlenXmlGetChild(iqNode, "query");
- if (queryNode == nullptr) return;
- XmlNode *itemNode = TlenXmlGetChild(queryNode, "item");
- if (itemNode == nullptr) return;
- char *jid = TlenXmlGetAttrValue(itemNode, "jid");
- if (jid != nullptr) {
- if (db_get(NULL, proto->m_szModuleName, "LoginServer", &dbv)) return;
- if (strchr(jid, '@') != nullptr) {
- strncpy_s(text, jid, _TRUNCATE);
- }
- else {
- mir_snprintf(text, "%s@%s", jid, dbv.pszVal); // Add @tlen.pl
- }
- db_free(&dbv);
- if ((hContact = TlenHContactFromJID(proto, text)) == NULL) {
- if (db_get(NULL, proto->m_szModuleName, "LoginName", &dbv)) return;
- if (mir_strcmp(dbv.pszVal, jid)) {
- db_free(&dbv);
- return;
- }
- db_free(&dbv);
- }
- }
- else {
- hContact = NULL;
- }
- bool hasFirst = false, hasLast = false, hasNick = false, hasEmail = false, hasCity = false, hasAge = false,
- hasGender = false, hasSchool = false, hasLookFor = false, hasOccupation = false;
- for (int i = 0; i < itemNode->numChild; i++) {
- XmlNode *n = itemNode->child[i];
- if (n == nullptr || n->name == nullptr) continue;
- if (!mir_strcmp(n->name, "first")) {
- if (n->text != nullptr) {
- hasFirst = true;
- nText = TlenTextDecode(n->text);
- db_set_s(hContact, proto->m_szModuleName, "FirstName", nText);
- mir_free(nText);
- }
- }
- else if (!mir_strcmp(n->name, "last")) {
- if (n->text != nullptr) {
- hasLast = true;
- nText = TlenTextDecode(n->text);
- db_set_s(hContact, proto->m_szModuleName, "LastName", nText);
- mir_free(nText);
- }
- }
- else if (!mir_strcmp(n->name, "nick")) {
- if (n->text != nullptr) {
- hasNick = true;
- nText = TlenTextDecode(n->text);
- db_set_s(hContact, proto->m_szModuleName, "Nick", nText);
- mir_free(nText);
- }
- }
- else if (!mir_strcmp(n->name, "email")) {
- if (n->text != nullptr) {
- hasEmail = true;
- nText = TlenTextDecode(n->text);
- db_set_s(hContact, proto->m_szModuleName, "e-mail", nText);
- mir_free(nText);
- }
- }
- else if (!mir_strcmp(n->name, "c")) {
- if (n->text != nullptr) {
- hasCity = true;
- nText = TlenTextDecode(n->text);
- db_set_s(hContact, proto->m_szModuleName, "City", nText);
- mir_free(nText);
- }
- }
- else if (!mir_strcmp(n->name, "b")) {
- if (n->text != nullptr) {
- WORD nAge = atoi(n->text);
- hasAge = true;
- db_set_w(hContact, proto->m_szModuleName, "Age", nAge);
- }
- }
- else if (!mir_strcmp(n->name, "s")) {
- if (n->text != nullptr && n->text[1] == '\0' && (n->text[0] == '1' || n->text[0] == '2')) {
- hasGender = true;
- db_set_b(hContact, proto->m_szModuleName, "Gender", (BYTE)(n->text[0] == '1' ? 'M' : 'F'));
- }
- }
- else if (!mir_strcmp(n->name, "e")) {
- if (n->text != nullptr) {
- hasSchool = true;
- nText = TlenTextDecode(n->text);
- db_set_s(hContact, proto->m_szModuleName, "School", nText);
- mir_free(nText);
- }
- }
- else if (!mir_strcmp(n->name, "j")) {
- if (n->text != nullptr) {
- hasOccupation = true;
- WORD nOccupation = atoi(n->text);
- db_set_w(hContact, proto->m_szModuleName, "Occupation", nOccupation);
- }
- }
- else if (!mir_strcmp(n->name, "r")) {
- if (n->text != nullptr) {
- WORD nLookFor = atoi(n->text);
- hasLookFor = true;
- db_set_w(hContact, proto->m_szModuleName, "LookingFor", nLookFor);
- }
- }
- else if (!mir_strcmp(n->name, "g")) { // voice chat enabled
- if (n->text != nullptr) {
- BYTE bVoice = atoi(n->text);
- db_set_w(hContact, proto->m_szModuleName, "VoiceChat", bVoice);
- }
- }
- else if (!mir_strcmp(n->name, "v")) { // status visibility
- if (n->text != nullptr) {
- BYTE bPublic = atoi(n->text);
- db_set_w(hContact, proto->m_szModuleName, "PublicStatus", bPublic);
- }
- }
- }
- if (!hasFirst)
- db_unset(hContact, proto->m_szModuleName, "FirstName");
- if (!hasLast)
- db_unset(hContact, proto->m_szModuleName, "LastName");
- // We are not removing "Nick"
-// if (!hasNick)
-// db_unset(hContact, m_szModuleName, "Nick");
- if (!hasEmail)
- db_unset(hContact, proto->m_szModuleName, "e-mail");
- if (!hasCity)
- db_unset(hContact, proto->m_szModuleName, "City");
- if (!hasAge)
- db_unset(hContact, proto->m_szModuleName, "Age");
- if (!hasGender)
- db_unset(hContact, proto->m_szModuleName, "Gender");
- if (!hasSchool)
- db_unset(hContact, proto->m_szModuleName, "School");
- if (!hasOccupation)
- db_unset(hContact, proto->m_szModuleName, "Occupation");
- if (!hasLookFor)
- db_unset(hContact, proto->m_szModuleName, "LookingFor");
- ProtoBroadcastAck(proto->m_szModuleName, hContact, ACKTYPE_GETINFO, ACKRESULT_SUCCESS, (HANDLE)1, 0);
- }
-}
-
-void TlenIqResultSearch(TlenProtocol *proto, XmlNode *iqNode)
-{
- XmlNode *queryNode, *itemNode, *n;
- char *jid;
- int i, found = 0;
- TLEN_SEARCH_RESULT jsr = { 0 };
- DBVARIANT dbv = { 0 };
-
- // TlenLog("<iq/> iqIdGetSearch");
- char *type = TlenXmlGetAttrValue(iqNode, "type");
- if (type == nullptr)
- return;
- char *str = TlenXmlGetAttrValue(iqNode, "id");
- if (str == nullptr)
- return;
- int id = atoi(str + mir_strlen(TLEN_IQID));
-
- if (!mir_strcmp(type, "result")) {
- if ((queryNode = TlenXmlGetChild(iqNode, "query")) == nullptr) return;
- if (!db_get(NULL, proto->m_szModuleName, "LoginServer", &dbv)) {
- jsr.hdr.cbSize = sizeof(TLEN_SEARCH_RESULT);
- jsr.hdr.flags = PSR_UNICODE;
- for (i = 0; i < queryNode->numChild; i++) {
- itemNode = queryNode->child[i];
- if (!mir_strcmp(itemNode->name, "item")) {
- if ((jid = TlenXmlGetAttrValue(itemNode, "jid")) != nullptr) {
- if (strchr(jid, '@') != nullptr) {
- strncpy_s(jsr.jid, jid, _TRUNCATE);
- }
- else {
- mir_snprintf(jsr.jid, "%s@%s", jid, dbv.pszVal);
- }
- jsr.jid[sizeof(jsr.jid) - 1] = '\0';
- jsr.hdr.id.w = mir_a2u(jid);
- if ((n = TlenXmlGetChild(itemNode, "nick")) != nullptr && n->text != nullptr) {
- char* buf = TlenTextDecode(n->text);
- jsr.hdr.nick.w = mir_a2u(buf);
- mir_free(buf);
- }
- else {
- jsr.hdr.nick.w = mir_wstrdup(TEXT(""));
- }
- if ((n = TlenXmlGetChild(itemNode, "first")) != nullptr && n->text != nullptr) {
- char* buf = TlenTextDecode(n->text);
- jsr.hdr.firstName.w = mir_a2u(buf);
- mir_free(buf);
- }
- else {
- jsr.hdr.firstName.w = mir_wstrdup(TEXT(""));
- }
- if ((n = TlenXmlGetChild(itemNode, "last")) != nullptr && n->text != nullptr) {
- char* buf = TlenTextDecode(n->text);
- jsr.hdr.lastName.w = mir_a2u(buf);
- mir_free(buf);
- }
- else {
- jsr.hdr.lastName.w = mir_wstrdup(TEXT(""));
- }
- if ((n = TlenXmlGetChild(itemNode, "email")) != nullptr && n->text != nullptr) {
- char* buf = TlenTextDecode(n->text);
- jsr.hdr.email.w = mir_a2u(buf);
- mir_free(buf);
- }
- else {
- jsr.hdr.email.w = mir_wstrdup(TEXT(""));
- }
-
- ProtoBroadcastAck(proto->m_szModuleName, NULL, ACKTYPE_SEARCH, ACKRESULT_DATA, (HANDLE)id, (LPARAM)&jsr);
- found = 1;
- mir_free(jsr.hdr.id.w);
- mir_free(jsr.hdr.nick.w);
- mir_free(jsr.hdr.firstName.w);
- mir_free(jsr.hdr.lastName.w);
- mir_free(jsr.hdr.email.w);
- }
- }
- }
- if (proto->searchJID != nullptr) {
- if (!found) {
- if (strchr(proto->searchJID, '@') != nullptr)
- strncpy_s(jsr.jid, proto->searchJID, _TRUNCATE);
- else
- mir_snprintf(jsr.jid, "%s@%s", proto->searchJID, dbv.pszVal);
-
- jsr.hdr.nick.w = jsr.hdr.firstName.w = jsr.hdr.lastName.w = jsr.hdr.email.w = jsr.hdr.id.w = TEXT("");
- ProtoBroadcastAck(proto->m_szModuleName, NULL, ACKTYPE_SEARCH, ACKRESULT_DATA, (HANDLE)id, (LPARAM)&jsr);
- }
- mir_free(proto->searchJID);
- proto->searchJID = nullptr;
- }
- db_free(&dbv);
- }
- found = 0;
- if (queryNode->numChild == TLEN_MAX_SEARCH_RESULTS_PER_PAGE) {
- found = TlenRunSearch(proto);
- }
- if (!found) {
- ProtoBroadcastAck(proto->m_szModuleName, NULL, ACKTYPE_SEARCH, ACKRESULT_SUCCESS, (HANDLE)id, 0);
- }
- }
- else if (!mir_strcmp(type, "error")) {
- // ProtoBroadcastAck(proto->m_szModuleName, NULL, ACKTYPE_SEARCH, ACKRESULT_FAILED, (HANDLE) id, 0);
- // There is no ACKRESULT_FAILED for ACKTYPE_SEARCH :) look at findadd.c
- // So we will just send a SUCCESS
- ProtoBroadcastAck(proto->m_szModuleName, NULL, ACKTYPE_SEARCH, ACKRESULT_SUCCESS, (HANDLE)id, 0);
- }
-}
-
-
-void GetConfigItem(XmlNode *node, char *dest, BOOL bMethod, int *methodDest) {
- mir_strcpy(dest, node->text);
- TlenUrlDecode(dest);
- if (bMethod) {
- char *method = TlenXmlGetAttrValue(node, "method");
- if (method != nullptr && !strcmpi(method, "POST")) {
- *methodDest = REQUEST_POST;
- }
- else {
- *methodDest = REQUEST_GET;
- }
- }
-}
-
-void TlenIqResultTcfg(TlenProtocol *proto, XmlNode *iqNode)
-{
- XmlNode *queryNode, *miniMailNode, *node;
-
- char *type = TlenXmlGetAttrValue(iqNode, "type");
- if (type == nullptr) return;
- if (!mir_strcmp(type, "result")) {
- if ((queryNode = TlenXmlGetChild(iqNode, "query")) == nullptr) return;
- if ((miniMailNode = TlenXmlGetChild(queryNode, "mini-mail")) == nullptr) return;
- if ((node = TlenXmlGetChild(miniMailNode, "base")) != nullptr) {
- GetConfigItem(node, proto->threadData->tlenConfig.mailBase, FALSE, nullptr);
- }
- if ((node = TlenXmlGetChild(miniMailNode, "msg")) != nullptr) {
- GetConfigItem(node, proto->threadData->tlenConfig.mailMsg, TRUE, &proto->threadData->tlenConfig.mailMsgMthd);
- }
- if ((node = TlenXmlGetChild(miniMailNode, "index")) != nullptr) {
- GetConfigItem(node, proto->threadData->tlenConfig.mailIndex, TRUE, &proto->threadData->tlenConfig.mailIndexMthd);
- }
- if ((node = TlenXmlGetChild(miniMailNode, "login")) != nullptr) {
- GetConfigItem(node, proto->threadData->tlenConfig.mailLogin, TRUE, &proto->threadData->tlenConfig.mailLoginMthd);
- }
- if ((node = TlenXmlGetChild(miniMailNode, "compose")) != nullptr) {
- GetConfigItem(node, proto->threadData->tlenConfig.mailCompose, TRUE, &proto->threadData->tlenConfig.mailComposeMthd);
- }
- if ((node = TlenXmlGetChild(miniMailNode, "avatar-get")) != nullptr) {
- GetConfigItem(node, proto->threadData->tlenConfig.avatarGet, TRUE, &proto->threadData->tlenConfig.avatarGetMthd);
- }
- if ((node = TlenXmlGetChild(miniMailNode, "avatar-upload")) != nullptr) {
- GetConfigItem(node, proto->threadData->tlenConfig.avatarUpload, TRUE, &proto->threadData->tlenConfig.avatarUploadMthd);
- }
- if ((node = TlenXmlGetChild(miniMailNode, "avatar-remove")) != nullptr) {
- GetConfigItem(node, proto->threadData->tlenConfig.avatarRemove, TRUE, &proto->threadData->tlenConfig.avatarRemoveMthd);
- }
-
-
- //continue connecting
- Menu_ModifyItem(proto->hMenuMUC, nullptr, INVALID_HANDLE_VALUE, 0);
- if (proto->hMenuChats != nullptr)
- Menu_ModifyItem(proto->hMenuChats, nullptr, INVALID_HANDLE_VALUE, 0);
-
- proto->isOnline = TRUE;
- proto->debugLogA("Status changed via THREADSTART");
- int oldStatus = proto->m_iStatus;
- TlenSendPresence(proto, proto->m_iDesiredStatus);
- ProtoBroadcastAck(proto->m_szModuleName, NULL, ACKTYPE_STATUS, ACKRESULT_SUCCESS, (HANDLE)oldStatus, proto->m_iStatus);
-
- }
-}
-
-void TlenIqResultVersion(TlenProtocol *proto, XmlNode *iqNode)
-{
- XmlNode *queryNode = TlenXmlGetChild(iqNode, "query");
- if (queryNode != nullptr) {
- char *from = TlenXmlGetAttrValue(iqNode, "from");
- if (from != nullptr) {
- TLEN_LIST_ITEM *item = TlenListGetItemPtr(proto, LIST_ROSTER, from);
- if (item != nullptr) {
- XmlNode *n;
- if (item->software) mir_free(item->software);
- if (item->version) mir_free(item->version);
- if (item->system) mir_free(item->system);
- if ((n = TlenXmlGetChild(queryNode, "name")) != nullptr && n->text) {
- item->software = TlenTextDecode(n->text);
- }
- else
- item->software = nullptr;
- if ((n = TlenXmlGetChild(queryNode, "version")) != nullptr && n->text)
- item->version = TlenTextDecode(n->text);
- else
- item->version = nullptr;
- if ((n = TlenXmlGetChild(queryNode, "os")) != nullptr && n->text)
- item->system = TlenTextDecode(n->text);
- else
- item->system = nullptr;
- MCONTACT hContact = TlenHContactFromJID(proto, item->jid);
- if (hContact != NULL) {
- if (item->software != nullptr) {
- db_set_s(hContact, proto->m_szModuleName, "MirVer", item->software);
- }
- else {
- db_unset(hContact, proto->m_szModuleName, "MirVer");
- }
- }
- }
- }
- }
-}
-
-void TlenIqResultInfo(TlenProtocol *proto, XmlNode *iqNode)
-{
- XmlNode *queryNode = TlenXmlGetChild(iqNode, "query");
- if (queryNode != nullptr) {
- char *from = TlenXmlGetAttrValue(queryNode, "from");
- if (from != nullptr) {
- TLEN_LIST_ITEM *item = TlenListGetItemPtr(proto, LIST_ROSTER, from);
- if (item != nullptr) {
- XmlNode *version = TlenXmlGetChild(queryNode, "version");
- if (version != nullptr) {
- item->protocolVersion = TlenTextDecode(version->text);
- MCONTACT hContact = TlenHContactFromJID(proto, item->jid);
- if (hContact != NULL) {
- if (item->software == nullptr) {
- char str[128];
- mir_snprintf(str, "Tlen Protocol %s", item->protocolVersion);
- db_set_s(hContact, proto->m_szModuleName, "MirVer", str);
- }
- }
- }
- }
- }
- }
-}
-
diff --git a/protocols/Tlen/src/tlen_list.cpp b/protocols/Tlen/src/tlen_list.cpp
deleted file mode 100644
index f7a141cea6..0000000000
--- a/protocols/Tlen/src/tlen_list.cpp
+++ /dev/null
@@ -1,292 +0,0 @@
-/*
-
-Jabber Protocol Plugin for Miranda IM
-Tlen Protocol Plugin for Miranda NG
-Copyright (C) 2002-2004 Santithorn Bunchua
-Copyright (C) 2004-2007 Piotr Piastucki
-
-This program is free software; you can redistribute it and/or
-modify it under the terms of the GNU General Public License
-as published by the Free Software Foundation; either version 2
-of the License, or (at your option) any later version.
-
-This program 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 General Public License for more details.
-
-You should have received a copy of the GNU General Public License
-along with this program; if not, write to the Free Software
-Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
-
-*/
-
-#include "stdafx.h"
-#include "tlen_list.h"
-
-
-static void TlenListFreeItemInternal(TLEN_LIST_ITEM *item);
-
-void TlenListInit(TlenProtocol *proto)
-{
- proto->lists = nullptr;
- proto->listsCount = 0;
-}
-
-void TlenListUninit(TlenProtocol *proto)
-{
- TlenListWipe(proto);
-}
-
-void TlenListWipe(TlenProtocol *proto)
-{
- int i;
-
- mir_cslock lck(proto->csLists);
- for (i=0; i<proto->listsCount; i++)
- TlenListFreeItemInternal(&(proto->lists[i]));
- if (proto->lists != nullptr) {
- mir_free(proto->lists);
- proto->lists = nullptr;
- }
- proto->listsCount=0;
-}
-
-void TlenListWipeSpecial(TlenProtocol *proto)
-{
- int i;
- mir_cslock lck(proto->csLists);
- for (i=0; i<proto->listsCount; i++) {
- if (proto->lists[i].list != LIST_FILE && proto->lists[i].list != LIST_VOICE) {
- TlenListFreeItemInternal(&(proto->lists[i]));
- proto->listsCount--;
- memmove(proto->lists+i, proto->lists+i+1, sizeof(TLEN_LIST_ITEM)*(proto->listsCount-i));
- i--;
- }
- }
- proto->lists = (TLEN_LIST_ITEM *) mir_realloc(proto->lists, sizeof(TLEN_LIST_ITEM)*proto->listsCount);
-}
-
-static void TlenListFreeItemInternal(TLEN_LIST_ITEM *item)
-{
- if (item == nullptr)
- return;
-
- if (item->jid) mir_free(item->jid);
- if (item->nick) mir_free(item->nick);
- if (item->statusMessage) mir_free(item->statusMessage);
- if (item->group) mir_free(item->group);
- if (item->messageEventIdStr) mir_free(item->messageEventIdStr);
-// if (item->type) mir_free(item->type);
- //if (item->ft) TlenFileFreeFt(item->ft); // No need to free (it is always free when exit from TlenFileServerThread())
- if (item->roomName) mir_free(item->roomName);
- if (item->version) mir_free(item->version);
- if (item->software) mir_free(item->software);
- if (item->system) mir_free(item->system);
- if (item->avatarHash) mir_free(item->avatarHash);
-
- if (item->protocolVersion) mir_free(item->protocolVersion);
- if (item->id2) mir_free(item->id2);
-}
-
-static char * GetItemId(TLEN_LIST list, const char *jid)
-{
- char *s, *p, *q;
- s = mir_strdup(jid);
- if (list != LIST_PICTURE) {
- _strlwr(s);
- // strip resouce name if any
- if ((p=strchr(s, '@')) != nullptr) {
- if ((q=strchr(p, '/')) != nullptr)
- *q = '\0';
- }
- }
- return s;
-}
-
-
-int TlenListExist(TlenProtocol *proto, TLEN_LIST list, const char *jid)
-{
- int i;
- size_t len;
- char *s, *p;
- s = GetItemId(list, jid);
- len = mir_strlen(s);
-
- mir_cslock lck(proto->csLists);
- for (i=0; i<proto->listsCount; i++)
- if (proto->lists[i].list == list) {
- p = proto->lists[i].jid;
- if (p && mir_strlen(p) >= len && (p[(int)len] == '\0' || p[(int)len] == '/') && !strncmp(p, s, len)) {
- mir_free(s);
- return i+1;
- }
- }
- mir_free(s);
- return 0;
-}
-
-TLEN_LIST_ITEM *TlenListAdd(TlenProtocol *proto, TLEN_LIST list, const char *jid)
-{
- char *s;
- TLEN_LIST_ITEM *item;
-
- mir_cslock lck(proto->csLists);
- if ((item=TlenListGetItemPtr(proto, list, jid)) != nullptr) {
- return item;
- }
-
- s = GetItemId(list, jid);
- proto->lists = (TLEN_LIST_ITEM *) mir_realloc(proto->lists, sizeof(TLEN_LIST_ITEM)*(proto->listsCount+1));
- item = &(proto->lists[proto->listsCount]);
- memset(item, 0, sizeof(TLEN_LIST_ITEM));
- item->list = list;
- item->jid = s;
- item->nick = nullptr;
- item->status = ID_STATUS_OFFLINE;
- item->statusMessage = nullptr;
- item->group = nullptr;
- item->messageEventIdStr = nullptr;
- item->wantComposingEvent = FALSE;
- item->isTyping = FALSE;
-// item->type = NULL;
- item->ft = nullptr;
- item->roomName = nullptr;
- item->version = nullptr;
- item->software = nullptr;
- item->system = nullptr;
- item->avatarHash = nullptr;
- item->avatarFormat = PA_FORMAT_UNKNOWN;
- item->newAvatarDownloading = FALSE;
- item->versionRequested = FALSE;
- item->infoRequested = FALSE;
- proto->listsCount++;
-
- return item;
-}
-
-void TlenListRemove(TlenProtocol *proto, TLEN_LIST list, const char *jid)
-{
- int i;
-
- mir_cslock lck(proto->csLists);
- i = TlenListExist(proto, list, jid);
- if (!i) {
- return;
- }
- i--;
- TlenListFreeItemInternal(&(proto->lists[i]));
- proto->listsCount--;
- memmove(proto->lists+i, proto->lists+i+1, sizeof(TLEN_LIST_ITEM)*(proto->listsCount-i));
- proto->lists = (TLEN_LIST_ITEM *) mir_realloc(proto->lists, sizeof(TLEN_LIST_ITEM)*proto->listsCount);
-}
-
-void TlenListRemoveList(TlenProtocol *proto, TLEN_LIST list)
-{
- int i;
-
- i = 0;
- while ((i=TlenListFindNext(proto, list, i)) >= 0) {
- TlenListRemoveByIndex(proto, i);
- }
-}
-
-void TlenListRemoveByIndex(TlenProtocol *proto, int index)
-{
- mir_cslock lck(proto->csLists);
- if (index >= 0 && index<proto->listsCount) {
- TlenListFreeItemInternal(&(proto->lists[index]));
- proto->listsCount--;
- memmove(proto->lists+index, proto->lists+index+1, sizeof(TLEN_LIST_ITEM)*(proto->listsCount-index));
- proto->lists = (TLEN_LIST_ITEM *) mir_realloc(proto->lists, sizeof(TLEN_LIST_ITEM)*proto->listsCount);
- }
-}
-
-void TlenListAddResource(TlenProtocol *proto, TLEN_LIST list, const char *jid, int, const char *statusMessage)
-{
- int i;
-
- mir_cslock lck(proto->csLists);
- i = TlenListExist(proto, list, jid);
- if (!i) {
- return;
- }
- i--;
-
- if (proto->lists[i].statusMessage != nullptr)
- mir_free(proto->lists[i].statusMessage);
- if (statusMessage)
- proto->lists[i].statusMessage = mir_strdup(statusMessage);
- else
- proto->lists[i].statusMessage = nullptr;
-}
-
-void TlenListRemoveResource(TlenProtocol *proto, TLEN_LIST list, const char *jid)
-{
- int i;
- mir_cslock lck(proto->csLists);
- i = TlenListExist(proto, list, jid);
- if (!i) {
- return;
- }
- i--;
-}
-
-int TlenListFindNext(TlenProtocol *proto, TLEN_LIST list, int fromOffset)
-{
- int i;
-
- mir_cslock lck(proto->csLists);
- i = (fromOffset >= 0) ? fromOffset : 0;
- for (; i<proto->listsCount; i++)
- if (proto->lists[i].list == list) {
- return i;
- }
- return -1;
-}
-
-TLEN_LIST_ITEM *TlenListGetItemPtr(TlenProtocol *proto, TLEN_LIST list, const char *jid)
-{
- int i;
-
- mir_cslock lck(proto->csLists);
- i = TlenListExist(proto, list, jid);
- if (!i) {
- return nullptr;
- }
- i--;
- return &(proto->lists[i]);
-}
-
-TLEN_LIST_ITEM *TlenListFindItemPtrById2(TlenProtocol *proto, TLEN_LIST list, const char *id)
-{
-
- int i;
- size_t len;
- char *p;
-
- len = mir_strlen(id);
-
- mir_cslock lck(proto->csLists);
- for (i=0; i<proto->listsCount; i++) {
- if (proto->lists[i].list == list) {
- p = proto->lists[i].id2;
- if (p != nullptr) {
- if (!strncmp(p, id, len)) {
- return &(proto->lists[i]);
- }
- }
- }
- }
- return nullptr;
-}
-
-TLEN_LIST_ITEM *TlenListGetItemPtrFromIndex(TlenProtocol *proto, int index)
-{
- mir_cslock lck(proto->csLists);
- if (index >= 0 && index<proto->listsCount) {
- return &(proto->lists[index]);
- }
- return nullptr;
-}
-
diff --git a/protocols/Tlen/src/tlen_list.h b/protocols/Tlen/src/tlen_list.h
deleted file mode 100644
index 3130f41c14..0000000000
--- a/protocols/Tlen/src/tlen_list.h
+++ /dev/null
@@ -1,90 +0,0 @@
-/*
-
-Jabber Protocol Plugin for Miranda IM
-Tlen Protocol Plugin for Miranda NG
-Copyright (C) 2002-2004 Santithorn Bunchua
-Copyright (C) 2004-2007 Piotr Piastucki
-
-This program is free software; you can redistribute it and/or
-modify it under the terms of the GNU General Public License
-as published by the Free Software Foundation; either version 2
-of the License, or (at your option) any later version.
-
-This program 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 General Public License for more details.
-
-You should have received a copy of the GNU General Public License
-along with this program; if not, write to the Free Software
-Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
-
-*/
-
-#include "stdafx.h"
-
-#ifndef _TLEN_LIST_H_
-#define _TLEN_LIST_H_
-
-typedef struct TLEN_LIST_ITEM_STRUCT {
- TLEN_LIST list;
- char *jid;
- char *id2;
-
- // LIST_ROSTER
- // jid = jid of the contact
- char *nick;
- int status; // Main status, currently useful for transport where no resource information is kept.
- // On normal contact, this is the same status as shown on contact list.
- TLEN_SUBSCRIPTION subscription;
- char *statusMessage; // Status message when the update is to JID with no resource specified (e.g. transport user)
- char *software;
- char *version;
- char *system;
- char *group;
- char *protocolVersion;
- int avatarFormat;
- char *avatarHash;
- BOOL newAvatarDownloading;
- BOOL versionRequested;
- BOOL infoRequested;
- int idMsgAckPending;
- char *messageEventIdStr;
- BOOL wantComposingEvent;
- BOOL isTyping;
-
- // LIST_ROOM
- // jid = room JID
- // char *name; // room name
- //char *type; // room type
-
- // LIST_CHATROOM
- // jid = room JID
- // char *nick; // my nick in this chat room (SPECIAL: in UTF8)
- // TLEN_RESOURCE_STATUS *resource; // participant nicks in this room
- char *roomName;
-
- // LIST_FILE
- struct TLEN_FILE_TRANSFER_STRUCT *ft;
-} TLEN_LIST_ITEM;
-
-
-void TlenListInit(TlenProtocol *proto);
-void TlenListUninit(TlenProtocol *proto);
-void TlenListWipe(TlenProtocol *proto);
-void TlenListWipeSpecial(TlenProtocol *proto);
-int TlenListExist(TlenProtocol *proto, TLEN_LIST list, const char *jid);
-TLEN_LIST_ITEM *TlenListAdd(TlenProtocol *proto, TLEN_LIST list, const char *jid);
-void TlenListRemove(TlenProtocol *proto, TLEN_LIST list, const char *jid);
-void TlenListRemoveList(TlenProtocol *proto, TLEN_LIST list);
-void TlenListRemoveByIndex(TlenProtocol *proto, int index);
-int TlenListFindNext(TlenProtocol *proto, TLEN_LIST list, int fromOffset);
-TLEN_LIST_ITEM *TlenListGetItemPtr(TlenProtocol *proto, TLEN_LIST list, const char *jid);
-TLEN_LIST_ITEM *TlenListGetItemPtrFromIndex(TlenProtocol *proto, int index);
-TLEN_LIST_ITEM *TlenListFindItemPtrById2(TlenProtocol *proto, TLEN_LIST list, const char *id);
-
-void TlenListAddResource(TlenProtocol *proto, TLEN_LIST list, const char *jid, int status, const char *statusMessage);
-void TlenListRemoveResource(TlenProtocol *proto, TLEN_LIST list, const char *jid);
-
-#endif
-
diff --git a/protocols/Tlen/src/tlen_misc.cpp b/protocols/Tlen/src/tlen_misc.cpp
deleted file mode 100644
index 4cb071da5a..0000000000
--- a/protocols/Tlen/src/tlen_misc.cpp
+++ /dev/null
@@ -1,110 +0,0 @@
-/*
-
-Jabber Protocol Plugin for Miranda IM
-Tlen Protocol Plugin for Miranda NG
-Copyright (C) 2002-2004 Santithorn Bunchua
-Copyright (C) 2004-2007 Piotr Piastucki
-
-This program is free software; you can redistribute it and/or
-modify it under the terms of the GNU General Public License
-as published by the Free Software Foundation; either version 2
-of the License, or (at your option) any later version.
-
-This program 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 General Public License for more details.
-
-You should have received a copy of the GNU General Public License
-along with this program; if not, write to the Free Software
-Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
-
-*/
-
-#include "stdafx.h"
-#include "tlen_list.h"
-
-void TlenDBAddEvent(TlenProtocol *proto, MCONTACT hContact, int eventType, DWORD flags, PBYTE pBlob, DWORD cbBlob)
-{
- DBEVENTINFO dbei = {};
- dbei.szModule = proto->m_szModuleName;
- dbei.timestamp = (DWORD) time(nullptr);
- dbei.flags = flags;
- dbei.eventType = eventType;
- dbei.cbBlob = cbBlob;
- dbei.pBlob = pBlob;
- db_event_add(hContact, &dbei);
-}
-
-void TlenDBAddAuthRequest(TlenProtocol *proto, char *jid, char *nick)
-{
- MCONTACT hContact = TlenHContactFromJID(proto, jid);
- if (hContact == NULL) {
- hContact = db_add_contact();
- Proto_AddToContact(hContact, proto->m_szModuleName);
- // strip resource if present
- char *s = TlenLoginFromJID(jid);
- _strlwr(s);
- db_set_s(hContact, proto->m_szModuleName, "jid", s);
- mir_free(s);
- }
- else db_unset(hContact, proto->m_szModuleName, "Hidden");
-
- db_set_s(hContact, proto->m_szModuleName, "Nick", nick);
-
- proto->debugLogA("auth request: %s, %s", jid, nick);
- DB_AUTH_BLOB blob(hContact, nick, nullptr, nullptr, jid, nullptr);
- TlenDBAddEvent(proto, NULL, EVENTTYPE_AUTHREQUEST, 0, blob, blob.size());
-}
-
-char *TlenJIDFromHContact(TlenProtocol *proto, MCONTACT hContact)
-{
- char *p = nullptr;
- DBVARIANT dbv;
- if (!db_get(hContact, proto->m_szModuleName, "jid", &dbv)) {
- p = mir_strdup(dbv.pszVal);
- db_free(&dbv);
- }
- return p;
-}
-
-MCONTACT TlenHContactFromJID(TlenProtocol *proto, const char *jid)
-{
- DBVARIANT dbv;
- char *p;
- if (jid == nullptr)
- return NULL;
-
- for (MCONTACT hContact = db_find_first(proto->m_szModuleName); hContact; hContact = db_find_next(hContact, proto->m_szModuleName)) {
- if ( db_get_s(hContact, proto->m_szModuleName, "jid", &dbv))
- continue;
-
- if ((p=dbv.pszVal) != nullptr) {
- if (!mir_strcmpi(p, jid)) { // exact match (node@domain/resource)
- db_free(&dbv);
- return hContact;
- }
- }
- db_free(&dbv);
- }
-
- return NULL;
-}
-
-MCONTACT TlenDBCreateContact(TlenProtocol *proto, char *jid, char *nick, BOOL temporary)
-{
- MCONTACT hContact;
- if (jid == nullptr || jid[0] == '\0')
- return NULL;
-
- if ((hContact=TlenHContactFromJID(proto, jid)) == NULL) {
- hContact = db_add_contact();
- Proto_AddToContact(hContact, proto->m_szModuleName);
- db_set_s(hContact, proto->m_szModuleName, "jid", jid);
- if (nick != nullptr && nick[0] != '\0')
- db_set_s(hContact, proto->m_szModuleName, "Nick", nick);
- if (temporary)
- db_set_b(hContact, "CList", "NotOnList", 1);
- }
- return hContact;
-}
diff --git a/protocols/Tlen/src/tlen_muc.cpp b/protocols/Tlen/src/tlen_muc.cpp
deleted file mode 100644
index 475ffa504d..0000000000
--- a/protocols/Tlen/src/tlen_muc.cpp
+++ /dev/null
@@ -1,237 +0,0 @@
-/*
-
-Tlen Protocol Plugin for Miranda NG
-Copyright (C) 2004-2007 Piotr Piastucki
-
-This program is free software; you can redistribute it and/or
-modify it under the terms of the GNU General Public License
-as published by the Free Software Foundation; either version 2
-of the License, or (at your option) any later version.
-
-This program 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 General Public License for more details.
-
-You should have received a copy of the GNU General Public License
-along with this program; if not, write to the Free Software
-Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
-
-*/
-
-#include "stdafx.h"
-#include "tlen_list.h"
-#include "tlen_iq.h"
-#include "resource.h"
-#include "tlen_muc.h"
-
-static int TlenMUCHandleEvent(void *ptr, WPARAM wParam, LPARAM lParam);
-static int TlenMUCQueryContacts(TlenProtocol *proto, const char *roomId);
-static int TlenMUCSendInvitation(TlenProtocol *proto, const char *roomID, const char *user);
-static int TlenMUCSendPresence(TlenProtocol *proto, const char *roomID, const char *nick, int desiredStatus);
-static int TlenMUCSendQuery(TlenProtocol *proto, int type, const char *parent, int page);
-
-static int isSelf(TlenProtocol *proto, const char *roomID, const char *nick)
-{
- TLEN_LIST_ITEM *item;
- int result;
- result=0;
- item = TlenListGetItemPtr(proto, LIST_CHATROOM, roomID);
- if (item != nullptr) {
- if (item->nick == nullptr) {
- if (!mir_strcmp(nick, proto->threadData->username)) result = 1;
- } else if (nick[0] == '~') {
- if (!mir_strcmp(nick+1, item->nick)) {
- result = 1;
- }
- }
- }
- return result;
-}
-
-static int stringToHex(const char *str)
-{
- int i, val;
- val = 0;
- for (i=0;i<2;i++) {
- val <<= 4;
- if (str[i] >= 'A' && str[i] <= 'F') {
- val += 10 + str[i]-'A';
- } else if (str[i] >= '0' && str[i] <= '9') {
- val += str[i]-'0';
- }
- }
- return val;
-
-}
-static char *getDisplayName(TlenProtocol *proto, const char *id)
-{
- char jid[256];
- MCONTACT hContact;
- DBVARIANT dbv;
- if (!db_get(NULL, proto->m_szModuleName, "LoginServer", &dbv)) {
- mir_snprintf(jid, "%s@%s", id, dbv.pszVal);
- db_free(&dbv);
- if (((hContact = TlenHContactFromJID(proto, jid)) != NULL) || !mir_strcmp(id, proto->threadData->username)) {
- ptrW szName(Contact_GetInfo(CNF_DISPLAY, hContact, proto->m_szModuleName));
- if (szName)
- return mir_u2a(szName);
- }
- }
- return mir_strdup(id);
-}
-
-int TlenMUCRecvInvitation(TlenProtocol *proto, const char *roomId, const char*, const char *from, const char*)
-{
- int ignore, ask, groupChatPolicy;
- if (roomId == nullptr) return 1;
- groupChatPolicy = db_get_w(NULL, proto->m_szModuleName, "GroupChatPolicy", 0);
- ask = TRUE;
- ignore = FALSE;
- if (groupChatPolicy == TLEN_MUC_ASK) {
- ignore = FALSE;
- ask = TRUE;
- } else if (groupChatPolicy == TLEN_MUC_IGNORE_ALL) {
- ignore = TRUE;
- } else if (groupChatPolicy == TLEN_MUC_IGNORE_NIR) {
- char jid[256];
- DBVARIANT dbv;
- if (!db_get(NULL, proto->m_szModuleName, "LoginServer", &dbv)) {
- mir_snprintf(jid, "%s@%s", from, dbv.pszVal);
- db_free(&dbv);
- } else {
- mir_strcpy(jid, from);
- }
- ignore = !IsAuthorized(proto, jid);
- ask = TRUE;
- } else if (groupChatPolicy == TLEN_MUC_ACCEPT_IR) {
- char jid[256];
- TLEN_LIST_ITEM *item;
- DBVARIANT dbv;
- if (!db_get(NULL, proto->m_szModuleName, "LoginServer", &dbv)) {
- mir_snprintf(jid, "%s@%s", from, dbv.pszVal);
- db_free(&dbv);
- } else {
- mir_strcpy(jid, from);
- }
- item = TlenListGetItemPtr(proto, LIST_ROSTER, jid);
- ask = !IsAuthorized(proto, jid);
- ignore = FALSE;
- } else if (groupChatPolicy == TLEN_MUC_ACCEPT_ALL) {
- ask = FALSE;
- ignore = FALSE;
- }
- return 0;
-}
-
-static int TlenMUCSendInvitation(TlenProtocol *proto, const char *roomID, const char *user)
-{
- if (!proto->isOnline) {
- return 1;
- }
- TlenSend(proto, "<m to='%s'><x><inv to='%s'><r></r></inv></x></m>", roomID, user);
- return 0;
-}
-
-static int TlenMUCSendPresence(TlenProtocol *proto, const char *roomID, const char *nick, int desiredStatus)
-{
- char str[512];
- char *jid;
- TLEN_LIST_ITEM *item;
- if (!proto->isOnline) {
- return 1;
- }
- if (nick != nullptr) {
- mir_snprintf(str, "%s/%s", roomID, nick);
- } else {
- strncpy_s(str, roomID, _TRUNCATE);
- }
- if ((jid = TlenTextEncode(str)) != nullptr) {
- switch (desiredStatus) {
- case ID_STATUS_ONLINE:
- TlenSend(proto, "<p to='%s'/>", jid);
- item = TlenListGetItemPtr(proto, LIST_CHATROOM, roomID);
- if (item != nullptr) {
- if (item->nick != nullptr) mir_free(item->nick);
- item->nick = nullptr;
- if (nick != nullptr) {
- item->nick = mir_strdup(nick);
- }
- }
- break;
- default:
- item = TlenListGetItemPtr(proto, LIST_CHATROOM, roomID);
- if (item != nullptr) {
- TlenSend(proto, "<p to='%s'><s>unavailable</s></p>", jid);
- TlenListRemove(proto, LIST_CHATROOM, roomID);
- }
- break;
- }
- mir_free(jid);
- }
- return 0;
-}
-
-static int TlenMUCSendQuery(TlenProtocol *proto, int type, const char *parent, int page)
-{
- if (!proto->isOnline) {
- return 1;
- }
- if (type == 3) { // find chat room by name
- char serialId[32];
- TLEN_LIST_ITEM *item;
- mir_snprintf(serialId, TLEN_IQID"%d", TlenSerialNext(proto));
- item = TlenListAdd(proto, LIST_SEARCH, serialId);
- item->roomName = mir_strdup(parent);
- TlenSend(proto, "<iq to='c' type='3' n='%s' id='%s'/>", parent, serialId);
- } else {
- if (parent == nullptr) {
- TlenSend(proto, "<iq to='c' type='%d'/>", type);
- } else { // 1 - groups, 2 - chat rooms, 7 - user nicks, 8 - user rooms
- if (type == 1 || (type == 2 && page == 0) || type == 7 || type == 8) {
- TlenSend(proto, "<iq to='c' type='%d' p='%s'/>", type, parent);
- } else if (type == 2) {
- TlenSend(proto, "<iq to='c' type='%d' p='%s' n='%d'/>", type, parent, page);
- } else if (type == 6) {
- if (page) {
- TlenSend(proto, "<iq to='c' type='%d' n='%s' k='u'/>", type, parent);
- } else {
- TlenSend(proto, "<iq to='c' type='%d' n='%s'/>", type, parent);
- }
- } else if (type == 4) { // list of users, admins etc.
- TlenSend(proto, "<iq to='%s' type='%d' k='%d'/>", parent, type, page);
- }
- }
- }
- return 0;
-}
-
-INT_PTR TlenProtocol::MUCMenuHandleMUC(WPARAM, LPARAM)
-{
- if (!isOnline)
- return 1;
-
- TlenSend(this, "<p to='c' tp='c' id='" TLEN_IQID "%d'/>", TlenSerialNext(this));
- return 0;
-}
-
-INT_PTR TlenProtocol::MUCContactMenuHandleMUC(WPARAM wParam, LPARAM)
-{
- MCONTACT hContact;
- DBVARIANT dbv;
- TLEN_LIST_ITEM *item;
- if (!isOnline)
- return 1;
-
- if ((hContact=wParam) != NULL && isOnline) {
- if (!db_get(hContact, m_szModuleName, "jid", &dbv)) {
- char serialId[32];
- mir_snprintf(serialId, TLEN_IQID"%d", TlenSerialNext(this));
- item = TlenListAdd(this, LIST_INVITATIONS, serialId);
- item->nick = mir_strdup(dbv.pszVal);
- TlenSend(this, "<p to='c' tp='c' id='%s'/>", serialId);
- db_free(&dbv);
- }
- }
- return 0;
-}
diff --git a/protocols/Tlen/src/tlen_muc.h b/protocols/Tlen/src/tlen_muc.h
deleted file mode 100644
index 419bf9ea2b..0000000000
--- a/protocols/Tlen/src/tlen_muc.h
+++ /dev/null
@@ -1,35 +0,0 @@
-/*
-
-Tlen Protocol Plugin for Miranda NG
-Copyright (C) 2004-2007 Piotr Piastucki
-
-This program is free software; you can redistribute it and/or
-modify it under the terms of the GNU General Public License
-as published by the Free Software Foundation; either version 2
-of the License, or (at your option) any later version.
-
-This program 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 General Public License for more details.
-
-You should have received a copy of the GNU General Public License
-along with this program; if not, write to the Free Software
-Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
-
-*/
-
-#ifndef _TLEN_MUC_H_
-#define _TLEN_MUC_H_
-
-#include <windows.h>
-
-#define USER_FLAGS_OWNER 0x01
-#define USER_FLAGS_ADMIN 0x02
-#define USER_FLAGS_REGISTERED 0x04
-#define USER_FLAGS_GLOBALOWNER 0x08
-#define USER_FLAGS_KICKED 0x80
-
-extern int TlenMUCRecvInvitation(TlenProtocol *proto, const char *roomJid, const char *roomName, const char *from, const char *reason);
-
-#endif
diff --git a/protocols/Tlen/src/tlen_opt.cpp b/protocols/Tlen/src/tlen_opt.cpp
deleted file mode 100644
index 2c7486e555..0000000000
--- a/protocols/Tlen/src/tlen_opt.cpp
+++ /dev/null
@@ -1,697 +0,0 @@
-/*
-
-Jabber Protocol Plugin for Miranda IM
-Tlen Protocol Plugin for Miranda NG
-Copyright (C) 2002-2004 Santithorn Bunchua
-Copyright (C) 2004-2007 Piotr Piastucki
-
-This program is free software; you can redistribute it and/or
-modify it under the terms of the GNU General Public License
-as published by the Free Software Foundation; either version 2
-of the License, or (at your option) any later version.
-
-This program 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 General Public License for more details.
-
-You should have received a copy of the GNU General Public License
-along with this program; if not, write to the Free Software
-Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
-
-*/
-
-#include "stdafx.h"
-#include "tlen_list.h"
-#include "tlen_voice.h"
-#include <commctrl.h>
-#include "resource.h"
-
-static INT_PTR CALLBACK TlenBasicOptDlgProc(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam);
-static INT_PTR CALLBACK TlenVoiceOptDlgProc(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam);
-static INT_PTR CALLBACK TlenAdvOptDlgProc(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam);
-static INT_PTR CALLBACK TlenPopupsDlgProc(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam);
-
-typedef struct TabDefStruct {
- DLGPROC dlgProc;
- DWORD dlgId;
- wchar_t *tabName;
-} TabDef;
-
-static TabDef tabPages[] = {
- {TlenBasicOptDlgProc, IDD_OPTIONS_BASIC, LPGENW("General")},
- {TlenVoiceOptDlgProc, IDD_OPTIONS_VOICE, LPGENW("Voice Chats")},
- {TlenAdvOptDlgProc, IDD_OPTIONS_ADVANCED, LPGENW("Advanced")},
- {TlenPopupsDlgProc, IDD_OPTIONS_POPUPS, LPGENW("Notifications")}
- };
-
-void TlenLoadOptions(TlenProtocol *proto)
-{
- proto->tlenOptions.savePassword = db_get_b(NULL, proto->m_szModuleName, "SavePassword", TRUE);
- proto->tlenOptions.useEncryption = db_get_b(NULL, proto->m_szModuleName, "UseEncryption", TRUE);
- proto->tlenOptions.reconnect = db_get_b(NULL, proto->m_szModuleName, "Reconnect", TRUE);
- proto->tlenOptions.alertPolicy = db_get_w(NULL, proto->m_szModuleName, "AlertPolicy", TLEN_ALERTS_IGNORE_NIR);
- proto->tlenOptions.rosterSync = db_get_b(NULL, proto->m_szModuleName, "RosterSync", FALSE);
- proto->tlenOptions.offlineAsInvisible = db_get_b(NULL, proto->m_szModuleName, "OfflineAsInvisible", FALSE);
- proto->tlenOptions.leaveOfflineMessage = db_get_b(NULL, proto->m_szModuleName, "LeaveOfflineMessage", TRUE);
- proto->tlenOptions.offlineMessageOption = db_get_w(NULL, proto->m_szModuleName, "OfflineMessageOption", 0);
- proto->tlenOptions.ignoreAdvertisements = db_get_b(NULL, proto->m_szModuleName, "IgnoreAdvertisements", TRUE);
- proto->tlenOptions.groupChatPolicy = db_get_w(NULL, proto->m_szModuleName, "GroupChatPolicy", TLEN_MUC_ASK);
- proto->tlenOptions.voiceChatPolicy = db_get_w(NULL, proto->m_szModuleName, "VoiceChatPolicy", TLEN_MUC_ASK);
- proto->tlenOptions.imagePolicy = db_get_w(NULL, proto->m_szModuleName, "ImagePolicy",TLEN_IMAGES_IGNORE_NIR);
- proto->tlenOptions.enableAvatars = db_get_b(NULL, proto->m_szModuleName, "EnableAvatars", TRUE);
- proto->tlenOptions.enableVersion = db_get_b(NULL, proto->m_szModuleName, "EnableVersion", TRUE);
- proto->tlenOptions.useNudge = db_get_b(NULL, proto->m_szModuleName, "UseNudge", FALSE);
- proto->tlenOptions.logAlerts = db_get_b(NULL, proto->m_szModuleName, "LogAlerts", TRUE);
- proto->tlenOptions.sendKeepAlive = db_get_b(NULL, proto->m_szModuleName, "KeepAlive", TRUE);
- proto->tlenOptions.useNewP2P = TRUE;
-}
-
-static int changed = 0;
-
-static void ApplyChanges(TlenProtocol *proto, int i) {
- changed &= ~i;
- if (changed == 0) {
- TlenLoadOptions(proto);
- }
-}
-
-static void MarkChanges(int i, HWND hWnd) {
- SendMessage(GetParent(hWnd), PSM_CHANGED, 0, 0);
- changed |= i;
-}
-
-
-int TlenProtocol::OptionsInit(WPARAM wParam, LPARAM)
-{
- OPTIONSDIALOGPAGE odp = { 0 };
- odp.hInstance = hInst;
- odp.szGroup.w = LPGENW("Network");
- odp.szTitle.w = m_tszUserName;
- odp.flags = ODPF_BOLDGROUPS | ODPF_UNICODE;
- odp.dwInitParam = (LPARAM)this;
- for (int i = 0; i < _countof(tabPages); i++) {
- odp.pszTemplate = MAKEINTRESOURCEA(tabPages[i].dlgId);
- odp.pfnDlgProc = tabPages[i].dlgProc;
- odp.szTab.w = tabPages[i].tabName;
- Options_AddPage(wParam, &odp);
- }
- return 0;
-}
-
-static LRESULT CALLBACK TlenValidateUsernameWndProc(HWND hwndEdit, UINT msg, WPARAM wParam, LPARAM lParam)
-{
- switch (msg) {
- case WM_CHAR:
- if (strchr("\"&'/:<>@", wParam&0xff) != nullptr)
- return 0;
- break;
- }
- return mir_callNextSubclass(hwndEdit, TlenValidateUsernameWndProc, msg, wParam, lParam);
-}
-
-INT_PTR CALLBACK TlenAccMgrUIDlgProc(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
-{
- char text[256];
-
- TlenProtocol *proto = (TlenProtocol *)GetWindowLongPtr(hwndDlg, GWLP_USERDATA);
- switch (msg) {
- case WM_INITDIALOG:
- {
- DBVARIANT dbv;
- proto = (TlenProtocol *)lParam;
- SetWindowLongPtr(hwndDlg, GWLP_USERDATA, (LONG_PTR)proto);
- TranslateDialogDefault(hwndDlg);
- if (!db_get_ws(NULL, proto->m_szModuleName, "LoginName", &dbv)) {
- SetDlgItemText(hwndDlg, IDC_EDIT_USERNAME, dbv.ptszVal);
- db_free(&dbv);
- }
- if (!db_get(NULL, proto->m_szModuleName, "Password", &dbv)) {
- SetDlgItemTextA(hwndDlg, IDC_EDIT_PASSWORD, dbv.pszVal);
- db_free(&dbv);
- }
- CheckDlgButton(hwndDlg, IDC_SAVEPASSWORD, db_get_b(NULL, proto->m_szModuleName, "SavePassword", BST_CHECKED));
-
- mir_subclassWindow(GetDlgItem(hwndDlg, IDC_EDIT_USERNAME), TlenValidateUsernameWndProc);
- }
- return TRUE;
-
- case WM_COMMAND:
- switch (LOWORD(wParam)) {
- case IDC_EDIT_USERNAME:
- case IDC_EDIT_PASSWORD:
- if ((HWND)lParam == GetFocus() && HIWORD(wParam) == EN_CHANGE)
- SendMessage(GetParent(hwndDlg), PSM_CHANGED, 0, 0);
- break;
- case IDC_SAVEPASSWORD:
- SendMessage(GetParent(hwndDlg), PSM_CHANGED, 0, 0);
- break;
- case IDC_REGISTERACCOUNT:
- Utils_OpenUrl(TLEN_REGISTER);
- break;
- }
- break;
- case WM_NOTIFY:
- switch (((LPNMHDR) lParam)->code) {
- case PSN_APPLY:
- {
- bool reconnectRequired = false;
- DBVARIANT dbv;
-
- GetDlgItemTextA(hwndDlg, IDC_EDIT_USERNAME, text, _countof(text));
- if (db_get(NULL, proto->m_szModuleName, "LoginName", &dbv)) {
- reconnectRequired = true;
- }
- else {
- if(mir_strcmp(text, dbv.pszVal))
- reconnectRequired = true;
- db_free(&dbv);
- }
- db_set_s(NULL, proto->m_szModuleName, "LoginName", strlwr(text));
-
- if (IsDlgButtonChecked(hwndDlg, IDC_SAVEPASSWORD)) {
- GetDlgItemTextA(hwndDlg, IDC_EDIT_PASSWORD, text, _countof(text));
- if (db_get(NULL, proto->m_szModuleName, "Password", &dbv)) {
- reconnectRequired = true;
- }
- else {
- if(mir_strcmp(text, dbv.pszVal))
- reconnectRequired = true;
- db_free(&dbv);
- }
- db_set_s(NULL, proto->m_szModuleName, "Password", text);
- }
- else
- db_unset(NULL, proto->m_szModuleName, "Password");
-
- db_set_b(NULL, proto->m_szModuleName, "SavePassword", (BYTE) IsDlgButtonChecked(hwndDlg, IDC_SAVEPASSWORD));
- if (reconnectRequired && proto->isConnected)
- MessageBox(hwndDlg, TranslateT("These changes will take effect the next time you connect to the Tlen network."), TranslateT("Tlen Protocol Option"), MB_OK|MB_SETFOREGROUND);
- TlenLoadOptions(proto);
- return TRUE;
- }
- }
- break;
- }
- return FALSE;
-}
-
-static INT_PTR CALLBACK TlenBasicOptDlgProc(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
-{
- char text[256];
-
- TlenProtocol *proto = (TlenProtocol *)GetWindowLongPtr(hwndDlg, GWLP_USERDATA);
- switch (msg) {
- case WM_INITDIALOG:
- {
- DBVARIANT dbv;
- proto = (TlenProtocol *)lParam;
- SetWindowLongPtr(hwndDlg, GWLP_USERDATA, (LONG_PTR)proto);
- TranslateDialogDefault(hwndDlg);
- if (!db_get_ws(NULL, proto->m_szModuleName, "LoginName", &dbv)) {
- SetDlgItemText(hwndDlg, IDC_EDIT_USERNAME, dbv.ptszVal);
- db_free(&dbv);
- }
- if (!db_get(NULL, proto->m_szModuleName, "Password", &dbv)) {
- SetDlgItemTextA(hwndDlg, IDC_EDIT_PASSWORD, dbv.pszVal);
- db_free(&dbv);
- }
- CheckDlgButton(hwndDlg, IDC_SAVEPASSWORD, db_get_b(NULL, proto->m_szModuleName, "SavePassword", BST_CHECKED));
-
- CheckDlgButton(hwndDlg, IDC_RECONNECT, proto->tlenOptions.reconnect ? BST_CHECKED : BST_UNCHECKED);
- CheckDlgButton(hwndDlg, IDC_ROSTER_SYNC, proto->tlenOptions.rosterSync ? BST_CHECKED : BST_UNCHECKED);
- CheckDlgButton(hwndDlg, IDC_SHOW_OFFLINE, proto->tlenOptions.offlineAsInvisible ? BST_CHECKED : BST_UNCHECKED);
- CheckDlgButton(hwndDlg, IDC_OFFLINE_MESSAGE, proto->tlenOptions.leaveOfflineMessage ? BST_CHECKED : BST_UNCHECKED);
- CheckDlgButton(hwndDlg, IDC_IGNORE_ADVERTISEMENTS, proto->tlenOptions.ignoreAdvertisements ? BST_CHECKED : BST_UNCHECKED);
- CheckDlgButton(hwndDlg, IDC_AVATARS, proto->tlenOptions.enableAvatars ? BST_CHECKED : BST_UNCHECKED);
- CheckDlgButton(hwndDlg, IDC_VERSIONINFO, proto->tlenOptions.enableVersion ? BST_CHECKED : BST_UNCHECKED);
- CheckDlgButton(hwndDlg, IDC_NUDGE_SUPPORT, proto->tlenOptions.useNudge ? BST_CHECKED : BST_UNCHECKED);
- CheckDlgButton(hwndDlg, IDC_LOG_ALERTS, proto->tlenOptions.logAlerts ? BST_CHECKED : BST_UNCHECKED);
-
- SendDlgItemMessage(hwndDlg, IDC_ALERT_POLICY, CB_ADDSTRING, 0, (LPARAM)TranslateT("Accept all alerts"));
- SendDlgItemMessage(hwndDlg, IDC_ALERT_POLICY, CB_ADDSTRING, 0, (LPARAM)TranslateT("Ignore alerts from unauthorized contacts"));
- SendDlgItemMessage(hwndDlg, IDC_ALERT_POLICY, CB_ADDSTRING, 0, (LPARAM)TranslateT("Ignore all alerts"));
- SendDlgItemMessage(hwndDlg, IDC_ALERT_POLICY, CB_SETCURSEL, proto->tlenOptions.alertPolicy, 0);
-
- SendDlgItemMessage(hwndDlg, IDC_MUC_POLICY, CB_ADDSTRING, 0, (LPARAM)TranslateT("Always ask me"));
- SendDlgItemMessage(hwndDlg, IDC_MUC_POLICY, CB_ADDSTRING, 0, (LPARAM)TranslateT("Accept invitations from authorized contacts"));
- SendDlgItemMessage(hwndDlg, IDC_MUC_POLICY, CB_ADDSTRING, 0, (LPARAM)TranslateT("Accept all invitations"));
- SendDlgItemMessage(hwndDlg, IDC_MUC_POLICY, CB_ADDSTRING, 0, (LPARAM)TranslateT("Ignore invitations from unauthorized contacts"));
- SendDlgItemMessage(hwndDlg, IDC_MUC_POLICY, CB_ADDSTRING, 0, (LPARAM)TranslateT("Ignore all invitation"));
- SendDlgItemMessage(hwndDlg, IDC_MUC_POLICY, CB_SETCURSEL, proto->tlenOptions.groupChatPolicy, 0);
-
- SendDlgItemMessage(hwndDlg, IDC_IMAGE_POLICY, CB_ADDSTRING, 0, (LPARAM)TranslateT("Accept all images"));
- SendDlgItemMessage(hwndDlg, IDC_IMAGE_POLICY, CB_ADDSTRING, 0, (LPARAM)TranslateT("Ignore images from unauthorized contacts"));
- SendDlgItemMessage(hwndDlg, IDC_IMAGE_POLICY, CB_ADDSTRING, 0, (LPARAM)TranslateT("Ignore all images"));
- SendDlgItemMessage(hwndDlg, IDC_IMAGE_POLICY, CB_SETCURSEL, proto->tlenOptions.imagePolicy, 0);
-
- SendDlgItemMessage(hwndDlg, IDC_OFFLINE_MESSAGE_OPTION, CB_ADDSTRING, 0, (LPARAM)TranslateT("<Last message>"));
- SendDlgItemMessage(hwndDlg, IDC_OFFLINE_MESSAGE_OPTION, CB_ADDSTRING, 0, (LPARAM)pcli->pfnGetStatusModeDescription(ID_STATUS_ONLINE, 0));
- SendDlgItemMessage(hwndDlg, IDC_OFFLINE_MESSAGE_OPTION, CB_ADDSTRING, 0, (LPARAM)pcli->pfnGetStatusModeDescription(ID_STATUS_AWAY, 0));
- SendDlgItemMessage(hwndDlg, IDC_OFFLINE_MESSAGE_OPTION, CB_ADDSTRING, 0, (LPARAM)pcli->pfnGetStatusModeDescription(ID_STATUS_NA, 0));
- SendDlgItemMessage(hwndDlg, IDC_OFFLINE_MESSAGE_OPTION, CB_ADDSTRING, 0, (LPARAM)pcli->pfnGetStatusModeDescription(ID_STATUS_DND, 0));
- SendDlgItemMessage(hwndDlg, IDC_OFFLINE_MESSAGE_OPTION, CB_ADDSTRING, 0, (LPARAM)pcli->pfnGetStatusModeDescription(ID_STATUS_FREECHAT, 0));
- SendDlgItemMessage(hwndDlg, IDC_OFFLINE_MESSAGE_OPTION, CB_ADDSTRING, 0, (LPARAM)pcli->pfnGetStatusModeDescription(ID_STATUS_INVISIBLE, 0));
- SendDlgItemMessage(hwndDlg, IDC_OFFLINE_MESSAGE_OPTION, CB_SETCURSEL, proto->tlenOptions.offlineMessageOption, 0);
-
- mir_subclassWindow(GetDlgItem(hwndDlg, IDC_EDIT_USERNAME), TlenValidateUsernameWndProc);
- return TRUE;
- }
- case WM_COMMAND:
- switch (LOWORD(wParam)) {
- case IDC_EDIT_USERNAME:
- case IDC_EDIT_PASSWORD:
- if ((HWND)lParam == GetFocus() && HIWORD(wParam) == EN_CHANGE)
- SendMessage(GetParent(hwndDlg), PSM_CHANGED, 0, 0);
- break;
- // Fall through
- case IDC_SAVEPASSWORD:
- case IDC_RECONNECT:
- case IDC_ROSTER_SYNC:
- case IDC_IGNORE_ADVERTISEMENTS:
- case IDC_SHOW_OFFLINE:
- case IDC_OFFLINE_MESSAGE:
- MarkChanges(1, hwndDlg);
- break;
- case IDC_LOG_ALERTS:
- CheckDlgButton(hwndDlg, IDC_NUDGE_SUPPORT, BST_UNCHECKED);
- MarkChanges(1, hwndDlg);
- break;
- case IDC_NUDGE_SUPPORT:
- CheckDlgButton(hwndDlg, IDC_LOG_ALERTS, BST_UNCHECKED);
- MarkChanges(1, hwndDlg);
- break;
- case IDC_REGISTERACCOUNT:
- Utils_OpenUrl(TLEN_REGISTER);
- break;
- case IDC_OFFLINE_MESSAGE_OPTION:
- case IDC_ALERT_POLICY:
- case IDC_MUC_POLICY:
- if (HIWORD(wParam) == CBN_SELCHANGE)
- MarkChanges(1, hwndDlg);
- break;
- default:
- MarkChanges(1, hwndDlg);
- break;
- }
- break;
- case WM_NOTIFY:
- switch (((LPNMHDR) lParam)->code) {
- case PSN_APPLY:
- {
- bool reconnectRequired = false;
- DBVARIANT dbv;
-
- GetDlgItemTextA(hwndDlg, IDC_EDIT_USERNAME, text, _countof(text));
- if (db_get(NULL, proto->m_szModuleName, "LoginName", &dbv)) {
- reconnectRequired = true;
- }
- else {
- if(mir_strcmp(text, dbv.pszVal))
- reconnectRequired = true;
- db_free(&dbv);
- }
- db_set_s(NULL, proto->m_szModuleName, "LoginName", strlwr(text));
-
- if (IsDlgButtonChecked(hwndDlg, IDC_SAVEPASSWORD)) {
- GetDlgItemTextA(hwndDlg, IDC_EDIT_PASSWORD, text, _countof(text));
- if (db_get(NULL, proto->m_szModuleName, "Password", &dbv)) {
- reconnectRequired = true;
- }
- else {
- if(mir_strcmp(text, dbv.pszVal))
- reconnectRequired = true;
- db_free(&dbv);
- }
- db_set_s(NULL, proto->m_szModuleName, "Password", text);
- }
- else
- db_unset(NULL, proto->m_szModuleName, "Password");
-
- db_set_b(NULL, proto->m_szModuleName, "SavePassword", (BYTE) IsDlgButtonChecked(hwndDlg, IDC_SAVEPASSWORD));
- db_set_b(NULL, proto->m_szModuleName, "Reconnect", (BYTE) IsDlgButtonChecked(hwndDlg, IDC_RECONNECT));
- db_set_b(NULL, proto->m_szModuleName, "RosterSync", (BYTE) IsDlgButtonChecked(hwndDlg, IDC_ROSTER_SYNC));
- db_set_b(NULL, proto->m_szModuleName, "OfflineAsInvisible", (BYTE) IsDlgButtonChecked(hwndDlg, IDC_SHOW_OFFLINE));
- db_set_b(NULL, proto->m_szModuleName, "IgnoreAdvertisements", (BYTE) IsDlgButtonChecked(hwndDlg, IDC_IGNORE_ADVERTISEMENTS));
- db_set_b(NULL, proto->m_szModuleName, "LeaveOfflineMessage", (BYTE) IsDlgButtonChecked(hwndDlg, IDC_OFFLINE_MESSAGE));
- db_set_w(NULL, proto->m_szModuleName, "OfflineMessageOption", (WORD) SendDlgItemMessage(hwndDlg, IDC_OFFLINE_MESSAGE_OPTION, CB_GETCURSEL, 0, 0));
- db_set_w(NULL, proto->m_szModuleName, "AlertPolicy", (WORD) SendDlgItemMessage(hwndDlg, IDC_ALERT_POLICY, CB_GETCURSEL, 0, 0));
- db_set_w(NULL, proto->m_szModuleName, "GroupChatPolicy", (WORD) SendDlgItemMessage(hwndDlg, IDC_MUC_POLICY, CB_GETCURSEL, 0, 0));
- db_set_w(NULL, proto->m_szModuleName, "ImagePolicy", (WORD) SendDlgItemMessage(hwndDlg, IDC_IMAGE_POLICY, CB_GETCURSEL, 0, 0));
- db_set_b(NULL, proto->m_szModuleName, "EnableAvatars", (BYTE) IsDlgButtonChecked(hwndDlg, IDC_AVATARS));
- db_set_b(NULL, proto->m_szModuleName, "EnableVersion", (BYTE) IsDlgButtonChecked(hwndDlg, IDC_VERSIONINFO));
- db_set_b(NULL, proto->m_szModuleName, "UseNudge", (BYTE) IsDlgButtonChecked(hwndDlg, IDC_NUDGE_SUPPORT));
- db_set_b(NULL, proto->m_szModuleName, "LogAlerts", (BYTE) IsDlgButtonChecked(hwndDlg, IDC_LOG_ALERTS));
- if (reconnectRequired && proto->isConnected)
- MessageBox(hwndDlg, TranslateT("These changes will take effect the next time you connect to the Tlen network."), TranslateT("Tlen Protocol Option"), MB_OK|MB_SETFOREGROUND);
- ApplyChanges(proto, 1);
- return TRUE;
- }
- }
- break;
- }
- return FALSE;
-}
-
-static INT_PTR CALLBACK TlenVoiceOptDlgProc(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
-{
- TlenProtocol *proto = (TlenProtocol *)GetWindowLongPtr(hwndDlg, GWLP_USERDATA);
- switch (msg) {
- case WM_INITDIALOG:
- {
- TranslateDialogDefault(hwndDlg);
- proto = (TlenProtocol *)lParam;
- SetWindowLongPtr(hwndDlg, GWLP_USERDATA, (LONG_PTR)proto);
- SendDlgItemMessage(hwndDlg, IDC_VOICE_POLICY, CB_ADDSTRING, 0, (LPARAM)TranslateT("Always ask me"));
- SendDlgItemMessage(hwndDlg, IDC_VOICE_POLICY, CB_ADDSTRING, 0, (LPARAM)TranslateT("Accept invitations from authorized contacts"));
- SendDlgItemMessage(hwndDlg, IDC_VOICE_POLICY, CB_ADDSTRING, 0, (LPARAM)TranslateT("Accept all invitations"));
- SendDlgItemMessage(hwndDlg, IDC_VOICE_POLICY, CB_ADDSTRING, 0, (LPARAM)TranslateT("Ignore invitations from unauthorized contacts"));
- SendDlgItemMessage(hwndDlg, IDC_VOICE_POLICY, CB_ADDSTRING, 0, (LPARAM)TranslateT("Ignore all invitation"));
- SendDlgItemMessage(hwndDlg, IDC_VOICE_POLICY, CB_SETCURSEL, proto->tlenOptions.voiceChatPolicy, 0);
- TlenVoiceBuildInDeviceList(proto, GetDlgItem(hwndDlg, IDC_VOICE_DEVICE_IN));
- TlenVoiceBuildOutDeviceList(proto, GetDlgItem(hwndDlg, IDC_VOICE_DEVICE_OUT));
- return TRUE;
- }
- case WM_COMMAND:
- switch (LOWORD(wParam)) {
- case IDC_VOICE_POLICY:
- case IDC_VOICE_DEVICE_IN:
- case IDC_VOICE_DEVICE_OUT:
- if (HIWORD(wParam) == CBN_SELCHANGE)
- MarkChanges(2, hwndDlg);
- break;
- }
- break;
- case WM_NOTIFY:
- switch (((LPNMHDR) lParam)->code) {
- case PSN_APPLY:
- {
- db_set_w(NULL, proto->m_szModuleName, "VoiceChatPolicy", (WORD) SendDlgItemMessage(hwndDlg, IDC_VOICE_POLICY, CB_GETCURSEL, 0, 0));
- db_set_w(NULL, proto->m_szModuleName, "VoiceDeviceIn", (WORD) SendDlgItemMessage(hwndDlg, IDC_VOICE_DEVICE_IN, CB_GETCURSEL, 0, 0));
- db_set_w(NULL, proto->m_szModuleName, "VoiceDeviceOut", (WORD) SendDlgItemMessage(hwndDlg, IDC_VOICE_DEVICE_OUT, CB_GETCURSEL, 0, 0));
- ApplyChanges(proto, 2);
- return TRUE;
- }
- }
- break;
- }
-
- return FALSE;
-}
-
-static INT_PTR CALLBACK TlenAdvOptDlgProc(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
-{
- char text[256];
- BOOL bChecked;
- TlenProtocol *proto = (TlenProtocol *)GetWindowLongPtr(hwndDlg, GWLP_USERDATA);
-
- switch (msg) {
- case WM_INITDIALOG:
- {
- DBVARIANT dbv;
- proto = (TlenProtocol *)lParam;
- SetWindowLongPtr(hwndDlg, GWLP_USERDATA, (LONG_PTR)proto);
- TranslateDialogDefault(hwndDlg);
- if (!db_get_ws(NULL, proto->m_szModuleName, "LoginServer", &dbv)) {
- SetDlgItemText(hwndDlg, IDC_EDIT_LOGIN_SERVER, dbv.ptszVal);
- db_free(&dbv);
- }
- else SetDlgItemText(hwndDlg, IDC_EDIT_LOGIN_SERVER, L"tlen.pl");
-
- EnableWindow(GetDlgItem(hwndDlg, IDC_HOST), TRUE);
- EnableWindow(GetDlgItem(hwndDlg, IDC_HOSTPORT), TRUE);
-
- if (!db_get_ws(NULL, proto->m_szModuleName, "ManualHost", &dbv)) {
- SetDlgItemText(hwndDlg, IDC_HOST, dbv.ptszVal);
- db_free(&dbv);
- }
- else SetDlgItemText(hwndDlg, IDC_HOST, L"s1.tlen.pl");
-
- SetDlgItemInt(hwndDlg, IDC_HOSTPORT, db_get_w(NULL, proto->m_szModuleName, "ManualPort", TLEN_DEFAULT_PORT), FALSE);
-
- CheckDlgButton(hwndDlg, IDC_KEEPALIVE, db_get_b(NULL, proto->m_szModuleName, "KeepAlive", TRUE) ? BST_CHECKED : BST_UNCHECKED);
-
- CheckDlgButton(hwndDlg, IDC_USE_SSL, db_get_b(NULL, proto->m_szModuleName, "UseEncryption", TRUE) ? BST_CHECKED : BST_UNCHECKED);
-
- CheckDlgButton(hwndDlg, IDC_VISIBILITY_SUPPORT, db_get_b(NULL, proto->m_szModuleName, "VisibilitySupport", FALSE) ? BST_CHECKED : BST_UNCHECKED);
- // File transfer options
- bChecked = FALSE;
- if (db_get_b(NULL, proto->m_szModuleName, "UseFileProxy", FALSE) == TRUE) {
- bChecked = TRUE;
- CheckDlgButton(hwndDlg, IDC_FILE_USE_PROXY, BST_CHECKED);
- }
- EnableWindow(GetDlgItem(hwndDlg, IDC_FILE_PROXY_TYPE_LABEL), bChecked);
- EnableWindow(GetDlgItem(hwndDlg, IDC_FILE_PROXY_TYPE), bChecked);
- EnableWindow(GetDlgItem(hwndDlg, IDC_FILE_PROXY_HOST_LABEL), bChecked);
- EnableWindow(GetDlgItem(hwndDlg, IDC_FILE_PROXY_HOST), bChecked);
- EnableWindow(GetDlgItem(hwndDlg, IDC_FILE_PROXY_PORT_LABEL), bChecked);
- EnableWindow(GetDlgItem(hwndDlg, IDC_FILE_PROXY_PORT), bChecked);
- EnableWindow(GetDlgItem(hwndDlg, IDC_FILE_PROXY_USE_AUTH), bChecked);
- if (db_get_b(NULL, proto->m_szModuleName, "FileProxyAuth", FALSE) == TRUE)
- CheckDlgButton(hwndDlg, IDC_FILE_PROXY_USE_AUTH, BST_CHECKED);
- else
- bChecked = FALSE;
-
- EnableWindow(GetDlgItem(hwndDlg, IDC_FILE_PROXY_USER_LABEL), bChecked);
- EnableWindow(GetDlgItem(hwndDlg, IDC_FILE_PROXY_USER), bChecked);
- EnableWindow(GetDlgItem(hwndDlg, IDC_FILE_PROXY_PASSWORD_LABEL), bChecked);
- EnableWindow(GetDlgItem(hwndDlg, IDC_FILE_PROXY_PASSWORD), bChecked);
-
- SendDlgItemMessage(hwndDlg, IDC_FILE_PROXY_TYPE, CB_ADDSTRING, 0, (LPARAM)TranslateT("Forwarding"));
- SendDlgItemMessage(hwndDlg, IDC_FILE_PROXY_TYPE, CB_ADDSTRING, 0, (LPARAM)L"SOCKS4");
- SendDlgItemMessage(hwndDlg, IDC_FILE_PROXY_TYPE, CB_ADDSTRING, 0, (LPARAM)L"SOCKS5");
- SendDlgItemMessage(hwndDlg, IDC_FILE_PROXY_TYPE, CB_SETCURSEL, db_get_w(NULL, proto->m_szModuleName, "FileProxyType", 0), 0);
- if (!db_get_ws(NULL, proto->m_szModuleName, "FileProxyHost", &dbv)) {
- SetDlgItemText(hwndDlg, IDC_FILE_PROXY_HOST, dbv.ptszVal);
- db_free(&dbv);
- }
- SetDlgItemInt(hwndDlg, IDC_FILE_PROXY_PORT, db_get_w(NULL, proto->m_szModuleName, "FileProxyPort", 0), FALSE);
- if (!db_get_ws(NULL, proto->m_szModuleName, "FileProxyUsername", &dbv)) {
- SetDlgItemText(hwndDlg, IDC_FILE_PROXY_USER, dbv.ptszVal);
- db_free(&dbv);
- }
- if (!db_get_s(NULL, proto->m_szModuleName, "FileProxyPassword", &dbv)) {
- SetDlgItemTextA(hwndDlg, IDC_FILE_PROXY_PASSWORD, dbv.pszVal);
- db_free(&dbv);
- }
- return TRUE;
- }
- case WM_COMMAND:
- {
- switch (LOWORD(wParam)) {
- case IDC_FILE_PROXY_TYPE:
- if (HIWORD(wParam) == CBN_SELCHANGE)
- MarkChanges(4, hwndDlg);
- break;
- case IDC_EDIT_LOGIN_SERVER:
- case IDC_HOST:
- case IDC_HOSTPORT:
- case IDC_FILE_PROXY_HOST:
- case IDC_FILE_PROXY_PORT:
- case IDC_FILE_PROXY_USER:
- case IDC_FILE_PROXY_PASSWORD:
- if ((HWND)lParam == GetFocus() && HIWORD(wParam) == EN_CHANGE)
- MarkChanges(4, hwndDlg);
- break;
- case IDC_FILE_USE_PROXY:
- bChecked = IsDlgButtonChecked(hwndDlg, IDC_FILE_USE_PROXY);
- EnableWindow(GetDlgItem(hwndDlg, IDC_FILE_PROXY_TYPE_LABEL), bChecked);
- EnableWindow(GetDlgItem(hwndDlg, IDC_FILE_PROXY_TYPE), bChecked);
- EnableWindow(GetDlgItem(hwndDlg, IDC_FILE_PROXY_HOST_LABEL), bChecked);
- EnableWindow(GetDlgItem(hwndDlg, IDC_FILE_PROXY_HOST), bChecked);
- EnableWindow(GetDlgItem(hwndDlg, IDC_FILE_PROXY_PORT_LABEL), bChecked);
- EnableWindow(GetDlgItem(hwndDlg, IDC_FILE_PROXY_PORT), bChecked);
- EnableWindow(GetDlgItem(hwndDlg, IDC_FILE_PROXY_USE_AUTH), bChecked);
- case IDC_FILE_PROXY_USE_AUTH:
- bChecked = IsDlgButtonChecked(hwndDlg, IDC_FILE_PROXY_USE_AUTH) & IsDlgButtonChecked(hwndDlg, IDC_FILE_USE_PROXY);
- EnableWindow(GetDlgItem(hwndDlg, IDC_FILE_PROXY_USER_LABEL), bChecked);
- EnableWindow(GetDlgItem(hwndDlg, IDC_FILE_PROXY_USER), bChecked);
- EnableWindow(GetDlgItem(hwndDlg, IDC_FILE_PROXY_PASSWORD_LABEL), bChecked);
- EnableWindow(GetDlgItem(hwndDlg, IDC_FILE_PROXY_PASSWORD), bChecked);
- MarkChanges(4, hwndDlg);
- break;
- case IDC_KEEPALIVE:
- case IDC_VISIBILITY_SUPPORT:
- case IDC_USE_SSL:
- MarkChanges(4, hwndDlg);
- break;
- }
- }
- break;
-
- case WM_NOTIFY:
- switch (((LPNMHDR) lParam)->code) {
- case PSN_APPLY:
- WORD port;
- BOOL useEncryption;
- bool reconnectRequired = false;
- DBVARIANT dbv;
-
- GetDlgItemTextA(hwndDlg, IDC_EDIT_LOGIN_SERVER, text, _countof(text));
- if (db_get(NULL, proto->m_szModuleName, "LoginServer", &dbv)) {
- reconnectRequired = true;
- }
- else {
- if(mir_strcmp(text, dbv.pszVal))
- reconnectRequired = true;
- db_free(&dbv);
- }
- db_set_s(NULL, proto->m_szModuleName, "LoginServer", strlwr(text));
-
- GetDlgItemTextA(hwndDlg, IDC_HOST, text, _countof(text));
- if (db_get(NULL, proto->m_szModuleName, "ManualHost", &dbv)) {
- reconnectRequired = true;
- }
- else {
- if(mir_strcmp(text, dbv.pszVal))
- reconnectRequired = true;
- db_free(&dbv);
- }
- db_set_s(NULL, proto->m_szModuleName, "ManualHost", text);
-
- port = (WORD) GetDlgItemInt(hwndDlg, IDC_HOSTPORT, nullptr, FALSE);
- if (db_get_w(NULL, proto->m_szModuleName, "ManualPort", TLEN_DEFAULT_PORT) != port)
- reconnectRequired = true;
- db_set_w(NULL, proto->m_szModuleName, "ManualPort", port);
-
- proto->tlenOptions.sendKeepAlive = IsDlgButtonChecked(hwndDlg, IDC_KEEPALIVE);
- db_set_b(NULL, proto->m_szModuleName, "KeepAlive", (BYTE) proto->tlenOptions.sendKeepAlive);
-
- useEncryption = IsDlgButtonChecked(hwndDlg, IDC_USE_SSL);
- if (db_get_b(NULL, proto->m_szModuleName, "UseEncryption", TRUE) != useEncryption)
- reconnectRequired = true;
- db_set_b(NULL, proto->m_szModuleName, "UseEncryption", (BYTE) useEncryption);
-
- db_set_b(NULL, proto->m_szModuleName, "VisibilitySupport", (BYTE) IsDlgButtonChecked(hwndDlg, IDC_VISIBILITY_SUPPORT));
-
- // File transfer options
- db_set_b(NULL, proto->m_szModuleName, "UseFileProxy", (BYTE) IsDlgButtonChecked(hwndDlg, IDC_FILE_USE_PROXY));
- db_set_w(NULL, proto->m_szModuleName, "FileProxyType", (WORD) SendDlgItemMessage(hwndDlg, IDC_FILE_PROXY_TYPE, CB_GETCURSEL, 0, 0));
-
- GetDlgItemTextA(hwndDlg, IDC_FILE_PROXY_HOST, text, _countof(text));
- db_set_s(NULL, proto->m_szModuleName, "FileProxyHost", text);
-
- db_set_w(NULL, proto->m_szModuleName, "FileProxyPort", (WORD) GetDlgItemInt(hwndDlg, IDC_FILE_PROXY_PORT, nullptr, FALSE));
- db_set_b(NULL, proto->m_szModuleName, "FileProxyAuth", (BYTE) IsDlgButtonChecked(hwndDlg, IDC_FILE_PROXY_USE_AUTH));
-
- GetDlgItemTextA(hwndDlg, IDC_FILE_PROXY_USER, text, _countof(text));
- db_set_s(NULL, proto->m_szModuleName, "FileProxyUsername", text);
-
- GetDlgItemTextA(hwndDlg, IDC_FILE_PROXY_PASSWORD, text, _countof(text));
- db_set_s(NULL, proto->m_szModuleName, "FileProxyPassword", text);
-
- if (reconnectRequired && proto->isConnected)
- MessageBox(hwndDlg, TranslateT("These changes will take effect the next time you connect to the Tlen network."), TranslateT("Tlen Protocol Option"), MB_OK|MB_SETFOREGROUND);
- ApplyChanges(proto, 4);
- return TRUE;
- }
- break;
- }
-
- return FALSE;
-}
-
-#define POPUP_DEFAULT_COLORBKG 0xDCBDA5
-#define POPUP_DEFAULT_COLORTXT 0x000000
-
-static void MailPopupPreview(DWORD colorBack, DWORD colorText, wchar_t *title, wchar_t *emailInfo, int delay)
-{
- if (!ServiceExists(MS_POPUP_ADDPOPUPT))
- return;
- POPUPDATAT ppd = { 0 };
- HICON hIcon = GetIcolibIcon(IDI_MAIL);
- ppd.lchIcon = CopyIcon(hIcon);
- ReleaseIcolibIcon(hIcon);
- wcsncpy(ppd.lptzContactName, title, MAX_CONTACTNAME-1);
- wcsncpy(ppd.lptzText, emailInfo,MAX_SECONDLINE-1);
- ppd.colorBack = colorBack;
- ppd.colorText = colorText;
- ppd.iSeconds = delay;
- PUAddPopupT(&ppd);
-}
-
-static INT_PTR CALLBACK TlenPopupsDlgProc(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
-{
- TlenProtocol *proto = (TlenProtocol *)GetWindowLongPtr(hwndDlg, GWLP_USERDATA);
- switch (msg) {
- case WM_INITDIALOG:
- {
- BYTE delayMode;
- proto = (TlenProtocol *)lParam;
- SetWindowLongPtr(hwndDlg, GWLP_USERDATA, (LONG_PTR)proto);
- TranslateDialogDefault(hwndDlg);
- CheckDlgButton(hwndDlg, IDC_ENABLEPOPUP, db_get_b(NULL, proto->m_szModuleName, "MailPopupEnabled", TRUE) ? BST_CHECKED : BST_UNCHECKED);
- SendDlgItemMessage(hwndDlg, IDC_COLORBKG, CPM_SETCOLOUR, 0, db_get_dw(NULL, proto->m_szModuleName, "MailPopupBack", POPUP_DEFAULT_COLORBKG));
- SendDlgItemMessage(hwndDlg, IDC_COLORTXT, CPM_SETCOLOUR, 0, db_get_dw(NULL, proto->m_szModuleName, "MailPopupText", POPUP_DEFAULT_COLORTXT));
- SetDlgItemInt(hwndDlg, IDC_DELAY, db_get_dw(NULL, proto->m_szModuleName, "MailPopupDelay", 4), FALSE);
- delayMode = db_get_b(NULL, proto->m_szModuleName, "MailPopupDelayMode", 0);
- if (delayMode == 1) {
- CheckDlgButton(hwndDlg, IDC_DELAY_CUSTOM, BST_CHECKED);
- } else if (delayMode == 2) {
- CheckDlgButton(hwndDlg, IDC_DELAY_PERMANENT, BST_CHECKED);
- } else {
- CheckDlgButton(hwndDlg, IDC_DELAY_POPUP, BST_CHECKED);
- }
- return TRUE;
- }
- case WM_COMMAND:
- switch (LOWORD(wParam)) {
- case IDC_COLORTXT:
- case IDC_COLORBKG:
- case IDC_ENABLEPOPUP:
- case IDC_DELAY:
- case IDC_DELAY_POPUP:
- case IDC_DELAY_CUSTOM:
- case IDC_DELAY_PERMANENT:
- MarkChanges(8, hwndDlg);
- break;
- case IDC_PREVIEW:
- {
- int delay;
- wchar_t title[256];
- if (IsDlgButtonChecked(hwndDlg, IDC_DELAY_POPUP)) {
- delay=0;
- } else if (IsDlgButtonChecked(hwndDlg, IDC_DELAY_PERMANENT)) {
- delay=-1;
- } else {
- delay=GetDlgItemInt(hwndDlg, IDC_DELAY, nullptr, FALSE);
- }
- mir_snwprintf(title, TranslateT("%S mail"), proto->m_szModuleName);
- MailPopupPreview((DWORD) SendDlgItemMessage(hwndDlg,IDC_COLORBKG,CPM_GETCOLOUR,0,0),
- (DWORD) SendDlgItemMessage(hwndDlg,IDC_COLORTXT,CPM_GETCOLOUR,0,0),
- title,
- L"From: test@test.test\nSubject: test",
- delay);
- }
-
- }
- break;
-
-
- case WM_NOTIFY:
- switch (((LPNMHDR) lParam)->code) {
- case PSN_APPLY:
- {
- BYTE delayMode;
- db_set_b(NULL, proto->m_szModuleName, "MailPopupEnabled", (BYTE) IsDlgButtonChecked(hwndDlg, IDC_ENABLEPOPUP));
- db_set_dw(NULL, proto->m_szModuleName, "MailPopupBack", (DWORD) SendDlgItemMessage(hwndDlg,IDC_COLORBKG,CPM_GETCOLOUR,0,0));
- db_set_dw(NULL, proto->m_szModuleName, "MailPopupText", (DWORD) SendDlgItemMessage(hwndDlg,IDC_COLORTXT,CPM_GETCOLOUR,0,0));
- db_set_dw(NULL, proto->m_szModuleName, "MailPopupDelay", (DWORD) GetDlgItemInt(hwndDlg,IDC_DELAY, nullptr, FALSE));
- delayMode=0;
- if (IsDlgButtonChecked(hwndDlg, IDC_DELAY_CUSTOM)) {
- delayMode=1;
- } else if (IsDlgButtonChecked(hwndDlg, IDC_DELAY_PERMANENT)) {
- delayMode=2;
-
- }
- db_set_b(NULL, proto->m_szModuleName, "MailPopupDelayMode", delayMode);
- ApplyChanges(proto, 8);
- return TRUE;
- }
- }
- break;
-
- }
- return FALSE;
-}
-
diff --git a/protocols/Tlen/src/tlen_p2p_new.cpp b/protocols/Tlen/src/tlen_p2p_new.cpp
deleted file mode 100644
index f8c8c0d15a..0000000000
--- a/protocols/Tlen/src/tlen_p2p_new.cpp
+++ /dev/null
@@ -1,343 +0,0 @@
-/*
-
-Tlen Protocol Plugin for Miranda NG
-Copyright (C) 2004-2007 Piotr Piastucki
-
-This program is free software; you can redistribute it and/or
-modify it under the terms of the GNU General Public License
-as published by the Free Software Foundation; either version 2
-of the License, or (at your option) any later version.
-
-This program 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 General Public License for more details.
-
-You should have received a copy of the GNU General Public License
-along with this program; if not, write to the Free Software
-Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
-
-*/
-
-#include "stdafx.h"
-#include "tlen_p2p_old.h"
-#include <io.h>
-#include <fcntl.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include "tlen_list.h"
-
-
-static void logInfo(const char *filename, const char *fmt, ...) {
- va_list vararg;
- FILE *flog=fopen(filename,"at");
- if (flog != nullptr) {
- SYSTEMTIME time;
- GetLocalTime(&time);
- va_start(vararg, fmt);
- int strsize=2048;
- char *str = (char *) mir_alloc(strsize);
- while (mir_vsnprintf(str, strsize, fmt, vararg) == -1)
- str = (char *) realloc(str, strsize+=2048);
- va_end(vararg);
- fprintf(flog,"%04d-%02d-%02d %02d:%02d:%02d,%03d [%s]",time.wYear,time.wMonth,time.wDay,time.wHour,time.wMinute,time.wSecond,time.wMilliseconds, "INFO");
- fprintf(flog," %s\n",str);
- mir_free(str);
- fclose(flog);
- }
-}
-
-void __cdecl TlenNewFileReceiveThread(TLEN_FILE_TRANSFER *ft)
-{
- ft->proto->debugLogA("P2P receive thread started");
- ProtoBroadcastAck(ft->proto->m_szModuleName, ft->hContact, ACKTYPE_FILE, ACKRESULT_CONNECTING, ft, 0);
-// ft->mode = FT_RECV;
-// ft->currentFile = 0;
-// ft->state = FT_CONNECTING;
- {
- FILE * fout = fopen("tlen_recv.dxx", "wt");
- SOCKADDR_IN toad;
- toad.sin_family = AF_INET;
- toad.sin_addr.s_addr = inet_addr(ft->hostName);
- toad.sin_port = htons(ft->wPort);
- if (fout != nullptr) {
- fprintf(fout, "START:\n");
- }
- if (fout != nullptr) {
- fclose(fout);
- }
- while (ft->udps != INVALID_SOCKET) {
- SOCKADDR_IN cad;
- int alen;
- int j, n;
- unsigned char buff[1024];
- alen = sizeof(struct sockaddr);
- n=recvfrom(ft->udps, (char*)buff,sizeof(buff),0, (struct sockaddr *) &cad, &alen);
- if (n<0) {
- break;
- }
- logInfo("tlen_recv.dxx", "UDP");
- fout = fopen("tlen_recv.dxx", "at");
- if (fout != nullptr) {
- fprintf(fout, "\n|RECV %d bytes from %s:%d|",n, inet_ntoa(cad.sin_addr), cad.sin_port);
- for (j = 0; j < n; j++) {
- fprintf(fout, "%02X-", buff[j]);
- }
- }
- if (n == 1) {
- alen = sizeof(struct sockaddr);
- n = sendto(ft->udps, (char*)buff, n, 0,(struct sockaddr *) &toad, alen);
- if (fout != nullptr) {
- fprintf(fout, "\n|SEND |");
- for (j = 0; j < n; j++) {
- fprintf(fout, "%02X-", buff[j]);
- }
- }
- }
- if (fout != nullptr) {
- fprintf(fout, "\n");
- fclose(fout);
- }
- }
- }
- if (ft->udps != INVALID_SOCKET) {
- closesocket(ft->udps);
- }
-
- TlenListRemove(ft->proto, LIST_FILE, ft->iqId);
- if (ft->state == FT_DONE)
- ProtoBroadcastAck(ft->proto->m_szModuleName, ft->hContact, ACKTYPE_FILE, ACKRESULT_SUCCESS, ft, 0);
- else {
- ProtoBroadcastAck(ft->proto->m_szModuleName, ft->hContact, ACKTYPE_FILE, ACKRESULT_FAILED, ft, 0);
- }
- ft->proto->debugLogA("P2P receive thread ended");
- TlenP2PFreeFileTransfer(ft);
-}
-
-void __cdecl TlenNewFileSendThread(TLEN_FILE_TRANSFER *ft)
-{
- ft->proto->debugLogA("P2P send thread started");
-// ft->mode = FT_RECV;
-// ProtoBroadcastAck(ft->proto->m_szModuleName, ft->hContact, ACKTYPE_FILE, ACKRESULT_CONNECTING, ft, 0);
-// ft->currentFile = 0;
-// ft->state = FT_CONNECTING;
- {
- FILE * fout = fopen("tlen_send.gxx", "wt");
- int step = 0;
- SOCKADDR_IN toad;
- toad.sin_family = AF_INET;
- toad.sin_addr.s_addr = inet_addr(ft->hostName);
- toad.sin_port = htons(ft->wPort);
-
- if (fout != nullptr) {
- fprintf(fout, "START:");
- }
- for (step = 0; step < 10; step ++) {
-// while (ft->udps != INVALID_SOCKET) {
- int alen;
- int j, n;
- unsigned char buff[1024];
- alen = sizeof(struct sockaddr);
- if (step < 3 || step > 5) {
- buff[0] = 1;
- n = 1;
- } else {
- buff[0] = '$';
- buff[1] = '^';
- buff[2] = '&';
- buff[3] = '%';
- n = 4;
- }
- n=sendto(ft->udps, (char*)buff, n, 0, (struct sockaddr *)&toad, alen);
- logInfo("tlen_send.dxx", "UDP");
- if (fout != nullptr) {
- fprintf(fout, "|send: %d %s %d|",n, inet_ntoa(toad.sin_addr), toad.sin_port);
- for (j = 0; j < n; j++) {
- fprintf(fout, "%02X-", buff[j]);
- }
- }
- if (fout != nullptr) {
- fprintf(fout, "\n");
- }
- SleepEx(1000, TRUE);
- }
- if (fout != nullptr) {
- fclose(fout);
- }
- }
- ft->proto->debugLogA("P2P send thread ended");
-}
-
-void TlenBindUDPSocket(TLEN_FILE_TRANSFER *ft)
-{
- ft->proto->debugLogA("Binding UDP socket");
- ft->udps = socket(PF_INET, SOCK_DGRAM, 0);
- if (ft->udps != INVALID_SOCKET) {
- SOCKADDR_IN sin;
- int len = sizeof(struct sockaddr);
- memset((char *)&sin,0,sizeof(sin));
- sin.sin_family = AF_INET;
- sin.sin_addr.s_addr = htonl(INADDR_ANY);
- sin.sin_port = 0;
- if (bind(ft->udps, (struct sockaddr *)&sin, sizeof(sin)) >= 0) {
- if (!getsockname((SOCKET)ft->udps,(SOCKADDR *)&sin,&len)) {
- struct hostent *hp;
- char host_name[256];
- gethostname(host_name, sizeof(host_name));
- hp = gethostbyname(host_name);
- mir_snprintf(host_name, "%u.%u.%u.%u", (unsigned char)hp->h_addr_list[0][0],
- (unsigned char)hp->h_addr_list[0][1],
- (unsigned char)hp->h_addr_list[0][2],
- (unsigned char)hp->h_addr_list[0][3]);
-
- ft->wLocalPort = ntohs(sin.sin_port);
- ft->localName= mir_strdup(host_name);
- ft->proto->debugLogA("UDP socket bound to %s:%d", ft->localName, ft->wLocalPort);
- }
- }
- }
-}
-
-
-/*
-aaa sends a file to bbb:
-
-1) aaa wants to SEND bbb a file
-A SEND: <iq to='bbb@tlen.pl'><query xmlns='p2p'><fs t="bbb@tlen.pl" e="1" i="179610858" c="1" s="308278" v="2" /></query></iq>
-B RECV: <iq from='aaa@tlen.pl'><query xmlns='p2p'><fs t="bbb@tlen.pl" e="1" i="179610858" c="1" s="308278" v="2" /></query></iq>
-
-2) bbb ACCEPTs
-B SEND: <iq to='aaa@tlen.pl'><query xmlns='p2p'><fs t="aaa@tlen.pl" e="5" i="179610858" v="2" /></query></iq>
-A RECV: <iq from='bbb@tlen.pl'><query xmlns='p2p'><fs t="aaa@tlen.pl" e="5" i="179610858" v="2" /></query></iq>
-
-3) aaa tries to establish p2p connection:
-A SEND: <iq to='bbb@tlen.pl'><query xmlns='p2p'><dcng n='file_send' k='5' v='2' s='1' i='179610858' ck='rNbjnfRSQwJyaI+oRAXmfQ==' ks='16' iv='MAfaCCrqtuU2Ngw=rNbjnfRSQwJyaI+oRAXmfQ==' mi='24835584'/></query></iq>
-B SEND: <iq from='aaa@tlen.pl'><query xmlns='p2p'><dcng n='file_send' k='5' v='2' s='1' i='179610858' ck='rNbjnfRSQwJyaI+oRAXmfQ==' ks='16' iv='MAfaCCrqtuU2Ngw=rNbjnfRSQwJyaI+oRAXmfQ==' mi='24835584'/></query></iq>
-
-3) bbb sends IP/port details
-B SEND: <iq to='bbb@tlen.pl'><query xmlns='p2p'><dcng la='xx.xx.xx.xx' lp='nnn' pa='xx.xx.xx.xx' pp='nnn' i='179610858' v='2' k='5' s='2'/></query></iq>
-
-*/
-
-void __cdecl TlenProcessP2P(XmlNode *node, ThreadData *info) {
- XmlNode *queryNode;
- TLEN_LIST_ITEM *item;
- char *from;
-
- if (info == nullptr) return;
-
- queryNode = TlenXmlGetChild(node, "query");
- if ((from=TlenXmlGetAttrValue(node, "from")) != nullptr) {
- XmlNode *fs , *vs, *dcng, *dc;
- /* file send */
- fs = TlenXmlGetChild(queryNode, "fs");
- /* voice send */
- vs = TlenXmlGetChild(queryNode, "vs");
- dcng = TlenXmlGetChild(queryNode, "dcng");
- dc = TlenXmlGetChild(queryNode, "dc");
- if (fs != nullptr) {
- char *e, *id;
- /* e - step in the process (starting with 1)*/
- /* i - id of the file */
- /* s - size of the file */
- /* c - number of files */
- /* v - ??? */
- e = TlenXmlGetAttrValue(fs, "e");
- id = TlenXmlGetAttrValue(fs, "i");
- if (e != nullptr) {
- if (!mir_strcmp(e, "1")) {
- TLEN_FILE_TRANSFER * ft = (TLEN_FILE_TRANSFER *) mir_alloc(sizeof(TLEN_FILE_TRANSFER));
- memset(ft, 0, sizeof(TLEN_FILE_TRANSFER));
- char *c = TlenXmlGetAttrValue(fs, "c");
- char *s = TlenXmlGetAttrValue(fs, "s");
- ft->jid = mir_strdup(from);
- ft->proto = info->proto;
- ft->hContact = TlenHContactFromJID(info->proto, from);
- ft->iqId = mir_strdup(id);
- ft->fileTotalSize = atoi(s);
- ft->newP2P = TRUE;
- if ((item=TlenListAdd(ft->proto, LIST_FILE, ft->iqId)) != nullptr) {
- char fileInfo[128];
- item->ft = ft;
- mir_snprintf(fileInfo, "%s file(s), %s bytes", c, s);
- wchar_t* filenameT = mir_utf8decodeW((char*)fileInfo);
- PROTORECVFILET pre = { 0 };
- pre.dwFlags = PRFF_UNICODE;
- pre.fileCount = 1;
- pre.timestamp = time(nullptr);
- pre.descr.w = filenameT;
- pre.files.w = &filenameT;
- pre.lParam = (LPARAM)ft;
- ft->proto->debugLogA("sending chainrecv");
- ProtoChainRecvFile(ft->hContact, &pre);
- mir_free(filenameT);
- }
- } else if (!mir_strcmp(e, "3")) {
- /* transfer error */
- } else if (!mir_strcmp(e, "4")) {
- /* transfer denied */
- } else if (!mir_strcmp(e, "5")) {
- /* transfer accepted */
- if ((item=TlenListGetItemPtr(info->proto, LIST_FILE, id)) != nullptr) {
- item->id2 = mir_strdup("84273372");
- item->ft->id2 = mir_strdup("84273372");
- TlenSend(info->proto, "<iq to='%s'><query xmlns='p2p'><dcng n='file_send' k='5' v='2' s='1' i='%s' ck='o7a32V9n2UZYCWpBUhSbFw==' ks='16' iv='MhjWEj9WTsovrQc=o7a32V9n2UZYCWpBUhSbFw==' mi='%s'/></query></iq>", from, item->id2, id);
- }
- }
- }
- } else if (vs != nullptr) {
-
- } else if (dcng != nullptr) {
- info->proto->debugLogA("DCNG");
- char *s = TlenXmlGetAttrValue(dcng, "s");
- char *id2 = TlenXmlGetAttrValue(dcng, "i");
- char *id = TlenXmlGetAttrValue(dcng, "mi");
- if (!mir_strcmp(s, "1")) {
- /* Keys */
- /* s - step */
- /* i - id of the file */
- /* ks - key size (in bytes) */
- /* mi - p2p connection id */
- /* ck - aes key */
- /* iv - aes initial vector */
- /* k - ? */
- /* v - ? */
- char *n = TlenXmlGetAttrValue(dcng, "n"); // n - name (file_send)
- if (!mir_strcmp(n, "file_send")) {
- if ((item=TlenListGetItemPtr(info->proto, LIST_FILE, id)) != nullptr) {
- item->id2 = mir_strdup(id2);
- item->ft->id2 = mir_strdup(id2);
- TlenBindUDPSocket(item->ft);
- TlenSend(info->proto, "<iq to='%s'><query xmlns='p2p'><dcng la='%s' lp='%d' pa='%s' pp='%d' i='%s' v='2' k='5' s='2'/></query></iq>",
- item->ft->jid, item->ft->localName, item->ft->wLocalPort, item->ft->localName, item->ft->wLocalPort, item->ft->id2);
- }
- }
- } else if (!mir_strcmp(s, "2")) {
- info->proto->debugLogA("step = 2");
- info->proto->debugLogA("%s",from);
- info->proto->debugLogA("%s",id2);
- /* IP and port */
- if ((item=TlenListFindItemPtrById2(info->proto, LIST_FILE, id2)) != nullptr) {
- item->ft->hostName = mir_strdup(TlenXmlGetAttrValue(dcng, "pa"));
- item->ft->wPort = atoi(TlenXmlGetAttrValue(dcng, "pp"));
- TlenBindUDPSocket(item->ft);
- TlenSend(info->proto, "<iq to='%s'><query xmlns='p2p'><dcng la='%s' lp='%d' pa='%s' pp='%d' i='%s' k='5' s='4'/></query></iq>",
- item->ft->jid, item->ft->localName, item->ft->wLocalPort, item->ft->localName, item->ft->wLocalPort, item->ft->id2);
- mir_forkthread((pThreadFunc)TlenNewFileReceiveThread, item->ft);
- mir_forkthread((pThreadFunc)TlenNewFileSendThread, item->ft);
- }
- } else if (!mir_strcmp(s, "4")) {
- /* IP and port */
- if ((item=TlenListFindItemPtrById2(info->proto, LIST_FILE, id2)) != nullptr) {
- info->proto->debugLogA("step = 4");
- item->ft->hostName = mir_strdup(TlenXmlGetAttrValue(dcng, "pa"));
- item->ft->wPort = atoi(TlenXmlGetAttrValue(dcng, "pp"));
- mir_forkthread((pThreadFunc)TlenNewFileReceiveThread, item->ft);
- }
- }
-
- } else if (dc != nullptr) {
-
- }
- }
-}
diff --git a/protocols/Tlen/src/tlen_p2p_old.cpp b/protocols/Tlen/src/tlen_p2p_old.cpp
deleted file mode 100644
index d5a71a204e..0000000000
--- a/protocols/Tlen/src/tlen_p2p_old.cpp
+++ /dev/null
@@ -1,522 +0,0 @@
-/*
-
-Tlen Protocol Plugin for Miranda NG
-Copyright (C) 2004-2007 Piotr Piastucki
-
-This program is free software; you can redistribute it and/or
-modify it under the terms of the GNU General Public License
-as published by the Free Software Foundation; either version 2
-of the License, or (at your option) any later version.
-
-This program 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 General Public License for more details.
-
-You should have received a copy of the GNU General Public License
-along with this program; if not, write to the Free Software
-Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
-
-*/
-
-#include "stdafx.h"
-#include "tlen_p2p_old.h"
-#include <io.h>
-#include <fcntl.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include "tlen_list.h"
-
-
-void TlenP2PFreeFileTransfer(TLEN_FILE_TRANSFER *ft)
-{
- if (ft->jid) mir_free(ft->jid);
- if (ft->iqId) mir_free(ft->iqId);
- if (ft->id2) mir_free(ft->id2);
- if (ft->hostName) mir_free(ft->hostName);
- if (ft->localName) mir_free(ft->localName);
- if (ft->szSavePath) mir_free(ft->szSavePath);
- if (ft->szDescription) mir_free(ft->szDescription);
- if (ft->filesSize) mir_free(ft->filesSize);
- if (ft->files) {
- for (int i=0; i<ft->fileCount; i++) {
- if (ft->files[i]) mir_free(ft->files[i]);
- }
- mir_free(ft->files);
- }
- mir_free(ft);
-}
-
-TLEN_FILE_PACKET *TlenP2PPacketCreate(int datalen)
-{
- TLEN_FILE_PACKET *packet=(TLEN_FILE_PACKET *) mir_alloc(sizeof(TLEN_FILE_PACKET));
- if (packet == nullptr)
- return nullptr;
- packet->packet = nullptr;
- if (datalen > 0) {
- if ((packet->packet=(char *) mir_alloc(datalen)) == nullptr) {
- mir_free(packet);
- return nullptr;
- }
- }
- packet->maxDataLen = datalen;
- packet->type=0;
- packet->len=0;
- return packet;
-}
-
-void TlenP2PPacketFree(TLEN_FILE_PACKET *packet)
-{
- if (packet != nullptr) {
- if (packet->packet != nullptr)
- mir_free(packet->packet);
- mir_free(packet);
- }
-}
-
-void TlenP2PPacketSetType(TLEN_FILE_PACKET *packet, DWORD type)
-{
- if (packet != nullptr) {
- packet->type = type;
- }
-}
-
-void TlenP2PPacketSetLen(TLEN_FILE_PACKET *packet, DWORD len)
-{
- if (packet != nullptr) {
- packet->len = len;
- }
-}
-
-void TlenP2PPacketPackDword(TLEN_FILE_PACKET *packet, DWORD data)
-{
- if (packet != nullptr && packet->packet != nullptr) {
- if (packet->len + sizeof(DWORD) <= packet->maxDataLen) {
- (*((DWORD*)((packet->packet)+(packet->len)))) = data;
- packet->len += sizeof(DWORD);
- }
- }
-}
-
-void TlenP2PPacketPackBuffer(TLEN_FILE_PACKET *packet, char *buffer, int len)
-{
- if (packet != nullptr && packet->packet != nullptr) {
- if (packet->len + len <= packet->maxDataLen) {
- memcpy((packet->packet)+(packet->len), buffer, len);
- packet->len += len;
- }
- }
-}
-
-void TlenP2PInit() {
-}
-
-void TlenP2PUninit() {
-}
-
-int TlenP2PPacketSend(HNETLIBCONN s, TLEN_FILE_PACKET *packet)
-{
- DWORD sendResult;
- if (packet != nullptr && packet->packet != nullptr) {
- Netlib_Send(s, (char *)&packet->type, 4, MSG_NODUMP);
- Netlib_Send(s, (char *)&packet->len, 4, MSG_NODUMP);
- sendResult=Netlib_Send(s, packet->packet, packet->len, MSG_NODUMP);
- if (sendResult == SOCKET_ERROR || sendResult != packet->len) return 0;
- }
- return 1;
-}
-
-TLEN_FILE_PACKET* TlenP2PPacketReceive(HNETLIBCONN s)
-{
- DWORD type, len, pos;
- DWORD recvResult = Netlib_Recv(s, (char *)&type, 4, MSG_NODUMP);
- if (recvResult == 0 || recvResult == SOCKET_ERROR) return nullptr;
- recvResult = Netlib_Recv(s, (char *)&len, 4, MSG_NODUMP);
- if (recvResult == 0 || recvResult == SOCKET_ERROR) return nullptr;
- TLEN_FILE_PACKET *packet = TlenP2PPacketCreate(len);
- TlenP2PPacketSetType(packet, type);
- TlenP2PPacketSetLen(packet, len);
- pos = 0;
- while (len > 0) {
- recvResult = Netlib_Recv(s, packet->packet+pos, len, MSG_NODUMP);
- if (recvResult == 0 || recvResult == SOCKET_ERROR) {
- TlenP2PPacketFree(packet);
- return nullptr;
- }
- len -= recvResult;
- pos += recvResult;
- }
- return packet;
-}
-
-void TlenP2PEstablishOutgoingConnection(TLEN_FILE_TRANSFER *ft, BOOL sendAck)
-{
- char str[300];
- TlenProtocol *proto = ft->proto;
-
- proto->debugLogA("Establishing outgoing connection.");
- ft->state = FT_ERROR;
- TLEN_FILE_PACKET *packet = TlenP2PPacketCreate(2*sizeof(DWORD) + 20);
- if (packet != nullptr) {
- TlenP2PPacketSetType(packet, TLEN_FILE_PACKET_CONNECTION_REQUEST);
- TlenP2PPacketPackDword(packet, 1);
- TlenP2PPacketPackDword(packet, (DWORD) atoi(ft->iqId));
- size_t srt_len = mir_snprintf(str, "%08X%s%d", atoi(ft->iqId), proto->threadData->username, atoi(ft->iqId));
- char *hash = TlenSha1(str, (int)srt_len);
- TlenP2PPacketPackBuffer(packet, hash, 20);
- mir_free(hash);
- TlenP2PPacketSend(ft->s, packet);
- TlenP2PPacketFree(packet);
- packet = TlenP2PPacketReceive(ft->s);
- if (packet != nullptr) {
- if (packet->type == TLEN_FILE_PACKET_CONNECTION_REQUEST_ACK) { // acknowledge
- if ((int)(*((DWORD*)packet->packet)) == atoi(ft->iqId)) {
- ft->state = FT_CONNECTING;
- if (sendAck) {
- ProtoBroadcastAck(proto->m_szModuleName, ft->hContact, ACKTYPE_FILE, ACKRESULT_CONNECTED, ft, 0);
- }
- }
- }
- TlenP2PPacketFree(packet);
- }
- }
-}
-
-TLEN_FILE_TRANSFER* TlenP2PEstablishIncomingConnection(TlenProtocol *proto, HNETLIBCONN s, TLEN_LIST list, BOOL sendAck)
-{
- char str[300];
- DWORD iqId;
- // TYPE: 0x1
- // LEN:
- // (DWORD) 0x1
- // (DWORD) id
- // (BYTE) hash[20]
- TLEN_FILE_PACKET *packet = TlenP2PPacketReceive(s);
- if (packet == nullptr || packet->type != TLEN_FILE_PACKET_CONNECTION_REQUEST || packet->len<28) {
- if (packet != nullptr) {
- TlenP2PPacketFree(packet);
- }
- return nullptr;
- }
- iqId = *((DWORD *)(packet->packet+sizeof(DWORD)));
- int i = 0;
- TLEN_LIST_ITEM *item;
- while ((i=TlenListFindNext(proto, list, i)) >= 0) {
- item = TlenListGetItemPtrFromIndex(proto, i);
- if (item != nullptr) {
- mir_snprintf(str, "%d", iqId);
- if (!mir_strcmp(item->ft->iqId, str)) {
- char *hash, *nick;
- int j;
- nick = TlenNickFromJID(item->ft->jid);
- j = mir_snprintf(str, "%08X%s%d", iqId, nick, iqId);
- mir_free(nick);
- hash = TlenSha1(str, j);
- for (j=0;j<20;j++) {
- if (hash[j] != packet->packet[2*sizeof(DWORD)+j]) break;
- }
- mir_free(hash);
- if (j == 20) break;
- }
- }
- i++;
- }
- TlenP2PPacketFree(packet);
- if (i >=0) {
- if ((packet=TlenP2PPacketCreate(sizeof(DWORD))) != nullptr) {
- // Send connection establishment acknowledgement
- TlenP2PPacketSetType(packet, TLEN_FILE_PACKET_CONNECTION_REQUEST_ACK);
- TlenP2PPacketPackDword(packet, (DWORD) atoi(item->ft->iqId));
- TlenP2PPacketSend(s, packet);
- TlenP2PPacketFree(packet);
- item->ft->state = FT_CONNECTING;
- if (sendAck) {
- ProtoBroadcastAck(item->ft->proto->m_szModuleName, item->ft->hContact, ACKTYPE_FILE, ACKRESULT_CONNECTED, item->ft, 0);
- }
- return item->ft;
- }
- }
- return nullptr;
-}
-
-static void __cdecl TlenFileBindSocks4Thread(TLEN_FILE_TRANSFER* ft)
-{
- BYTE buf[8];
-
-// TlenLog("Waiting for the file to be sent via SOCKS...");
- int status = Netlib_Recv(ft->s, (char*)buf, 8, MSG_NODUMP);
-// TlenLog("accepted connection !!!");
- if ( status == SOCKET_ERROR || status < 8 || buf[1] != 90) {
- status = 1;
- } else {
- status = 0;
- }
- if (!status) {
- ft->pfnNewConnectionV2(ft->s, 0, nullptr);
- } else {
- if (ft->state != FT_SWITCH) {
- ft->state = FT_ERROR;
- }
- }
- ft->proto->debugLogA("Closing connection for this file transfer...");
-// Netlib_CloseHandle(ft->s);
- if (ft->hFileEvent != nullptr)
- SetEvent(ft->hFileEvent);
-
-}
-static void __cdecl TlenFileBindSocks5Thread(TLEN_FILE_TRANSFER* ft)
-{
- BYTE buf[256];
-
-// TlenLog("Waiting for the file to be sent via SOCKS...");
- int status = Netlib_Recv(ft->s, (char*)buf, sizeof(buf), MSG_NODUMP);
-// TlenLog("accepted connection !!!");
- if ( status == SOCKET_ERROR || status < 7 || buf[1] != 0) {
- status = 1;
- } else {
- status = 0;
- }
- if (!status) {
- ft->pfnNewConnectionV2(ft->s, 0, nullptr);
- } else {
- if (ft->state != FT_SWITCH) {
- ft->state = FT_ERROR;
- }
- }
-// TlenLog("Closing connection for this file transfer...");
-// Netlib_CloseHandle(ft->s);
- if (ft->hFileEvent != nullptr)
- SetEvent(ft->hFileEvent);
-
-}
-
-static HNETLIBCONN TlenP2PBindSocks4(SOCKSBIND * sb, TLEN_FILE_TRANSFER *ft)
-{ //rfc1928
- int len;
- BYTE buf[256];
- int status;
- struct in_addr in;
-
- NETLIBOPENCONNECTION nloc = { sizeof(nloc) };
- nloc.szHost = sb->szHost;
- nloc.wPort = sb->wPort;
- HNETLIBCONN s = Netlib_OpenConnection(ft->proto->hFileNetlibUser, &nloc);
- if (s == nullptr) {
-// TlenLog("Connection failed (%d), thread ended", WSAGetLastError());
- return nullptr;
- }
- buf[0] = 4; //socks4
- buf[1] = 2; //2-bind, 1-connect
- *(PWORD)(buf+2) = htons(0); // port
- *(PDWORD)(buf+4) = INADDR_ANY;
- if (sb->useAuth) {
- mir_strcpy((char*)buf+8, sb->szUser);
- len = (int)mir_strlen(sb->szUser);
- } else {
- buf[8] = 0;
- len = 0;
- }
- len += 9;
- status = Netlib_Send(s, (char*)buf, len, MSG_NODUMP);
- if (status == SOCKET_ERROR || status < len) {
-// TlenLog("Send failed (%d), thread ended", WSAGetLastError());
- Netlib_CloseHandle(s);
- return nullptr;
- }
- status = Netlib_Recv(s, (char*)buf, 8, MSG_NODUMP);
- if (status == SOCKET_ERROR || status < 8 || buf[1] != 90) {
-// TlenLog("SOCKS4 negotiation failed");
- Netlib_CloseHandle(s);
- return nullptr;
- }
- status = Netlib_Recv(s, (char*)buf, sizeof(buf), MSG_NODUMP);
- if ( status == SOCKET_ERROR || status < 7 || buf[0] != 5 || buf[1] != 0) {
-// TlenLog("SOCKS5 request failed");
- Netlib_CloseHandle(s);
- return nullptr;
- }
- in.S_un.S_addr = *(PDWORD)(buf+4);
- strncpy(sb->szHost, inet_ntoa(in), sizeof(sb->szHost)-1);
- sb->wPort = htons(*(PWORD)(buf+2));
- ft->s = s;
- mir_forkthread(pThreadFunc(TlenFileBindSocks4Thread), ft);
- return s;
-}
-
-static HNETLIBCONN TlenP2PBindSocks5(SOCKSBIND * sb, TLEN_FILE_TRANSFER *ft)
-{ //rfc1928
- BYTE buf[512];
- int len, status;
- struct in_addr in;
-
- NETLIBOPENCONNECTION nloc = { sizeof(nloc) };
- nloc.szHost = sb->szHost;
- nloc.wPort = sb->wPort;
- HNETLIBCONN s = Netlib_OpenConnection(ft->proto->hFileNetlibUser, &nloc);
- if (s == nullptr) {
- ft->proto->debugLogA("Connection failed (%d), thread ended", WSAGetLastError());
- return nullptr;
- }
- buf[0] = 5; //yep, socks5
- buf[1] = 1; //one auth method
- buf[2] = sb->useAuth?2:0; // authorization
- status = Netlib_Send(s, (char*)buf, 3, MSG_NODUMP);
- if (status == SOCKET_ERROR || status < 3) {
- ft->proto->debugLogA("Send failed (%d), thread ended", WSAGetLastError());
- Netlib_CloseHandle(s);
- return nullptr;
- }
- status = Netlib_Recv(s, (char*)buf, 2, MSG_NODUMP);
- if (status == SOCKET_ERROR || status < 2 || (buf[1] != 0 && buf[1] != 2)) {
- ft->proto->debugLogA("SOCKS5 negotiation failed");
- Netlib_CloseHandle(s);
- return nullptr;
- }
- if (buf[1] == 2) { //rfc1929
- int nUserLen, nPassLen;
- PBYTE pAuthBuf;
-
- nUserLen = (int)mir_strlen(sb->szUser);
- nPassLen = (int)mir_strlen(sb->szPassword);
- pAuthBuf = (PBYTE)mir_alloc(3+nUserLen+nPassLen);
- pAuthBuf[0] = 1; //auth version
- pAuthBuf[1] = nUserLen;
- memcpy(pAuthBuf+2, sb->szUser, nUserLen);
- pAuthBuf[2+nUserLen]=nPassLen;
- memcpy(pAuthBuf+3+nUserLen,sb->szPassword,nPassLen);
- status = Netlib_Send(s, (char*)pAuthBuf, 3+nUserLen+nPassLen, MSG_NODUMP);
- mir_free(pAuthBuf);
- if (status == SOCKET_ERROR || status < 3 + nUserLen+nPassLen) {
- ft->proto->debugLogA("Send failed (%d), thread ended", WSAGetLastError());
- Netlib_CloseHandle(s);
- return nullptr;
- }
- status = Netlib_Recv(s, (char*)buf, sizeof(buf), MSG_NODUMP);
- if (status == SOCKET_ERROR || status < 2 || buf[1] != 0) {
- ft->proto->debugLogA("SOCKS5 sub-negotiation failed");
- Netlib_CloseHandle(s);
- return nullptr;
- }
- }
-
- { PBYTE pInit;
- int nHostLen=4;
- DWORD hostIP=INADDR_ANY;
- pInit=(PBYTE)mir_alloc(6+nHostLen);
- pInit[0]=5; //SOCKS5
- pInit[1]=2; //bind
- pInit[2]=0; //reserved
- pInit[3]=1;
- *(PDWORD)(pInit+4)=hostIP;
- *(PWORD)(pInit+4+nHostLen)=htons(0);
- status = Netlib_Send(s, (char*)pInit, 6+nHostLen, MSG_NODUMP);
- mir_free(pInit);
- if (status == SOCKET_ERROR || status < 6 + nHostLen) {
-// TlenLog("Send failed (%d), thread ended", WSAGetLastError());
- Netlib_CloseHandle(s);
- return nullptr;
- }
- }
- status = Netlib_Recv(s, (char*)buf, sizeof(buf), MSG_NODUMP);
- if ( status == SOCKET_ERROR || status < 7 || buf[0] != 5 || buf[1] != 0) {
-// TlenLog("SOCKS5 request failed");
- Netlib_CloseHandle(s);
- return nullptr;
- }
- if (buf[2] == 1) { // domain
- len = buf[4];
- memcpy(sb->szHost, buf+5, len);
- sb->szHost[len]=0;
- len += 4;
- } else { // ip address
- in.S_un.S_addr = *(PDWORD)(buf+4);
- mir_strcpy(sb->szHost, inet_ntoa(in));
- len = 8;
- }
- sb->wPort = htons(*(PWORD)(buf+len));
- ft->s = s;
-
- mir_forkthread(pThreadFunc(TlenFileBindSocks5Thread), ft);
- return s;
-}
-
-
-HNETLIBCONN TlenP2PListen(TLEN_FILE_TRANSFER *ft)
-{
- HNETLIBCONN s = nullptr;
- int useProxy;
- DBVARIANT dbv;
- SOCKSBIND sb;
- struct in_addr in;
- TlenProtocol *proto = ft->proto;
- useProxy=0;
- if (ft->localName != nullptr) mir_free(ft->localName);
- ft->localName = nullptr;
- ft->wPort = 0;
- if (db_get_b(NULL, proto->m_szModuleName, "UseFileProxy", FALSE)) {
- if (!db_get(NULL, proto->m_szModuleName, "FileProxyHost", &dbv)) {
- mir_strcpy(sb.szHost, dbv.pszVal);
- db_free(&dbv);
- sb.wPort = db_get_w(NULL, proto->m_szModuleName, "FileProxyPort", 0);
- sb.useAuth = FALSE;
- mir_strcpy(sb.szUser, "");
- mir_strcpy(sb.szPassword, "");
- if (db_get_b(NULL, proto->m_szModuleName, "FileProxyAuth", FALSE)) {
- sb.useAuth = TRUE;
- if (!db_get_s(NULL, proto->m_szModuleName, "FileProxyUsername", &dbv)) {
- strncpy(sb.szUser, dbv.pszVal, sizeof(sb.szUser)-1);
- db_free(&dbv);
- }
- if (!db_get_s(NULL, proto->m_szModuleName, "FileProxyPassword", &dbv)) {
- strncpy(sb.szPassword, dbv.pszVal, sizeof(sb.szPassword)-1);
- db_free(&dbv);
- }
- }
- switch (db_get_w(NULL, proto->m_szModuleName, "FileProxyType", 0)) {
- case 0: // forwarding
- useProxy = 1;
- break;
- case 1: // socks4
- s = TlenP2PBindSocks4(&sb, ft);
- useProxy = 2;
- break;
- case 2: // socks5
- s = TlenP2PBindSocks5(&sb, ft);
- useProxy = 2;
- break;
- }
- ft->localName = mir_strdup(sb.szHost);
- ft->wPort = sb.wPort;
- ft->wLocalPort = sb.wPort;
- }
- }
-
- NETLIBBIND nlb = {};
- if (useProxy<2) {
- nlb.pfnNewConnectionV2 = ft->pfnNewConnectionV2;
- nlb.wPort = 0; // Use user-specified incoming port ranges, if available
- nlb.pExtra = proto;
- ft->proto->debugLogA("Calling MS_NETLIB_BINDPORT");
- s = (HNETLIBCONN)Netlib_BindPort(ft->proto->m_hNetlibUser, &nlb);
- ft->proto->debugLogA("listening on %d",s);
- }
- if (useProxy == 0) {
- in.S_un.S_addr = htonl(nlb.dwExternalIP);
- ft->localName = mir_strdup(inet_ntoa(in));
- ft->wPort = nlb.wPort;
- ft->wLocalPort = nlb.wExPort;
- }
- if (s != nullptr) {
-// listenCount++;
- }
- return s;
-}
-
-void TlenP2PStopListening(HANDLE s) {
- if (s != nullptr) {
-// listenCount--;
-// if (listenCount <= 0) {
-// Netlib_CloseHandle(s);
-// }
- }
-}
diff --git a/protocols/Tlen/src/tlen_p2p_old.h b/protocols/Tlen/src/tlen_p2p_old.h
deleted file mode 100644
index 8b5a6ffde4..0000000000
--- a/protocols/Tlen/src/tlen_p2p_old.h
+++ /dev/null
@@ -1,70 +0,0 @@
-/*
-
-Tlen Protocol Plugin for Miranda NG
-Copyright (C) 2004-2007 Piotr Piastucki
-
-This program is free software; you can redistribute it and/or
-modify it under the terms of the GNU General Public License
-as published by the Free Software Foundation; either version 2
-of the License, or (at your option) any later version.
-
-This program 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 General Public License for more details.
-
-You should have received a copy of the GNU General Public License
-along with this program; if not, write to the Free Software
-Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
-
-*/
-
-#include "stdafx.h"
-
-#ifndef _TLEN_P2P_OLD_H_
-#define _TLEN_P2P_OLD_H_
-
-enum {
- TLEN_FILE_PACKET_CONNECTION_REQUEST = 0x01,
- TLEN_FILE_PACKET_CONNECTION_REQUEST_ACK = 0x02,
- TLEN_FILE_PACKET_FILE_LIST = 0x32,
- TLEN_FILE_PACKET_FILE_LIST_ACK = 0x33,
- TLEN_FILE_PACKET_FILE_REQUEST = 0x34,
- TLEN_FILE_PACKET_FILE_DATA = 0x35,
- TLEN_FILE_PACKET_END_OF_FILE = 0x37,
- TLEN_VOICE_PACKET = 0x96
-};
-
-typedef struct {
- unsigned int maxDataLen;
- char *packet;
- DWORD type;
- DWORD len;
-} TLEN_FILE_PACKET;
-
-typedef struct {
- char szHost[256];
- int wPort;
- int useAuth;
- char szUser[256];
- char szPassword[256];
-}SOCKSBIND;
-
-extern void TlenP2PFreeFileTransfer(TLEN_FILE_TRANSFER *ft);
-extern TLEN_FILE_PACKET *TlenP2PPacketCreate(int datalen);
-extern void TlenP2PPacketFree(TLEN_FILE_PACKET *packet);
-extern void TlenP2PPacketSetType(TLEN_FILE_PACKET *packet, DWORD type);
-extern void TlenP2PPacketSetLen(TLEN_FILE_PACKET *packet, DWORD len);
-extern void TlenP2PPacketPackDword(TLEN_FILE_PACKET *packet, DWORD data);
-extern void TlenP2PPacketPackBuffer(TLEN_FILE_PACKET *packet, char *buffer, int len);
-extern int TlenP2PPacketSend(HNETLIBCONN s, TLEN_FILE_PACKET *packet);
-extern TLEN_FILE_PACKET* TlenP2PPacketReceive(HNETLIBCONN s);
-extern void TlenP2PEstablishOutgoingConnection(TLEN_FILE_TRANSFER *ft, BOOL sendAck);
-extern TLEN_FILE_TRANSFER* TlenP2PEstablishIncomingConnection(TlenProtocol *proto, HNETLIBCONN s, TLEN_LIST list, BOOL sendAck);
-extern HNETLIBCONN TlenP2PListen(TLEN_FILE_TRANSFER *ft);
-extern void TlenP2PStopListening(HNETLIBCONN s);
-
-void __cdecl TlenProcessP2P(XmlNode *node, void *userdata);
-
-
-#endif
diff --git a/protocols/Tlen/src/tlen_picture.cpp b/protocols/Tlen/src/tlen_picture.cpp
deleted file mode 100644
index f75e0a42e6..0000000000
--- a/protocols/Tlen/src/tlen_picture.cpp
+++ /dev/null
@@ -1,312 +0,0 @@
-/*
-
-Tlen Protocol Plugin for Miranda NG
-Copyright (C) 2002-2004 Santithorn Bunchua
-Copyright (C) 2004-2009 Piotr Piastucki
-
-This program is free software; you can redistribute it and/or
-modify it under the terms of the GNU General Public License
-as published by the Free Software Foundation; either version 2
-of the License, or (at your option) any later version.
-
-This program 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 General Public License for more details.
-
-You should have received a copy of the GNU General Public License
-along with this program; if not, write to the Free Software
-Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
-
-*/
-
-#include "stdafx.h"
-#include "tlen_list.h"
-#include "tlen_file.h"
-#include "tlen_p2p_old.h"
-
-typedef struct {
- TlenProtocol *proto;
- TLEN_LIST_ITEM *item;
-} TLENPSREQUESTTHREADDATA;
-
-static void LogPictureMessage(TlenProtocol *proto, const char *jid, const char *filename, BOOL isSent)
-{
- char message[1024];
- const char *msg = isSent ? LPGEN("Image sent file://%s") : LPGEN("Image received file://%s");
- mir_snprintf(message, Translate(msg), filename);
- TlenLogMessage(proto, TlenHContactFromJID(proto, jid), isSent ? DBEF_SENT : 0, message);
-}
-
-static void TlenPsPostThread(void *ptr) {
- TLENPSREQUESTTHREADDATA *data = (TLENPSREQUESTTHREADDATA *)ptr;
- TlenProtocol *proto = data->proto;
- TLEN_LIST_ITEM *item = data->item;
- HNETLIBCONN socket = TlenWsConnect(proto, "ps.tlen.pl", 443);
- BOOL bSent = FALSE;
- if (socket != nullptr) {
- char header[512];
- item->ft->s = socket;
- item->ft->hFileEvent = CreateEvent(nullptr, FALSE, FALSE, nullptr);
- int ret = mir_snprintf(header, "<pic auth='%s' t='p' to='%s' size='%d' idt='%s'/>", proto->threadData->username, item->ft->jid, item->ft->fileTotalSize, item->jid);
- TlenWsSend(proto, socket, header, ret);
- ret = WaitForSingleObject(item->ft->hFileEvent, 1000 * 60 * 5);
- if (ret == WAIT_OBJECT_0) {
- FILE *fp = fopen( item->ft->files[0], "rb" );
- if (fp) {
- char fileBuffer[2048];
- ret = mir_snprintf(header, "<pic st='%s' idt='%s'/>", item->ft->iqId, item->jid);
- TlenWsSend(proto, socket, header, ret);
- proto->debugLogA("Sending picture data...");
- for (int i = item->ft->filesSize[0]; i > 0; ) {
- int toread = min(2048, i);
- int readcount = (int)fread(fileBuffer, (size_t)1, (size_t)toread, fp);
- i -= readcount;
- if (readcount > 0)
- TlenWsSend(proto, socket, fileBuffer, readcount);
-
- if (toread != readcount)
- break;
- }
- fclose(fp);
- SleepEx(3000, TRUE);
- bSent = TRUE;
- }
- }
- Netlib_CloseHandle(socket);
- if (bSent) {
- TlenSend(proto, "<message to='%s' idt='%s' rt='%s' pid='1001' type='pic' />", item->ft->jid, item->jid, item->ft->id2);
- LogPictureMessage(proto, item->ft->jid, item->ft->files[0], TRUE);
- }
- TlenP2PFreeFileTransfer(item->ft);
- TlenListRemove(proto, LIST_PICTURE, item->jid);
- }
-
- mir_free(data);
-}
-
-static void TlenPsPost(TlenProtocol *proto, TLEN_LIST_ITEM *item) {
- TLENPSREQUESTTHREADDATA *threadData = (TLENPSREQUESTTHREADDATA *)mir_alloc(sizeof(TLENPSREQUESTTHREADDATA));
- threadData->proto = proto;
- threadData->item = item;
- mir_forkthread(TlenPsPostThread, threadData);
-}
-
-static void TlenPsGetThread(void *ptr) {
- TLENPSREQUESTTHREADDATA *data = (TLENPSREQUESTTHREADDATA *)ptr;
- TlenProtocol *proto = data->proto;
- TLEN_LIST_ITEM *item = data->item;
- FILE *fp;
- fp = fopen( item->ft->files[0], "wb" );
- if (fp) {
- HNETLIBCONN socket = TlenWsConnect(proto, "ps.tlen.pl", 443);
- if (socket != nullptr) {
- XmlState xmlState;
- char header[512];
- char fileBuffer[2048];
- TlenXmlInitState(&xmlState);
- int header_len = mir_snprintf(header, "<pic auth='%s' t='g' to='%s' pid='1001' idt='%s' rt='%s'/>", proto->threadData->username, item->ft->jid, item->jid, item->ft->id2);
- TlenWsSend(proto, socket, header, header_len);
- proto->debugLogA("Reveiving picture data...");
- {
- int totalcount = 0;
- int size = item->ft->filesSize[0];
- BOOL bHeader = TRUE;
- while (TRUE) {
- int readcount = TlenWsRecv(proto, socket, fileBuffer, 2048 - totalcount);
- if (readcount == 0) {
- break;
- }
- totalcount += readcount;
- if (bHeader) {
- char * tagend = (char*)memchr(fileBuffer, '/', totalcount);
- tagend = (char*)memchr(tagend + 1, '>', totalcount - (tagend - fileBuffer) - 1);
- if (tagend != nullptr) {
- int parsed = TlenXmlParse(&xmlState, fileBuffer, tagend - fileBuffer + 1);
- if (parsed == 0) {
- continue;
- }
- bHeader = FALSE;
- totalcount -= parsed;
- memmove(fileBuffer, fileBuffer+parsed, totalcount);
- }
- }
- if (!bHeader) {
- if (totalcount > 0) {
- fwrite(fileBuffer, 1, totalcount, fp);
- size -= totalcount;
- totalcount = 0;
- }
- if (size == 0) {
- break;
- }
- }
- }
- }
- Netlib_CloseHandle(socket);
- proto->debugLogA("Picture received...");
- LogPictureMessage(proto, item->ft->jid, item->ft->files[0], FALSE);
- } else {
- /* cannot connect to ps server */
- }
- fclose(fp);
- } else {
- /* cannot create file */
- }
- TlenP2PFreeFileTransfer(item->ft);
- TlenListRemove(proto, LIST_PICTURE, item->jid);
- mir_free(data);
-}
-
-static void TlenPsGet(TlenProtocol *proto, TLEN_LIST_ITEM *item) {
- TLENPSREQUESTTHREADDATA *threadData = (TLENPSREQUESTTHREADDATA *)mir_alloc(sizeof(TLENPSREQUESTTHREADDATA));
- threadData->proto = proto;
- threadData->item = item;
- mir_forkthread(TlenPsGetThread, threadData);
-}
-
-void TlenProcessPic(XmlNode *node, TlenProtocol *proto) {
- TLEN_LIST_ITEM *item = nullptr;
- char *crc, *crc_c, *idt, *size, *from, *fromRaw, *rt;
- from = TlenXmlGetAttrValue(node, "from");
- fromRaw = TlenLoginFromJID(from);
- idt = TlenXmlGetAttrValue(node, "idt");
- size = TlenXmlGetAttrValue(node, "size");
- crc_c = TlenXmlGetAttrValue(node, "crc_c");
- crc = TlenXmlGetAttrValue(node, "crc");
- rt = TlenXmlGetAttrValue(node, "rt");
- if (idt != nullptr) {
- item = TlenListGetItemPtr(proto, LIST_PICTURE, idt);
- }
- if (item != nullptr) {
- if (!mir_strcmp(from, "ps")) {
- char *st = TlenXmlGetAttrValue(node, "st");
- if (st != nullptr) {
- item->ft->iqId = mir_strdup(st);
- item->ft->id2 = mir_strdup(rt);
- if (item->ft->hFileEvent != nullptr) {
- SetEvent(item->ft->hFileEvent);
- item->ft->hFileEvent = nullptr;
- }
- }
- } else if (!mir_strcmp(item->ft->jid, fromRaw)) {
- if (crc_c != nullptr) {
- if (!mir_strcmp(crc_c, "n")) {
- /* crc_c = n, picture transfer accepted */
- TlenPsPost(proto, item);
- } else if (!mir_strcmp(crc_c, "f")) {
- /* crc_c = f, picture cached, no need to transfer again */
- LogPictureMessage(proto, item->ft->jid, item->ft->files[0], TRUE);
- TlenP2PFreeFileTransfer(item->ft);
- TlenListRemove(proto, LIST_PICTURE, idt);
- }
- } else if (rt != nullptr) {
- item->ft->id2 = mir_strdup(rt);
- TlenPsGet(proto, item);
- }
- }
- } else if (crc != nullptr) {
- BOOL bAccept = proto->tlenOptions.imagePolicy == TLEN_IMAGES_ACCEPT_ALL || (proto->tlenOptions.imagePolicy == TLEN_IMAGES_IGNORE_NIR && IsAuthorized(proto, from));
- if (bAccept) {
- FILE* fp;
- char fileName[MAX_PATH];
- char *ext = TlenXmlGetAttrValue(node, "ext");
- char *tmpPath = Utils_ReplaceVars( "%miranda_userdata%" );
- int tPathLen = mir_snprintf(fileName, "%s\\Images\\Tlen", tmpPath);
- long oldSize = 0, lSize = atol(size);
- DWORD dwAttributes = GetFileAttributesA( fileName );
- if ( dwAttributes == 0xffffffff || ( dwAttributes & FILE_ATTRIBUTE_DIRECTORY ) == 0 )
- CreateDirectoryTree(fileName);
-
- mir_free(tmpPath);
- fileName[ tPathLen++ ] = '\\';
- mir_snprintf( fileName + tPathLen, _countof(fileName) - tPathLen, "%s.%s", crc, ext );
- fp = fopen( fileName, "rb" );
- if (fp) {
- fseek(fp, 0, SEEK_END);
- oldSize = ftell(fp);
- fclose(fp);
- }
- if (oldSize != lSize) {
- item = TlenListAdd(proto, LIST_PICTURE, idt);
- item->ft = TlenFileCreateFT(proto, from);
- item->ft->files = (char **) mir_alloc(sizeof(char *));
- item->ft->filesSize = (long *) mir_alloc(sizeof(long));
- item->ft->files[0] = mir_strdup(fileName);
- item->ft->filesSize[0] = lSize;
- item->ft->fileTotalSize = item->ft->filesSize[0];
- TlenSend(proto, "<message type='pic' to='%s' crc_c='n' idt='%s'/>", from, idt);
- } else {
- TlenSend(proto, "<message type='pic' to='%s' crc_c='f' idt='%s'/>", from, idt);
- LogPictureMessage(proto, from, fileName, FALSE);
- }
- }
- }
- mir_free(fromRaw);
-}
-
-BOOL SendPicture(TlenProtocol *proto, MCONTACT hContact) {
- DBVARIANT dbv;
- if (!db_get(hContact, proto->m_szModuleName, "jid", &dbv)) {
- char *jid = dbv.pszVal;
-
- wchar_t tszFilter[512], tszFileName[MAX_PATH];
- Bitmap_GetFilter(tszFilter, _countof(tszFilter));
- tszFileName[0] = '\0';
-
- OPENFILENAME ofn = {0};
- ofn.lStructSize = OPENFILENAME_SIZE_VERSION_400;
- ofn.lpstrFilter = tszFilter;
- ofn.lpstrCustomFilter = nullptr;
- ofn.lpstrFile = tszFileName;
- ofn.nMaxFile = _MAX_PATH;
- ofn.Flags = OFN_FILEMUSTEXIST;
- if (GetOpenFileName(&ofn)) {
- long size;
- FILE* fp = _wfopen(tszFileName, L"rb");
- if (fp) {
- fseek(fp, 0, SEEK_END);
- size = ftell(fp);
- if (size > 0 && size < 256*1024) {
- TLEN_LIST_ITEM *item;
- mir_sha1_ctx sha;
- DWORD digest[5];
- int i;
- char idStr[10];
- char fileBuffer[2048];
- int id = TlenSerialNext(proto);
- T2Utf szFileName(tszFileName);
- mir_snprintf(idStr, "%d", id);
- item = TlenListAdd(proto, LIST_PICTURE, idStr);
- item->ft = TlenFileCreateFT(proto, jid);
- item->ft->files = (char **) mir_alloc(sizeof(char *));
- item->ft->filesSize = (long *) mir_alloc(sizeof(long));
- item->ft->files[0] = szFileName;
- item->ft->filesSize[0] = size;
- item->ft->fileTotalSize = size;
- fseek(fp, 0, SEEK_SET);
- mir_sha1_init( &sha );
- for (i = item->ft->filesSize[0]; i > 0; ) {
- int toread = min(2048, i);
- int readcount = (int)fread(fileBuffer, (size_t)1, (size_t)toread, fp);
- i -= readcount;
- if (readcount > 0) {
- mir_sha1_append( &sha, (BYTE* )fileBuffer, readcount);
- }
- if (toread != readcount) {
- break;
- }
- }
- mir_sha1_finish( &sha, (BYTE* )digest );
- TlenSend(proto, "<message type='pic' to='%s' crc='%08x%08x%08x%08x%08x' idt='%s' size='%d' ext='%s'/>", jid,
- (int)htonl(digest[0]), (int)htonl(digest[1]), (int)htonl(digest[2]), (int)htonl(digest[3]), (int)htonl(digest[4]), idStr, item->ft->filesSize[0], "jpg");
- } else {
- /* file too big */
- }
- fclose(fp);
- }
- }
- db_free(&dbv);
- }
- return FALSE;
-}
diff --git a/protocols/Tlen/src/tlen_picture.h b/protocols/Tlen/src/tlen_picture.h
deleted file mode 100644
index 07e608c90c..0000000000
--- a/protocols/Tlen/src/tlen_picture.h
+++ /dev/null
@@ -1,33 +0,0 @@
-/*
-
-Jabber Protocol Plugin for Miranda IM
-Tlen Protocol Plugin for Miranda NG
-Copyright (C) 2002-2004 Santithorn Bunchua
-Copyright (C) 2004-2009 Piotr Piastucki
-
-This program is free software; you can redistribute it and/or
-modify it under the terms of the GNU General Public License
-as published by the Free Software Foundation; either version 2
-of the License, or (at your option) any later version.
-
-This program 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 General Public License for more details.
-
-You should have received a copy of the GNU General Public License
-along with this program; if not, write to the Free Software
-Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
-
-*/
-#ifndef _TLEN_PICTURE_H
-#define _TLEN_PICTURE_H
-
-#include <windows.h>
-#include "stdafx.h"
-
-void TlenProcessPic(XmlNode *node, TlenProtocol *proto);
-BOOL SendPicture(TlenProtocol *, MCONTACT hContact);
-
-#endif /* _TLEN_PICTURE_H */
-
diff --git a/protocols/Tlen/src/tlen_presence.cpp b/protocols/Tlen/src/tlen_presence.cpp
deleted file mode 100644
index 3e12761582..0000000000
--- a/protocols/Tlen/src/tlen_presence.cpp
+++ /dev/null
@@ -1,341 +0,0 @@
-/*
-
-Tlen Protocol Plugin for Miranda NG
-Copyright (C) 2002-2004 Santithorn Bunchua
-Copyright (C) 2004-2009 Piotr Piastucki
-
-This program is free software; you can redistribute it and/or
-modify it under the terms of the GNU General Public License
-as published by the Free Software Foundation; either version 2
-of the License, or (at your option) any later version.
-
-This program 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 General Public License for more details.
-
-You should have received a copy of the GNU General Public License
-along with this program; if not, write to the Free Software
-Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
-
-*/
-
-#include "stdafx.h"
-#include "tlen_list.h"
-#include "tlen_avatar.h"
-
-void TlenProcessPresence(XmlNode *node, TlenProtocol *proto)
-{
- MCONTACT hContact;
- XmlNode *showNode, *statusNode;
- TLEN_LIST_ITEM *item;
- char *from, *type, *nick, *show;
- int status, laststatus = ID_STATUS_OFFLINE;
- char *p;
-
- if ((from=TlenXmlGetAttrValue(node, "from")) != nullptr) {
- if (TlenListExist(proto, LIST_CHATROOM, from)); //TlenGroupchatProcessPresence(node, userdata);
-
- else {
- type = TlenXmlGetAttrValue(node, "type");
- item = TlenListGetItemPtr(proto, LIST_ROSTER, from);
- if (item != nullptr) {
- if (proto->tlenOptions.enableAvatars) {
- TlenProcessPresenceAvatar(proto, node, item);
- }
- }
- if (type == nullptr || (!mir_strcmp(type, "available"))) {
- if ((nick=TlenLocalNickFromJID(from)) != nullptr) {
- if ((hContact=TlenHContactFromJID(proto, from)) == NULL)
- hContact = TlenDBCreateContact(proto, from, nick, FALSE);
- if (!TlenListExist(proto, LIST_ROSTER, from)) {
- proto->debugLogA("Receive presence online from %s (who is not in my roster)", from);
- TlenListAdd(proto, LIST_ROSTER, from);
- }
- status = ID_STATUS_ONLINE;
- if ((showNode=TlenXmlGetChild(node, "show")) != nullptr) {
- if ((show=showNode->text) != nullptr) {
- if (!mir_strcmp(show, "away")) status = ID_STATUS_AWAY;
- else if (!mir_strcmp(show, "xa")) status = ID_STATUS_NA;
- else if (!mir_strcmp(show, "dnd")) status = ID_STATUS_DND;
- else if (!mir_strcmp(show, "chat")) status = ID_STATUS_FREECHAT;
- else if (!mir_strcmp(show, "unavailable")) {
- // Always show invisible (on old Tlen client) as invisible (not offline)
- status = ID_STATUS_OFFLINE;
- }
- }
- }
-
- statusNode = TlenXmlGetChild(node, "status");
- if (statusNode)
- p = TlenTextDecode(statusNode->text);
- else
- p = nullptr;
- TlenListAddResource(proto, LIST_ROSTER, from, status, statusNode?p:nullptr);
- if (p != nullptr && *p) {
- char* statusMsg_utf8 = mir_utf8encode(p);
- db_set_utf(hContact, "CList", "StatusMsg", statusMsg_utf8);
- mir_free(statusMsg_utf8);
- mir_free(p);
- } else {
- db_unset(hContact, "CList", "StatusMsg");
- }
- // Determine status to show for the contact and request version information
- if (item != nullptr) {
- laststatus = item->status;
- item->status = status;
- }
- if (strchr(from, '@') != nullptr || db_get_b(NULL, proto->m_szModuleName, "ShowTransport", TRUE) == TRUE) {
- if (db_get_w(hContact, proto->m_szModuleName, "Status", ID_STATUS_OFFLINE) != status)
- db_set_w(hContact, proto->m_szModuleName, "Status", (WORD) status);
- }
- if (item != nullptr) {
- if (!item->infoRequested) {
- int iqId = TlenSerialNext(proto);
- item->infoRequested = TRUE;
- TlenSend( proto, "<iq type='get' id='" TLEN_IQID "%d'><query xmlns='jabber:iq:info' to='%s'></query></iq>", iqId, from);
- }
- if (proto->tlenOptions.enableVersion && !item->versionRequested) {
- item->versionRequested = TRUE;
- if (proto->m_iStatus != ID_STATUS_INVISIBLE) {
- TlenSend( proto, "<message to='%s' type='iq'><iq type='get'><query xmlns='jabber:iq:version'/></iq></message>", from );
- }
- }
- }
- proto->debugLogA("%s (%s) online, set contact status to %d", nick, from, status);
- mir_free(nick);
- }
- }
- else if (!mir_strcmp(type, "unavailable")) {
- if (!TlenListExist(proto, LIST_ROSTER, from)) {
- proto->debugLogA("Receive presence offline from %s (who is not in my roster)", from);
- TlenListAdd(proto, LIST_ROSTER, from);
- }
- else {
- TlenListRemoveResource(proto, LIST_ROSTER, from);
- }
- status = ID_STATUS_OFFLINE;
- statusNode = TlenXmlGetChild(node, "status");
- if (statusNode) {
- if (proto->tlenOptions.offlineAsInvisible) {
- status = ID_STATUS_INVISIBLE;
- }
- p = TlenTextDecode(statusNode->text);
- }
- else
- p = nullptr;
- TlenListAddResource(proto, LIST_ROSTER, from, status, p);
- if ((hContact=TlenHContactFromJID(proto, from)) != NULL) {
- if (p != nullptr && *p) {
- char* statusMsg_utf8 = mir_utf8encode(p);
- db_set_utf(hContact, "CList", "StatusMsg", statusMsg_utf8);
- mir_free(statusMsg_utf8);
- mir_free(p);
- } else {
- db_unset(hContact, "CList", "StatusMsg");
- }
- }
- if ((item=TlenListGetItemPtr(proto, LIST_ROSTER, from)) != nullptr) {
- // Determine status to show for the contact based on the remaining resources
- item->status = status;
- item->versionRequested = FALSE;
- item->infoRequested = FALSE;
- }
- if ((hContact=TlenHContactFromJID(proto, from)) != NULL) {
- if (strchr(from, '@') != nullptr || db_get_b(NULL, proto->m_szModuleName, "ShowTransport", TRUE) == TRUE) {
- if (db_get_w(hContact, proto->m_szModuleName, "Status", ID_STATUS_OFFLINE) != status)
- db_set_w(hContact, proto->m_szModuleName, "Status", (WORD) status);
- }
- if (item != nullptr && item->isTyping) {
- item->isTyping = FALSE;
- CallService(MS_PROTO_CONTACTISTYPING, hContact, PROTOTYPE_CONTACTTYPING_OFF);
- }
- proto->debugLogA("%s offline, set contact status to %d", from, status);
- }
- }
- else if (!mir_strcmp(type, "subscribe")) {
- if (strchr(from, '@') == nullptr) {
- // automatically send authorization allowed to agent/transport
- TlenSend(proto, "<presence to='%s' type='subscribed'/>", from);
- }
- else if ((nick=TlenNickFromJID(from)) != nullptr) {
- proto->debugLogA("%s (%s) requests authorization", nick, from);
- TlenDBAddAuthRequest(proto, from, nick);
- mir_free(nick);
- }
- }
- else if (!mir_strcmp(type, "subscribed")) {
- if ((item=TlenListGetItemPtr(proto, LIST_ROSTER, from)) != nullptr) {
- if (item->subscription == SUB_FROM) item->subscription = SUB_BOTH;
- else if (item->subscription == SUB_NONE) {
- item->subscription = SUB_TO;
- }
- }
- }
- }
- }
-}
-
-/* change status and status msg on own contact on contact list (if present) */
-void setOwnStatusOnCList(TlenProtocol *proto, int status, char *statusMsg)
-{
- ptrA ownJid(db_get_sa(NULL, proto->m_szModuleName, "jid"));
- MCONTACT hContact = TlenHContactFromJID(proto, ownJid);
- if(hContact){
- if (db_get_w(hContact, proto->m_szModuleName, "Status", ID_STATUS_OFFLINE) != status)
- db_set_w(hContact, proto->m_szModuleName, "Status", (WORD)status);
- if (statusMsg != nullptr && *statusMsg) {
- char* statusMsg_utf8 = mir_utf8encode(statusMsg);
- db_set_utf(hContact, "CList", "StatusMsg", statusMsg_utf8);
- mir_free(statusMsg_utf8);
- } else {
- db_unset(hContact, "CList", "StatusMsg");
- }
- }
-}
-
-static void TlenSendPresenceTo(TlenProtocol *proto, int status, char*)
-{
- char *showBody, *statusMsg, *presenceType;
- char *ptr = nullptr;
-
- if (!proto->isOnline) return;
-
- // Send <presence/> update for status (we won't handle ID_STATUS_OFFLINE here)
- // Note: tlenModeMsg is already encoded using TlenTextEncode()
- mir_cslock lck(proto->modeMsgMutex);
-
- showBody = nullptr;
- statusMsg = nullptr;
- presenceType = nullptr;
- switch (status) {
- case ID_STATUS_ONLINE:
- showBody = "available";
- statusMsg = proto->modeMsgs.szOnline;
- break;
- case ID_STATUS_AWAY:
- case ID_STATUS_ONTHEPHONE:
- case ID_STATUS_OUTTOLUNCH:
- showBody = "away";
- statusMsg = proto->modeMsgs.szAway;
- break;
- case ID_STATUS_NA:
- showBody = "xa";
- statusMsg = proto->modeMsgs.szNa;
- break;
- case ID_STATUS_DND:
- case ID_STATUS_OCCUPIED:
- showBody = "dnd";
- statusMsg = proto->modeMsgs.szDnd;
- break;
- case ID_STATUS_FREECHAT:
- showBody = "chat";
- statusMsg = proto->modeMsgs.szFreechat;
- break;
- case ID_STATUS_INVISIBLE:
- presenceType = "invisible";
- statusMsg = proto->modeMsgs.szInvisible;
- break;
- case ID_STATUS_OFFLINE:
- presenceType = "unavailable";
- if (db_get_b(NULL, proto->m_szModuleName, "LeaveOfflineMessage", FALSE)) {
- int offlineMessageOption = db_get_w(NULL, proto->m_szModuleName, "OfflineMessageOption", 0);
- if (offlineMessageOption == 0) {
- switch (proto->m_iStatus) {
- case ID_STATUS_ONLINE:
- ptr = mir_strdup(proto->modeMsgs.szOnline);
- break;
- case ID_STATUS_AWAY:
- case ID_STATUS_ONTHEPHONE:
- case ID_STATUS_OUTTOLUNCH:
- ptr = mir_strdup(proto->modeMsgs.szAway);
- break;
- case ID_STATUS_NA:
- ptr = mir_strdup(proto->modeMsgs.szNa);
- break;
- case ID_STATUS_DND:
- case ID_STATUS_OCCUPIED:
- ptr = mir_strdup(proto->modeMsgs.szDnd);
- break;
- case ID_STATUS_FREECHAT:
- ptr = mir_strdup(proto->modeMsgs.szFreechat);
- break;
- case ID_STATUS_INVISIBLE:
- ptr = mir_strdup(proto->modeMsgs.szInvisible);
- break;
- }
- } else if (offlineMessageOption == 99) {
-
- } else if (offlineMessageOption < 7) {
- DBVARIANT dbv;
- const char *statusNames[] = {"OnDefault", "AwayDefault", "NaDefault", "DndDefault", "FreeChatDefault", "InvDefault"};
- if (!db_get(NULL, "SRAway", statusNames[offlineMessageOption-1], &dbv)) {
- int i;
- char substituteStr[128];
- ptr = mir_strdup(dbv.pszVal);
- db_free(&dbv);
- for (i=0;ptr[i];i++) {
- if (ptr[i] != '%') continue;
- if (!_strnicmp(ptr+i,"%time%",6))
- GetTimeFormatA(LOCALE_USER_DEFAULT,TIME_NOSECONDS,nullptr,nullptr,substituteStr,sizeof(substituteStr));
- else if (!_strnicmp(ptr+i,"%date%",6))
- GetDateFormatA(LOCALE_USER_DEFAULT,DATE_SHORTDATE,nullptr,nullptr,substituteStr,sizeof(substituteStr));
- else continue;
- if (mir_strlen(substituteStr)>6) ptr=(char*)mir_realloc(ptr,mir_strlen(ptr)+1+mir_strlen(substituteStr)-6);
- memmove(ptr+i+mir_strlen(substituteStr),ptr+i+6,mir_strlen(ptr)-i-5);
- memcpy(ptr+i,substituteStr,mir_strlen(substituteStr));
- }
- }
- }
- }
- mir_free(statusMsg);
- statusMsg = ptr;
- break;
- default:
- // Should not reach here
- break;
- }
- proto->m_iStatus = status;
- if (presenceType) {
- if (statusMsg != nullptr && *statusMsg)
- TlenSend(proto, "<presence type='%s'><status>%s</status></presence>", presenceType, ptrA(TlenTextEncode(statusMsg)));
- else
- TlenSend(proto, "<presence type='%s'></presence>", presenceType);
- } else {
- if (statusMsg != nullptr && *statusMsg)
- TlenSend(proto, "<presence><show>%s</show><status>%s</status></presence>", showBody, ptrA(TlenTextEncode(statusMsg)));
- else
- TlenSend(proto, "<presence><show>%s</show></presence>", showBody);
- }
-
- setOwnStatusOnCList(proto, proto->m_iStatus, statusMsg);
-}
-
-
-
-void TlenSendPresence(TlenProtocol *proto, int statusIn)
-{
- int statusOut;
- switch (statusIn) {
- case ID_STATUS_ONLINE:
- case ID_STATUS_OFFLINE:
- case ID_STATUS_NA:
- case ID_STATUS_FREECHAT:
- case ID_STATUS_INVISIBLE:
- statusOut = statusIn;
- break;
- case ID_STATUS_AWAY:
- case ID_STATUS_ONTHEPHONE:
- case ID_STATUS_OUTTOLUNCH:
- default:
- statusOut = ID_STATUS_AWAY;
- break;
- case ID_STATUS_DND:
- case ID_STATUS_OCCUPIED:
- statusOut = ID_STATUS_DND;
- break;
- }
- TlenSendPresenceTo(proto, statusOut, nullptr);
-}
-
-
diff --git a/protocols/Tlen/src/tlen_presence.h b/protocols/Tlen/src/tlen_presence.h
deleted file mode 100644
index caf92ddab5..0000000000
--- a/protocols/Tlen/src/tlen_presence.h
+++ /dev/null
@@ -1,29 +0,0 @@
-/*
-
-Jabber Protocol Plugin for Miranda IM
-Tlen Protocol Plugin for Miranda NG
-Copyright (C) 2002-2004 Santithorn Bunchua
-Copyright (C) 2004-2009 Piotr Piastucki
-
-This program is free software; you can redistribute it and/or
-modify it under the terms of the GNU General Public License
-as published by the Free Software Foundation; either version 2
-of the License, or (at your option) any later version.
-
-This program 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 General Public License for more details.
-
-You should have received a copy of the GNU General Public License
-along with this program; if not, write to the Free Software
-Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
-
-*/
-#ifndef _TLEN_PRESENCE_H
-#define _TLEN_PRESENCE_H
-
-void TlenProcessPresence(XmlNode *node, TlenProtocol *proto);
-
-#endif /* _TLEN_PRESENCE_H */
-
diff --git a/protocols/Tlen/src/tlen_svc.cpp b/protocols/Tlen/src/tlen_svc.cpp
deleted file mode 100644
index 1366155ccb..0000000000
--- a/protocols/Tlen/src/tlen_svc.cpp
+++ /dev/null
@@ -1,1208 +0,0 @@
-/*
-
-Jabber Protocol Plugin for Miranda IM
-Tlen Protocol Plugin for Miranda NG
-Copyright (C) 2002-2004 Santithorn Bunchua
-Copyright (C) 2004-2007 Piotr Piastucki
-
-This program is free software; you can redistribute it and/or
-modify it under the terms of the GNU General Public License
-as published by the Free Software Foundation; either version 2
-of the License, or (at your option) any later version.
-
-This program 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 General Public License for more details.
-
-You should have received a copy of the GNU General Public License
-along with this program; if not, write to the Free Software
-Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
-
-*/
-
-#include "stdafx.h"
-#include <io.h>
-#include <fcntl.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include "resource.h"
-#include "tlen_list.h"
-#include "tlen_iq.h"
-#include "tlen_p2p_old.h"
-#include "tlen_avatar.h"
-#include "tlen_file.h"
-
-DWORD_PTR TlenProtocol::GetCaps(int type, MCONTACT)
-{
- switch (type) {
- case PFLAGNUM_1:
- return PF1_IM | PF1_AUTHREQ | PF1_SERVERCLIST | PF1_MODEMSG | PF1_BASICSEARCH | PF1_SEARCHBYEMAIL | PF1_EXTSEARCH | PF1_EXTSEARCHUI | PF1_SEARCHBYNAME | PF1_FILE;//|PF1_VISLIST|PF1_INVISLIST;
- case PFLAGNUM_2:
- return PF2_ONLINE | PF2_INVISIBLE | PF2_SHORTAWAY | PF2_LONGAWAY | PF2_HEAVYDND | PF2_FREECHAT;
- case PFLAGNUM_3:
- return PF2_ONLINE | PF2_INVISIBLE | PF2_SHORTAWAY | PF2_LONGAWAY | PF2_HEAVYDND | PF2_FREECHAT;
- case PFLAGNUM_4:
- return PF4_FORCEAUTH | PF4_NOCUSTOMAUTH | PF4_SUPPORTTYPING | PF4_AVATARS | PF4_IMSENDOFFLINE | PF4_OFFLINEFILES;
- case PFLAG_UNIQUEIDTEXT:
- return (INT_PTR)Translate("Tlen login");
- case PFLAG_UNIQUEIDSETTING:
- return (INT_PTR) "jid";
- default:
- return 0;
- }
-}
-
-INT_PTR TlenProtocol::GetName(WPARAM wParam, LPARAM lParam)
-{
- strncpy((char*)lParam, m_szModuleName, wParam);
- return 0;
-}
-
-int TlenRunSearch(TlenProtocol *proto) {
- int iqId = 0;
- if (!proto->isOnline) return 0;
- if (proto->searchQuery != nullptr && proto->searchIndex < 10) {
- iqId = proto->searchID;
- TlenIqAdd(proto, iqId, IQ_PROC_GETSEARCH, TlenIqResultSearch);
- if (proto->searchIndex == 0) {
- TlenSend(proto, "<iq type='get' id='" TLEN_IQID "%d' to='tuba'><query xmlns='jabber:iq:search'>%s</query></iq>", iqId, proto->searchQuery);
- }
- else {
- TlenSend(proto, "<iq type='get' id='" TLEN_IQID "%d' to='tuba'><query xmlns='jabber:iq:search'>%s<f>%d</f></query></iq>", iqId, proto->searchQuery, proto->searchIndex * TLEN_MAX_SEARCH_RESULTS_PER_PAGE);
- }
- proto->searchIndex++;
- }
- return iqId;
-}
-
-void TlenResetSearchQuery(TlenProtocol *proto) {
- if (proto->searchQuery != nullptr) {
- mir_free(proto->searchQuery);
- proto->searchQuery = nullptr;
- }
- proto->searchQueryLen = 0;
- proto->searchIndex = 0;
- proto->searchID = TlenSerialNext(proto);
-}
-
-HANDLE TlenProtocol::SearchBasic(const wchar_t* id)
-{
- int iqId = 0;
- if (!isOnline) return nullptr;
- if (id == nullptr) return nullptr;
- char* id_A = mir_u2a(id);
- char *jid = TlenTextEncode(id_A);
- if (jid != nullptr) {
- searchJID = mir_strdup(id_A);
- TlenResetSearchQuery(this);
- TlenStringAppend(&searchQuery, &searchQueryLen, "<i>%s</i>", jid);
- iqId = TlenRunSearch(this);
- mir_free(jid);
- }
- mir_free(id_A);
- return (HANDLE)iqId;
-}
-
-HANDLE TlenProtocol::SearchByEmail(const wchar_t* email)
-{
- int iqId = 0;
-
- if (!isOnline) return nullptr;
- if (email == nullptr) return nullptr;
-
- char* email_A = mir_u2a(email);
- char *emailEnc = TlenTextEncode(email_A);
- if (emailEnc != nullptr) {
- TlenResetSearchQuery(this);
- TlenStringAppend(&searchQuery, &searchQueryLen, "<email>%s</email>", emailEnc);
- iqId = TlenRunSearch(this);
- mir_free(emailEnc);
- }
- mir_free(email_A);
- return (HANDLE)iqId;
-}
-
-HANDLE TlenProtocol::SearchByName(const wchar_t* nickT, const wchar_t* firstNameT, const wchar_t* lastNameT)
-{
- if (!isOnline) return nullptr;
-
- char *nick = mir_u2a(nickT);
- char *firstName = mir_u2a(firstNameT);
- char *lastName = mir_u2a(lastNameT);
-
- char *p;
- int iqId = 0;
-
- TlenResetSearchQuery(this);
-
- if (nick != nullptr && nick[0] != '\0') {
- if ((p = TlenTextEncode(nick)) != nullptr) {
- TlenStringAppend(&searchQuery, &searchQueryLen, "<nick>%s</nick>", p);
- mir_free(p);
- }
- }
- if (firstName != nullptr && firstName[0] != '\0') {
- if ((p = TlenTextEncode(firstName)) != nullptr) {
- TlenStringAppend(&searchQuery, &searchQueryLen, "<first>%s</first>", p);
- mir_free(p);
- }
- }
- if (lastName != nullptr && lastName[0] != '\0') {
- if ((p = TlenTextEncode(lastName)) != nullptr) {
- TlenStringAppend(&searchQuery, &searchQueryLen, "<last>%s</last>", p);
- mir_free(p);
- }
- }
-
- iqId = TlenRunSearch(this);
- return (HANDLE)iqId;
-}
-
-HWND TlenProtocol::CreateExtendedSearchUI(HWND owner)
-{
- return (HWND)CreateDialog(hInst, MAKEINTRESOURCE(IDD_ADVSEARCH), owner, TlenAdvSearchDlgProc);
-}
-
-HWND TlenProtocol::SearchAdvanced(HWND owner)
-{
- if (!isOnline) return nullptr;
-
- TlenResetSearchQuery(this);
- int iqId = TlenSerialNext(this);
- if ((searchQuery = TlenAdvSearchCreateQuery(owner, iqId)) != nullptr) {
- iqId = TlenRunSearch(this);
- }
- return (HWND)iqId;
-}
-
-
-static MCONTACT AddToListByJID(TlenProtocol *proto, const char *newJid, DWORD flags)
-{
- MCONTACT hContact = TlenHContactFromJID(proto, newJid);
- if (hContact == NULL) {
- // not already there: add
- char *jid = mir_strdup(newJid); _strlwr(jid);
- hContact = db_add_contact();
- Proto_AddToContact(hContact, proto->m_szModuleName);
- db_set_s(hContact, proto->m_szModuleName, "jid", jid);
- char *nick = TlenNickFromJID(newJid);
- if (nick == nullptr)
- nick = mir_strdup(newJid);
- db_set_s(hContact, "CList", "MyHandle", nick);
- mir_free(nick);
- mir_free(jid);
-
- // Note that by removing or disable the "NotOnList" will trigger
- // the plugin to add a particular contact to the roster list.
- // See DBSettingChanged hook at the bottom part of this source file.
- // But the add module will delete "NotOnList". So we will not do it here.
- // Also because we need "MyHandle" and "Group" info, which are set after
- // PS_ADDTOLIST is called but before the add dialog issue deletion of
- // "NotOnList".
- // If temporary add, "NotOnList" won't be deleted, and that's expected.
- db_set_b(hContact, "CList", "NotOnList", 1);
- if (flags & PALF_TEMPORARY)
- db_set_b(hContact, "CList", "Hidden", 1);
- }
- else {
- // already exist
- // Set up a dummy "NotOnList" when adding permanently only
- if (!(flags&PALF_TEMPORARY))
- db_set_b(hContact, "CList", "NotOnList", 1);
- }
-
- return hContact;
-}
-
-MCONTACT TlenProtocol::AddToList(int flags, PROTOSEARCHRESULT *psr)
-{
- TLEN_SEARCH_RESULT *jsr = (TLEN_SEARCH_RESULT*)psr;
- if (jsr->hdr.cbSize != sizeof(TLEN_SEARCH_RESULT))
- return NULL;
- return AddToListByJID(this, jsr->jid, flags);// wParam is flag e.g. PALF_TEMPORARY
-}
-
-MCONTACT TlenProtocol::AddToListByEvent(int flags, int, MEVENT hDbEvent)
-{
- DBEVENTINFO dbei = {};
- if ((dbei.cbBlob = db_event_getBlobSize(hDbEvent)) == (DWORD)(-1))
- return NULL;
- if ((dbei.pBlob = (PBYTE)mir_alloc(dbei.cbBlob)) == nullptr)
- return NULL;
- if (db_event_get(hDbEvent, &dbei)) {
- mir_free(dbei.pBlob);
- return NULL;
- }
- if (mir_strcmp(dbei.szModule, m_szModuleName)) {
- mir_free(dbei.pBlob);
- return NULL;
- }
-
- /*
- // EVENTTYPE_CONTACTS is when adding from when we receive contact list (not used in Tlen)
- // EVENTTYPE_ADDED is when adding from when we receive "You are added" (also not used in Tlen)
- // Tlen will only handle the case of EVENTTYPE_AUTHREQUEST
- // EVENTTYPE_AUTHREQUEST is when adding from the authorization request dialog
- */
-
- if (dbei.eventType != EVENTTYPE_AUTHREQUEST) {
- mir_free(dbei.pBlob);
- return NULL;
- }
-
- DB_AUTH_BLOB blob(dbei.pBlob);
- MCONTACT hContact = (MCONTACT)AddToListByJID(this, blob.get_email(), flags);
- mir_free(dbei.pBlob);
- return hContact;
-}
-
-int TlenProtocol::Authorize(MEVENT hDbEvent)
-{
- if (!isOnline)
- return 1;
-
- DBEVENTINFO dbei = {};
- if ((dbei.cbBlob = db_event_getBlobSize(hDbEvent)) == (DWORD)-1)
- return 1;
- if ((dbei.pBlob = (PBYTE)mir_alloc(dbei.cbBlob)) == nullptr)
- return 1;
- if (db_event_get(hDbEvent, &dbei)) {
- mir_free(dbei.pBlob);
- return 1;
- }
- if (dbei.eventType != EVENTTYPE_AUTHREQUEST) {
- mir_free(dbei.pBlob);
- return 1;
- }
- if (mir_strcmp(dbei.szModule, m_szModuleName)) {
- mir_free(dbei.pBlob);
- return 1;
- }
-
- DB_AUTH_BLOB blob(dbei.pBlob);
- TlenSend(this, "<presence to='%s' type='subscribed'/>", blob.get_email());
-
- // Automatically add this user to my roster if option is enabled
- if (db_get_b(NULL, m_szModuleName, "AutoAdd", TRUE) == TRUE) {
- MCONTACT hContact;
- TLEN_LIST_ITEM *item = TlenListGetItemPtr(this, LIST_ROSTER, blob.get_email());
-
- if (item == nullptr || (item->subscription != SUB_BOTH && item->subscription != SUB_TO)) {
- debugLogA("Try adding contact automatically jid=%s", blob.get_email());
- if ((hContact = AddToListByJID(this, blob.get_email(), 0)) != NULL) {
- // Trigger actual add by removing the "NotOnList" added by AddToListByJID()
- // See AddToListByJID() and TlenDbSettingChanged().
- db_unset(hContact, "CList", "NotOnList");
- }
- }
- }
-
- mir_free(dbei.pBlob);
- return 0;
-}
-
-int TlenProtocol::AuthDeny(MEVENT hDbEvent, const wchar_t*)
-{
- if (!isOnline)
- return 1;
-
- DBEVENTINFO dbei = {};
- if ((dbei.cbBlob = db_event_getBlobSize(hDbEvent)) == (DWORD)(-1))
- return 1;
- if ((dbei.pBlob = (PBYTE)mir_alloc(dbei.cbBlob)) == nullptr)
- return 1;
- if (db_event_get(hDbEvent, &dbei)) {
- mir_free(dbei.pBlob);
- return 1;
- }
- if (dbei.eventType != EVENTTYPE_AUTHREQUEST) {
- mir_free(dbei.pBlob);
- return 1;
- }
- if (mir_strcmp(dbei.szModule, m_szModuleName)) {
- mir_free(dbei.pBlob);
- return 1;
- }
-
- DB_AUTH_BLOB blob(dbei.pBlob);
- TlenSend(this, "<presence to='%s' type='unsubscribed'/>", blob.get_email());
- TlenSend(this, "<iq type='set'><query xmlns='jabber:iq:roster'><item jid='%s' subscription='remove'/></query></iq>", blob.get_email());
- mir_free(dbei.pBlob);
- return 0;
-}
-
-static void TlenConnect(TlenProtocol *proto, int initialStatus)
-{
- if (!proto->isConnected) {
- ThreadData *thread = (ThreadData *)mir_alloc(sizeof(ThreadData));
- memset(thread, 0, sizeof(ThreadData));
- thread->proto = proto;
- proto->m_iDesiredStatus = initialStatus;
-
- int oldStatus = proto->m_iStatus;
- proto->m_iStatus = ID_STATUS_CONNECTING;
- ProtoBroadcastAck(proto->m_szModuleName, NULL, ACKTYPE_STATUS, ACKRESULT_SUCCESS, (HANDLE)oldStatus, proto->m_iStatus);
- thread->hThread = mir_forkthread((pThreadFunc)TlenServerThread, thread);
- }
-}
-
-int TlenProtocol::SetStatus(int iNewStatus)
-{
- int oldStatus;
- HANDLE s;
-
- m_iDesiredStatus = iNewStatus;
-
- if (iNewStatus == ID_STATUS_OFFLINE) {
- if (threadData) {
- if (isConnected) {
- TlenSendPresence(this, ID_STATUS_OFFLINE);
- }
-
- // TODO bug? s = proto;
- s = threadData->s;
-
- threadData = nullptr;
- if (isConnected) {
- Sleep(200);
- // TlenSend(s, "</s>");
- // Force closing connection
- isConnected = FALSE;
- isOnline = FALSE;
- Netlib_CloseHandle(s);
- }
- }
- else {
- if (m_iStatus != ID_STATUS_OFFLINE) {
- oldStatus = m_iStatus;
- m_iStatus = ID_STATUS_OFFLINE;
- ProtoBroadcastAck(NULL, ACKTYPE_STATUS, ACKRESULT_SUCCESS, (HANDLE)oldStatus, m_iStatus);
- }
- }
- }
- else if (iNewStatus != m_iStatus) {
- if (!isConnected) {
- TlenConnect(this, iNewStatus);
- }
- else {
- // change status
- oldStatus = m_iStatus;
- // send presence update
- TlenSendPresence(this, iNewStatus);
- ProtoBroadcastAck(NULL, ACKTYPE_STATUS, ACKRESULT_SUCCESS, (HANDLE)oldStatus, m_iStatus);
- }
- }
- return 0;
-}
-
-INT_PTR TlenProtocol::GetStatus(WPARAM, LPARAM)
-{
- return m_iStatus;
-}
-
-int TlenProtocol::SetAwayMsg(int iStatus, const wchar_t* msg)
-{
- char **szMsg;
- char *newModeMsg;
-
- newModeMsg = mir_u2a(msg);
-
- debugLogA("SetAwayMsg called, wParam=%d lParam=%s", iStatus, newModeMsg);
-
- mir_cslock lck(modeMsgMutex);
-
- switch (iStatus) {
- case ID_STATUS_ONLINE:
- szMsg = &modeMsgs.szOnline;
- break;
- case ID_STATUS_AWAY:
- case ID_STATUS_ONTHEPHONE:
- case ID_STATUS_OUTTOLUNCH:
- szMsg = &modeMsgs.szAway;
- break;
- case ID_STATUS_NA:
- szMsg = &modeMsgs.szNa;
- break;
- case ID_STATUS_DND:
- case ID_STATUS_OCCUPIED:
- szMsg = &modeMsgs.szDnd;
- break;
- case ID_STATUS_FREECHAT:
- szMsg = &modeMsgs.szFreechat;
- break;
- case ID_STATUS_INVISIBLE:
- szMsg = &modeMsgs.szInvisible;
- break;
- default:
- return 1;
- }
-
- if ((*szMsg == nullptr && newModeMsg == nullptr) ||
- (*szMsg != nullptr && newModeMsg != nullptr && !mir_strcmp(*szMsg, newModeMsg))) {
- // Message is the same, no update needed
- if (newModeMsg != nullptr) mir_free(newModeMsg);
- }
- else {
- // Update with the new mode message
- if (*szMsg != nullptr) mir_free(*szMsg);
- *szMsg = newModeMsg;
- // Send a presence update if needed
- if (iStatus == m_iStatus) {
- TlenSendPresence(this, m_iStatus);
- }
- }
-
- return 0;
-}
-
-int TlenProtocol::GetInfo(MCONTACT hContact, int)
-{
- DBVARIANT dbv;
- int iqId;
- char *nick, *pNick;
-
- if (!isOnline) return 1;
- if (hContact == NULL) {
- iqId = TlenSerialNext(this);
- TlenIqAdd(this, iqId, IQ_PROC_NONE, TlenIqResultVcard);
- TlenSend(this, "<iq type='get' id='" TLEN_IQID "%d' to='tuba'><query xmlns='jabber:iq:register'></query></iq>", iqId);
- }
- else {
- if (db_get(hContact, m_szModuleName, "jid", &dbv)) return 1;
- if ((nick = TlenNickFromJID(dbv.pszVal)) != nullptr) {
- if ((pNick = TlenTextEncode(nick)) != nullptr) {
- iqId = TlenSerialNext(this);
- TlenIqAdd(this, iqId, IQ_PROC_NONE, TlenIqResultVcard);
- TlenSend(this, "<iq type='get' id='" TLEN_IQID "%d' to='tuba'><query xmlns='jabber:iq:search'><i>%s</i></query></iq>", iqId, pNick);
- mir_free(pNick);
- }
- mir_free(nick);
- }
- db_free(&dbv);
- }
- return 0;
-}
-
-int TlenProtocol::SetApparentMode(MCONTACT hContact, int mode)
-{
- DBVARIANT dbv;
- int oldMode;
- char *jid;
-
- if (!isOnline) return 0;
- if (!db_get_b(NULL, m_szModuleName, "VisibilitySupport", FALSE)) return 0;
- if (mode != 0 && mode != ID_STATUS_ONLINE && mode != ID_STATUS_OFFLINE) return 1;
- oldMode = db_get_w(hContact, m_szModuleName, "ApparentMode", 0);
- if ((int)mode == oldMode) return 1;
- db_set_w(hContact, m_szModuleName, "ApparentMode", (WORD)mode);
- if (!db_get(hContact, m_szModuleName, "jid", &dbv)) {
- jid = dbv.pszVal;
- switch (mode) {
- case ID_STATUS_ONLINE:
- if (m_iStatus == ID_STATUS_INVISIBLE || oldMode == ID_STATUS_OFFLINE)
- TlenSend(this, "<presence to='%s'><show>available</show></presence>", jid);
- break;
- case ID_STATUS_OFFLINE:
- if (m_iStatus != ID_STATUS_INVISIBLE || oldMode == ID_STATUS_ONLINE)
- TlenSend(this, "<presence to='%s' type='invisible'/>", jid);
- break;
- case 0:
- if (oldMode == ID_STATUS_ONLINE && m_iStatus == ID_STATUS_INVISIBLE)
- TlenSend(this, "<presence to='%s' type='invisible'/>", jid);
- else if (oldMode == ID_STATUS_OFFLINE && m_iStatus != ID_STATUS_INVISIBLE)
- TlenSend(this, "<presence to='%s'><show>available</show></presence>", jid);
- break;
- }
- db_free(&dbv);
- }
- return 0;
-}
-
-/////////////////////////////////////////////////////////////////////////////////////////
-
-struct SENDACKTHREADDATA
-{
- __inline SENDACKTHREADDATA(TlenProtocol *_ppro, MCONTACT _hContact, int _msgid = 0) :
- proto(_ppro), hContact(_hContact), msgid(_msgid)
- {}
-
- TlenProtocol *proto;
- MCONTACT hContact;
- int msgid;
-};
-
-static void __cdecl TlenSendMessageAckThread(void *ptr)
-{
- SENDACKTHREADDATA *data = (SENDACKTHREADDATA *)ptr;
- SleepEx(10, TRUE);
- ProtoBroadcastAck(data->proto->m_szModuleName, data->hContact, ACKTYPE_MESSAGE, ACKRESULT_SUCCESS, (HANDLE)data->msgid, 0);
- delete data;
-}
-
-static void __cdecl TlenSendMessageFailedThread(void *ptr)
-{
- SENDACKTHREADDATA *data = (SENDACKTHREADDATA *)ptr;
- SleepEx(10, TRUE);
- ProtoBroadcastAck(data->proto->m_szModuleName, data->hContact, ACKTYPE_MESSAGE, ACKRESULT_FAILED, (HANDLE)data->msgid, 0);
- delete data;
-}
-
-static void __cdecl TlenGetAwayMsgThread(void *ptr)
-{
- DBVARIANT dbv;
- SENDACKTHREADDATA *data = (SENDACKTHREADDATA *)ptr;
-
- Sleep(50);
-
- if (!db_get(data->hContact, data->proto->m_szModuleName, "jid", &dbv)) {
- TLEN_LIST_ITEM *item = TlenListGetItemPtr(data->proto, LIST_ROSTER, dbv.pszVal);
- if (item != nullptr) {
- db_free(&dbv);
- ProtoBroadcastAck(data->proto->m_szModuleName, data->hContact, ACKTYPE_AWAYMSG, ACKRESULT_SUCCESS, (HANDLE)1,
- item->statusMessage == NULL ? NULL : _A2T(item->statusMessage));
- }
- else {
- ptrA ownJid(db_get_sa(NULL, data->proto->m_szModuleName, "jid"));
- if (!mir_strcmp(ownJid, dbv.pszVal)) {
- DBVARIANT dbv2;
- if (!db_get_s(data->hContact, "CList", "StatusMsg", &dbv2, DBVT_WCHAR)) {
- data->proto->ProtoBroadcastAck(data->hContact, ACKTYPE_AWAYMSG, ACKRESULT_SUCCESS, (HANDLE)1, (LPARAM)dbv2.ptszVal);
- db_free(&dbv2);
- }
- else {
- data->proto->ProtoBroadcastAck(data->hContact, ACKTYPE_AWAYMSG, ACKRESULT_SUCCESS, (HANDLE)1, NULL);
- }
- }
- db_free(&dbv);
- }
- }
- else {
- data->proto->ProtoBroadcastAck(data->hContact, ACKTYPE_AWAYMSG, ACKRESULT_SUCCESS, (HANDLE)1, NULL);
- }
-
- delete data;
-}
-
-INT_PTR TlenProtocol::SendAlert(WPARAM hContact, LPARAM)
-{
- DBVARIANT dbv;
- if (isOnline && !db_get(hContact, m_szModuleName, "jid", &dbv)) {
- TlenSend(this, "<m tp='a' to='%s'/>", dbv.pszVal);
-
- db_free(&dbv);
- }
- return 0;
-}
-
-int TlenProtocol::SendMsg(MCONTACT hContact, int, const char* msgRAW)
-{
- DBVARIANT dbv;
- if (!isOnline || db_get(hContact, m_szModuleName, "jid", &dbv)) {
- mir_forkthread(TlenSendMessageFailedThread, new SENDACKTHREADDATA(this, hContact, 2));
- return 2;
- }
-
- TLEN_LIST_ITEM *item;
- char msgType[16];
-
- char* msg = mir_utf8decodeA(msgRAW);
- int id = TlenSerialNext(this);
-
- if (!mir_strcmp(msg, "<alert>")) {
- TlenSend(this, "<m tp='a' to='%s'/>", dbv.pszVal);
- mir_forkthread(TlenSendMessageAckThread, new SENDACKTHREADDATA(this, hContact, id));
- }
- else if (!mir_strcmp(msg, "<image>")) {
- TlenSend(this, "<message to='%s' type='%s' crc='%x' idt='%d'/>", dbv.pszVal, "pic", 0x757f044, id);
- mir_forkthread(TlenSendMessageAckThread, new SENDACKTHREADDATA(this, hContact, id));
- }
- else {
- char *msgEnc = TlenTextEncode(msg);
- if (msgEnc != nullptr) {
- if (TlenListExist(this, LIST_CHATROOM, dbv.pszVal) && strchr(dbv.pszVal, '/') == nullptr)
- mir_strcpy(msgType, "groupchat");
- else if (db_get_b(hContact, m_szModuleName, "bChat", FALSE))
- mir_strcpy(msgType, "privchat");
- else
- mir_strcpy(msgType, "chat");
-
- if (!mir_strcmp(msgType, "groupchat") || db_get_b(NULL, m_szModuleName, "MsgAck", FALSE) == FALSE) {
- if (!mir_strcmp(msgType, "groupchat"))
- TlenSend(this, "<message to='%s' type='%s'><body>%s</body></message>", dbv.pszVal, msgType, msgEnc);
- else if (!mir_strcmp(msgType, "privchat"))
- TlenSend(this, "<m to='%s'><b n='6' s='10' f='0' c='000000'>%s</b></m>", dbv.pszVal, msgEnc);
- else
- TlenSend(this, "<message to='%s' type='%s' id='" TLEN_IQID "%d'><body>%s</body><x xmlns='jabber:x:event'><composing/></x></message>", dbv.pszVal, msgType, id, msgEnc);
-
- mir_forkthread(TlenSendMessageAckThread, new SENDACKTHREADDATA(this, hContact, id));
- }
- else {
- if ((item = TlenListGetItemPtr(this, LIST_ROSTER, dbv.pszVal)) != nullptr)
- item->idMsgAckPending = id;
- TlenSend(this, "<message to='%s' type='%s' id='" TLEN_IQID "%d'><body>%s</body><x xmlns='jabber:x:event'><offline/><delivered/><composing/></x></message>", dbv.pszVal, msgType, id, msgEnc);
- }
- }
- mir_free(msgEnc);
- }
-
- mir_free(msg);
- db_free(&dbv);
- return id;
-}
-
-/////////////////////////////////////////////////////////////////////////////////////////
-// TlenGetAvatarInfo - retrieves the avatar info
-
-INT_PTR TlenProtocol::GetAvatarInfo(WPARAM wParam, LPARAM lParam)
-{
- if (!tlenOptions.enableAvatars) return GAIR_NOAVATAR;
- BOOL downloadingAvatar = FALSE;
- char *avatarHash = nullptr;
- TLEN_LIST_ITEM *item = nullptr;
- DBVARIANT dbv;
- PROTO_AVATAR_INFORMATION *pai = (PROTO_AVATAR_INFORMATION*)lParam;
-
- if (pai->hContact != NULL) {
- if (!db_get(pai->hContact, m_szModuleName, "jid", &dbv)) {
- item = TlenListGetItemPtr(this, LIST_ROSTER, dbv.pszVal);
- db_free(&dbv);
- if (item != nullptr) {
- downloadingAvatar = item->newAvatarDownloading;
- avatarHash = item->avatarHash;
- }
- }
- }
- else if (threadData != nullptr)
- avatarHash = threadData->avatarHash;
-
- if ((avatarHash == nullptr || avatarHash[0] == '\0') && !downloadingAvatar)
- return GAIR_NOAVATAR;
-
- if (avatarHash != nullptr && !downloadingAvatar) {
- TlenGetAvatarFileName(this, item, pai->filename, _countof(pai->filename) - 1);
- pai->format = (pai->hContact == NULL) ? threadData->avatarFormat : item->avatarFormat;
- return GAIR_SUCCESS;
- }
-
- /* get avatar */
- if ((wParam & GAIF_FORCE) != 0 && pai->hContact != NULL && isOnline)
- return GAIR_WAITFOR;
-
- return GAIR_NOAVATAR;
-}
-
-HANDLE TlenProtocol::GetAwayMsg(MCONTACT hContact)
-{
- SENDACKTHREADDATA *tdata = new SENDACKTHREADDATA(this, hContact, 0);
- mir_forkthread((pThreadFunc)TlenGetAwayMsgThread, tdata);
- return (HANDLE)1;
-}
-
-int TlenProtocol::RecvAwayMsg(MCONTACT, int, PROTORECVEVENT*)
-{
- return 0;
-}
-
-HANDLE TlenProtocol::FileAllow(MCONTACT, HANDLE hTransfer, const wchar_t* szPath)
-{
- if (!isOnline) return nullptr;
-
- TLEN_FILE_TRANSFER *ft = (TLEN_FILE_TRANSFER *)hTransfer;
- ft->szSavePath = mir_strdup(mir_u2a(szPath)); //TODO convert to wchar_t*
- TLEN_LIST_ITEM *item = TlenListAdd(this, LIST_FILE, ft->iqId);
- if (item != nullptr) {
- item->ft = ft;
- }
- char *nick = TlenNickFromJID(ft->jid);
- if (ft->newP2P) {
- TlenSend(this, "<iq to='%s'><query xmlns='p2p'><fs t='%s' e='5' i='%s' v='1'/></query></iq>", ft->jid, ft->jid, ft->iqId);
- }
- else {
- TlenSend(this, "<f t='%s' i='%s' e='5' v='1'/>", nick, ft->iqId);
- }
- mir_free(nick);
- return (HANDLE)hTransfer;
-}
-
-int TlenProtocol::FileDeny(MCONTACT, HANDLE hTransfer, const wchar_t*)
-{
- if (!isOnline) return 1;
-
- TLEN_FILE_TRANSFER *ft = (TLEN_FILE_TRANSFER *)hTransfer;
- char *nick = TlenNickFromJID(ft->jid);
- TlenSend(this, "<f i='%s' e='4' t='%s'/>", ft->iqId, nick); \
- mir_free(nick);
- TlenP2PFreeFileTransfer(ft);
- return 0;
-}
-
-int TlenProtocol::FileResume(HANDLE, int*, const wchar_t**)
-{
- return 0;
-}
-
-int TlenProtocol::FileCancel(MCONTACT, HANDLE hTransfer)
-{
- TLEN_FILE_TRANSFER *ft = (TLEN_FILE_TRANSFER *)hTransfer;
- debugLogA("Invoking FileCancel()");
- if (ft->s != nullptr) {
- ft->state = FT_ERROR;
- Netlib_CloseHandle(ft->s);
- ft->s = nullptr;
- if (ft->hFileEvent != nullptr) {
- HANDLE hEvent = ft->hFileEvent;
- ft->hFileEvent = nullptr;
- SetEvent(hEvent);
- }
- }
- else {
- TlenP2PFreeFileTransfer(ft);
- }
- return 0;
-}
-
-HANDLE TlenProtocol::SendFile(MCONTACT hContact, const wchar_t* szDescription, wchar_t** ppszFiles)
-{
- int i, j;
- struct _stat statbuf;
- DBVARIANT dbv;
- char *nick, *p, idStr[10];
-
- if (!isOnline) return nullptr;
- // if (db_get_w(ccs->hContact, m_szModuleName, "Status", ID_STATUS_OFFLINE) == ID_STATUS_OFFLINE) return 0;
- if (db_get(hContact, m_szModuleName, "jid", &dbv)) return nullptr;
- TLEN_FILE_TRANSFER *ft = TlenFileCreateFT(this, dbv.pszVal);
- for (ft->fileCount = 0; ppszFiles[ft->fileCount]; ft->fileCount++);
- ft->files = (char **)mir_alloc(sizeof(char *) * ft->fileCount);
- ft->filesSize = (long *)mir_alloc(sizeof(long) * ft->fileCount);
- ft->allFileTotalSize = 0;
- for (i = j = 0; i < ft->fileCount; i++) {
- char* ppszFiles_i_A = mir_u2a(ppszFiles[i]);
- if (_stat(ppszFiles_i_A, &statbuf))
- debugLogA("'%s' is an invalid filename", ppszFiles[i]);
- else {
- ft->filesSize[j] = statbuf.st_size;
- ft->files[j++] = mir_strdup(ppszFiles_i_A);
- ft->allFileTotalSize += statbuf.st_size;
- }
- mir_free(ppszFiles_i_A);
- }
- ft->fileCount = j;
- ft->szDescription = mir_u2a(szDescription);
- ft->hContact = hContact;
- ft->currentFile = 0;
- db_free(&dbv);
-
- int id = TlenSerialNext(this);
- mir_snprintf(idStr, "%d", id);
- TLEN_LIST_ITEM *item = TlenListAdd(this, LIST_FILE, idStr);
- if (item != nullptr) {
- ft->iqId = mir_strdup(idStr);
- nick = TlenNickFromJID(ft->jid);
- item->ft = ft;
- if (tlenOptions.useNewP2P) {
- TlenSend(this, "<iq to='%s'><query xmlns='p2p'><fs t='%s' e='1' i='%s' c='%d' s='%d' v='%d'/></query></iq>",
- ft->jid, ft->jid, idStr, ft->fileCount, ft->allFileTotalSize, ft->fileCount);
-
- ft->newP2P = TRUE;
- }
- else {
- if (ft->fileCount == 1) {
- char* ppszFiles_0_A = mir_u2a(ppszFiles[0]);
- if ((p = strrchr(ppszFiles_0_A, '\\')) != nullptr) {
- p++;
- }
- else {
- p = ppszFiles_0_A;
- }
- p = TlenTextEncode(p);
- TlenSend(this, "<f t='%s' n='%s' e='1' i='%s' c='1' s='%d' v='1'/>", nick, p, idStr, ft->allFileTotalSize);
- mir_free(ppszFiles[0]);
- mir_free(p);
- }
- else {
- TlenSend(this, "<f t='%s' e='1' i='%s' c='%d' s='%d' v='1'/>", nick, idStr, ft->fileCount, ft->allFileTotalSize);
- }
- }
- mir_free(nick);
- }
-
- return (HANDLE)ft;
-}
-
-static char* settingToChar(DBCONTACTWRITESETTING* cws)
-{
- switch (cws->value.type) {
- case DBVT_ASCIIZ:
- return mir_strdup(cws->value.pszVal);
- case DBVT_UTF8:
- return mir_utf8decode(mir_strdup(cws->value.pszVal), nullptr);
- }
- return nullptr;
-}
-
-int TlenProtocol::TlenDbSettingChanged(WPARAM wParam, LPARAM lParam)
-{
- MCONTACT hContact = (MCONTACT)wParam;
- DBCONTACTWRITESETTING *cws = (DBCONTACTWRITESETTING *)lParam;
- // no action for hContact == NULL or when offline
- if (hContact == NULL) return 0;
- if (!isConnected) return 0;
-
- if (!strcmp(cws->szModule, "CList")) {
- DBVARIANT dbv;
- TLEN_LIST_ITEM *item;
- char *nick, *jid, *group;
-
- char *szProto = GetContactProto(hContact);
- if (szProto == nullptr || strcmp(szProto, m_szModuleName)) return 0;
- // A contact's group is changed
- if (!strcmp(cws->szSetting, "Group")) {
- if (!db_get(hContact, m_szModuleName, "jid", &dbv)) {
- if ((item = TlenListGetItemPtr(this, LIST_ROSTER, dbv.pszVal)) != nullptr) {
- db_free(&dbv);
- if (!db_get(hContact, "CList", "MyHandle", &dbv)) {
- nick = TlenTextEncode(dbv.pszVal);
- db_free(&dbv);
- }
- else if (!db_get(hContact, this->m_szModuleName, "Nick", &dbv)) {
- nick = TlenTextEncode(dbv.pszVal);
- db_free(&dbv);
- }
- else nick = TlenNickFromJID(item->jid);
-
- if (nick != nullptr) {
- // Note: we need to compare with item->group to prevent infinite loop
- if (cws->value.type == DBVT_DELETED && item->group != nullptr) {
- debugLogA("Group set to nothing");
- TlenSend(this, "<iq type='set'><query xmlns='jabber:iq:roster'><item name='%s' jid='%s'></item></query></iq>", nick, item->jid);
- }
- else if (cws->value.pszVal != nullptr) {
- char *newGroup = settingToChar(cws);
- if (item->group == nullptr || strcmp(newGroup, item->group)) {
- debugLogA("Group set to %s", newGroup);
- if ((group = TlenGroupEncode(newGroup)) != nullptr) {
- TlenSend(this, "<iq type='set'><query xmlns='jabber:iq:roster'><item name='%s' jid='%s'><group>%s</group></item></query></iq>", nick, item->jid, group);
- mir_free(group);
- }
- }
- mir_free(newGroup);
- }
- mir_free(nick);
- }
- }
- else {
- db_free(&dbv);
- }
- }
- }
- // A contact is renamed
- else if (!strcmp(cws->szSetting, "MyHandle")) {
- char *newNick;
-
- // hContact = (MCONTACT) wParam;
- // szProto = GetContactProto(hContact);
- // if (szProto == NULL || strcmp(szProto, proto->m_szModuleName)) return 0;
-
- if (!db_get(hContact, m_szModuleName, "jid", &dbv)) {
- jid = dbv.pszVal;
- if ((item = TlenListGetItemPtr(this, LIST_ROSTER, dbv.pszVal)) != nullptr) {
- if (cws->value.type == DBVT_DELETED) {
- newNick = mir_u2a(pcli->pfnGetContactDisplayName(hContact, GCDNF_NOMYHANDLE));
- }
- else if (cws->value.pszVal != nullptr) {
- newNick = settingToChar(cws);
- }
- else {
- newNick = nullptr;
- }
- // Note: we need to compare with item->nick to prevent infinite loop
- if (newNick != nullptr && (item->nick == nullptr || (item->nick != nullptr && strcmp(item->nick, newNick)))) {
- if ((nick = TlenTextEncode(newNick)) != nullptr) {
- debugLogA("Nick set to %s", newNick);
- if (item->group != nullptr && (group = TlenGroupEncode(item->group)) != nullptr) {
- TlenSend(this, "<iq type='set'><query xmlns='jabber:iq:roster'><item name='%s' jid='%s'><group>%s</group></item></query></iq>", nick, jid, group);
- mir_free(group);
- }
- else {
- TlenSend(this, "<iq type='set'><query xmlns='jabber:iq:roster'><item name='%s' jid='%s'></item></query></iq>", nick, jid);
- }
- mir_free(nick);
- }
- }
- if (newNick != nullptr) mir_free(newNick);
- }
- db_free(&dbv);
- }
- }
- // A temporary contact has been added permanently
- else if (!strcmp(cws->szSetting, "NotOnList")) {
- if (cws->value.type == DBVT_DELETED || (cws->value.type == DBVT_BYTE && cws->value.bVal == 0)) {
- ptrA szJid(db_get_sa(hContact, m_szModuleName, "jid"));
- if (szJid != NULL) {
- char *szNick, *pGroup;
- debugLogA("Add %s permanently to list", szJid);
- if (!db_get(hContact, "CList", "MyHandle", &dbv)) {
- szNick = TlenTextEncode(dbv.pszVal); //Utf8Encode
- db_free(&dbv);
- }
- else szNick = TlenNickFromJID(szJid);
-
- if (szNick != nullptr) {
- debugLogA("jid=%s szNick=%s", szJid, szNick);
- if (!db_get(hContact, "CList", "Group", &dbv)) {
- if ((pGroup = TlenGroupEncode(dbv.pszVal)) != nullptr) {
- TlenSend(this, "<iq type='set'><query xmlns='jabber:iq:roster'><item name='%s' szJid='%s'><group>%s</group></item></query></iq>", szNick, szJid, pGroup);
- TlenSend(this, "<presence to='%s' type='subscribe'/>", szJid);
- mir_free(pGroup);
- }
- db_free(&dbv);
- }
- else {
- TlenSend(this, "<iq type='set'><query xmlns='jabber:iq:roster'><item name='%s' szJid='%s'/></query></iq>", szNick, szJid);
- TlenSend(this, "<presence to='%s' type='subscribe'/>", szJid);
- }
- mir_free(szNick);
- db_unset(hContact, "CList", "Hidden");
- }
- }
- }
- }
- }
-
- return 0;
-}
-
-int TlenProtocol::TlenContactDeleted(WPARAM wParam, LPARAM)
-{
- if (!isOnline) // should never happen
- return 0;
-
- char *szProto = GetContactProto(wParam);
- if (szProto == nullptr || mir_strcmp(szProto, m_szModuleName))
- return 0;
-
- DBVARIANT dbv;
- if (!db_get(wParam, m_szModuleName, "jid", &dbv)) {
- char *p, *q;
-
- char *jid = dbv.pszVal;
- if ((p = strchr(jid, '@')) != nullptr) {
- if ((q = strchr(p, '/')) != nullptr)
- *q = '\0';
- }
-
- // Remove from roster, server also handles the presence unsubscription process.
- if (TlenListExist(this, LIST_ROSTER, jid))
- TlenSend(this, "<iq type='set'><query xmlns='jabber:iq:roster'><item jid='%s' subscription='remove'/></query></iq>", jid);
-
- db_free(&dbv);
- }
- return 0;
-}
-
-int TlenProtocol::UserIsTyping(MCONTACT hContact, int type)
-{
- DBVARIANT dbv;
-
- if (!isOnline) return 0;
- if (!db_get(hContact, m_szModuleName, "jid", &dbv)) {
- TLEN_LIST_ITEM *item = TlenListGetItemPtr(this, LIST_ROSTER, dbv.pszVal);
- if (item != nullptr /*&& item->wantComposingEvent == TRUE*/) {
- switch (type) {
- case PROTOTYPE_SELFTYPING_OFF:
- TlenSend(this, "<m tp='u' to='%s'/>", dbv.pszVal);
- break;
- case PROTOTYPE_SELFTYPING_ON:
- TlenSend(this, "<m tp='t' to='%s'/>", dbv.pszVal);
- break;
- }
- }
- db_free(&dbv);
- }
- return 0;
-}
-
-INT_PTR TlenProtocol::GetMyAvatar(WPARAM wParam, LPARAM lParam)
-{
- wchar_t* buf = (wchar_t*)wParam;
- int size = (int)lParam;
- if (buf == nullptr || size <= 0)
- return -1;
-
- TlenGetAvatarFileName(this, nullptr, buf, size);
- return 0;
-}
-
-static INT_PTR CALLBACK TlenChangeAvatarDlgProc(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM)
-{
- switch (msg) {
- case WM_INITDIALOG:
- TranslateDialogDefault(hwndDlg);
- Window_SetIcon_IcoLib(hwndDlg, GetIconHandle(IDI_TLEN));
- CheckDlgButton(hwndDlg, IDC_PUBLICAVATAR, BST_CHECKED);
- return TRUE;
-
- case WM_COMMAND:
- switch (LOWORD(wParam)) {
- case IDOK:
- {
- int result = LOWORD(wParam);
- if (IsDlgButtonChecked(hwndDlg, IDC_PUBLICAVATAR)) {
- result |= 0x10000;
- }
- EndDialog(hwndDlg, result);
- }
- return TRUE;
- }
- break;
- }
- return 0;
-}
-
-INT_PTR TlenProtocol::SetMyAvatar(WPARAM, LPARAM lParam)
-{
- if (!isOnline) {
- PUShowMessageT(TranslateT("You need to be connected to Tlen account to set avatar."), SM_WARNING);
- return 1;
- }
- wchar_t* szFileName = (wchar_t*)lParam;
- wchar_t tFileName[MAX_PATH];
- if (szFileName != nullptr) {
- int result = DialogBoxParam(hInst, MAKEINTRESOURCE(IDD_USER_CHANGEAVATAR), nullptr, TlenChangeAvatarDlgProc, (LPARAM)NULL);
- TlenGetAvatarFileName(this, nullptr, tFileName, MAX_PATH);
- if (CopyFile(szFileName, tFileName, FALSE) == FALSE)
- return 1;
-
- char* tFileNameA = mir_u2a(tFileName); //TODO - drop io.h
- int fileIn = open(tFileNameA, O_RDWR | O_BINARY, S_IREAD | S_IWRITE);
- if (fileIn != -1) {
- long dwPngSize = filelength(fileIn);
- BYTE* pResult = (BYTE *)mir_alloc(dwPngSize);
- if (pResult != nullptr) {
- read(fileIn, pResult, dwPngSize);
- close(fileIn);
- TlenUploadAvatar(this, pResult, dwPngSize, (result & 0x10000) != 0);
- mir_free(pResult);
- }
- }
- else debugLogA("SetMyAvatar open error");
- mir_free(tFileName);
- mir_free(tFileNameA);
- }
- else TlenRemoveAvatar(this);
-
- return 0;
-}
-
-INT_PTR TlenProtocol::GetAvatarCaps(WPARAM wParam, LPARAM lParam)
-{
- switch (wParam) {
- case AF_MAXSIZE:
- {
- POINT* size = (POINT*)lParam;
- if (size)
- size->x = size->y = 64;
- }
- return 0;
- case AF_PROPORTION:
- return PIP_SQUARE;
- case AF_FORMATSUPPORTED:
- return (lParam == PA_FORMAT_PNG) ? 1 : 0;
- case AF_ENABLED:
- return tlenOptions.enableAvatars;
- case AF_DONTNEEDDELAYS:
- return 1;
- case AF_MAXFILESIZE:
- return 10 * 1024;
- case AF_FETCHIFCONTACTOFFLINE:
- return 1;
- default:
- return 0;
- }
-}
-
-int TlenProtocol::OnEvent(PROTOEVENTTYPE iEventType, WPARAM wParam, LPARAM lParam)
-{
- //TlenProtocol *proto = (TlenProtocol *)ptr;
- switch (iEventType) {
- case EV_PROTO_ONLOAD: return OnModulesLoaded(0, 0);
- case EV_PROTO_ONOPTIONS: return OptionsInit(wParam, lParam);
- case EV_PROTO_ONEXIT: return PreShutdown(0, 0);
-
- case EV_PROTO_ONRENAME:
- Menu_ModifyItem(hMenuRoot, m_tszUserName);
- }
- return 1;
-}
-
-extern INT_PTR CALLBACK TlenAccMgrUIDlgProc(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam);
-
-INT_PTR TlenProtocol::AccMgrUI(WPARAM, LPARAM lParam)
-{
- return (INT_PTR)CreateDialogParam(hInst, MAKEINTRESOURCE(IDD_ACCMGRUI), (HWND)lParam, TlenAccMgrUIDlgProc, (LPARAM)this);
-}
-
-void TlenInitServicesVTbl(TlenProtocol *proto)
-{
- proto->CreateProtoService(PS_GETNAME, &TlenProtocol::GetName);
- proto->CreateProtoService(PS_GETAVATARINFO, &TlenProtocol::GetAvatarInfo);
- proto->CreateProtoService(PS_SEND_NUDGE, &TlenProtocol::SendAlert);
- proto->CreateProtoService(PS_GETAVATARCAPS, &TlenProtocol::GetAvatarCaps);
- proto->CreateProtoService(PS_SETMYAVATAR, &TlenProtocol::SetMyAvatar);
- proto->CreateProtoService(PS_GETMYAVATAR, &TlenProtocol::GetMyAvatar);
- proto->CreateProtoService(PS_GETSTATUS, &TlenProtocol::GetStatus);
- proto->CreateProtoService(PS_CREATEACCMGRUI, &TlenProtocol::AccMgrUI);
-}
-
-TlenProtocol::TlenProtocol(const char *aProtoName, const wchar_t *aUserName) :
- PROTO<TlenProtocol>(aProtoName, aUserName)
-{
- TlenInitServicesVTbl(this);
-
- hTlenNudge = CreateProtoEvent("/Nudge");
-
- HookProtoEvent(ME_OPT_INITIALISE, &TlenProtocol::OptionsInit);
- HookProtoEvent(ME_DB_CONTACT_SETTINGCHANGED, &TlenProtocol::TlenDbSettingChanged);
- HookProtoEvent(ME_DB_CONTACT_DELETED, &TlenProtocol::TlenContactDeleted);
- HookProtoEvent(ME_CLIST_PREBUILDCONTACTMENU, &TlenProtocol::PrebuildContactMenu);
-
- DBVARIANT dbv;
- if (!db_get(NULL, m_szModuleName, "LoginServer", &dbv))
- db_free(&dbv);
- else
- db_set_s(NULL, m_szModuleName, "LoginServer", "tlen.pl");
-
- if (!db_get(NULL, m_szModuleName, "ManualHost", &dbv))
- db_free(&dbv);
- else
- db_set_s(NULL, m_szModuleName, "ManualHost", "s1.tlen.pl");
-
- TlenLoadOptions(this);
-
- TlenWsInit(this);
- TlenSerialInit(this);
- TlenIqInit(this);
- TlenListInit(this);
-
- initMenuItems();
-}
-
-TlenProtocol::~TlenProtocol()
-{
- TlenVoiceCancelAll(this);
- TlenFileCancelAll(this);
- if (hTlenNudge)
- DestroyHookableEvent(hTlenNudge);
- TlenListUninit(this);
- TlenIqUninit(this);
- TlenWsUninit(this);
-
- mir_free(modeMsgs.szOnline);
- mir_free(modeMsgs.szAway);
- mir_free(modeMsgs.szNa);
- mir_free(modeMsgs.szDnd);
- mir_free(modeMsgs.szFreechat);
- mir_free(modeMsgs.szInvisible);
-}
diff --git a/protocols/Tlen/src/tlen_thread.cpp b/protocols/Tlen/src/tlen_thread.cpp
deleted file mode 100644
index 0cbe1c4819..0000000000
--- a/protocols/Tlen/src/tlen_thread.cpp
+++ /dev/null
@@ -1,1321 +0,0 @@
-/*
-
-Jabber Protocol Plugin for Miranda IM
-Tlen Protocol Plugin for Miranda NG
-Copyright (C) 2002-2004 Santithorn Bunchua
-Copyright (C) 2004-2007 Piotr Piastucki
-
-This program is free software; you can redistribute it and/or
-modify it under the terms of the GNU General Public License
-as published by the Free Software Foundation; either version 2
-of the License, or (at your option) any later version.
-
-This program 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 General Public License for more details.
-
-You should have received a copy of the GNU General Public License
-along with this program; if not, write to the Free Software
-Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
-
-*/
-
-#include "stdafx.h"
-
-#include "commons.h"
-#include "tlen_list.h"
-#include "tlen_iq.h"
-#include "resource.h"
-#include "tlen_p2p_old.h"
-#include "tlen_file.h"
-#include "tlen_muc.h"
-#include "tlen_voice.h"
-#include "tlen_avatar.h"
-#include "tlen_presence.h"
-#include "tlen_picture.h"
-#include <io.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-
-
-extern void __cdecl TlenProcessP2P(XmlNode *node, ThreadData *info);
-
-
-//static void __cdecl TlenProcessInvitation(struct ThreadData *info);
-static void __cdecl TlenKeepAliveThread(void *ptr);
-static void TlenProcessStreamOpening(XmlNode *node, ThreadData *info);
-static void TlenProcessStreamClosing(XmlNode *node, ThreadData *info);
-static void TlenProcessProtocol(XmlNode *node, ThreadData *info);
-static void TlenProcessMessage(XmlNode *node, ThreadData *info);
-static void TlenProcessIq(XmlNode *node, ThreadData *info);
-static void TlenProcessW(XmlNode *node, ThreadData *info);
-static void TlenProcessM(XmlNode *node, ThreadData *info);
-static void TlenProcessN(XmlNode *node, ThreadData *info);
-static void TlenProcessP(XmlNode *node, ThreadData *info);
-static void TlenProcessV(XmlNode *node, ThreadData *info);
-static void TlenProcessAvatar(XmlNode* node, ThreadData *info);
-static void TlenProcessCipher(XmlNode *node, ThreadData *info);
-
-static VOID NTAPI TlenDummyApcFunc(ULONG_PTR)
-{
- return;
-}
-
-static char onlinePassword[128];
-static HANDLE hEventPasswdDlg;
-
-static INT_PTR CALLBACK TlenPasswordDlgProc(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
-{
- char text[128];
-
- switch (msg) {
- case WM_INITDIALOG:
- TranslateDialogDefault(hwndDlg);
- mir_snprintf(text, Translate("Enter password for %s"), (char *) lParam);
- SetDlgItemTextA(hwndDlg, IDC_JID, text);
- return TRUE;
- case WM_COMMAND:
- switch (LOWORD(wParam)) {
- case IDOK:
- GetDlgItemTextA(hwndDlg, IDC_PASSWORD, onlinePassword, _countof(onlinePassword));
- //EndDialog(hwndDlg, (int) onlinePassword);
- //return TRUE;
- // Fall through
- case IDCANCEL:
- //EndDialog(hwndDlg, 0);
- SetEvent(hEventPasswdDlg);
- DestroyWindow(hwndDlg);
- return TRUE;
- }
- break;
- }
-
- return FALSE;
-}
-
-static VOID NTAPI TlenPasswordCreateDialogApcProc(ULONG_PTR param)
-{
- CreateDialogParam(hInst, MAKEINTRESOURCE(IDD_PASSWORD), nullptr, TlenPasswordDlgProc, (LPARAM) param);
-}
-
-void __cdecl TlenServerThread(ThreadData *info)
-{
- char *connectHost;
- int datalen;
- XmlState xmlState;
- int tlenNetworkBufferSize;
- int oldStatus = ID_STATUS_OFFLINE;
- int reconnectMaxTime;
- int numRetry;
- int reconnectTime;
- int loginErr = 0;
- info->proto->debugLogA("Thread started");
-
- // Normal server connection, we will fetch all connection parameters
- // e.g. username, password, etc. from the database.
-
- if (info->proto->threadData != nullptr) {
- // Will not start another connection thread if a thread is already running.
- // Make APC call to the main thread. This will immediately wake the thread up
- // in case it is asleep in the reconnect loop so that it will immediately
- // reconnect.
- QueueUserAPC(TlenDummyApcFunc, info->proto->threadData->hThread, 0);
- info->proto->debugLogA("Thread ended, another normal thread is running");
- mir_free(info);
- return;
- }
-
- info->proto->threadData = info;
-
- DBVARIANT dbv;
- if (!db_get(NULL, info->proto->m_szModuleName, "LoginName", &dbv)) {
- strncpy(info->username, dbv.pszVal, sizeof(info->username));
- info->username[sizeof(info->username)-1] = '\0';
- _strlwr(info->username);
- db_set_s(NULL, info->proto->m_szModuleName, "LoginName", info->username);
- db_free(&dbv);
-
- } else {
- info->proto->debugLogA("Thread ended, login name is not configured");
- loginErr = LOGINERR_BADUSERID;
- }
-
- if (loginErr == 0) {
- if (!db_get(NULL, info->proto->m_szModuleName, "LoginServer", &dbv)) {
- strncpy(info->server, dbv.pszVal, sizeof(info->server));
- info->server[sizeof(info->server)-1] = '\0';
- _strlwr(info->server);
- db_set_s(NULL, info->proto->m_szModuleName, "LoginServer", info->server);
- db_free(&dbv);
- } else {
- info->proto->debugLogA("Thread ended, login server is not configured");
- loginErr = LOGINERR_NONETWORK;
- }
- }
-
- char jidStr[128];
- if (loginErr == 0) {
- if (!info->proto->tlenOptions.savePassword) {
- // Ugly hack: continue logging on only the return value is &(onlinePassword[0])
- // because if WM_QUIT while dialog box is still visible, p is returned with some
- // exit code which may not be NULL.
- // Should be better with modeless.
- onlinePassword[0] = (char) -1;
- hEventPasswdDlg = CreateEvent(nullptr, FALSE, FALSE, nullptr);
- QueueUserAPC(TlenPasswordCreateDialogApcProc, hMainThread, (UINT_PTR)jidStr);
- WaitForSingleObject(hEventPasswdDlg, INFINITE);
- CloseHandle(hEventPasswdDlg);
-
- if (onlinePassword[0] != (char) -1) {
- strncpy(info->password, onlinePassword, sizeof(info->password));
- info->password[sizeof(info->password)-1] = '\0';
- }
- else {
- info->proto->debugLogA("Thread ended, password request dialog was canceled");
- loginErr = LOGINERR_BADUSERID;
- }
- }
- else {
- if (!db_get(NULL, info->proto->m_szModuleName, "Password", &dbv)) {
- strncpy(info->password, dbv.pszVal, sizeof(info->password));
- info->password[sizeof(info->password)-1] = '\0';
- db_free(&dbv);
- }
- else {
- info->proto->debugLogA("Thread ended, password is not configured");
- loginErr = LOGINERR_BADUSERID;
- }
- }
- }
-
- tlenNetworkBufferSize = 2048;
- char *buffer = (char *) mir_alloc(tlenNetworkBufferSize+1); // +1 is for '\0' when debug logging this buffer
- if (buffer == nullptr) {
- info->proto->debugLogA("Thread ended, network buffer cannot be allocated");
- loginErr = LOGINERR_NONETWORK;
- }
-
- if (loginErr != 0) {
- info->proto->threadData = nullptr;
- oldStatus = info->proto->m_iStatus;
- info->proto->m_iStatus = ID_STATUS_OFFLINE;
- ProtoBroadcastAck(info->proto->m_szModuleName, NULL, ACKTYPE_STATUS, ACKRESULT_SUCCESS, (HANDLE) oldStatus, info->proto->m_iStatus);
- ProtoBroadcastAck(info->proto->m_szModuleName, NULL, ACKTYPE_LOGIN, ACKRESULT_FAILED, nullptr, loginErr);
- mir_free(info);
- mir_free(buffer);
- return;
- }
-
- mir_snprintf(jidStr, "%s@%s", info->username, info->server);
- db_set_s(NULL, info->proto->m_szModuleName, "jid", jidStr);
-
- if (!db_get(NULL, info->proto->m_szModuleName, "ManualHost", &dbv)) {
- strncpy(info->manualHost, dbv.pszVal, sizeof(info->manualHost));
- info->manualHost[sizeof(info->manualHost)-1] = '\0';
- db_free(&dbv);
- }
- info->port = db_get_w(NULL, info->proto->m_szModuleName, "ManualPort", TLEN_DEFAULT_PORT);
- info->useEncryption = info->proto->tlenOptions.useEncryption;
-
- if (info->manualHost[0])
- connectHost = info->manualHost;
- else
- connectHost = info->server;
-
- info->proto->debugLogA("Thread server='%s' port='%d'", connectHost, info->port);
-
-
- if (!db_get(NULL, info->proto->m_szModuleName, "AvatarHash", &dbv)) {
- strncpy(info->proto->threadData->avatarHash, dbv.pszVal, sizeof(info->proto->threadData->avatarHash)-1);
- db_free(&dbv);
- }
- info->avatarFormat = db_get_dw(NULL, info->proto->m_szModuleName, "AvatarFormat", PA_FORMAT_UNKNOWN);
-
-
- reconnectMaxTime = 10;
- numRetry = 0;
-
- for (;;) { // Reconnect loop
-
- info->s = TlenWsConnect(info->proto, connectHost, info->port);
- if (info->s == nullptr) {
- info->proto->debugLogA("Connection failed (%d)", WSAGetLastError());
- if (info->proto->threadData == info) {
- oldStatus = info->proto->m_iStatus;
- info->proto->m_iStatus = ID_STATUS_OFFLINE;
- ProtoBroadcastAck(info->proto->m_szModuleName, NULL, ACKTYPE_LOGIN, ACKRESULT_FAILED, nullptr, LOGINERR_NONETWORK);
- ProtoBroadcastAck(info->proto->m_szModuleName, NULL, ACKTYPE_STATUS, ACKRESULT_SUCCESS, (HANDLE) oldStatus, info->proto->m_iStatus);
- if (info->proto->tlenOptions.reconnect == TRUE) {
- reconnectTime = rand() % reconnectMaxTime;
- info->proto->debugLogA("Sleeping %d seconds before automatic reconnecting...", reconnectTime);
- SleepEx(reconnectTime * 1000, TRUE);
- if (reconnectMaxTime < 10*60) // Maximum is 10 minutes
- reconnectMaxTime *= 2;
- if (info->proto->threadData == info) { // Make sure this is still the active thread for the main Tlen connection
- info->proto->debugLogA("Reconnecting to the network...");
- if (numRetry < MAX_CONNECT_RETRIES)
- numRetry++;
- oldStatus = info->proto->m_iStatus;
- info->proto->m_iStatus = ID_STATUS_CONNECTING + numRetry;
- ProtoBroadcastAck(info->proto->m_szModuleName, NULL, ACKTYPE_STATUS, ACKRESULT_SUCCESS, (HANDLE) oldStatus, info->proto->m_iStatus);
- continue;
- }
- else {
- info->proto->debugLogA("Thread ended, connection failed");
- mir_free(buffer);
- mir_free(info);
- return;
- }
- }
- info->proto->threadData = nullptr;
- }
- info->proto->debugLogA("Thread ended, connection failed");
- mir_free(buffer);
- mir_free(info);
- return;
- }
-
- // User may change status to OFFLINE while we are connecting above
- if (info->proto->m_iDesiredStatus != ID_STATUS_OFFLINE) {
-
- info->proto->isConnected = TRUE;
- mir_forkthread(TlenKeepAliveThread, info->proto);
-
- TlenXmlInitState(&xmlState);
- TlenXmlSetCallback(&xmlState, 1, ELEM_OPEN, (void (__cdecl *)(XmlNode *,void *))TlenProcessStreamOpening, info);
- TlenXmlSetCallback(&xmlState, 1, ELEM_CLOSE, (void (__cdecl *)(XmlNode *,void *))TlenProcessStreamClosing, info);
- TlenXmlSetCallback(&xmlState, 2, ELEM_CLOSE, (void (__cdecl *)(XmlNode *,void *))TlenProcessProtocol, info);
-
- info->useAES = FALSE;
-
- if (info->useEncryption) {
- TlenSend(info->proto, "<s s='1' v='9' t='06000106'>");
-
- } else {
- TlenSend(info->proto, "<s v='3'>");
- }
-
- info->proto->debugLogA("Entering main recv loop");
- datalen = 0;
-
- for (;;) {
- int recvResult, bytesParsed;
-
- if (info->useAES) {
- recvResult = TlenWsRecvAES(info->proto, buffer+datalen, tlenNetworkBufferSize-datalen, &info->aes_in_context, info->aes_in_iv);
- } else {
- recvResult = TlenWsRecv(info->proto, info->s, buffer+datalen, tlenNetworkBufferSize-datalen);
- }
-
- if (recvResult <= 0)
- break;
- datalen += recvResult;
-
- buffer[datalen] = '\0';
- info->proto->debugLogA("RECV:%s", buffer);
-
- bytesParsed = TlenXmlParse(&xmlState, buffer, datalen);
- info->proto->debugLogA("bytesParsed = %d", bytesParsed);
- if (bytesParsed > 0) {
- if (bytesParsed < datalen)
- memmove(buffer, buffer+bytesParsed, datalen-bytesParsed);
- datalen -= bytesParsed;
- }
- else if (datalen == tlenNetworkBufferSize) {
- tlenNetworkBufferSize += 2048;
- info->proto->debugLogA("Increasing network buffer size to %d", tlenNetworkBufferSize);
- if ((buffer=(char *) mir_realloc(buffer, tlenNetworkBufferSize+1)) == nullptr) {
- info->proto->debugLogA("Cannot reallocate more network buffer, go offline now");
- break;
- }
- }
- else {
- info->proto->debugLogA("Unknown state: bytesParsed=%d, datalen=%d, tlenNetworkBufferSize=%d", bytesParsed, datalen, tlenNetworkBufferSize);
- }
- }
-
- TlenXmlDestroyState(&xmlState);
-
- info->proto->isOnline = FALSE;
- info->proto->isConnected = FALSE;
-
- Menu_EnableItem(info->proto->hMenuMUC, false);
- if (info->proto->hMenuChats != nullptr)
- Menu_EnableItem(info->proto->hMenuChats, false);
-
- // Set status to offline
- char *szProto = info->proto->m_szModuleName;
- oldStatus = info->proto->m_iStatus;
- info->proto->m_iStatus = ID_STATUS_OFFLINE;
- ProtoBroadcastAck(szProto, NULL, ACKTYPE_STATUS, ACKRESULT_SUCCESS, (HANDLE) oldStatus, info->proto->m_iStatus);
-
- // Set all contacts to offline
- for (MCONTACT hContact = db_find_first(szProto); hContact; hContact = db_find_next(hContact, szProto))
- if (db_get_w(hContact, szProto, "Status", ID_STATUS_OFFLINE) != ID_STATUS_OFFLINE)
- db_set_w(hContact, szProto, "Status", ID_STATUS_OFFLINE);
-
- TlenListWipeSpecial(info->proto);
- }
- else {
- oldStatus = info->proto->m_iStatus;
- info->proto->m_iStatus = ID_STATUS_OFFLINE;
- ProtoBroadcastAck(info->proto->m_szModuleName, NULL, ACKTYPE_STATUS, ACKRESULT_SUCCESS, (HANDLE) oldStatus, info->proto->m_iStatus);
- }
-
- Netlib_CloseHandle(info->s);
-
- if (info->proto->tlenOptions.reconnect == FALSE)
- break;
-
- if (info->proto->threadData != info) // Make sure this is still the main Tlen connection thread
- break;
- reconnectTime = rand() % 10;
- info->proto->debugLogA("Sleeping %d seconds before automatic reconnecting...", reconnectTime);
- SleepEx(reconnectTime * 1000, TRUE);
- reconnectMaxTime = 20;
- if (info->proto->threadData != info) // Make sure this is still the main Tlen connection thread
- break;
- info->proto->debugLogA("Reconnecting to the network...");
- info->proto->m_iDesiredStatus = oldStatus; // Reconnect to my last status
- oldStatus = info->proto->m_iStatus;
- info->proto->m_iStatus = ID_STATUS_CONNECTING;
- numRetry = 1;
- ProtoBroadcastAck(info->proto->m_szModuleName, NULL, ACKTYPE_STATUS, ACKRESULT_SUCCESS, (HANDLE) oldStatus, info->proto->m_iStatus);
- }
-
- info->proto->debugLogA("Thread ended: server='%s'", info->server);
-
- if (info->proto->threadData == info) {
- info->proto->threadData = nullptr;
- }
-
- mir_free(buffer);
- if (info->streamId) mir_free(info->streamId);
- info->proto->debugLogA("Exiting ServerThread");
- mir_free(info);
-}
-
-static void TlenSendAuth(TlenProtocol *proto) {
- int iqId;
- char text[128];
- char *str = TlenPasswordHash(proto->threadData->password);
- mir_snprintf(text, "%s%s", proto->threadData->streamId, str);
- mir_free(str);
- str = TlenSha1(text);
- char *p=TlenTextEncode(proto->threadData->username);
- if (p != nullptr) {
- iqId = TlenSerialNext(proto->threadData->proto);
- TlenIqAdd(proto, iqId, IQ_PROC_NONE, TlenIqResultAuth);
- TlenSend(proto, "<iq type='set' id='" TLEN_IQID "%d'><query xmlns='jabber:iq:auth'><username>%s</username><digest>%s</digest><resource>t</resource><host>tlen.pl</host></query></iq>", iqId, p /*info->username*/, str);
- mir_free(p);
- }
- mir_free(str);
-}
-
-/* processing <s ... > tag sent from server on session opening */
-static void TlenProcessStreamOpening(XmlNode *node, ThreadData *info)
-{
- if (node->name == nullptr || mir_strcmp(node->name, "s"))
- return;
-
- char *sid=TlenXmlGetAttrValue(node, "i");
- if (sid != nullptr) {
- if (info->streamId) mir_free(info->streamId);
- info->streamId = mir_strdup(sid);
- }
- char *s=TlenXmlGetAttrValue(node, "s");
- if (s != nullptr && !mir_strcmp(s, "1")) {
- int i;
- unsigned char aes_key[32];
- char aes_key_str[140], aes_iv_str[40];
- mpi k1_mpi, k2_mpi, aes_mpi;
- size_t slen;
-
- char *k1=TlenXmlGetAttrValue(node, "k1");
- char *k2=TlenXmlGetAttrValue(node, "k2");
- char *k3=TlenXmlGetAttrValue(node, "k3");
-
- memset(&info->aes_in_context, 0, sizeof (aes_context));
- memset(&info->aes_out_context, 0, sizeof (aes_context));
- memset(&aes_mpi, 0, sizeof (mpi));
- mpi_read_string(&aes_mpi, 16, k3);
- mpi_write_binary(&aes_mpi, info->aes_in_iv, 16);
- for (i = 0; i < 16; i++) {
- info->aes_out_iv[i] = rand();
- aes_key[i] = rand();
- }
- memset(&aes_mpi, 0, sizeof (mpi));
- mpi_read_binary(&aes_mpi, info->aes_out_iv, 16);
- slen = 40;
- mpi_write_string(&aes_mpi, 16, aes_iv_str, &slen);
- aes_setkey_dec(&info->aes_in_context, aes_key, 128);
- aes_setkey_enc(&info->aes_out_context, aes_key, 128);
- memset(&aes_mpi, 0, sizeof (mpi));
- mpi_read_binary(&aes_mpi, aes_key, 16);
- memset(&k1_mpi, 0, sizeof (mpi));
- mpi_read_string( &k1_mpi, 16, k1 );
- memset(&k2_mpi, 0, sizeof (mpi));
- mpi_read_string( &k2_mpi, 16, k2 );
- memset(&aes_mpi, 0, sizeof (mpi));
- mpi_read_binary(&aes_mpi, (unsigned char *)aes_key, 16);
- mpi_exp_mod( &aes_mpi, &aes_mpi, &k1_mpi, &k2_mpi, nullptr );
- slen = 140;
- mpi_write_string(&aes_mpi, 16, aes_key_str, &slen);
- TlenSend(info->proto, "<cipher k1='%s' k2='%s'/>", aes_key_str, aes_iv_str);
- } else {
- TlenSendAuth(info->proto);
- }
-}
-
-/* processing </s> tag sent from server on session close */
-static void TlenProcessStreamClosing(XmlNode *node, ThreadData *info)
-{
- Netlib_CloseHandle(info->proto);
- if (node->name && !mir_strcmp(node->name, "stream:error") && node->text){
- char buffer[1024];
- mir_snprintf(buffer, "%s\n%s", Translate("Tlen Connection Error"), Translate(node->text));
- PUShowMessage(buffer, SM_WARNING);
- } else if (!mir_strcmp(node->name, "s")){
- info->proto->debugLogA("Disconnected server message");
- }
-}
-
-/* processing session tags sent from server */
-static void TlenProcessProtocol(XmlNode *node, ThreadData *info)
-{
- if (!mir_strcmp(node->name, "message"))
- TlenProcessMessage(node, info);
- else if (!mir_strcmp(node->name, "presence"))
- TlenProcessPresence(node, info->proto);
- else if (!mir_strcmp(node->name, "iq"))
- TlenProcessIq(node, info);
- else if (!mir_strcmp(node->name, "f"))
- TlenProcessF(node, info);
- else if (!mir_strcmp(node->name, "w"))
- TlenProcessW(node, info);
- else if (!mir_strcmp(node->name, "m"))
- TlenProcessM(node, info);
- else if (!mir_strcmp(node->name, "n"))
- TlenProcessN(node, info);
- else if (!mir_strcmp(node->name, "p"))
- TlenProcessP(node, info);
- else if (!mir_strcmp(node->name, "v"))
- TlenProcessV(node, info);
- else if (!mir_strcmp(node->name, "avatar"))
- TlenProcessAvatar(node, info);
- else if (!mir_strcmp(node->name, "cipher"))
- TlenProcessCipher(node, info);
- else
- info->proto->debugLogA("Invalid top-level tag (only <message/> <presence/> <iq/> <f/> <w/> <m/> <n/> <p/> <v/> <cipher/> and <avatar/> allowed)");
-
-}
-
-static void TlenProcessCipher(XmlNode*, ThreadData *info)
-{
- info->useAES = TRUE;
- TlenSend(info->proto, "<cipher type='ok'/>");
- TlenSendAuth(info->proto);
-}
-
-static void TlenProcessIqGetVersion(TlenProtocol *proto, XmlNode *node)
-{
- OSVERSIONINFO osvi = { 0 };
- char mversion[256];
- char *mver;
- char* os = nullptr;
-
- if (proto->m_iStatus == ID_STATUS_INVISIBLE) return;
- if (!proto->tlenOptions.enableVersion) return;
- char *from = TlenXmlGetAttrValue(node, "from");
- if (from == nullptr)
- return;
-
- TLEN_LIST_ITEM *item = TlenListGetItemPtr(proto, LIST_ROSTER, from);
- if (item == nullptr)
- return;
-
- char *version = TlenTextEncode(TLEN_VERSION_STRING);
- osvi.dwOSVersionInfoSize = sizeof( OSVERSIONINFO );
- if ( GetVersionEx( &osvi )) {
- switch ( osvi.dwPlatformId ) {
- case VER_PLATFORM_WIN32_NT:
- if ( osvi.dwMajorVersion == 5 ) {
- if ( osvi.dwMinorVersion == 2 ) os = TlenTextEncode("Windows Server 2003");
- else if ( osvi.dwMinorVersion == 1 ) os = TlenTextEncode("Windows XP");
- else if ( osvi.dwMinorVersion == 0 ) os = TlenTextEncode("Windows 2000");
- }
- else if ( osvi.dwMajorVersion <= 4 ) {
- os = TlenTextEncode("Windows NT");
- }
- break;
- case VER_PLATFORM_WIN32_WINDOWS:
- if ( osvi.dwMajorVersion == 4 ) {
- if ( osvi.dwMinorVersion == 0 ) os = TlenTextEncode("Windows 95");
- if ( osvi.dwMinorVersion == 10 ) os = TlenTextEncode("Windows 98");
- if ( osvi.dwMinorVersion == 90 ) os = TlenTextEncode("Windows ME");
- }
- break;
- } }
-
- if ( os == nullptr ) os = TlenTextEncode("Windows");
-
- mir_strcpy(mversion, "Miranda NG ");
- Miranda_GetVersionText(mversion + 11, sizeof(mversion) - 11);
- mir_strcat(mversion, " (Tlen v.");
- mir_strcat(mversion, TLEN_VERSION_STRING);
- mir_strcat(mversion, ")");
- mver = TlenTextEncode( mversion );
- TlenSend( proto, "<message to='%s' type='iq'><iq type='result'><query xmlns='jabber:iq:version'><name>%s</name><version>%s</version><os>%s</os></query></iq></message>", from, mver?mver:"", version?version:"", os?os:"" );
- if (!item->versionRequested) {
- item->versionRequested = TRUE;
- TlenSend(proto, "<message to='%s' type='iq'><iq type='get'><query xmlns='jabber:iq:version'/></iq></message>", from);
- }
-
- if ( mver ) mir_free( mver );
- if ( version ) mir_free( version );
- if ( os ) mir_free( os );
-}
-
-// Support for Tlen avatars - avatar token used to access web interface
-static void TlenProcessAvatar(XmlNode* node, ThreadData *info)
-{
- XmlNode *tokenNode = TlenXmlGetChild(node, "token");
- XmlNode *aNode = TlenXmlGetChild(node, "a");
- if (tokenNode != nullptr) {
- char *token = tokenNode->text;
- strncpy(info->avatarToken, token, sizeof(info->avatarToken)-1);
- }
- if (aNode != nullptr) {
- if (TlenProcessAvatarNode(info->proto, node, nullptr)) {
- }
- }
-}
-
-static void TlenProcessMessage(XmlNode *node, ThreadData *info)
-{
- MCONTACT hContact;
- XmlNode *bodyNode, *subjectNode, *xNode, *n;
- char *nick, *p, *localMessage, *idStr;
- DWORD msgTime;
- BOOL delivered, composing;
- int i;
-
- if (!node->name || mir_strcmp(node->name, "message")) return;
-
- char *type=TlenXmlGetAttrValue(node, "type");
- if (type != nullptr && !mir_strcmp(type, "error")) {
- }
- else {
- char *from=TlenXmlGetAttrValue(node, "from");
- if (from != nullptr) {
- if (info->proto->tlenOptions.ignoreAdvertisements && strstr(from, "b73@tlen.pl") == from) {
- return;
- }
- char *fromJid = TlenLoginFromJID(from);
- // If message is from a stranger (not in roster), item is NULL
- TLEN_LIST_ITEM *item = TlenListGetItemPtr(info->proto, LIST_ROSTER, fromJid);
- BOOL isChatRoomJid = TlenListExist(info->proto, LIST_CHATROOM, from);
-
- if (isChatRoomJid && type != nullptr && !mir_strcmp(type, "groupchat")) {
- //TlenGroupchatProcessMessage(node, userdata);
- } else if (type != nullptr && !mir_strcmp(type, "pic")) {
- TlenProcessPic(node, info->proto);
- } else if (type != nullptr && !mir_strcmp(type, "iq")) {
- XmlNode *iqNode;
- // Tlen-compatible iq
- if ((iqNode=TlenXmlGetChild(node, "iq")) != nullptr) {
- TlenXmlAddAttr(iqNode, "from", from);
- TlenProcessIq(iqNode, info);
- }
- } else {
- if ((bodyNode=TlenXmlGetChild(node, "body")) != nullptr) {
- if (bodyNode->text != nullptr) {
- if ((subjectNode=TlenXmlGetChild(node, "subject")) != nullptr && subjectNode->text != nullptr && subjectNode->text[0] != '\0') {
- size_t size = mir_strlen(subjectNode->text)+mir_strlen(bodyNode->text)+5;
- p = (char *)mir_alloc(size);
- mir_snprintf(p, size, "%s\r\n%s", subjectNode->text, bodyNode->text);
- localMessage = TlenTextDecode(p);
- mir_free(p);
- } else {
- localMessage = TlenTextDecode(bodyNode->text);
- }
-
- msgTime = 0;
- delivered = composing = FALSE;
- i = 1;
- while ((xNode=TlenXmlGetNthChild(node, "x", i)) != nullptr) {
- if ((p=TlenXmlGetAttrValue(xNode, "xmlns")) != nullptr) {
- if (!mir_strcmp(p, "jabber:x:delay") && msgTime==0) {
- if ((p=TlenXmlGetAttrValue(xNode, "stamp")) != nullptr) {
- msgTime = TlenIsoToUnixTime(p);
- }
- }
- else if (!mir_strcmp(p, "jabber:x:event")) {
- // Check whether any event is requested
- if (!delivered && (n=TlenXmlGetChild(xNode, "delivered")) != nullptr) {
- delivered = TRUE;
- idStr = TlenXmlGetAttrValue(node, "id");
- TlenSend(info->proto, "<message to='%s'><x xmlns='jabber:x:event'><delivered/><id>%s</id></x></message>", from, (idStr != nullptr)?idStr:"");
- }
- if (item != nullptr && TlenXmlGetChild(xNode, "composing") != nullptr) {
- composing = TRUE;
- if (item->messageEventIdStr)
- mir_free(item->messageEventIdStr);
- idStr = TlenXmlGetAttrValue(node, "id");
- item->messageEventIdStr = (idStr == nullptr)?nullptr:mir_strdup(idStr);
- }
- }
- }
- i++;
- }
-
- if (item != nullptr) {
- item->wantComposingEvent = composing;
- if (item->isTyping) {
- item->isTyping = FALSE;
- if ((hContact=TlenHContactFromJID(info->proto, fromJid)) != NULL)
- CallService(MS_PROTO_CONTACTISTYPING, hContact, PROTOTYPE_CONTACTTYPING_OFF);
- }
- }
-
- if ((hContact=TlenHContactFromJID(info->proto, fromJid)) == NULL) {
- // Create a temporary contact
- if (isChatRoomJid) {
- if ((p=strchr(from, '/')) != nullptr && p[1]!='\0')
- p++;
- else
- p = from;
- nick = TlenTextEncode(p);
- hContact = TlenDBCreateContact(info->proto, from, nick, TRUE);
- }
- else {
- nick = TlenLocalNickFromJID(from);
- hContact = TlenDBCreateContact(info->proto, from, nick, TRUE);
- }
- mir_free(nick);
- }
-
- if (msgTime == 0) {
- msgTime = time(nullptr);
- } else {
- MEVENT hDbEvent = db_event_last(hContact);
- if (hDbEvent != NULL) {
- DBEVENTINFO dbei = {};
- db_event_get( hDbEvent, &dbei);
- if (msgTime < dbei.timestamp) {
- msgTime = dbei.timestamp + 1;
- }
- }
- if (msgTime > (DWORD)time(nullptr)) {
- msgTime = time(nullptr);
- }
- }
- char* localMessage_Utf8 = mir_utf8encode(localMessage);
-
- PROTORECVEVENT recv = { 0 };
- recv.timestamp = (DWORD) msgTime;
- recv.szMessage = localMessage_Utf8;
- recv.lParam = 0;
- ProtoChainRecvMsg(hContact, &recv);
-
- mir_free(localMessage_Utf8);
- mir_free(localMessage);
- }
- }
- }
- mir_free(fromJid);
- }
- }
-}
-
-static void TlenProcessIq(XmlNode *node, ThreadData *info)
-{
- MCONTACT hContact;
- XmlNode *queryNode = nullptr;
- char *jid, *nick;
- char *xmlns = nullptr;
- char *idStr, *str;
- int id;
- int i;
-
- if (!node->name || mir_strcmp(node->name, "iq")) return;
- char *type=TlenXmlGetAttrValue(node, "type");
-// if ((type=TlenXmlGetAttrValue(node, "type")) == NULL) return;
-
- id = -1;
- if ((idStr=TlenXmlGetAttrValue(node, "id")) != nullptr) {
- if (!strncmp(idStr, TLEN_IQID, mir_strlen(TLEN_IQID)))
- id = atoi(idStr+mir_strlen(TLEN_IQID));
- }
-
- queryNode = TlenXmlGetChild(node, "query");
- if (queryNode != nullptr) {
- xmlns = TlenXmlGetAttrValue(queryNode, "xmlns");
- }
-
-
- /////////////////////////////////////////////////////////////////////////
- // MATCH BY ID
- /////////////////////////////////////////////////////////////////////////
- TLEN_IQ_PFUNC pfunc=TlenIqFetchFunc(info->proto, id);
- if (pfunc != nullptr) {
- info->proto->debugLogA("Handling iq request for id=%d", id);
- pfunc(info->proto, node);
- /////////////////////////////////////////////////////////////////////////
- // MORE GENERAL ROUTINES, WHEN ID DOES NOT MATCH
- /////////////////////////////////////////////////////////////////////////
- // new p2p connections
- } else if (xmlns != nullptr && !mir_strcmp(xmlns, "p2p")) {
- if (info->proto->tlenOptions.useNewP2P) {
- TlenProcessP2P(node, info);
- }
- }
- // RECVED: <iq type='set'><query ...
- else if (!mir_strcmp(type, "set") && queryNode != nullptr && xmlns != nullptr) {
-
- // RECVED: roster push
- // ACTION: similar to iqIdGetRoster above
- if (!mir_strcmp(xmlns, "jabber:iq:roster")) {
- XmlNode *itemNode, *groupNode;
- TLEN_LIST_ITEM *item;
- char *name;
-
- info->proto->debugLogA("<iq/> Got roster push, query has %d children", queryNode->numChild);
- for (i=0; i<queryNode->numChild; i++) {
- itemNode = queryNode->child[i];
- if (!mir_strcmp(itemNode->name, "item")) {
- if ((jid=TlenXmlGetAttrValue(itemNode, "jid")) != nullptr) {
- if ((str=TlenXmlGetAttrValue(itemNode, "subscription")) != nullptr) {
- // we will not add new account when subscription=remove
- if (!mir_strcmp(str, "to") || !mir_strcmp(str, "both") || !mir_strcmp(str, "from") || !mir_strcmp(str, "none")) {
- if ((name=TlenXmlGetAttrValue(itemNode, "name")) != nullptr) {
- nick = TlenTextDecode(name);
- } else {
- nick = TlenLocalNickFromJID(jid);
- }
- if (nick != nullptr) {
- if ((item=TlenListAdd(info->proto, LIST_ROSTER, jid)) != nullptr) {
- if (item->nick) mir_free(item->nick);
- item->nick = nick;
- if ((hContact=TlenHContactFromJID(info->proto, jid)) == NULL) {
- // Received roster has a new JID.
- // Add the jid (with empty resource) to Miranda contact list.
- hContact = TlenDBCreateContact(info->proto, jid, nick, FALSE);
- }
- db_set_s(hContact, "CList", "MyHandle", nick);
- if (item->group) mir_free(item->group);
- if ((groupNode=TlenXmlGetChild(itemNode, "group")) != nullptr && groupNode->text != nullptr) {
- item->group = TlenGroupDecode(groupNode->text);
- Clist_GroupCreate(0, _A2T(item->group));
- db_set_s(hContact, "CList", "Group", item->group);
- }
- else {
- item->group = nullptr;
- db_unset(hContact, "CList", "Group");
- }
- if (!mir_strcmp(str, "none") || (!mir_strcmp(str, "from") && strchr(jid, '@') != nullptr)) {
- if (db_get_w(hContact, info->proto->m_szModuleName, "Status", ID_STATUS_OFFLINE) != ID_STATUS_OFFLINE)
- db_set_w(hContact, info->proto->m_szModuleName, "Status", ID_STATUS_OFFLINE);
- }
- }
- else {
- mir_free(nick);
- }
- }
- }
- if ((item=TlenListGetItemPtr(info->proto, LIST_ROSTER, jid)) != nullptr) {
- if (!mir_strcmp(str, "both"))
- item->subscription = SUB_BOTH;
- else if (!mir_strcmp(str, "to"))
- item->subscription = SUB_TO;
- else if (!mir_strcmp(str, "from"))
- item->subscription = SUB_FROM;
- else
- item->subscription = SUB_NONE;
- info->proto->debugLogA("Roster push for jid=%s, set subscription to %s", jid, str);
- // subscription = remove is to remove from roster list
- // but we will just set the contact to offline and not actually
- // remove, so that history will be retained.
- if (!mir_strcmp(str, "remove")) {
- if ((hContact=TlenHContactFromJID(info->proto, jid)) != NULL) {
- if (db_get_w(hContact, info->proto->m_szModuleName, "Status", ID_STATUS_OFFLINE) != ID_STATUS_OFFLINE)
- db_set_w(hContact, info->proto->m_szModuleName, "Status", ID_STATUS_OFFLINE);
- TlenListRemove(info->proto, LIST_ROSTER, jid);
- }
- }
- }
- }
- }
- }
- }
- }
-
- }
- // RECVED: <iq type='get'><query ...
- else if ( !mir_strcmp( type, "get" ) && queryNode != nullptr && xmlns != nullptr ) {
- // RECVED: software version query
- // ACTION: return my software version
- if ( !mir_strcmp( xmlns, "jabber:iq:version" )) TlenProcessIqGetVersion(info->proto, node);
- }
- // RECVED: <iq type='result'><query ...
- else if ( !mir_strcmp( type, "result") && queryNode != nullptr) {
- if (xmlns != nullptr ) {
- if ( !mir_strcmp(xmlns, "jabber:iq:roster" )) {
- TlenIqResultRoster(info->proto, node);
- } else if ( !mir_strcmp( xmlns, "jabber:iq:version" )) {
- TlenIqResultVersion(info->proto, node);
- } else if ( !mir_strcmp( xmlns, "jabber:iq:info" )) {
- TlenIqResultInfo(info->proto, node);
- }
- } else {
- char *from;
- if (( from=TlenXmlGetAttrValue( node, "from" )) != nullptr ) {
- if ( !mir_strcmp(from, "tcfg" )) {
- TlenIqResultTcfg(info->proto, node);
- }
- }
- }
- }
- // RECVED: <iq type='error'> ...
- else if (!mir_strcmp(type, "error")) {
- TLEN_LIST_ITEM *item;
- // Check for file transfer deny by comparing idStr with ft->iqId
- i = 0;
- while ((i=TlenListFindNext(info->proto, LIST_FILE, i)) >= 0) {
- item = TlenListGetItemPtrFromIndex(info->proto,i);
- if (item->ft->state==FT_CONNECTING && !mir_strcmp(idStr, item->ft->iqId)) {
- item->ft->state = FT_DENIED;
- if (item->ft->hFileEvent != nullptr)
- SetEvent(item->ft->hFileEvent); // Simulate the termination of file server connection
- }
- i++;
- }
- }
-}
-
-/*
- * Web messages
- */
-static void TlenProcessW(XmlNode *node, ThreadData *info)
-{
- char *e, *s;
- char *str, *localMessage;
- int strSize;
-
- if (!node->name || mir_strcmp(node->name, "w"))
- return;
-
- char *body=node->text;
- if (body == nullptr)
- return;
-
- char *f = TlenXmlGetAttrValue(node, "f");
- if (f != nullptr) {
- char webContactName[128];
- mir_snprintf(webContactName, Translate("%s Web Messages"), info->proto->m_szModuleName);
- MCONTACT hContact = TlenHContactFromJID(info->proto, webContactName);
- if (hContact == NULL) {
- hContact = TlenDBCreateContact(info->proto, webContactName, webContactName, TRUE);
- }
-
- s = TlenXmlGetAttrValue(node, "s");
- e = TlenXmlGetAttrValue(node, "e");
-
- str = nullptr;
- strSize = 0;
- TlenStringAppend(&str, &strSize, "%s\r\n%s: ", Translate("Web message"), Translate("From"));
-
- if (f != nullptr)
- TlenStringAppend(&str, &strSize, "%s", f);
- TlenStringAppend(&str, &strSize, "\r\n%s: ", Translate("E-mail"));
- if (e != nullptr)
- TlenStringAppend(&str, &strSize, "%s", e);
- TlenStringAppend(&str, &strSize, "\r\n\r\n%s", body);
-
- localMessage = TlenTextDecode(str);
- char* localMessage_Utf8 = mir_utf8encode(localMessage);
-
- PROTORECVEVENT recv = { 0 };
- recv.timestamp = (DWORD) time(nullptr);
- recv.szMessage = localMessage_Utf8;
- ProtoChainRecvMsg(hContact, &recv);
-
- mir_free(localMessage_Utf8);
- mir_free(localMessage);
- mir_free(str);
- }
-}
-
-/*
- * Typing notification, multi-user conference messages and invitations
- */
-static void TlenProcessM(XmlNode *node, ThreadData *info)
-{
- char *p, *n, *r, *s, *str, *localMessage;
- int i;
- XmlNode *xNode, *invNode, *bNode;
-
- if (!node->name || mir_strcmp(node->name, "m")) return;
-
- char *f = TlenXmlGetAttrValue(node, "f"); //, *from;//username
- if (f != nullptr) {
- char *fLogin = TlenLoginFromJID(f);
- MCONTACT hContact=TlenHContactFromJID(info->proto, fLogin);
- if (hContact != NULL) {
- char *tp=TlenXmlGetAttrValue(node, "tp");//typing start/stop
- if (tp != nullptr) {
- TLEN_LIST_ITEM *item = TlenListGetItemPtr(info->proto, LIST_ROSTER, fLogin);
- if (!mir_strcmp(tp, "t")) { //contact is writing
- if (item != nullptr ) {
- item->isTyping = TRUE;
- CallService(MS_PROTO_CONTACTISTYPING, hContact, (LPARAM)PROTOTYPE_CONTACTTYPING_INFINITE);
- }
- }
- else if (!mir_strcmp(tp, "u")) {//contact stopped writing
- if (item != nullptr) {
- item->isTyping = FALSE;
- CallService(MS_PROTO_CONTACTISTYPING, hContact, (LPARAM)PROTOTYPE_CONTACTTYPING_OFF);
- }
- }
- else if (!mir_strcmp(tp, "a")) {//alert was received
- int bAlert = TRUE;
- if (info->proto->tlenOptions.alertPolicy == TLEN_ALERTS_IGNORE_ALL) {
- bAlert = FALSE;
- } else if (info->proto->tlenOptions.alertPolicy == TLEN_ALERTS_IGNORE_NIR) {
- bAlert = IsAuthorized(info->proto, fLogin);
- }
- if (bAlert) {
- if (info->proto->tlenOptions.useNudge)
- NotifyEventHooks(info->proto->hTlenNudge, hContact, 0);
- else {
- if (info->proto->tlenOptions.logAlerts)
- TlenLogMessage(info->proto, hContact, 0, Translate("An alert has been received."));
- Skin_PlaySound("TlenAlertNotify");
- }
- }
- }
- }
- }
- mir_free(fLogin);
- if ((p=strchr(f, '@')) != nullptr) {
- if ((p=strchr(p, '/')) != nullptr && p[1]!='\0') { // message from user
- time_t timestamp;
- s = TlenXmlGetAttrValue(node, "s");
- if (s != nullptr) {
- timestamp = TlenTimeToUTC(atol(s));
- if (timestamp > time(nullptr)) {
- timestamp = time(nullptr);
- }
- } else {
- timestamp = time(nullptr);
- }
- char *tp=TlenXmlGetAttrValue(node, "tp");//typing start/stop
- bNode = TlenXmlGetChild(node, "b");
- f = TlenTextDecode(f);
- if (bNode != nullptr && bNode->text != nullptr) {
- if (tp != nullptr && !mir_strcmp(tp, "p")) {
- /* MUC private message */
- str = TlenResourceFromJID(f);
- hContact = TlenDBCreateContact(info->proto, f, str, TRUE);
- db_set_b(hContact, info->proto->m_szModuleName, "bChat", TRUE);
- mir_free(str);
- localMessage = TlenTextDecode(bNode->text);
- char* localMessage_Utf8 = mir_utf8encode(localMessage);
-
- PROTORECVEVENT recv = { 0 };
- recv.timestamp = (DWORD) timestamp;
- recv.szMessage = localMessage_Utf8;
- ProtoChainRecvMsg(hContact, &recv);
-
- mir_free(localMessage_Utf8);
- mir_free(localMessage);
- }
- }
- mir_free(f);
- }
- }
- i=1;
- while ((xNode=TlenXmlGetNthChild(node, "x", i)) != nullptr) {
- invNode=TlenXmlGetChild(xNode, "inv");
- if (invNode != nullptr) {
- r = TlenTextDecode(f);
- f = TlenXmlGetAttrValue(invNode, "f");
- f = TlenTextDecode(f);
- n = TlenXmlGetAttrValue(invNode, "n");
- if (n != nullptr && strstr(r, n) != r) {
- n = TlenTextDecode(n);
- } else {
- n = mir_strdup(Translate("Private conference"));
- //n = TlenNickFromJID(r);
- }
- TlenMUCRecvInvitation(info->proto, r, n, f, "");
- mir_free(n);
- mir_free(r);
- mir_free(f);
- break;
- }
- i++;
- }
- }
-}
-
-static void TlenMailPopup(TlenProtocol *proto, char *title, char *emailInfo)
-{
- if ( !ServiceExists(MS_POPUP_ADDPOPUPT))
- return;
- if (!db_get_b(NULL, proto->m_szModuleName, "MailPopupEnabled", TRUE))
- return;
-
- POPUPDATAT ppd = { 0 };
- ppd.lchIcon = LoadIcon(hInst, MAKEINTRESOURCE(IDI_MAIL));
- wcsncpy(ppd.lptzContactName, _A2T(title), MAX_CONTACTNAME -1);
- wcsncpy(ppd.lptzText, _A2T(emailInfo), MAX_SECONDLINE - 1);
- ppd.colorBack = db_get_dw(NULL, proto->m_szModuleName, "MailPopupBack", 0);
- ppd.colorText = db_get_dw(NULL, proto->m_szModuleName, "MailPopupText", 0);
- BYTE delayMode = db_get_b(NULL, proto->m_szModuleName, "MailPopupDelayMode", 0);
- if (delayMode == 1)
- ppd.iSeconds = db_get_dw(NULL, proto->m_szModuleName, "MailPopupDelay", 4);
- else if (delayMode == 2)
- ppd.iSeconds = -1;
- PUAddPopupT(&ppd);
-}
-/*
- * Incoming e-mail notification
- */
-static void TlenProcessN(XmlNode *node, ThreadData *info)
-{
- char *str, *popupTitle, *popupText;
- int strSize;
-
- if (!node->name || mir_strcmp(node->name, "n")) return;
-
- char *s = TlenXmlGetAttrValue(node, "s");
- char *f = TlenXmlGetAttrValue(node, "f");
- if (s != nullptr && f != nullptr) {
- str = nullptr;
- strSize = 0;
-
- TlenStringAppend(&str, &strSize, Translate("%s mail"), info->proto->m_szModuleName);
- popupTitle = TlenTextDecode(str);
- mir_free(str);
-
- str = nullptr;
- strSize = 0;
-
- TlenStringAppend(&str, &strSize, "%s: %s\n", Translate("From"), f);
- TlenStringAppend(&str, &strSize, "%s: %s", Translate("Subject"), s);
- popupText = TlenTextDecode(str);
- TlenMailPopup(info->proto, popupTitle, popupText);
- Skin_PlaySound("TlenMailNotify");
-
- mir_free(popupTitle);
- mir_free(popupText);
- mir_free(str);
- }
-}
-
-/*
- * Presence is chat rooms
- */
-static void TlenProcessP(XmlNode *node, ThreadData*)
-{
- char *f, *a, *k;
- XmlNode *sNode, *xNode, *iNode, *kNode;
- int status, flags;
-
- if (!node->name || mir_strcmp(node->name, "p")) return;
-
-// presence from users in chat room
- flags = 0;
- status = ID_STATUS_ONLINE;
- f = TlenXmlGetAttrValue(node, "f");
- xNode = TlenXmlGetChild(node, "x");
- if (xNode != nullptr) { // x subtag present (message from chat room) - change user rights only
- char *temp, *iStr;
- iNode = TlenXmlGetChild(xNode, "i");
- if (iNode != nullptr) {
- iStr = TlenXmlGetAttrValue(iNode, "i");
- temp = (char*)mir_alloc(mir_strlen(f)+mir_strlen(iStr)+2);
- mir_strcpy(temp, f);
- mir_strcat(temp, "/");
- mir_strcat(temp, iStr);
- f = TlenTextDecode(temp);
- mir_free(temp);
- node = iNode;
- status = 0;
- } else {
- f = TlenTextDecode(f);
- }
- } else {
- f = TlenTextDecode(f);
- }
- a = TlenXmlGetAttrValue(node, "z");
- if (a != nullptr) {
- if (atoi(a) &1 ) {
- flags |= USER_FLAGS_REGISTERED;
- }
- }
- a = TlenXmlGetAttrValue(node, "a");
- if (a != nullptr) {
- if (atoi(a) == 2) {
- flags |= USER_FLAGS_ADMIN;
- }
- if (atoi(a) == 1) {
- flags |= USER_FLAGS_OWNER;
- }
- if (atoi(a) == 3) {
- //flags |= USER_FLAGS_MEMBER;
- }
- if (atoi(a) == 5) {
- flags |= USER_FLAGS_GLOBALOWNER;
- }
- }
- sNode = TlenXmlGetChild(node, "s");
- if (sNode != nullptr) {
- if (!mir_strcmp(sNode->text, "unavailable")) {
- status = ID_STATUS_OFFLINE;
- }
- }
- kNode = TlenXmlGetChild(node, "kick");
- k = nullptr;
- if (kNode != nullptr) {
- k = TlenXmlGetAttrValue(kNode, "r");
- if (k == nullptr) {
- k = "";
- }
- k = TlenTextDecode(k);
- }
- if (k != nullptr) {
- mir_free(k);
- }
- mir_free(f);
-}
-/*
- * Voice chat
- */
-static void TlenProcessV(XmlNode *node, ThreadData *info)
-{
- char jid[128];
- TLEN_LIST_ITEM *item;
- char *id, *e, *p;
-// if (!node->name || mir_strcmp(node->name, "v")) return;
-
- char *from=TlenXmlGetAttrValue(node, "f");
- if (from != nullptr) {
- if (strchr(from, '@') == nullptr) {
- mir_snprintf(jid, "%s@%s", from, info->server);
- } else {
- strncpy_s(jid, from, _TRUNCATE);
- }
- if ((e=TlenXmlGetAttrValue(node, "e")) != nullptr) {
- if (!mir_strcmp(e, "1")) {
- if ((id=TlenXmlGetAttrValue(node, "i")) != nullptr) {
- Skin_PlaySound("TlenVoiceNotify");
- TlenVoiceAccept(info->proto, id, from);
- }
- } else if (!mir_strcmp(e, "3")) {
- // FILE_RECV : e='3' : invalid transfer error
- if ((p=TlenXmlGetAttrValue(node, "i")) != nullptr) {
- if ((item=TlenListGetItemPtr(info->proto, LIST_VOICE, p)) != nullptr) {
- if (item->ft != nullptr) {
- HANDLE hEvent = item->ft->hFileEvent;
- item->ft->hFileEvent = nullptr;
- item->ft->state = FT_ERROR;
- if (item->ft->s != nullptr) {
- Netlib_CloseHandle(item->ft->s);
- item->ft->s = nullptr;
- if (hEvent != nullptr) {
- SetEvent(hEvent);
- }
- } else {
- TlenP2PFreeFileTransfer(item->ft);
- }
- } else {
- TlenListRemove(info->proto, LIST_VOICE, p);
- }
- }
- }
- } else if (!mir_strcmp(e, "4")) {
- // FILE_SEND : e='4' : File sending request was denied by the remote client
- if ((p=TlenXmlGetAttrValue(node, "i")) != nullptr) {
- if ((item=TlenListGetItemPtr(info->proto, LIST_VOICE, p)) != nullptr) {
- if (!mir_strcmp(item->ft->jid, jid)) {
- TlenVoiceCancelAll(info->proto);
- //TlenListRemove(info->proto, LIST_VOICE, p);
- }
- }
- }
- } else if (!mir_strcmp(e, "5")) {
- // FILE_SEND : e='5' : Voice request was accepted
- if ((p=TlenXmlGetAttrValue(node, "i")) != nullptr) {
- if ((item=TlenListGetItemPtr(info->proto, LIST_VOICE, p)) != nullptr) {
- info->proto->debugLogA("should start voice 1 ? %s ?? %s", jid, item->ft->jid);
- if (!mir_strcmp(item->ft->jid, jid)) {
- info->proto->debugLogA("starting voice 1");
- TlenVoiceStart(item->ft, 1);
- }
- }
- }
- } else if (!mir_strcmp(e, "6")) {
- // FILE_RECV : e='6' : IP and port information to connect to get file
- if ((p=TlenXmlGetAttrValue(node, "i")) != nullptr) {
- if ((item=TlenListGetItemPtr(info->proto, LIST_VOICE, p)) != nullptr) {
- if ((p=TlenXmlGetAttrValue(node, "a")) != nullptr) {
- item->ft->hostName = mir_strdup(p);
- if ((p=TlenXmlGetAttrValue(node, "p")) != nullptr) {
- item->ft->wPort = atoi(p);
- TlenVoiceStart(item->ft, 0);
- //forkthread((pThreadFunc)TlenVoiceReceiveThread, 0, item->ft);
- }
- }
- }
- }
- }
- else if (!mir_strcmp(e, "7")) {
- // FILE_RECV : e='7' : IP and port information to connect to send file
- // in case the conection to the given server was not successful
- if ((p=TlenXmlGetAttrValue(node, "i")) != nullptr) {
- if ((item=TlenListGetItemPtr(info->proto, LIST_VOICE, p)) != nullptr) {
- if ((p=TlenXmlGetAttrValue(node, "a")) != nullptr) {
- if (item->ft->hostName != nullptr) mir_free(item->ft->hostName);
- item->ft->hostName = mir_strdup(p);
- if ((p=TlenXmlGetAttrValue(node, "p")) != nullptr) {
- item->ft->wPort = atoi(p);
- item->ft->state = FT_SWITCH;
- SetEvent(item->ft->hFileEvent);
- }
- }
- }
- }
- }
- else if (!mir_strcmp(e, "8")) {
- // FILE_RECV : e='8' : transfer error
- if ((p=TlenXmlGetAttrValue(node, "i")) != nullptr) {
- if ((item=TlenListGetItemPtr(info->proto, LIST_VOICE, p)) != nullptr) {
- item->ft->state = FT_ERROR;
- SetEvent(item->ft->hFileEvent);
- }
- }
- }
-
- }
- }
-}
-
-static void __cdecl TlenKeepAliveThread(void *ptr)
-{
- TlenProtocol *proto = (TlenProtocol *)ptr;
-
- NETLIBSELECT nls = {};
- nls.dwTimeout = 60000; // 60000 millisecond (1 minute)
- nls.hExceptConns[0] = proto->threadData->s;
- for (;;) {
- if (Netlib_Select(&nls) != 0)
- break;
- if (proto->tlenOptions.sendKeepAlive)
- TlenSend(proto, " \t ");
- }
- proto->debugLogA("Exiting KeepAliveThread");
-}
-
diff --git a/protocols/Tlen/src/tlen_userinfo.cpp b/protocols/Tlen/src/tlen_userinfo.cpp
deleted file mode 100644
index 8bf7c07045..0000000000
--- a/protocols/Tlen/src/tlen_userinfo.cpp
+++ /dev/null
@@ -1,328 +0,0 @@
-/*
-
-Jabber Protocol Plugin for Miranda IM
-Tlen Protocol Plugin for Miranda NG
-Copyright (C) 2002-2004 Santithorn Bunchua
-Copyright (C) 2004-2007 Piotr Piastucki
-
-This program is free software; you can redistribute it and/or
-modify it under the terms of the GNU General Public License
-as published by the Free Software Foundation; either version 2
-of the License, or (at your option) any later version.
-
-This program 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 General Public License for more details.
-
-You should have received a copy of the GNU General Public License
-along with this program; if not, write to the Free Software
-Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
-
-*/
-
-#include "stdafx.h"
-#include <commctrl.h>
-#include <io.h>
-#include <fcntl.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include "tlen_list.h"
-#include "resource.h"
-#include "tlen_avatar.h"
-
-TLEN_FIELD_MAP tlenFieldGender[] = {
- { 1, L"Male" },
- { 2, L"Female" },
- { 0, nullptr }
-};
-TLEN_FIELD_MAP tlenFieldLookfor[] = {
- { 1, L"Somebody to talk" },
- { 2, L"Friendship" },
- { 3, L"Flirt/romance" },
- { 4, L"Love" },
- { 5, L"Nothing" },
- { 0, nullptr }
-};
-TLEN_FIELD_MAP tlenFieldStatus[] = {
- { 1, L"All" },
- { 2, L"Available" },
- { 3, L"Free for chat" },
- { 0, nullptr }
-};
-TLEN_FIELD_MAP tlenFieldOccupation[] = {
- { 1, L"Student" },
- { 2, L"College student" },
- { 3, L"Farmer" },
- { 4, L"Manager" },
- { 5, L"Specialist" },
- { 6, L"Clerk" },
- { 7, L"Unemployed" },
- { 8, L"Pensioner" },
- { 9, L"Housekeeper" },
- { 10, L"Teacher" },
- { 11, L"Doctor" },
- { 12, L"Other" },
- { 0, nullptr }
-};
-TLEN_FIELD_MAP tlenFieldPlan[] = {
- { 1, L"I'd like to go downtown" },
- { 2, L"I'd like to go to the cinema" },
- { 3, L"I'd like to take a walk" },
- { 4, L"I'd like to go to the disco" },
- { 5, L"I'd like to go on a blind date" },
- { 6, L"Waiting for suggestion" },
- { 0, nullptr }
-};
-
-static INT_PTR CALLBACK TlenUserInfoDlgProc(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam);
-
-static void InitComboBox(HWND hwndCombo, TLEN_FIELD_MAP *fieldMap)
-{
- int i, n;
-
- n = SendMessage(hwndCombo, CB_ADDSTRING, 0, (LPARAM)L"");
- SendMessage(hwndCombo, CB_SETITEMDATA, n, 0);
- SendMessage(hwndCombo, CB_SETCURSEL, n, 0);
- for (i=0;;i++) {
- if (fieldMap[i].name == nullptr)
- break;
- n = SendMessage(hwndCombo, CB_ADDSTRING, 0, (LPARAM) TranslateW(fieldMap[i].name));
- SendMessage(hwndCombo, CB_SETITEMDATA, n, fieldMap[i].id);
- }
-}
-
-static void FetchField(HWND hwndDlg, UINT idCtrl, char *fieldName, char **str, int *strSize)
-{
- char text[512];
- char *localFieldName, *localText;
-
- if (hwndDlg == nullptr || fieldName == nullptr || str == nullptr || strSize == nullptr)
- return;
- GetDlgItemTextA(hwndDlg, idCtrl, text, _countof(text));
- if (text[0]) {
- if ((localFieldName=TlenTextEncode(fieldName)) != nullptr) {
- if ((localText=TlenTextEncode(text)) != nullptr) {
- TlenStringAppend(str, strSize, "<%s>%s</%s>", localFieldName, localText, localFieldName);
- mir_free(localText);
- }
- mir_free(localFieldName);
- }
- }
-}
-
-static void FetchCombo(HWND hwndDlg, UINT idCtrl, char *fieldName, char **str, int *strSize)
-{
- if (hwndDlg == nullptr || fieldName == nullptr || str == nullptr || strSize == nullptr)
- return;
-
- int value = (int) SendDlgItemMessage(hwndDlg, idCtrl, CB_GETITEMDATA, SendDlgItemMessage(hwndDlg, idCtrl, CB_GETCURSEL, 0, 0), 0);
- if (value > 0) {
- if (char *localFieldName = TlenTextEncode(fieldName)) {
- TlenStringAppend(str, strSize, "<%s>%d</%s>", localFieldName, value, localFieldName);
- mir_free(localFieldName);
- }
- }
-}
-
-int TlenProtocol::UserInfoInit(WPARAM wParam, LPARAM lParam)
-{
- if (!Proto_GetAccount(m_szModuleName))
- return 0;
-
- MCONTACT hContact = (MCONTACT) lParam;
- char *szProto = GetContactProto(hContact);
- if ((szProto != nullptr && !mir_strcmp(szProto, m_szModuleName)) || !lParam) {
- OPTIONSDIALOGPAGE odp = { 0 };
- odp.hInstance = hInst;
- odp.flags = ODPF_UNICODE;
- odp.pfnDlgProc = TlenUserInfoDlgProc;
- odp.position = -2000000000;
- odp.pszTemplate = ((HANDLE)lParam != nullptr) ? MAKEINTRESOURCEA(IDD_USER_INFO):MAKEINTRESOURCEA(IDD_USER_VCARD);
- odp.szTitle.w = (hContact != NULL) ? LPGENW("Account") : m_tszUserName;
- odp.dwInitParam = (LPARAM)this;
- UserInfo_AddPage(wParam, &odp);
-
- }
- if (!lParam && isOnline) {
- CCSDATA ccs = {0};
- GetInfo(0, (LPARAM) &ccs);
- }
- return 0;
-}
-
-typedef struct {
- TlenProtocol *proto;
- MCONTACT hContact;
-}TLENUSERINFODLGDATA;
-
-
-static INT_PTR CALLBACK TlenUserInfoDlgProc(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
-{
- TLENUSERINFODLGDATA *data = (TLENUSERINFODLGDATA *) GetWindowLongPtr(hwndDlg, GWLP_USERDATA);
- switch (msg) {
- case WM_INITDIALOG:
-
- data = (TLENUSERINFODLGDATA*)mir_alloc(sizeof(TLENUSERINFODLGDATA));
- data->hContact = (MCONTACT) lParam;
- SetWindowLongPtr(hwndDlg, GWLP_USERDATA, (LONG_PTR)data);
- // lParam is hContact
- TranslateDialogDefault(hwndDlg);
- InitComboBox(GetDlgItem(hwndDlg, IDC_GENDER), tlenFieldGender);
- InitComboBox(GetDlgItem(hwndDlg, IDC_OCCUPATION), tlenFieldOccupation);
- InitComboBox(GetDlgItem(hwndDlg, IDC_LOOKFOR), tlenFieldLookfor);
-
- return TRUE;
- case WM_TLEN_REFRESH:
- {
- DBVARIANT dbv;
- char *jid;
- int i;
- TLEN_LIST_ITEM *item;
-
- SetDlgItemText(hwndDlg, IDC_INFO_JID, L"");
- SetDlgItemText(hwndDlg, IDC_SUBSCRIPTION, L"");
- SetFocus(GetDlgItem(hwndDlg, IDC_STATIC));
-
- if (!db_get_ws(data->hContact, data->proto->m_szModuleName, "FirstName", &dbv)) {
- SetDlgItemText(hwndDlg, IDC_FIRSTNAME, dbv.ptszVal);
- db_free(&dbv);
- } else SetDlgItemText(hwndDlg, IDC_FIRSTNAME, L"");
- if (!db_get_ws(data->hContact, data->proto->m_szModuleName, "LastName", &dbv)) {
- SetDlgItemText(hwndDlg, IDC_LASTNAME, dbv.ptszVal);
- db_free(&dbv);
- } else SetDlgItemText(hwndDlg, IDC_LASTNAME, L"");
- if (!db_get_ws(data->hContact, data->proto->m_szModuleName, "Nick", &dbv)) {
- SetDlgItemText(hwndDlg, IDC_NICKNAME, dbv.ptszVal);
- db_free(&dbv);
- } else SetDlgItemText(hwndDlg, IDC_NICKNAME, L"");
- if (!db_get_ws(data->hContact, data->proto->m_szModuleName, "e-mail", &dbv)) {
- SetDlgItemText(hwndDlg, IDC_EMAIL, dbv.ptszVal);
- db_free(&dbv);
- } else SetDlgItemText(hwndDlg, IDC_EMAIL, L"");
- if (!db_get(data->hContact, data->proto->m_szModuleName, "Age", &dbv)) {
- SetDlgItemInt(hwndDlg, IDC_AGE, dbv.wVal, FALSE);
- db_free(&dbv);
- } else SetDlgItemText(hwndDlg, IDC_AGE, L"");
- if (!db_get_ws(data->hContact, data->proto->m_szModuleName, "City", &dbv)) {
- SetDlgItemText(hwndDlg, IDC_CITY, dbv.ptszVal);
- db_free(&dbv);
- } else SetDlgItemText(hwndDlg, IDC_CITY, L"");
- if (!db_get_ws(data->hContact, data->proto->m_szModuleName, "School", &dbv)) {
- SetDlgItemText(hwndDlg, IDC_SCHOOL, dbv.ptszVal);
- db_free(&dbv);
- } else SetDlgItemText(hwndDlg, IDC_SCHOOL, L"");
- switch (db_get_b(data->hContact, data->proto->m_szModuleName, "Gender", '?')) {
- case 'M':
- SendDlgItemMessage(hwndDlg, IDC_GENDER, CB_SETCURSEL, 1, 0);
- SetDlgItemText(hwndDlg, IDC_GENDER_TEXT, TranslateW(tlenFieldGender[0].name));
- break;
- case 'F':
- SendDlgItemMessage(hwndDlg, IDC_GENDER, CB_SETCURSEL, 2, 0);
- SetDlgItemText(hwndDlg, IDC_GENDER_TEXT, TranslateW(tlenFieldGender[1].name));
- break;
- default:
- SendDlgItemMessage(hwndDlg, IDC_GENDER, CB_SETCURSEL, 0, 0);
- SetDlgItemText(hwndDlg, IDC_GENDER_TEXT, L"");
- break;
- }
- i = db_get_w(data->hContact, data->proto->m_szModuleName, "Occupation", 0);
- if (i>0 && i<13) {
- SetDlgItemText(hwndDlg, IDC_OCCUPATION_TEXT, TranslateW(tlenFieldOccupation[i-1].name));
- SendDlgItemMessage(hwndDlg, IDC_OCCUPATION, CB_SETCURSEL, i, 0);
- } else {
- SetDlgItemText(hwndDlg, IDC_OCCUPATION_TEXT, L"");
- SendDlgItemMessage(hwndDlg, IDC_OCCUPATION, CB_SETCURSEL, 0, 0);
- }
- i = db_get_w(data->hContact, data->proto->m_szModuleName, "LookingFor", 0);
- if (i>0 && i<6) {
- SetDlgItemText(hwndDlg, IDC_LOOKFOR_TEXT, TranslateW(tlenFieldLookfor[i-1].name));
- SendDlgItemMessage(hwndDlg, IDC_LOOKFOR, CB_SETCURSEL, i, 0);
- } else {
- SetDlgItemText(hwndDlg, IDC_LOOKFOR_TEXT, L"");
- SendDlgItemMessage(hwndDlg, IDC_LOOKFOR, CB_SETCURSEL, 0, 0);
- }
- i = db_get_w(data->hContact, data->proto->m_szModuleName, "VoiceChat", 0);
- CheckDlgButton(hwndDlg, IDC_VOICECONVERSATIONS, i ? BST_CHECKED : BST_UNCHECKED);
- i = db_get_w(data->hContact, data->proto->m_szModuleName, "PublicStatus", 0);
- CheckDlgButton(hwndDlg, IDC_PUBLICSTATUS, i ? BST_CHECKED : BST_UNCHECKED);
- if (!db_get(data->hContact, data->proto->m_szModuleName, "jid", &dbv)) {
- jid = TlenTextDecode(dbv.pszVal);
- SetDlgItemTextA(hwndDlg, IDC_INFO_JID, jid);
- mir_free(jid);
- jid = dbv.pszVal;
- if (data->proto->isOnline) {
- if ((item=TlenListGetItemPtr(data->proto, LIST_ROSTER, jid)) != nullptr) {
- switch (item->subscription) {
- case SUB_BOTH:
- SetDlgItemText(hwndDlg, IDC_SUBSCRIPTION, TranslateT("both"));
- break;
- case SUB_TO:
- SetDlgItemText(hwndDlg, IDC_SUBSCRIPTION, TranslateT("to"));
- break;
- case SUB_FROM:
- SetDlgItemText(hwndDlg, IDC_SUBSCRIPTION, TranslateT("from"));
- break;
- default:
- SetDlgItemText(hwndDlg, IDC_SUBSCRIPTION, TranslateT("none"));
- break;
- }
- SetDlgItemTextA(hwndDlg, IDC_SOFTWARE, item->software);
- SetDlgItemTextA(hwndDlg, IDC_VERSION, item->version);
- SetDlgItemTextA(hwndDlg, IDC_SYSTEM, item->system);
- } else {
- SetDlgItemText(hwndDlg, IDC_SUBSCRIPTION, TranslateT("not on roster"));
- }
- }
- db_free(&dbv);
- }
- }
- break;
- case WM_NOTIFY:
- switch (((LPNMHDR)lParam)->idFrom) {
- case 0:
- switch (((LPNMHDR)lParam)->code) {
- case PSN_INFOCHANGED:
- {
- MCONTACT hContact = (MCONTACT) ((LPPSHNOTIFY) lParam)->lParam;
- SendMessage(hwndDlg, WM_TLEN_REFRESH, 0, (LPARAM) hContact);
- }
- break;
- case PSN_PARAMCHANGED:
- {
- data->proto = ( TlenProtocol* )(( LPPSHNOTIFY )lParam )->lParam;
- SendMessage(hwndDlg, WM_TLEN_REFRESH, 0, 0);
- }
- }
- break;
- }
- break;
- case WM_COMMAND:
- if (LOWORD(wParam) == IDC_SAVE && HIWORD(wParam) == BN_CLICKED) {
- char *str = nullptr;
- int strSize;
- TlenStringAppend(&str, &strSize, "<iq type='set' id='" TLEN_IQID "%d' to='tuba'><query xmlns='jabber:iq:register'>", TlenSerialNext(data->proto));
- FetchField(hwndDlg, IDC_FIRSTNAME, "first", &str, &strSize);
- FetchField(hwndDlg, IDC_LASTNAME, "last", &str, &strSize);
- FetchField(hwndDlg, IDC_NICKNAME, "nick", &str, &strSize);
- FetchField(hwndDlg, IDC_EMAIL, "email", &str, &strSize);
- FetchCombo(hwndDlg, IDC_GENDER, "s", &str, &strSize);
- FetchField(hwndDlg, IDC_AGE, "b", &str, &strSize);
- FetchField(hwndDlg, IDC_CITY, "c", &str, &strSize);
- FetchCombo(hwndDlg, IDC_OCCUPATION, "j", &str, &strSize);
- FetchField(hwndDlg, IDC_SCHOOL, "e", &str, &strSize);
- FetchCombo(hwndDlg, IDC_LOOKFOR, "r", &str, &strSize);
- TlenStringAppend(&str, &strSize, "<g>%d</g>", IsDlgButtonChecked(hwndDlg, IDC_VOICECONVERSATIONS) ? 1 : 0);
- TlenStringAppend(&str, &strSize, "<v>%d</v>", IsDlgButtonChecked(hwndDlg, IDC_PUBLICSTATUS) ? 1 : 0);
- TlenStringAppend(&str, &strSize, "</query></iq>");
- TlenSend(data->proto, "%s", str);
- mir_free(str);
- data->proto->GetInfo(NULL, 0);
- }
- break;
- case WM_DESTROY:
- mir_free(data);
- break;
- }
- return FALSE;
-}
diff --git a/protocols/Tlen/src/tlen_util.cpp b/protocols/Tlen/src/tlen_util.cpp
deleted file mode 100644
index b0efd769ba..0000000000
--- a/protocols/Tlen/src/tlen_util.cpp
+++ /dev/null
@@ -1,444 +0,0 @@
-/*
-
-Jabber Protocol Plugin for Miranda IM
-Tlen Protocol Plugin for Miranda NG
-Copyright (C) 2002-2004 Santithorn Bunchua
-Copyright (C) 2004-2007 Piotr Piastucki
-
-This program is free software; you can redistribute it and/or
-modify it under the terms of the GNU General Public License
-as published by the Free Software Foundation; either version 2
-of the License, or (at your option) any later version.
-
-This program 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 General Public License for more details.
-
-You should have received a copy of the GNU General Public License
-along with this program; if not, write to the Free Software
-Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
-
-*/
-
-#include "stdafx.h"
-#include "tlen_list.h"
-#include <ctype.h>
-#include <win2k.h>
-
-void TlenSerialInit(TlenProtocol *proto)
-{
- proto->serial = 0;
-}
-
-unsigned int TlenSerialNext(TlenProtocol *proto)
-{
- unsigned int ret;
-
- mir_cslock lck(proto->csSerial);
- ret = proto->serial;
- proto->serial++;
- return ret;
-}
-
-// Caution: DO NOT use TlenSend() to send binary (non-string) data
-
-int TlenSend(TlenProtocol *proto, const char *fmt, ...)
-{
- char *str;
- int size;
- va_list vararg;
- int result = 0;
-
- mir_cslock lck(proto->csSend);
-
- va_start(vararg, fmt);
- size = 512;
- str = (char *)mir_alloc(size);
- while (mir_vsnprintf(str, size, fmt, vararg) == -1) {
- size += 512;
- str = (char *)mir_realloc(str, size);
- }
- va_end(vararg);
-
- proto->debugLogA("SEND:%s", str);
- size = (int)mir_strlen(str);
- if (proto->threadData != nullptr) {
- if (proto->threadData->useAES) {
- result = TlenWsSendAES(proto, str, size, &proto->threadData->aes_out_context, proto->threadData->aes_out_iv);
- }
- else {
- result = TlenWsSend(proto, proto->threadData->s, str, size);
- }
- }
-
- mir_free(str);
- return result;
-}
-
-char *TlenResourceFromJID(const char *jid2)
-{
- char *p;
- char *nick;
- char* jid = mir_strdup(jid2);
-
- p = strchr(jid, '/');
- if (p != nullptr && p[1] != '\0') {
- p++;
- if ((nick = (char *)mir_alloc(1 + mir_strlen(jid) - (p - jid))) != nullptr) {
- strncpy(nick, p, mir_strlen(jid) - (p - jid));
- nick[mir_strlen(jid) - (p - jid)] = '\0';
- }
- }
- else {
- nick = mir_strdup(jid);
- }
-
- mir_free(jid);
- return nick;
-}
-
-char *TlenNickFromJID(const char *jid2)
-{
- char *p;
- char *nick;
- char* jid = mir_strdup(jid2);
-
- if ((p = strchr(jid, '@')) == nullptr)
- p = strchr(jid, '/');
- if (p != nullptr) {
- if ((nick = (char *)mir_alloc((p - jid) + 1)) != nullptr) {
- strncpy(nick, jid, p - jid);
- nick[p - jid] = '\0';
- }
- }
- else {
- nick = mir_strdup(jid);
- }
-
- mir_free(jid);
- return nick;
-}
-
-char *TlenLoginFromJID(const char *jid2)
-{
- char *p;
- char *nick;
- char* jid = mir_strdup(jid2);
-
- p = strchr(jid, '/');
- if (p != nullptr) {
- if ((nick = (char *)mir_alloc((p - jid) + 1)) != nullptr) {
- strncpy(nick, jid, p - jid);
- nick[p - jid] = '\0';
- }
- }
- else {
- nick = mir_strdup(jid);
- }
-
- mir_free(jid);
- return nick;
-}
-
-char *TlenLocalNickFromJID(const char *jid)
-{
- char *p;
- char *localNick;
-
- p = TlenNickFromJID(jid);
- localNick = TlenTextDecode(p);
- mir_free(p);
- return localNick;
-}
-
-char *TlenSha1(char *str)
-{
- mir_sha1_ctx sha;
- DWORD digest[5];
- char* result;
-
- if (str == nullptr)
- return nullptr;
-
- mir_sha1_init(&sha);
- mir_sha1_append(&sha, (BYTE*)str, (int)mir_strlen(str));
- mir_sha1_finish(&sha, (BYTE*)digest);
- if ((result = (char *)mir_alloc(41)) == nullptr)
- return nullptr;
- sprintf(result, "%08x%08x%08x%08x%08x", (int)htonl(digest[0]), (int)htonl(digest[1]), (int)htonl(digest[2]), (int)htonl(digest[3]), (int)htonl(digest[4])); //!!!!!!!!!!!
- return result;
-}
-
-char *TlenSha1(char *str, int len)
-{
- mir_sha1_ctx sha;
- BYTE digest[20];
- char* result;
- int i;
-
- if (str == nullptr)
- return nullptr;
-
- mir_sha1_init(&sha);
- mir_sha1_append(&sha, (BYTE*)str, len);
- mir_sha1_finish(&sha, digest);
- if ((result = (char*)mir_alloc(20)) == nullptr)
- return nullptr;
- for (i = 0; i < 20; i++)
- result[i] = digest[4 * (i >> 2) + (3 - (i & 0x3))];
- return result;
-}
-
-char *TlenPasswordHash(const char *str)
-{
- int magic1 = 0x50305735, magic2 = 0x12345671, sum = 7;
- char *p, *res;
-
- if (str == nullptr) return nullptr;
- for (p = (char *)str; *p != '\0'; p++) {
- if (*p != ' ' && *p != '\t') {
- magic1 ^= (((magic1 & 0x3f) + sum) * ((char)*p)) + (magic1 << 8);
- magic2 += (magic2 << 8) ^ magic1;
- sum += ((char)*p);
- }
- }
- magic1 &= 0x7fffffff;
- magic2 &= 0x7fffffff;
- res = (char *)mir_alloc(17);
- sprintf(res, "%08x%08x", magic1, magic2); //!!!!!!!!!!!
- return res;
-}
-
-char *TlenUrlEncode(const char *str)
-{
- char *p, *q, *res;
- unsigned char c;
-
- if (str == nullptr) return nullptr;
- res = (char *)mir_alloc(3 * mir_strlen(str) + 1);
- for (p = (char *)str, q = res; *p != '\0'; p++, q++) {
- if (*p == ' ') {
- *q = '+';
- }
- else if (*p < 0x20 || *p >= 0x7f || strchr("%&+:'<>\"", *p) != nullptr) {
- // Convert first from CP1252 to ISO8859-2
- switch ((unsigned char)*p) {
- case 0xa5: c = (unsigned char)0xa1; break;
- case 0x8c: c = (unsigned char)0xa6; break;
- case 0x8f: c = (unsigned char)0xac; break;
- case 0xb9: c = (unsigned char)0xb1; break;
- case 0x9c: c = (unsigned char)0xb6; break;
- case 0x9f: c = (unsigned char)0xbc; break;
- default: c = (unsigned char)*p; break;
- }
- sprintf(q, "%%%02X", c); //!!!!!!!!!!!
- q += 2;
- }
- else {
- *q = *p;
- }
- }
- *q = '\0';
- return res;
-}
-
-void TlenUrlDecode(char *str)
-{
- char *p, *q;
- unsigned int code;
-
- if (str == nullptr) return;
- for (p = q = str; *p != '\0'; p++, q++) {
- if (*p == '+') {
- *q = ' ';
- }
- else if (*p == '%' && *(p + 1) != '\0' && isxdigit(*(p + 1)) && *(p + 2) != '\0' && isxdigit(*(p + 2))) {
- sscanf(p + 1, "%2x", &code);
- *q = (char)code;
- // Convert from ISO8859-2 to CP1252
- switch ((unsigned char)*q) {
- case 0xa1: *q = (char)0xa5; break;
- case 0xa6: *q = (char)0x8c; break;
- case 0xac: *q = (char)0x8f; break;
- case 0xb1: *q = (char)0xb9; break;
- case 0xb6: *q = (char)0x9c; break;
- case 0xbc: *q = (char)0x9f; break;
- }
- p += 2;
- }
- else {
- *q = *p;
- }
- }
- *q = '\0';
-}
-
-char * TlenGroupDecode(const char *str)
-{
- char *p, *q;
- if (str == nullptr) return nullptr;
- p = q = TlenTextDecode(str);
- for (; *p != '\0'; p++) {
- if (*p == '/') {
- *p = '\\';
- }
- }
- return q;
-}
-
-char * TlenGroupEncode(const char *str)
-{
- char *p, *q;
- if (str == nullptr) return nullptr;
- p = q = mir_strdup(str);
- for (; *p != '\0'; p++) {
- if (*p == '\\') {
- *p = '/';
- }
- }
- p = TlenTextEncode(q);
- mir_free(q);
- return p;
-}
-
-char *TlenTextEncode(const char *str)
-{
- char *s1;
-
- if (str == nullptr) return nullptr;
- if ((s1 = TlenUrlEncode(str)) == nullptr)
- return nullptr;
- return s1;
-}
-
-char *TlenTextDecode(const char *str)
-{
- char *s1;
-
- if (str == nullptr) return nullptr;
- s1 = mir_strdup(str);
- TlenUrlDecode(s1);
- return s1;
-}
-
-/*
- * Apply Polish Daylight Saving Time rules to get "DST-unbiased" timestamp
- */
-
-time_t TlenTimeToUTC(time_t time) {
- struct tm *timestamp;
- timestamp = gmtime(&time);
- if ((timestamp->tm_mon > 2 && timestamp->tm_mon < 9) ||
- (timestamp->tm_mon == 2 && timestamp->tm_mday - timestamp->tm_wday >= 25) ||
- (timestamp->tm_mon == 9 && timestamp->tm_mday - timestamp->tm_wday < 25)) {
- //time -= 3600;
- }
- else {
- //time += 3600;
- }
- return time;
-}
-
-time_t TlenIsoToUnixTime(char *stamp)
-{
- struct tm timestamp;
- char date[9];
- char *p;
- int i, y;
- time_t t;
-
- if (stamp == nullptr) return (time_t)0;
-
- p = stamp;
-
- // Get the date part
- for (i = 0; *p != '\0' && i < 8 && isdigit(*p); p++, i++)
- date[i] = *p;
-
- // Parse year
- if (i == 6) {
- // 2-digit year (1970-2069)
- y = (date[0] - '0') * 10 + (date[1] - '0');
- if (y < 70) y += 100;
- }
- else if (i == 8) {
- // 4-digit year
- y = (date[0] - '0') * 1000 + (date[1] - '0') * 100 + (date[2] - '0') * 10 + date[3] - '0';
- y -= 1900;
- }
- else
- return (time_t)0;
- timestamp.tm_year = y;
- // Parse month
- timestamp.tm_mon = (date[i - 4] - '0') * 10 + date[i - 3] - '0' - 1;
- // Parse date
- timestamp.tm_mday = (date[i - 2] - '0') * 10 + date[i - 1] - '0';
-
- // Skip any date/time delimiter
- for (; *p != '\0' && !isdigit(*p); p++);
-
- // Parse time
- if (sscanf(p, "%d:%d:%d", &(timestamp.tm_hour), &(timestamp.tm_min), &(timestamp.tm_sec)) != 3)
- return (time_t)0;
-
- timestamp.tm_isdst = 0; // DST is already present in _timezone below
- _tzset();
- t = mktime(&timestamp);
- t -= _timezone;
- t = TlenTimeToUTC(t);
-
- if (t >= 0)
- return t;
- else
- return (time_t)0;
-}
-
-void TlenStringAppend(char **str, int *sizeAlloced, const char *fmt, ...)
-{
- va_list vararg;
- char *p;
- int size, len;
-
- if (str == nullptr) return;
-
- if (*str == nullptr || *sizeAlloced <= 0) {
- *sizeAlloced = size = 2048;
- *str = (char *)mir_alloc(size);
- len = 0;
- }
- else {
- len = (int)mir_strlen(*str);
- size = *sizeAlloced - (int)mir_strlen(*str);
- }
-
- p = *str + len;
- va_start(vararg, fmt);
- while (mir_vsnprintf(p, size, fmt, vararg) == -1) {
- size += 2048;
- (*sizeAlloced) += 2048;
- *str = (char *)mir_realloc(*str, *sizeAlloced);
- p = *str + len;
- }
- va_end(vararg);
-}
-
-BOOL IsAuthorized(TlenProtocol *proto, const char *jid)
-{
- TLEN_LIST_ITEM *item = TlenListGetItemPtr(proto, LIST_ROSTER, jid);
- if (item != nullptr) {
- return item->subscription == SUB_BOTH || item->subscription == SUB_FROM;
- }
- return FALSE;
-}
-
-
-void TlenLogMessage(TlenProtocol *proto, MCONTACT hContact, DWORD flags, const char *message)
-{
- int size = (int)mir_strlen(message) + 2;
- char *localMessage = (char *)mir_alloc(size);
- mir_strcpy(localMessage, message);
- localMessage[size - 1] = '\0';
- TlenDBAddEvent(proto, hContact, EVENTTYPE_MESSAGE, flags, (PBYTE)message, (DWORD)size);
- mir_free(localMessage);
-}
diff --git a/protocols/Tlen/src/tlen_voice.cpp b/protocols/Tlen/src/tlen_voice.cpp
deleted file mode 100644
index a209ea3edd..0000000000
--- a/protocols/Tlen/src/tlen_voice.cpp
+++ /dev/null
@@ -1,1109 +0,0 @@
-/*
-
-Tlen Protocol Plugin for Miranda NG
-Copyright (C) 2004-2007 Piotr Piastucki
-
-This program is free software; you can redistribute it and/or
-modify it under the terms of the GNU General Public License
-as published by the Free Software Foundation; either version 2
-of the License, or (at your option) any later version.
-
-This program 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 General Public License for more details.
-
-You should have received a copy of the GNU General Public License
-along with this program; if not, write to the Free Software
-Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
-
-*/
-
-#include "stdafx.h"
-#include <io.h>
-#include <fcntl.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <math.h>
-#include <m_button.h>
-//#include <win2k.h>
-#include "resource.h"
-#include "tlen_list.h"
-#include "tlen_voice.h"
-#include "tlen_p2p_old.h"
-#include "tlen_file.h"
-
-static int MODE_FREQUENCY[] = { 0, 0, 8000, 11025, 22050, 44100 };
-static int MODE_FRAME_SIZE[] = { 0, 0, 5, 5, 10, 25 };
-static int FRAMES_AVAILABLE_MAX_LIMIT = 2;
-static int VU_METER_HEIGHT = 64;
-static int VU_METER_WIDTH = 20;
-static int VU_METER_LEVELS = 16;
-static HBITMAP vuMeterBitmaps[100];
-
-static void TlenVoiceReceiveParse(TLEN_FILE_TRANSFER *ft);
-static void TlenVoiceSendParse(TLEN_FILE_TRANSFER *ft);
-static void TlenVoiceReceivingConnection(HNETLIBCONN hNewConnection, DWORD dwRemoteIP, void * pExtra);
-
-static void CALLBACK TlenVoicePlaybackCallback(HWAVEOUT hwo, UINT uMsg, DWORD* dwInstance, DWORD dwParam1, DWORD)
-{
- if (uMsg == WOM_DONE) {
- TLEN_VOICE_CONTROL *control = (TLEN_VOICE_CONTROL *)dwInstance;
- waveOutUnprepareHeader(hwo, (WAVEHDR *)dwParam1, sizeof(WAVEHDR));
- if (control->proto->framesAvailableForPlayback > 0) {
- control->proto->framesAvailableForPlayback--;
- }
- // playbackControl->waveHeaders[playbackControl->waveHeadersPos].dwFlags = WHDR_DONE;
- // playbackControl->waveHeaders[playbackControl->waveHeadersPos].lpData = (char *) (playbackControl->waveData + playbackControl->waveHeadersPos * playbackControl->waveFrameSize);
- // playbackControl->waveHeaders[playbackControl->waveHeadersPos].dwBufferLength = playbackControl->waveFrameSize * 2;
- // waveOutPrepareHeader(playbackControl->hWaveOut, &playbackControl->waveHeaders[playbackControl->waveHeadersPos], sizeof(WAVEHDR));
- // waveOutWrite(playbackControl->hWaveOut, &playbackControl->waveHeaders[playbackControl->waveHeadersPos], sizeof(WAVEHDR));
-
- }
-}
-
-static void __cdecl TlenVoiceRecordingThreadProc(void *param)
-{
- TLEN_VOICE_CONTROL *control = (TLEN_VOICE_CONTROL *)param;
- MSG msg;
- HWAVEIN hWaveIn;
- WAVEHDR *hWaveHdr;
- control->isRunning = 1;
- control->stopThread = 0;
- while (TRUE) {
- GetMessage(&msg, nullptr, 0, 0);
- if (msg.message == MM_WIM_DATA) {
- // TlenLog("recording thread running...%d", msg.message);
- hWaveIn = (HWAVEIN)msg.wParam;
- hWaveHdr = (WAVEHDR *)msg.lParam;
- waveInUnprepareHeader(hWaveIn, hWaveHdr, sizeof(WAVEHDR));
- if (hWaveHdr->dwBytesRecorded > 0 && !control->bDisable) {
- control->recordingData = (short *)hWaveHdr->lpData;
- TlenVoiceSendParse(control->ft);
- }
- if (!control->stopThread) {
- waveInPrepareHeader(hWaveIn, &control->waveHeaders[control->waveHeadersPos], sizeof(WAVEHDR));
- waveInAddBuffer(hWaveIn, &control->waveHeaders[control->waveHeadersPos], sizeof(WAVEHDR));
- control->waveHeadersPos = (control->waveHeadersPos + 1) % control->waveHeadersNum;
- }
- }
- else if (msg.message == MM_WIM_CLOSE) {
- break;
- }
- }
- control->isRunning = 0;
- control->proto->debugLogA("recording thread ended...");
-}
-
-static int TlenVoicePlaybackStart(TLEN_VOICE_CONTROL *control)
-{
- int i;
- int iNumDevs, iSelDev;
- WAVEOUTCAPS wic;
-
- WAVEFORMATEX wfm;
- memset(&wfm, 0, sizeof(wfm));
- wfm.cbSize = sizeof(WAVEFORMATEX);
- wfm.nChannels = 1;
- wfm.wBitsPerSample = 16;
- wfm.nSamplesPerSec = MODE_FREQUENCY[control->codec];
- wfm.nAvgBytesPerSec = wfm.nSamplesPerSec * wfm.nChannels * wfm.wBitsPerSample / 8;
- wfm.nBlockAlign = 2 * wfm.nChannels;
- wfm.wFormatTag = WAVE_FORMAT_PCM;
-
- control->waveMode = 0;
- control->waveFrameSize = MODE_FRAME_SIZE[control->codec] * 160 * wfm.nChannels;// * wfm.wBitsPerSample / 8;
- control->waveHeadersPos = 0;
- control->waveHeadersNum = FRAMES_AVAILABLE_MAX_LIMIT + 2;
-
- int j = db_get_w(NULL, control->proto->m_szModuleName, "VoiceDeviceOut", 0);
- iSelDev = WAVE_MAPPER;
- if (j != 0) {
- iNumDevs = waveOutGetNumDevs();
- for (i = 0; i < iNumDevs; i++) {
- if (!waveOutGetDevCaps(i, &wic, sizeof(WAVEOUTCAPS))) {
- if (wic.dwFormats != 0) {
- j--;
- if (j == 0) {
- iSelDev = i;
- break;
- }
- }
- }
- }
- }
- if (!waveOutGetDevCaps(iSelDev, &wic, sizeof(WAVEOUTCAPS))) {
- control->proto->debugLogA("Playback device ID #%u: %s\r\n", iSelDev, wic.szPname);
- }
-
- MMRESULT mmres = waveOutOpen(&control->hWaveOut, iSelDev, &wfm, (UINT_PTR)&TlenVoicePlaybackCallback, (UINT_PTR)control, CALLBACK_FUNCTION);
- if (mmres != MMSYSERR_NOERROR) {
- control->proto->debugLogA("TlenVoiceStart FAILED!");
- return 1;
- }
- control->waveData = (short *)mir_alloc(control->waveHeadersNum * control->waveFrameSize * 2);
- memset(control->waveData, 0, control->waveHeadersNum * control->waveFrameSize * 2);
- control->waveHeaders = (WAVEHDR *)mir_alloc(control->waveHeadersNum * sizeof(WAVEHDR));
- control->proto->debugLogA("TlenVoiceStart OK!");
- return 0;
-}
-
-
-static int TlenVoiceRecordingStart(TLEN_VOICE_CONTROL *control)
-{
- WAVEFORMATEX wfm;
- int i, j;
- int iNumDevs, iSelDev;
- WAVEINCAPS wic;
-
- memset(&wfm, 0, sizeof(wfm));
- wfm.cbSize = sizeof(WAVEFORMATEX);
- wfm.nChannels = 1;
- wfm.wBitsPerSample = 16;
- wfm.nSamplesPerSec = MODE_FREQUENCY[control->codec];
- wfm.nAvgBytesPerSec = wfm.nSamplesPerSec * wfm.nChannels * wfm.wBitsPerSample / 8;
- wfm.nBlockAlign = 2 * wfm.nChannels;
- wfm.wFormatTag = WAVE_FORMAT_PCM;
-
-
- control->waveMode = 0;
- // control->isRunning = 1;
- control->waveFrameSize = MODE_FRAME_SIZE[control->codec] * 160 * wfm.nChannels;// * wfm.wBitsPerSample / 8;
- control->waveHeadersPos = 0;
- control->waveHeadersNum = 2;
-
- control->hThread = mir_forkthread(TlenVoiceRecordingThreadProc, control);
-
-
- SetThreadPriority(control->hThread, THREAD_PRIORITY_ABOVE_NORMAL);
-
- j = db_get_w(NULL, control->proto->m_szModuleName, "VoiceDeviceIn", 0);
- iSelDev = WAVE_MAPPER;
- if (j != 0) {
- iNumDevs = waveInGetNumDevs();
- for (i = 0; i < iNumDevs; i++) {
- if (!waveInGetDevCaps(i, &wic, sizeof(WAVEINCAPS))) {
- if (wic.dwFormats != 0) {
- j--;
- if (j == 0) {
- iSelDev = i;
- break;
- }
- }
- }
- }
- }
- if (!waveInGetDevCaps(iSelDev, &wic, sizeof(WAVEINCAPS))) {
- control->proto->debugLogA("Recording device ID #%u: %s\r\n", iSelDev, wic.szPname);
- }
-
- MMRESULT mmres = waveInOpen(&control->hWaveIn, iSelDev, &wfm, control->threadID, 0, CALLBACK_THREAD);
- // mmres = waveInOpen(&control->hWaveIn, 3, &wfm, (DWORD) &TlenVoiceRecordingCallback, (DWORD) control, CALLBACK_FUNCTION);
- if (mmres != MMSYSERR_NOERROR) {
- PostThreadMessage(control->threadID, WIM_CLOSE, 0, 0);
- control->proto->debugLogA("TlenVoiceStart FAILED %d!", mmres);
- return 1;
- }
- control->waveData = (short *)mir_alloc(control->waveHeadersNum * control->waveFrameSize * 2);
- memset(control->waveData, 0, control->waveHeadersNum * control->waveFrameSize * 2);
- control->waveHeaders = (WAVEHDR *)mir_alloc(control->waveHeadersNum * sizeof(WAVEHDR));
- for (i = 0; i < control->waveHeadersNum; i++) {
- control->waveHeaders[i].dwFlags = 0;//WHDR_DONE;
- control->waveHeaders[i].lpData = (char *)(control->waveData + i * control->waveFrameSize);
- control->waveHeaders[i].dwBufferLength = control->waveFrameSize * 2;
- mmres = waveInPrepareHeader(control->hWaveIn, &control->waveHeaders[i], sizeof(WAVEHDR));
- if (mmres != MMSYSERR_NOERROR) {
- waveInClose(control->hWaveIn);
- // PostThreadMessage(control->threadID, WIM_CLOSE, 0, 0);
- control->proto->debugLogA("TlenVoiceStart FAILED #2!");
- return 1;
- }
- }
- for (i = 0; i < control->waveHeadersNum; i++) {
- waveInAddBuffer(control->hWaveIn, &control->waveHeaders[i], sizeof(WAVEHDR));
- }
- waveInStart(control->hWaveIn);
- control->proto->debugLogA("TlenVoiceRStart OK!");
- return 0;
-}
-
-
-static TLEN_VOICE_CONTROL *TlenVoiceCreateVC(TlenProtocol *proto, int codec)
-{
- TLEN_VOICE_CONTROL *vc = (TLEN_VOICE_CONTROL *)mir_alloc(sizeof(TLEN_VOICE_CONTROL));
- memset(vc, 0, sizeof(TLEN_VOICE_CONTROL));
- vc->gsmstate = gsm_create();
- vc->codec = codec;
- vc->proto = proto;
- return vc;
-}
-static void TlenVoiceFreeVc(TLEN_VOICE_CONTROL *vc)
-{
- vc->proto->debugLogA("-> TlenVoiceFreeVc");
- vc->stopThread = 1;
- PostThreadMessage(vc->threadID, MM_WIM_CLOSE, 0, 0);
- while (vc->isRunning) {
- Sleep(50);
- }
- if (vc->hThread != nullptr) CloseHandle(vc->hThread);
- if (vc->hWaveIn) {
- for (int i = 0; i < vc->waveHeadersNum; i++) {
- while (waveInUnprepareHeader(vc->hWaveIn, &vc->waveHeaders[i], sizeof(WAVEHDR)) == WAVERR_STILLPLAYING) {
- Sleep(50);
- }
- }
- while (waveInClose(vc->hWaveIn) == WAVERR_STILLPLAYING) {
- Sleep(50);
- }
- }
- if (vc->hWaveOut) {
- for (int i = 0; i < vc->waveHeadersNum; i++) {
- while (waveOutUnprepareHeader(vc->hWaveOut, &vc->waveHeaders[i], sizeof(WAVEHDR)) == WAVERR_STILLPLAYING) {
- Sleep(50);
- }
- }
- while (waveOutClose(vc->hWaveOut) == WAVERR_STILLPLAYING) {
- Sleep(50);
- }
- }
- if (vc->waveData) mir_free(vc->waveData);
- if (vc->waveHeaders) mir_free(vc->waveHeaders);
- if (vc->gsmstate) gsm_release(vc->gsmstate);
- vc->proto->debugLogA("<- TlenVoiceFreeVc");
- mir_free(vc);
-}
-
-static void TlenVoiceCrypt(char *buffer, int len)
-{
- int i, j, k;
- j = 0x71;
- for (i = 0; i < len; i++) {
- k = j;
- j = j << 6;
- j += k;
- j = k + (j << 1);
- j += 0xBB;
- buffer[i] ^= j;
- }
-}
-
-static void __cdecl TlenVoiceReceiveThread(void *arg)
-{
- TLEN_FILE_TRANSFER *ft = (TLEN_FILE_TRANSFER *)arg;
- ft->proto->debugLogA("Thread started: type=file_receive server='%s' port='%d'", ft->hostName, ft->wPort);
-
- SetDlgItemText(ft->proto->voiceDlgHWND, IDC_STATUS, TranslateT("...Connecting..."));
-
- NETLIBOPENCONNECTION nloc = { sizeof(nloc) };
- nloc.szHost = ft->hostName;
- nloc.wPort = ft->wPort;
- HNETLIBCONN s = Netlib_OpenConnection(ft->proto->m_hNetlibUser, &nloc);
- if (s != nullptr) {
- ft->s = s;
- ft->proto->debugLogA("Entering file receive loop");
- TlenP2PEstablishOutgoingConnection(ft, FALSE);
- if (ft->state != FT_ERROR) {
- ft->proto->playbackControl = nullptr;
- ft->proto->recordingControl = TlenVoiceCreateVC(ft->proto, 3);
- ft->proto->recordingControl->ft = ft;
- TlenVoiceRecordingStart(ft->proto->recordingControl);
- while (ft->state != FT_DONE && ft->state != FT_ERROR) {
- TlenVoiceReceiveParse(ft);
- }
- TlenVoiceFreeVc(ft->proto->recordingControl);
- ft->proto->playbackControl = nullptr;
- ft->proto->recordingControl = nullptr;
- }
- if (ft->s) {
- Netlib_CloseHandle(s);
- }
- ft->s = nullptr;
- }
- else {
- ft->proto->debugLogA("Connection failed - receiving as server");
- ft->pfnNewConnectionV2 = TlenVoiceReceivingConnection;
- s = (HNETLIBCONN)TlenP2PListen(ft);
- if (s != nullptr) {
- SetDlgItemText(ft->proto->voiceDlgHWND, IDC_STATUS, TranslateT("...Waiting for connection..."));
- ft->s = s;
- HANDLE hEvent = CreateEvent(nullptr, FALSE, FALSE, nullptr);
- ft->hFileEvent = hEvent;
- ft->currentFile = 0;
- ft->state = FT_CONNECTING;
- char *nick = TlenNickFromJID(ft->jid);
- TlenSend(ft->proto, "<v t='%s' i='%s' e='7' a='%s' p='%d'/>", nick, ft->iqId, ft->localName, ft->wLocalPort);
- mir_free(nick);
- ft->proto->debugLogA("Waiting for the file to be received...");
- WaitForSingleObject(hEvent, INFINITE);
- ft->hFileEvent = nullptr;
- CloseHandle(hEvent);
- ft->proto->debugLogA("Finish all files");
- Netlib_CloseHandle(s);
- }
- else {
- ft->state = FT_ERROR;
- }
- }
- TlenListRemove(ft->proto, LIST_VOICE, ft->iqId);
- if (ft->state == FT_DONE) {
- SetDlgItemText(ft->proto->voiceDlgHWND, IDC_STATUS, TranslateT("...Finished..."));
- //ProtoBroadcastAck(ft->proto->m_szModuleName, ft->hContact, ACKTYPE_FILE, ACKRESULT_SUCCESS, ft, 0);
- }
- else {
- char *nick;
- nick = TlenNickFromJID(ft->jid);
- TlenSend(ft->proto, "<f t='%s' i='%s' e='8'/>", nick, ft->iqId);
- mir_free(nick);
- SetDlgItemText(ft->proto->voiceDlgHWND, IDC_STATUS, TranslateT("...Error..."));
- //ProtoBroadcastAck(ft->proto->m_szModuleName, ft->hContact, ACKTYPE_FILE, ACKRESULT_FAILED, ft, 0);
- }
- ft->proto->debugLogA("Thread ended: type=file_receive server='%s'", ft->hostName);
-
- TlenP2PFreeFileTransfer(ft);
-}
-
-static void TlenVoiceReceivingConnection(HNETLIBCONN hConnection, DWORD, void * pExtra)
-{
- HNETLIBCONN slisten;
- TlenProtocol *proto = (TlenProtocol *)pExtra;
-
- TLEN_FILE_TRANSFER *ft = TlenP2PEstablishIncomingConnection(proto, hConnection, LIST_VOICE, FALSE);
- if (ft != nullptr) {
- slisten = ft->s;
- ft->s = hConnection;
- ft->proto->debugLogA("Set ft->s to %d (saving %d)", hConnection, slisten);
- ft->proto->debugLogA("Entering send loop for this file connection... (ft->s is hConnection)");
- proto->playbackControl = nullptr;
- proto->recordingControl = TlenVoiceCreateVC(proto, 3);
- proto->recordingControl->ft = ft;
- TlenVoiceRecordingStart(proto->recordingControl);
- while (ft->state != FT_DONE && ft->state != FT_ERROR) {
- TlenVoiceReceiveParse(ft);
- }
- TlenVoiceFreeVc(proto->recordingControl);
- proto->playbackControl = nullptr;
- proto->recordingControl = nullptr;
- if (ft->state == FT_DONE) {
- SetDlgItemText(proto->voiceDlgHWND, IDC_STATUS, TranslateT("...Finished..."));
- // ProtoBroadcastAck(proto->m_szModuleName, ft->hContact, ACKTYPE_FILE, ACKRESULT_SUCCESS, ft, 0);
- }
- else {
- // ProtoBroadcastAck(proto->m_szModuleName, ft->hContact, ACKTYPE_FILE, ACKRESULT_FAILED, ft, 0);
- SetDlgItemText(ft->proto->voiceDlgHWND, IDC_STATUS, TranslateT("...Error..."));
- }
- ft->proto->debugLogA("Closing connection for this file transfer... (ft->s is now hBind)");
- ft->s = slisten;
- ft->proto->debugLogA("ft->s is restored to %d", ft->s);
- }
- Netlib_CloseHandle(hConnection);
- if (ft != nullptr && ft->hFileEvent != nullptr)
- SetEvent(ft->hFileEvent);
-}
-
-static void TlenVoiceReceiveParse(TLEN_FILE_TRANSFER *ft)
-{
- char *statusTxt;
- int i, j;
- char *p;
- float val;
- TLEN_FILE_PACKET *packet = TlenP2PPacketReceive(ft->s);
- if (packet != nullptr) {
- statusTxt = " Unknown packet ";
- p = packet->packet;
- if (packet->type == TLEN_VOICE_PACKET) {
- short *out;
- int codec, chunkNum;
- statusTxt = " OK ";
- TlenVoiceCrypt(packet->packet + 4, packet->len - 4);
- codec = *((int *)packet->packet);
- if (codec < 2 || codec>5) {
- statusTxt = " Unknown codec ";
- }
- else {
- if (ft->proto->playbackControl == nullptr) {
- ft->proto->playbackControl = TlenVoiceCreateVC(ft->proto, codec);
- TlenVoicePlaybackStart(ft->proto->playbackControl);
- ft->proto->framesAvailableForPlayback = 0;
- ft->proto->availOverrunValue = 0;
- }
- else if (ft->proto->playbackControl->codec != codec) {
- TlenVoiceFreeVc(ft->proto->playbackControl);
- ft->proto->playbackControl = TlenVoiceCreateVC(ft->proto, codec);
- TlenVoicePlaybackStart(ft->proto->playbackControl);
- ft->proto->framesAvailableForPlayback = 0;
- ft->proto->availOverrunValue = 0;
- }
- if (!ft->proto->playbackControl->bDisable) {
- ft->proto->playbackControl->waveHeaders[ft->proto->playbackControl->waveHeadersPos].dwFlags = WHDR_DONE;
- ft->proto->playbackControl->waveHeaders[ft->proto->playbackControl->waveHeadersPos].lpData = (char *)(ft->proto->playbackControl->waveData + ft->proto->playbackControl->waveHeadersPos * ft->proto->playbackControl->waveFrameSize);
- ft->proto->playbackControl->waveHeaders[ft->proto->playbackControl->waveHeadersPos].dwBufferLength = ft->proto->playbackControl->waveFrameSize * 2;
- /*
- if (availPlayback == 0) {
- statusTxt = "!! Buffer is empty !!";
- availPlayback++;
- waveOutPrepareHeader(playbackControl->hWaveOut, &playbackControl->waveHeaders[playbackControl->waveHeadersPos], sizeof(WAVEHDR));
- waveOutWrite(playbackControl->hWaveOut, &playbackControl->waveHeaders[playbackControl->waveHeadersPos], sizeof(WAVEHDR));
- playbackControl->waveHeadersPos = (playbackControl->waveHeadersPos +1) % playbackControl->waveHeadersNum;
- playbackControl->waveHeaders[playbackControl->waveHeadersPos].dwFlags = WHDR_DONE;
- playbackControl->waveHeaders[playbackControl->waveHeadersPos].lpData = (char *) (playbackControl->waveData + playbackControl->waveHeadersPos * playbackControl->waveFrameSize);
- playbackControl->waveHeaders[playbackControl->waveHeadersPos].dwBufferLength = playbackControl->waveFrameSize * 2;
- }
- */
- chunkNum = min(MODE_FRAME_SIZE[codec], (int)(packet->len - 4) / 33);
- out = (short *)ft->proto->playbackControl->waveHeaders[ft->proto->playbackControl->waveHeadersPos].lpData;
- for (i = 0; i < chunkNum; i++) {
- for (j = 0; j < 33; j++) {
- ft->proto->playbackControl->gsmstate->gsmFrame[j] = packet->packet[i * 33 + j + 4];
- }
- gsm_decode(ft->proto->playbackControl->gsmstate, out);
- out += 160;
- }
- out = (short *)ft->proto->playbackControl->waveHeaders[ft->proto->playbackControl->waveHeadersPos].lpData;
- val = 0;
- for (i = 0; i<MODE_FRAME_SIZE[codec] * 160; i += MODE_FRAME_SIZE[codec]) {
- val += out[i] * out[i];
- }
- j = (int)((log10(val) - 4) * 2.35);
- if (j > VU_METER_LEVELS - 1) {
- j = VU_METER_LEVELS - 1;
- }
- else if (j < 0) {
- j = 0;
- }
- ft->proto->playbackControl->vuMeter = j;
- ft->proto->playbackControl->bytesSum += 8 + packet->len;
- /* Simple logic to avoid huge delays. If a delay is detected a frame is not played */
- j = ft->proto->availOverrunValue > 0 ? -1 : 0;
- while (ft->proto->framesAvailableForPlayback > FRAMES_AVAILABLE_MAX_LIMIT) {
- j = 1;
- SleepEx(5, FALSE);
- }
- ft->proto->availOverrunValue += j;
- /* 40 frames - 800ms/8kHz */
- if (ft->proto->availOverrunValue < 40) {
- ft->proto->framesAvailableForPlayback++;
- waveOutPrepareHeader(ft->proto->playbackControl->hWaveOut, &ft->proto->playbackControl->waveHeaders[ft->proto->playbackControl->waveHeadersPos], sizeof(WAVEHDR));
- waveOutWrite(ft->proto->playbackControl->hWaveOut, &ft->proto->playbackControl->waveHeaders[ft->proto->playbackControl->waveHeadersPos], sizeof(WAVEHDR));
- ft->proto->playbackControl->waveHeadersPos = (ft->proto->playbackControl->waveHeadersPos + 1) % ft->proto->playbackControl->waveHeadersNum;
- }
- else {
- ft->proto->availOverrunValue -= 10;
- statusTxt = "!! Skipping frame !!";
- }
- }
- }
- }
- {
- char ttt[2048];
- mir_snprintf(ttt, "%s %d %d ", statusTxt, ft->proto->framesAvailableForPlayback, ft->proto->availOverrunValue);
- SetDlgItemTextA(ft->proto->voiceDlgHWND, IDC_STATUS, ttt);
- }
- TlenP2PPacketFree(packet);
- }
- else {
- if (ft->proto->playbackControl != nullptr) {
- TlenVoiceFreeVc(ft->proto->playbackControl);
- ft->proto->playbackControl = nullptr;
- }
- ft->state = FT_ERROR;
- }
-}
-
-
-static void __cdecl TlenVoiceSendingThread(void *arg)
-{
- TLEN_FILE_TRANSFER *ft = (TLEN_FILE_TRANSFER *)arg;
- char *nick;
-
- ft->proto->debugLogA("Thread started: type=voice_send");
- ft->pfnNewConnectionV2 = TlenVoiceReceivingConnection;
- HNETLIBCONN s = (HNETLIBCONN)TlenP2PListen(ft);
- if (s != nullptr) {
- SetDlgItemText(ft->proto->voiceDlgHWND, IDC_STATUS, TranslateT("...Waiting for connection..."));
- //ProtoBroadcastAck(ft->proto->m_szModuleName, ft->hContact, ACKTYPE_FILE, ACKRESULT_CONNECTING, ft, 0);
- ft->s = s;
- //TlenLog("ft->s = %d", s);
- //TlenLog("fileCount = %d", ft->fileCount);
-
- HANDLE hEvent = CreateEvent(nullptr, FALSE, FALSE, nullptr);
- ft->hFileEvent = hEvent;
- ft->currentFile = 0;
- ft->state = FT_CONNECTING;
-
- nick = TlenNickFromJID(ft->jid);
- TlenSend(ft->proto, "<v t='%s' i='%s' e='6' a='%s' p='%d'/>", nick, ft->iqId, ft->localName, ft->wLocalPort);
- mir_free(nick);
- ft->proto->debugLogA("Waiting for the voice data to be sent...");
- WaitForSingleObject(hEvent, INFINITE);
- ft->hFileEvent = nullptr;
- CloseHandle(hEvent);
- ft->proto->debugLogA("Finish voice");
- Netlib_CloseHandle(s);
- ft->s = nullptr;
- ft->proto->debugLogA("ft->s is NULL");
-
- if (ft->state == FT_SWITCH) {
- ft->proto->debugLogA("Sending as client...");
- ft->state = FT_CONNECTING;
-
- NETLIBOPENCONNECTION nloc = { sizeof(nloc) };
- nloc.szHost = ft->hostName;
- nloc.wPort = ft->wPort;
- HNETLIBCONN sock = Netlib_OpenConnection(ft->proto->m_hNetlibUser, &nloc);
- if (sock != nullptr) {
- SetDlgItemText(ft->proto->voiceDlgHWND, IDC_STATUS, TranslateT("...Connecting..."));
- //ProtoBroadcastAck(ft->proto->m_szModuleName, ft->hContact, ACKTYPE_FILE, ACKRESULT_CONNECTING, ft, 0);
- ft->s = sock;
- TlenP2PEstablishOutgoingConnection(ft, FALSE);
- if (ft->state != FT_ERROR) {
- ft->proto->debugLogA("Entering send loop for this file connection...");
- ft->proto->playbackControl = nullptr;
- ft->proto->recordingControl = TlenVoiceCreateVC(ft->proto, 3);
- ft->proto->recordingControl->ft = ft;
- TlenVoiceRecordingStart(ft->proto->recordingControl);
- while (ft->state != FT_DONE && ft->state != FT_ERROR) {
- TlenVoiceReceiveParse(ft);
- }
- }
- ft->proto->debugLogA("Closing connection for this file transfer... ");
- Netlib_CloseHandle(sock);
- }
- else ft->state = FT_ERROR;
- }
- }
- else {
- ft->proto->debugLogA("Cannot allocate port to bind for file server thread, thread ended.");
- ft->state = FT_ERROR;
- }
- TlenListRemove(ft->proto, LIST_VOICE, ft->iqId);
- switch (ft->state) {
- case FT_DONE:
- ft->proto->debugLogA("Finish successfully");
- SetDlgItemText(ft->proto->voiceDlgHWND, IDC_STATUS, TranslateT("...Finished..."));
- //ProtoBroadcastAck(ft->proto->m_szModuleName, ft->hContact, ACKTYPE_FILE, ACKRESULT_SUCCESS, ft, 0);
- break;
- case FT_DENIED:
- SetDlgItemText(ft->proto->voiceDlgHWND, IDC_STATUS, TranslateT("...Denied..."));
- //ProtoBroadcastAck(ft->proto->m_szModuleName, ft->hContact, ACKTYPE_FILE, ACKRESULT_DENIED, ft, 0);
- break;
- default: // FT_ERROR:
- nick = TlenNickFromJID(ft->jid);
- TlenSend(ft->proto, "<v t='%s' i='%s' e='8'/>", nick, ft->iqId);
- mir_free(nick);
- ft->proto->debugLogA("Finish with errors");
- SetDlgItemText(ft->proto->voiceDlgHWND, IDC_STATUS, TranslateT("...Error..."));
- //ProtoBroadcastAck(ft->proto->m_szModuleName, ft->hContact, ACKTYPE_FILE, ACKRESULT_FAILED, ft, 0);
- break;
- }
- ft->proto->debugLogA("Thread ended: type=voice_send");
- TlenP2PFreeFileTransfer(ft);
-}
-
-static void TlenVoiceSendParse(TLEN_FILE_TRANSFER *ft)
-{
- int i;
-
- int codec = ft->proto->recordingControl->codec;
- TLEN_FILE_PACKET *packet = TlenP2PPacketCreate(sizeof(DWORD) + MODE_FRAME_SIZE[codec] * 33);
- if (packet != nullptr) {
- short *in;
- float val;
- in = ft->proto->recordingControl->recordingData;
- TlenP2PPacketSetType(packet, 0x96);
- packet->packet[0] = codec;
- TlenP2PPacketPackDword(packet, codec);
- val = 0;
- for (i = 0; i<MODE_FRAME_SIZE[codec] * 160; i += MODE_FRAME_SIZE[codec]) {
- val += in[i] * in[i];
- }
- i = (int)((log10(val) - 4) * 2.35);
- if (i > VU_METER_LEVELS - 1) {
- i = VU_METER_LEVELS - 1;
- }
- else if (i < 0) {
- i = 0;
- }
- ft->proto->recordingControl->vuMeter = i;
- for (i = 0; i < MODE_FRAME_SIZE[codec]; i++) {
- gsm_encode(ft->proto->recordingControl->gsmstate, in + i * 160);
- TlenP2PPacketPackBuffer(packet, (char*)ft->proto->recordingControl->gsmstate->gsmFrame, 33);
- }
- TlenVoiceCrypt(packet->packet + 4, packet->len - 4);
- if (!TlenP2PPacketSend(ft->s, packet)) {
- ft->state = FT_ERROR;
- }
- ft->proto->recordingControl->bytesSum += 8 + packet->len;
- TlenP2PPacketFree(packet);
- }
- else {
- ft->state = FT_ERROR;
- }
-}
-
-int TlenVoiceCancelAll(TlenProtocol *proto)
-{
- HANDLE hEvent;
- int i = 0;
-
- while ((i = TlenListFindNext(proto, LIST_VOICE, 0)) >= 0) {
- TLEN_LIST_ITEM *item = TlenListGetItemPtrFromIndex(proto, i);
- if (item != nullptr) {
- TLEN_FILE_TRANSFER *ft = item->ft;
- TlenListRemoveByIndex(proto, i);
- if (ft != nullptr) {
- if (ft->s) {
- //ProtoBroadcastAck(proto->m_szModuleName, ft->hContact, ACKTYPE_FILE, ACKRESULT_FAILED, ft, 0);
- proto->debugLogA("Closing ft->s = %d", ft->s);
- ft->state = FT_ERROR;
- Netlib_CloseHandle(ft->s);
- ft->s = nullptr;
- if (ft->hFileEvent != nullptr) {
- hEvent = ft->hFileEvent;
- ft->hFileEvent = nullptr;
- SetEvent(hEvent);
- }
- }
- else {
- proto->debugLogA("freeing (V) ft struct");
- TlenP2PFreeFileTransfer(ft);
- }
- }
- }
- }
- if (proto->voiceDlgHWND != nullptr) {
- EndDialog(proto->voiceDlgHWND, 0);
- }
- return 0;
-}
-
-INT_PTR TlenProtocol::VoiceContactMenuHandleVoice(WPARAM wParam, LPARAM)
-{
- if (!isOnline)
- return 1;
-
- MCONTACT hContact = (MCONTACT)wParam;
- if (hContact != NULL) {
- DBVARIANT dbv;
- if (!db_get(hContact, m_szModuleName, "jid", &dbv)) {
- char serialId[32];
- mir_snprintf(serialId, "%d", TlenSerialNext(this));
- TLEN_LIST_ITEM *item = TlenListAdd(this, LIST_VOICE, serialId);
- if (item != nullptr) {
- TLEN_FILE_TRANSFER *ft = TlenFileCreateFT(this, dbv.pszVal);
- ft->iqId = mir_strdup(serialId);
- item->ft = ft;
- TlenVoiceStart(ft, 2);
- TlenSend(ft->proto, "<v t='%s' e='1' i='%s' v='1'/>", ft->jid, serialId);
- }
- db_free(&dbv);
- }
- }
- return 0;
-}
-
-int TlenVoiceIsInUse(TlenProtocol *proto) {
- if (TlenListFindNext(proto, LIST_VOICE, 0) >= 0 || proto->voiceDlgHWND != nullptr) {
- proto->debugLogA("voice in use ? %d", proto->voiceDlgHWND);
- return 1;
- }
- return 0;
-}
-
-static HBITMAP TlenVoiceMakeBitmap(int w, int h, int bpp, void *ptr)
-{
- BITMAPINFO bmih;
- bmih.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
- bmih.bmiHeader.biWidth = w & 0xFFFFFFFC;
- bmih.bmiHeader.biHeight = h;//&0xFFFFFFFC;
- bmih.bmiHeader.biPlanes = 1; // musi byc 1
- bmih.bmiHeader.biBitCount = bpp;
- bmih.bmiHeader.biCompression = BI_RGB;
- bmih.bmiHeader.biSizeImage = 0;
- bmih.bmiHeader.biXPelsPerMeter = 0;
- bmih.bmiHeader.biYPelsPerMeter = 0;
- bmih.bmiHeader.biClrUsed = 0;
- bmih.bmiHeader.biClrImportant = 0;
- HDC hdc = CreateDC(L"DISPLAY", nullptr, nullptr, nullptr);
- HBITMAP hbm = CreateDIBitmap(hdc, (PBITMAPINFOHEADER)&bmih, CBM_INIT, ptr, &bmih, DIB_RGB_COLORS);
- ReleaseDC(nullptr, hdc);
- return hbm;
-}
-
-static void TlenVoiceInitVUMeters()
-{
- int i, v, y, x, x0, col, col0;
- unsigned char *pBits;
- int ledWidth, ledHeight;
- ledWidth = 9;
- ledHeight = 6;
- VU_METER_HEIGHT = ledHeight;
- VU_METER_WIDTH = (VU_METER_LEVELS - 1) * ledWidth;
- VU_METER_WIDTH = (VU_METER_WIDTH + 3) & (~3);
- pBits = (unsigned char *)mir_alloc(3 * VU_METER_WIDTH*VU_METER_HEIGHT);
- memset(pBits, 0x80, 3 * VU_METER_WIDTH*VU_METER_HEIGHT);
- for (i = 0; i < VU_METER_LEVELS; i++) {
- for (v = 0; v < VU_METER_LEVELS - 1; v++) {
- if (v >= i) {
- if (v < 10) col0 = 0x104010;
- else if (v < 13) col0 = 0x404010;
- else col0 = 0x401010;
- }
- else {
- if (v < 10) col0 = 0x00f000;
- else if (v < 13) col0 = 0xf0f000;
- else col0 = 0xf00000;
- }
- x0 = v * ledWidth;
- for (y = 1; y < VU_METER_HEIGHT - 1; y++) {
- col = col0;
- for (x = 1; x < ledWidth; x++) {
- pBits[3 * (x + x0 + y*VU_METER_WIDTH)] = col & 0xFF;
- pBits[3 * (x + x0 + y*VU_METER_WIDTH) + 1] = (col >> 8) & 0xFF;
- pBits[3 * (x + x0 + y*VU_METER_WIDTH) + 2] = (col >> 16) & 0xFF;
- }
- }
- }
- vuMeterBitmaps[i] = TlenVoiceMakeBitmap(VU_METER_WIDTH, VU_METER_HEIGHT, 24, pBits);
- }
- mir_free(pBits);
-}
-
-static INT_PTR CALLBACK TlenVoiceDlgProc(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
-{
- HDC hDC, hMemDC;
- int v;
- static int counter;
- TlenProtocol *proto = (TlenProtocol *)GetWindowLongPtr(hwndDlg, GWLP_USERDATA);
- switch (msg) {
- case WM_INITDIALOG:
- proto = (TlenProtocol *)lParam;
- SetWindowLongPtr(hwndDlg, GWLP_USERDATA, (LONG_PTR)proto);
- proto->voiceDlgHWND = hwndDlg;
- TranslateDialogDefault(hwndDlg);
- SendDlgItemMessage(hwndDlg, IDC_VCQUALITY, CB_ADDSTRING, 0, (LPARAM)L"8000 Hz / 13.8 kbps");
- SendDlgItemMessage(hwndDlg, IDC_VCQUALITY, CB_ADDSTRING, 0, (LPARAM)L"11025 Hz / 19.1 kbps");
- SendDlgItemMessage(hwndDlg, IDC_VCQUALITY, CB_ADDSTRING, 0, (LPARAM)L"22050 Hz / 36.8 kbps");
- SendDlgItemMessage(hwndDlg, IDC_VCQUALITY, CB_ADDSTRING, 0, (LPARAM)L"44100 Hz / 72 kbps");
- SendDlgItemMessage(hwndDlg, IDC_VCQUALITY, CB_SETCURSEL, 1, 0);
- SendDlgItemMessage(hwndDlg, IDC_MICROPHONE, BUTTONSETASFLATBTN, TRUE, 0);
- SendDlgItemMessage(hwndDlg, IDC_SPEAKER, BUTTONSETASFLATBTN, TRUE, 0);
- SendDlgItemMessage(hwndDlg, IDC_MICROPHONE, BUTTONSETASPUSHBTN, TRUE, 0);
- SendDlgItemMessage(hwndDlg, IDC_SPEAKER, BUTTONSETASPUSHBTN, TRUE, 0);
- {
- HICON hIcon = GetIcolibIcon(IDI_MICROPHONE);
- SendDlgItemMessage(hwndDlg, IDC_MICROPHONE, BM_SETIMAGE, IMAGE_ICON, (LPARAM)hIcon);
- ReleaseIcolibIcon(hIcon);
- hIcon = GetIcolibIcon(IDI_SPEAKER);
- SendDlgItemMessage(hwndDlg, IDC_SPEAKER, BM_SETIMAGE, IMAGE_ICON, (LPARAM)hIcon);
- ReleaseIcolibIcon(hIcon);
- }
- CheckDlgButton(hwndDlg, IDC_MICROPHONE, BST_CHECKED);
- CheckDlgButton(hwndDlg, IDC_SPEAKER, BST_CHECKED);
- TlenVoiceInitVUMeters();
- SetDlgItemText(hwndDlg, IDC_STATUS, TranslateT("...???..."));
- counter = 0;
- SetTimer(hwndDlg, 1, 100, nullptr);
- return FALSE;
- case WM_TIMER:
- if (proto->recordingControl != nullptr && !proto->recordingControl->bDisable) {
- v = proto->recordingControl->vuMeter % VU_METER_LEVELS;
- if (proto->recordingControl->vuMeter > 0) {
- proto->recordingControl->vuMeter--;
- }
- }
- else {
- v = 0;
- }
- hDC = GetDC(GetDlgItem(hwndDlg, IDC_VUMETERIN));
- if (nullptr != (hMemDC = CreateCompatibleDC(hDC))) {
- SelectObject(hMemDC, vuMeterBitmaps[v]);
- BitBlt(hDC, 0, 0, VU_METER_WIDTH, VU_METER_HEIGHT, hMemDC, 0, 0, SRCCOPY);
- DeleteDC(hMemDC);
- }
- ReleaseDC(GetDlgItem(hwndDlg, IDC_PLAN), hDC);
- if (proto->playbackControl != nullptr && !proto->playbackControl->bDisable) {
- v = proto->playbackControl->vuMeter % VU_METER_LEVELS;
- if (proto->playbackControl->vuMeter > 0) {
- proto->playbackControl->vuMeter--;
- }
- }
- else {
- v = 0;
- }
- hDC = GetDC(GetDlgItem(hwndDlg, IDC_VUMETEROUT));
- if (nullptr != (hMemDC = CreateCompatibleDC(hDC))) {
- SelectObject(hMemDC, vuMeterBitmaps[v]);
- BitBlt(hDC, 0, 0, VU_METER_WIDTH, VU_METER_HEIGHT, hMemDC, 0, 0, SRCCOPY);
- DeleteDC(hMemDC);
- }
- ReleaseDC(GetDlgItem(hwndDlg, IDC_PLAN), hDC);
- counter++;
- if (counter % 10 == 0) {
- char str[50];
- float fv;
- if (proto->recordingControl != nullptr) {
- fv = (float)proto->recordingControl->bytesSum;
- proto->recordingControl->bytesSum = 0;
- }
- else {
- fv = 0;
- }
- mir_snprintf(str, "%.1f kB/s", fv / 1024);
- SetDlgItemTextA(hwndDlg, IDC_BYTESOUT, str);
- if (proto->playbackControl != nullptr) {
- fv = (float)proto->playbackControl->bytesSum;
- proto->playbackControl->bytesSum = 0;
- }
- else {
- fv = 0;
- }
- mir_snprintf(str, "%.1f kB/s", fv / 1024);
- SetDlgItemTextA(hwndDlg, IDC_BYTESIN, str);
- }
- break;
- case WM_COMMAND:
- switch (LOWORD(wParam)) {
- case IDCANCEL:
- EndDialog(hwndDlg, 0);
- return TRUE;
- case IDC_VCQUALITY:
- if (HIWORD(wParam) == CBN_SELCHANGE) {
- if (proto->recordingControl != nullptr) {
- int codec = SendDlgItemMessage(hwndDlg, IDC_VCQUALITY, CB_GETCURSEL, 0, 0) + 2;
- if (codec != proto->recordingControl->codec && codec > 1 && codec < 6) {
- TLEN_FILE_TRANSFER *ft = proto->recordingControl->ft;
- TlenVoiceFreeVc(proto->recordingControl);
- proto->recordingControl = TlenVoiceCreateVC(ft->proto, codec);
- proto->recordingControl->ft = ft;
- TlenVoiceRecordingStart(proto->recordingControl);
- }
- }
- }
- break;
- case IDC_MICROPHONE:
- if (proto->recordingControl != nullptr) {
- proto->recordingControl->bDisable = BST_UNCHECKED == IsDlgButtonChecked(hwndDlg, IDC_MICROPHONE);
- }
- break;
- case IDC_SPEAKER:
- if (proto->playbackControl != nullptr) {
- proto->playbackControl->bDisable = BST_UNCHECKED == IsDlgButtonChecked(hwndDlg, IDC_SPEAKER);
- }
- break;
- }
- break;
- case WM_CLOSE:
- EndDialog(hwndDlg, 0);
- break;
- case WM_DESTROY:
- proto->voiceDlgHWND = nullptr;
- break;
-
- }
- return FALSE;
-}
-
-static void __cdecl TlenVoiceDlgThread(void *ptr)
-{
-
- TLEN_FILE_TRANSFER *ft = (TLEN_FILE_TRANSFER *)ptr;
- TlenProtocol *proto = ft->proto;
- DialogBoxParam(hInst, MAKEINTRESOURCE(IDD_VOICE), nullptr, TlenVoiceDlgProc, (LPARAM)proto);
- TlenVoiceCancelAll(proto);
-}
-
-int TlenVoiceStart(TLEN_FILE_TRANSFER *ft, int mode)
-{
-
- ft->proto->debugLogA("starting voice %d", mode);
- if (mode == 0) {
- mir_forkthread(TlenVoiceReceiveThread, ft);
- }
- else if (mode == 1) {
- mir_forkthread(TlenVoiceSendingThread, ft);
- }
- else {
- mir_forkthread(TlenVoiceDlgThread, ft);
- }
- return 0;
-}
-
-static char *getDisplayName(TlenProtocol *proto, const char *id)
-{
- char jid[256];
- MCONTACT hContact;
- DBVARIANT dbv;
- if (!db_get(NULL, proto->m_szModuleName, "LoginServer", &dbv)) {
- mir_snprintf(jid, "%s@%s", id, dbv.pszVal);
- db_free(&dbv);
- if ((hContact = TlenHContactFromJID(proto, jid)) != NULL)
- return mir_u2a(pcli->pfnGetContactDisplayName(hContact, 0));
- }
- return mir_strdup(id);
-}
-
-typedef struct {
- TlenProtocol *proto;
- TLEN_LIST_ITEM *item;
-}ACCEPTDIALOGDATA;
-
-static INT_PTR CALLBACK TlenVoiceAcceptDlgProc(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
-{
- ACCEPTDIALOGDATA * data;
- char *str;
- switch (msg) {
- case WM_INITDIALOG:
- TranslateDialogDefault(hwndDlg);
- data = (ACCEPTDIALOGDATA *)lParam;
- str = getDisplayName(data->proto, data->item->nick);
- SetDlgItemTextA(hwndDlg, IDC_FROM, str);
- mir_free(str);
- return FALSE;
- case WM_COMMAND:
- switch (LOWORD(wParam)) {
- case IDC_ACCEPT:
- EndDialog(hwndDlg, 1);
- return TRUE;
- case IDCANCEL:
- case IDCLOSE:
- EndDialog(hwndDlg, 0);
- return TRUE;
- }
- break;
- case WM_CLOSE:
- EndDialog(hwndDlg, 0);
- break;
- }
- return FALSE;
-}
-
-static void __cdecl TlenVoiceAcceptDlgThread(void *ptr)
-{
-
- ACCEPTDIALOGDATA *data = (ACCEPTDIALOGDATA *)ptr;
- int result = DialogBoxParam(hInst, MAKEINTRESOURCE(IDD_ACCEPT_VOICE), nullptr, TlenVoiceAcceptDlgProc, (LPARAM)data);
- if (result && data->proto->isOnline) {
- data->item->ft = TlenFileCreateFT(data->proto, data->item->nick);
- data->item->ft->iqId = mir_strdup(data->item->jid);
- TlenVoiceStart(data->item->ft, 2);
- TlenSend(data->proto, "<v t='%s' i='%s' e='5' v='1'/>", data->item->nick, data->item->jid);
- }
- else {
- if (data->proto->isOnline) {
- TlenSend(data->proto, "<v t='%s' i='%s' e='4' />", data->item->nick, data->item->jid);
- }
- TlenListRemove(data->proto, LIST_VOICE, data->item->jid);
- }
- mir_free(data);
-}
-
-int TlenVoiceAccept(TlenProtocol *proto, const char *id, const char *from)
-{
- if (!TlenVoiceIsInUse(proto)) {
- TLEN_LIST_ITEM *item = TlenListAdd(proto, LIST_VOICE, id);
- if (item != nullptr) {
- bool ask = true, ignore = false;
- int voiceChatPolicy = db_get_w(NULL, proto->m_szModuleName, "VoiceChatPolicy", 0);
- if (voiceChatPolicy == TLEN_MUC_ASK) {
- ignore = false;
- ask = true;
- }
- else if (voiceChatPolicy == TLEN_MUC_IGNORE_ALL) {
- ignore = true;
- }
- else if (voiceChatPolicy == TLEN_MUC_IGNORE_NIR) {
- char jid[256];
- DBVARIANT dbv;
- if (!db_get(NULL, proto->m_szModuleName, "LoginServer", &dbv)) {
- mir_snprintf(jid, "%s@%s", from, dbv.pszVal);
- db_free(&dbv);
- }
- else {
- strncpy(jid, from, _countof(jid) - 1);
- }
- ignore = !IsAuthorized(proto, jid);
- ask = true;
- }
- else if (voiceChatPolicy == TLEN_MUC_ACCEPT_IR) {
- char jid[256];
- DBVARIANT dbv;
- if (!db_get(NULL, proto->m_szModuleName, "LoginServer", &dbv)) {
- mir_snprintf(jid, "%s@%s", from, dbv.pszVal);
- db_free(&dbv);
- }
- else {
- strncpy(jid, from, _countof(jid) - 1);
- }
- ask = !IsAuthorized(proto, jid);
- ignore = false;
- }
- else if (voiceChatPolicy == TLEN_MUC_ACCEPT_ALL) {
- ask = false;
- ignore = false;
- }
- if (ignore) {
- if (proto->isOnline) {
- TlenSend(proto, "<v t='%s' i='%s' e='4' />", from, id);
- }
- TlenListRemove(proto, LIST_VOICE, id);
- }
- else {
- item->nick = mir_strdup(from);
- if (ask) {
- ACCEPTDIALOGDATA *data = (ACCEPTDIALOGDATA *)mir_alloc(sizeof(ACCEPTDIALOGDATA));
- data->proto = proto;
- data->item = item;
- mir_forkthread(TlenVoiceAcceptDlgThread, data);
- }
- else if (proto->isOnline) {
- item->ft = TlenFileCreateFT(proto, from);
- item->ft->iqId = mir_strdup(id);
- TlenVoiceStart(item->ft, 2);
- TlenSend(proto, "<v t='%s' i='%s' e='5' v='1'/>", item->nick, item->jid);
- }
- }
- return 1;
- }
- }
- return 0;
-}
-
-int TlenVoiceBuildInDeviceList(TlenProtocol *proto, HWND hWnd)
-{
- int i, j, iNumDevs;
- WAVEINCAPS wic;
- iNumDevs = waveInGetNumDevs();
- SendMessage(hWnd, CB_ADDSTRING, 0, (LPARAM)TranslateT("Default"));
- for (i = j = 0; i < iNumDevs; i++) {
- if (!waveInGetDevCaps(i, &wic, sizeof(WAVEINCAPS))) {
- if (wic.dwFormats != 0) {
- SendMessage(hWnd, CB_ADDSTRING, 0, (LPARAM)wic.szPname);
- j++;
- }
- }
- }
- i = db_get_w(NULL, proto->m_szModuleName, "VoiceDeviceIn", 0);
- if (i > j) i = 0;
- SendMessage(hWnd, CB_SETCURSEL, i, 0);
- return 0;
-}
-
-int TlenVoiceBuildOutDeviceList(TlenProtocol *proto, HWND hWnd)
-{
- int i, j, iNumDevs;
- WAVEOUTCAPS woc;
- iNumDevs = waveInGetNumDevs();
- SendMessage(hWnd, CB_ADDSTRING, 0, (LPARAM)TranslateT("Default"));
- for (i = j = 0; i < iNumDevs; i++) {
- if (!waveOutGetDevCaps(i, &woc, sizeof(WAVEOUTCAPS))) {
- if (woc.dwFormats != 0) {
- SendMessage(hWnd, CB_ADDSTRING, 0, (LPARAM)woc.szPname);
- j++;
- }
- }
- }
- i = db_get_w(NULL, proto->m_szModuleName, "VoiceDeviceOut", 0);
- if (i > j) i = 0;
- SendMessage(hWnd, CB_SETCURSEL, i, 0);
- return 0;
-}
diff --git a/protocols/Tlen/src/tlen_voice.h b/protocols/Tlen/src/tlen_voice.h
deleted file mode 100644
index daaa13eda4..0000000000
--- a/protocols/Tlen/src/tlen_voice.h
+++ /dev/null
@@ -1,68 +0,0 @@
-/*
-
-Tlen Protocol Plugin for Miranda NG
-Copyright (C) 2004-2007 Piotr Piastucki
-
-This program is free software; you can redistribute it and/or
-modify it under the terms of the GNU General Public License
-as published by the Free Software Foundation; either version 2
-of the License, or (at your option) any later version.
-
-This program 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 General Public License for more details.
-
-You should have received a copy of the GNU General Public License
-along with this program; if not, write to the Free Software
-Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
-
-*/
-
-#ifndef _TLEN_VOICE_H_
-#define _TLEN_VOICE_H_
-
-#include <windows.h>
-#include <mmsystem.h>
-
-#include "stdafx.h"
-extern "C" {
- #include "codec/gsm.h"
-}
-
-typedef struct TLEN_VOICE_CONTROL_STRUCT {
- int waveMode;
- int codec;
- int bDisable;
-
- short *recordingData;
- short *waveData;
- WAVEHDR *waveHeaders;
- int waveFrameSize;
- int waveHeadersPos;
- int waveHeadersNum;
-
-// HANDLE hEvent;
- HANDLE hThread;
- DWORD threadID;
- HWAVEOUT hWaveOut;
- HWAVEIN hWaveIn;
- int isRunning;
- int stopThread;
- gsm_state *gsmstate;
- TLEN_FILE_TRANSFER *ft;
- int vuMeter;
- int bytesSum;
- TlenProtocol *proto;
-} TLEN_VOICE_CONTROL;
-
-
-int TlenVoiceStart(TLEN_FILE_TRANSFER *ft, int mode) ;
-int TlenVoiceAccept(TlenProtocol *proto, const char *id, const char *from);
-int TlenVoiceIsInUse(TlenProtocol *proto);
-int TlenVoiceCancelAll(TlenProtocol *proto);
-int TlenVoiceBuildInDeviceList(TlenProtocol *proto, HWND hWnd);
-int TlenVoiceBuildOutDeviceList(TlenProtocol *proto, HWND hWnd);
-
-#endif
-
diff --git a/protocols/Tlen/src/tlen_ws.cpp b/protocols/Tlen/src/tlen_ws.cpp
deleted file mode 100644
index 1cf07644d8..0000000000
--- a/protocols/Tlen/src/tlen_ws.cpp
+++ /dev/null
@@ -1,157 +0,0 @@
-/*
-
-Jabber Protocol Plugin for Miranda IM
-Tlen Protocol Plugin for Miranda NG
-Copyright (C) 2002-2004 Santithorn Bunchua
-Copyright (C) 2004-2007 Piotr Piastucki
-
-This program is free software; you can redistribute it and/or
-modify it under the terms of the GNU General Public License
-as published by the Free Software Foundation; either version 2
-of the License, or (at your option) any later version.
-
-This program 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 General Public License for more details.
-
-You should have received a copy of the GNU General Public License
-along with this program; if not, write to the Free Software
-Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
-
-*/
-
-#include "stdafx.h"
-
-BOOL TlenWsInit(TlenProtocol *proto)
-{
- wchar_t name[128];
-
- NETLIBUSER nlu = {};
- nlu.szDescriptiveName.w = name;
- nlu.szSettingsModule = proto->m_szModuleName;
-
- nlu.flags = NUF_OUTGOING | NUF_INCOMING | NUF_HTTPCONNS | NUF_UNICODE;
- mir_snwprintf(name, TranslateT("%s connection"), proto->m_tszUserName);
- proto->m_hNetlibUser = Netlib_RegisterUser(&nlu);
-
- nlu.flags = NUF_OUTGOING | NUF_INCOMING | NUF_NOOPTIONS | NUF_UNICODE;
- mir_snwprintf(name, TranslateT("%s SOCKS connection"), proto->m_tszUserName);
- proto->hFileNetlibUser = Netlib_RegisterUser(&nlu);
-
- NETLIBUSERSETTINGS nlus = {0};
- nlus.cbSize = sizeof(nlus);
- nlus.useProxy = 0;
- Netlib_SetUserSettings(proto->hFileNetlibUser, &nlus);
-
- return (proto->m_hNetlibUser != nullptr)?TRUE:FALSE;
-}
-
-void TlenWsUninit(TlenProtocol *proto)
-{
- if (proto->m_hNetlibUser != nullptr) Netlib_CloseHandle(proto->m_hNetlibUser);
- if (proto->hFileNetlibUser != nullptr) Netlib_CloseHandle(proto->hFileNetlibUser);
- proto->m_hNetlibUser = nullptr;
- proto->hFileNetlibUser = nullptr;
-}
-
-HNETLIBCONN TlenWsConnect(TlenProtocol *proto, char *host, WORD port)
-{
- NETLIBOPENCONNECTION nloc = {};
- nloc.cbSize = sizeof(NETLIBOPENCONNECTION); //NETLIBOPENCONNECTION_V1_SIZE;
- nloc.szHost = host;
- nloc.wPort = port;
- nloc.flags = 0;
- nloc.timeout = 6;
- return Netlib_OpenConnection(proto->m_hNetlibUser, &nloc);
-}
-
-int TlenWsSend(TlenProtocol *proto, HNETLIBCONN s, char *data, int datalen)
-{
- int len;
- if ((len=Netlib_Send(s, data, datalen, /*MSG_NODUMP|*/MSG_DUMPASTEXT)) == SOCKET_ERROR || len != datalen) {
- proto->debugLogA("Netlib_Send() failed, error=%d", WSAGetLastError());
- return FALSE;
- }
- return TRUE;
-}
-
-int TlenWsRecv(TlenProtocol *proto, HNETLIBCONN s, char *data, long datalen)
-{
- int ret;
- ret = Netlib_Recv(s, data, datalen, /*MSG_NODUMP|*/MSG_DUMPASTEXT);
- if (ret == SOCKET_ERROR) {
- proto->debugLogA("Netlib_Recv() failed, error=%d", WSAGetLastError());
- return 0;
- }
- if (ret == 0) {
- proto->debugLogA("Connection closed gracefully");
- return 0;
- }
- return ret;
-}
-
-
-int TlenWsSendAES(TlenProtocol *proto, char *data, int datalen, aes_context *aes_ctx, unsigned char *aes_iv)
-{
- int len, sendlen;
- unsigned char aes_input[16];
- unsigned char aes_output[256];
- if (proto->threadData == nullptr) {
- return FALSE;
- }
- while (datalen > 0) {
- len = 0;
- while (datalen > 0 && len < 256) {
- int pad = datalen < 16 ? 16 - datalen : 0;
- memcpy(aes_input, data, datalen < 16 ? datalen : 16);
- memset(aes_input + 16 - pad, ' ', pad);
- aes_crypt_cbc(aes_ctx, AES_ENCRYPT, 16, aes_iv, aes_input, aes_output + len);
- datalen -= 16;
- data += 16;
- len += 16;
- }
- if (len > 0) {
- proto->debugLogA("Sending %d bytes", len);
- if ((sendlen=Netlib_Send(proto->threadData->s, (char *)aes_output, len, MSG_NODUMP)) == SOCKET_ERROR || len != sendlen) {
- proto->debugLogA("Netlib_Send() failed, error=%d", WSAGetLastError());
- return FALSE;
- }
- }
- }
- return TRUE;
-}
-
-int TlenWsRecvAES(TlenProtocol *proto, char *data, long datalen, aes_context *aes_ctx, unsigned char *aes_iv)
-{
- int ret, len = 0, maxlen = datalen;
- unsigned char aes_input[16];
- unsigned char *aes_output = (unsigned char *)data;
- if (proto->threadData == nullptr) {
- return 0;
- }
- for (maxlen = maxlen & ~0xF; maxlen != 0; maxlen = maxlen & 0xF) {
- ret = Netlib_Recv(proto->threadData->s, data, maxlen, MSG_NODUMP);
- if (ret == SOCKET_ERROR) {
- proto->debugLogA("Netlib_Recv() failed, error=%d", WSAGetLastError());
- return 0;
- }
- if (ret == 0) {
- proto->debugLogA("Connection closed gracefully");
- return 0;
- }
- data += ret;
- len += ret;
- maxlen -= ret;
- }
-
- ret = len;
- while (len > 15) {
- memcpy(aes_input, aes_output, 16);
- aes_crypt_cbc(aes_ctx, AES_DECRYPT, 16, aes_iv, aes_input, aes_output);
- aes_output += 16;
- len -= 16;
- }
- return ret;
-}
-
diff --git a/protocols/Tlen/src/tlen_xml.cpp b/protocols/Tlen/src/tlen_xml.cpp
deleted file mode 100644
index 68ddc29145..0000000000
--- a/protocols/Tlen/src/tlen_xml.cpp
+++ /dev/null
@@ -1,572 +0,0 @@
-/*
-
-Jabber Protocol Plugin for Miranda IM
-Tlen Protocol Plugin for Miranda NG
-Copyright (C) 2002-2004 Santithorn Bunchua
-Copyright (C) 2004-2007 Piotr Piastucki
-
-This program is free software; you can redistribute it and/or
-modify it under the terms of the GNU General Public License
-as published by the Free Software Foundation; either version 2
-of the License, or (at your option) any later version.
-
-This program 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 General Public License for more details.
-
-You should have received a copy of the GNU General Public License
-along with this program; if not, write to the Free Software
-Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
-
-*/
-
-#include "stdafx.h"
-#include <ctype.h>
-
-static BOOL TlenXmlProcessElem(XmlState *xmlState, XmlElemType elemType, char *elemText, char *elemAttr);
-static void TlenXmlRemoveChild(XmlNode *node, XmlNode *child);
-
-void TlenXmlInitState(XmlState *xmlState)
-{
- if (xmlState == nullptr) return;
- xmlState->root.name = nullptr;
- xmlState->root.depth = 0;
- xmlState->root.numAttr = 0;
- xmlState->root.maxNumAttr = 0;
- xmlState->root.attr = nullptr;
- xmlState->root.numChild = 0;
- xmlState->root.maxNumChild = 0;
- xmlState->root.child = nullptr;
- xmlState->root.text = nullptr;
- xmlState->root.state = NODE_OPEN;
- xmlState->callback1_open = nullptr;
- xmlState->callback1_close = nullptr;
- xmlState->callback2_open = nullptr;
- xmlState->callback2_close = nullptr;
- xmlState->userdata1_open = nullptr;
- xmlState->userdata1_close = nullptr;
- xmlState->userdata2_open = nullptr;
- xmlState->userdata2_close = nullptr;
-}
-
-void TlenXmlDestroyState(XmlState *xmlState)
-{
- int i;
- XmlNode *node;
-
- if (xmlState == nullptr)
- return;
- // Note: cannot use TlenXmlFreeNode() to free xmlState->root
- // because it will do mir_free(xmlState->root) which is not freeable.
- node = &(xmlState->root);
- // Free all children first
- for (i=0; i<node->numChild; i++)
- TlenXmlFreeNode(node->child[i]);
- mir_free(node->child);
- // Free all attributes
- for (i=0; i<node->numAttr; i++) {
- mir_free(node->attr[i]->name);
- mir_free(node->attr[i]->value);
- mir_free(node->attr[i]);
- }
- mir_free(node->attr);
- // Free string field
- mir_free(node->text);
- mir_free(node->name);
- /* mir_free(xmlState) - no need, work with static. */
-}
-
-BOOL TlenXmlSetCallback(XmlState *xmlState, int depth, XmlElemType type, void (*callback)(XmlNode*, void*), void *userdata)
-{
- if (depth == 1 && type == ELEM_OPEN) {
- xmlState->callback1_open = callback;
- xmlState->userdata1_open = userdata;
- }
- else if (depth == 1 && type == ELEM_CLOSE) {
- xmlState->callback1_close = callback;
- xmlState->userdata1_close = userdata;
- }
- else if (depth == 2 && type == ELEM_OPEN) {
- xmlState->callback2_open = callback;
- xmlState->userdata2_open = userdata;
- }
- else if (depth == 2 && type == ELEM_CLOSE) {
- xmlState->callback2_close = callback;
- xmlState->userdata2_close = userdata;
- }
- else
- return FALSE;
-
- return TRUE;
-}
-
-#define TAG_MAX_LEN 50
-#define ATTR_MAX_LEN 1024
-int TlenXmlParse(XmlState *xmlState, char *buffer, int datalen)
-{
- char *p, *q, *r, *eob;
- char *str;
- int num;
- char tag[TAG_MAX_LEN];
- char attr[ATTR_MAX_LEN];
- XmlElemType elemType = ELEM_OPEN;
-
- eob = buffer + datalen;
- num = 0;
- // Skip leading whitespaces
- for (p=buffer; p<eob && isspace(*p); p++,num++);
- while (num < datalen) {
- if (*p == '<') { // found starting bracket
- for (q=p+1; q<eob && *q != '>'; q++);
- if (q < eob) { // found closing bracket
- for (r=p+1; *r != '>' && *r != ' ' && *r != '\t'; r++);
- if (r-(p+1) > TAG_MAX_LEN) {
-// TlenLog("TAG_MAX_LEN too small, ignore current tag");
- }
- else {
- if (*(p+1) == '/') { // closing tag
- strncpy(tag, p+2, r-(p+2));
- tag[r-(p+2)] = '\0';
- elemType = ELEM_CLOSE;
- }
- else {
- if (*(r-1) == '/') { // single open/close tag
- strncpy(tag, p+1, r-(p+1)-1);
- tag[r-(p+1)-1] = '\0';
- elemType = ELEM_OPENCLOSE;
- }
- else {
- strncpy(tag, p+1, r-(p+1));
- tag[r-(p+1)] = '\0';
- elemType = ELEM_OPEN;
- }
- }
- for (;r<q && (*r == ' ' || *r == '\t'); r++);
- if (q-r > ATTR_MAX_LEN) {
-// TlenLog("ATTR_MAX_LEN too small, ignore current tag");
- }
- else {
- strncpy(attr, r, q-r);
- if ((q-r)>0 && attr[q-r-1] == '/') {
- attr[q-r-1] = '\0';
- elemType = ELEM_OPENCLOSE;
- }
- else
- attr[q-r] = '\0';
- TlenXmlProcessElem(xmlState, elemType, tag, attr);
- }
- }
- num += (q-p+1);
- p = q + 1;
- if (elemType == ELEM_CLOSE || elemType == ELEM_OPENCLOSE) {
- // Skip whitespaces after end tags
- for (; p<eob && isspace(*p); p++,num++);
- }
- }
- else
- break;
- }
- else { // found inner text
- for (q=p+1; q<eob && *q != '<'; q++);
- if (q < eob) { // found starting bracket of the next element
- str = (char *) mir_alloc(q-p+1);
- strncpy(str, p, q-p);
- str[q-p] = '\0';
- TlenXmlProcessElem(xmlState, ELEM_TEXT, str, nullptr);
- mir_free(str);
- num += (q-p);
- p = q;
- }
- else
- break;
- }
- }
-
- return num;
-}
-
-static void TlenXmlParseAttr(XmlNode *node, char *text)
-{
- char *kstart, *vstart;
- int klen, vlen;
- char *p;
- XmlAttr *a;
-
- if (node == nullptr || text == nullptr || mir_strlen(text) <= 0)
- return;
-
- for (p=text;;) {
-
- // Skip leading whitespaces
- for (;*p != '\0' && (*p == ' ' || *p == '\t'); p++);
- if (*p == '\0')
- break;
-
- // Fetch key
- kstart = p;
- for (;*p != '\0' && *p != '=' && *p != ' ' && *p != '\t'; p++);
- klen = p-kstart;
-
- if (node->numAttr >= node->maxNumAttr) {
- node->maxNumAttr = node->numAttr + 20;
- node->attr = (XmlAttr **) mir_realloc(node->attr, node->maxNumAttr*sizeof(XmlAttr *));
- }
- a = node->attr[node->numAttr] = (XmlAttr *) mir_alloc(sizeof(XmlAttr));
- node->numAttr++;
-
- // Skip possible whitespaces between key and '='
- for (;*p != '\0' && (*p == ' ' || *p == '\t'); p++);
-
- if (*p == '\0') {
- a->name = (char *) mir_alloc(klen+1);
- strncpy(a->name, kstart, klen);
- a->name[klen] = '\0';
- a->value = mir_strdup("");
- break;
- }
-
- if (*p != '=') {
- a->name = (char *) mir_alloc(klen+1);
- strncpy(a->name, kstart, klen);
- a->name[klen] = '\0';
- a->value = mir_strdup("");
- continue;
- }
-
- // Found '='
- p++;
-
- // Skip possible whitespaces between '=' and value
- for (;*p != '\0' && (*p == ' ' || *p == '\t'); p++);
-
- if (*p == '\0') {
- a->name = (char *) mir_alloc(klen+1);
- strncpy(a->name, kstart, klen);
- a->name[klen] = '\0';
- a->value = mir_strdup("");
- break;
- }
-
- // Fetch value
- if (*p == '\'' || *p == '"') {
- p++;
- vstart = p;
- for (;*p != '\0' && *p != *(vstart-1); p++);
- vlen = p-vstart;
- if (*p != '\0') p++;
- }
- else {
- vstart = p;
- for (;*p != '\0' && *p != ' ' && *p != '\t'; p++);
- vlen = p-vstart;
- }
-
- a->name = (char *) mir_alloc(klen+1);
- strncpy(a->name, kstart, klen);
- a->name[klen] = '\0';
- a->value = (char *) mir_alloc(vlen+1);
- strncpy(a->value, vstart, vlen);
- a->value[vlen] = '\0';
- }
-}
-
-static BOOL TlenXmlProcessElem(XmlState *xmlState, XmlElemType elemType, char *elemText, char *elemAttr)
-{
- XmlNode *node, *parentNode, *n;
- //BOOL activateCallback = FALSE;
- char *text, *attr;
-
- if (elemText == nullptr) return FALSE;
-
- if (elemType == ELEM_OPEN && !mir_strcmp(elemText, "?xml")) {
-// TlenLog("XML: skip <?xml> tag");
- return TRUE;
- }
-
- // Find active node
- node = &(xmlState->root);
- parentNode = nullptr;
- while (node->numChild>0 && node->child[node->numChild-1]->state == NODE_OPEN) {
- parentNode = node;
- node = node->child[node->numChild-1];
- }
-
- if (node->state != NODE_OPEN) return FALSE;
-
- text = mir_strdup(elemText);
-
- if (elemAttr)
- attr = mir_strdup(elemAttr);
- else
- attr = nullptr;
-
- switch (elemType) {
- case ELEM_OPEN:
- if (node->numChild >= node->maxNumChild) {
- node->maxNumChild = node->numChild + 20;
- node->child = (XmlNode **) mir_realloc(node->child, node->maxNumChild*sizeof(XmlNode *));
- }
- n = node->child[node->numChild] = (XmlNode *) mir_alloc(sizeof(XmlNode));
- node->numChild++;
- n->name = text;
- n->depth = node->depth + 1;
- n->state = NODE_OPEN;
- n->numChild = n->maxNumChild = 0;
- n->child = nullptr;
- n->numAttr = n->maxNumAttr = 0;
- n->attr = nullptr;
- TlenXmlParseAttr(n, attr);
- n->text = nullptr;
- if (n->depth == 1 && xmlState->callback1_open != nullptr)
- (*(xmlState->callback1_open))(n, xmlState->userdata1_open);
- if (n->depth == 2 && xmlState->callback2_open != nullptr)
- (*xmlState->callback2_open)(n, xmlState->userdata2_open);
- break;
- case ELEM_OPENCLOSE:
- if (node->numChild >= node->maxNumChild) {
- node->maxNumChild = node->numChild + 20;
- node->child = (XmlNode **) mir_realloc(node->child, node->maxNumChild*sizeof(XmlNode *));
- }
- n = node->child[node->numChild] = (XmlNode *) mir_alloc(sizeof(XmlNode));
- node->numChild++;
- n->name = text;
- n->depth = node->depth + 1;
- n->state = NODE_CLOSE;
- n->numChild = n->maxNumAttr = 0;
- n->child = nullptr;
- n->numAttr = n->maxNumAttr = 0;
- n->attr = nullptr;
- TlenXmlParseAttr(n, attr);
- n->text = nullptr;
- if (n->depth == 1 && xmlState->callback1_close != nullptr) {
- (*(xmlState->callback1_close))(n, xmlState->userdata1_close);
- TlenXmlRemoveChild(node, n);
- }
- if (n->depth == 2 && xmlState->callback2_close != nullptr) {
- (*xmlState->callback2_close)(n, xmlState->userdata2_close);
- TlenXmlRemoveChild(node, n);
- }
- break;
- case ELEM_CLOSE:
- if (node->name != nullptr && !mir_strcmp(node->name, text)) {
- node->state = NODE_CLOSE;
- int nodeDepth = node->depth;
- if (nodeDepth == 1 && xmlState->callback1_close != nullptr) {
- (*(xmlState->callback1_close))(node, xmlState->userdata1_close);
- TlenXmlRemoveChild(parentNode, node);
- }
- if (nodeDepth == 2 && xmlState->callback2_close != nullptr) {
- (*xmlState->callback2_close)(node, xmlState->userdata2_close);
- TlenXmlRemoveChild(parentNode, node);
- }
- mir_free(text);
- }
- else {
-// TlenLog("XML: Closing </%s> without opening tag", text);
- mir_free(text);
- if (attr) mir_free(attr);
- return FALSE;
- }
- break;
- case ELEM_TEXT:
- node->text = text;
- break;
- default:
- mir_free(text);
- if (attr) mir_free(attr);
- return FALSE;
- }
-
- if (attr) mir_free(attr);
-
- return TRUE;
-}
-
-char *TlenXmlGetAttrValue(XmlNode *node, char *key)
-{
- int i;
-
- if (node == nullptr || node->numAttr <= 0 || key == nullptr || mir_strlen(key) <= 0)
- return nullptr;
- for (i=0; i<node->numAttr; i++) {
- if (node->attr[i]->name && !mir_strcmp(key, node->attr[i]->name))
- return node->attr[i]->value;
- }
- return nullptr;
-}
-
-XmlNode *TlenXmlGetChild(XmlNode *node, char *tag)
-{
- return TlenXmlGetNthChild(node, tag, 1);
-}
-
-XmlNode *TlenXmlGetNthChild(XmlNode *node, char *tag, int nth)
-{
- int i, num;
-
- if (node == nullptr || node->numChild <= 0 || tag == nullptr || mir_strlen(tag) <= 0 || nth < 1)
- return nullptr;
- num = 1;
- for (i=0; i<node->numChild; i++) {
- if (node->child[i]->name && !mir_strcmp(tag, node->child[i]->name)) {
- if (num == nth) {
- return node->child[i];
- }
- num++;
- }
- }
- return nullptr;
-}
-
-XmlNode *TlenXmlGetChildWithGivenAttrValue(XmlNode *node, char *tag, char *attrKey, char *attrValue)
-{
- int i;
- char *str;
-
- if (node == nullptr || node->numChild <= 0 || tag == nullptr || mir_strlen(tag) <= 0 || attrKey == nullptr || mir_strlen(attrKey) <= 0 || attrValue == nullptr || mir_strlen(attrValue) <= 0)
- return nullptr;
- for (i=0; i<node->numChild; i++) {
- if (node->child[i]->name && !mir_strcmp(tag, node->child[i]->name)) {
- if ((str=TlenXmlGetAttrValue(node->child[i], attrKey)) != nullptr)
- if (!mir_strcmp(str, attrValue))
- return node->child[i];
- }
- }
- return nullptr;
-}
-
-static void TlenXmlRemoveChild(XmlNode *node, XmlNode *child)
-{
- int i;
-
- if (node == nullptr || child == nullptr || node->numChild <= 0) return;
- for (i=0; i<node->numChild; i++) {
- if (node->child[i] == child)
- break;
- }
- if (i < node->numChild) {
- for (++i; i<node->numChild; i++)
- node->child[i-1] = node->child[i];
- node->numChild--;
- TlenXmlFreeNode(child);
- }
-}
-
-void TlenXmlFreeNode(XmlNode *node)
-{
- int i;
-
- if (node == nullptr)
- return;
- // Free all children first
- for (i=0; i<node->numChild; i++)
- TlenXmlFreeNode(node->child[i]);
- mir_free(node->child);
- // Free all attributes
- for (i=0; i<node->numAttr; i++) {
- mir_free(node->attr[i]->name);
- mir_free(node->attr[i]->value);
- mir_free(node->attr[i]);
- }
- mir_free(node->attr);
- // Free string field
- mir_free(node->text);
- mir_free(node->name);
- // Free the node itself
- mir_free(node);
-}
-
-XmlNode *TlenXmlCopyNode(XmlNode *node)
-{
- XmlNode *n;
- int i;
-
- if (node == nullptr) return nullptr;
- n = (XmlNode *) mir_alloc(sizeof(XmlNode));
- // Copy attributes
- if (node->numAttr > 0) {
- n->attr = (XmlAttr **) mir_alloc(node->numAttr*sizeof(XmlAttr *));
- for (i=0; i<node->numAttr; i++) {
- n->attr[i] = (XmlAttr *) mir_alloc(sizeof(XmlAttr));
- if (node->attr[i]->name) n->attr[i]->name = mir_strdup(node->attr[i]->name);
- else n->attr[i]->name = nullptr;
- if (node->attr[i]->value) n->attr[i]->value = mir_strdup(node->attr[i]->value);
- else n->attr[i]->value = nullptr;
- }
- }
- else
- n->attr = nullptr;
- // Recursively copy children
- if (node->numChild > 0) {
- n->child = (XmlNode **) mir_alloc(node->numChild*sizeof(XmlNode *));
- for (i=0; i<node->numChild; i++)
- n->child[i] = TlenXmlCopyNode(node->child[i]);
- }
- else
- n->child = nullptr;
- // Copy other fields
- n->numAttr = node->numAttr;
- n->maxNumAttr = node->numAttr;
- n->numChild = node->numChild;
- n->maxNumChild = node->numChild;
- n->depth = node->depth;
- n->state = node->state;
- n->name = (node->name)?mir_strdup(node->name):nullptr;
- n->text = (node->text)?mir_strdup(node->text):nullptr;
-
- return n;
-}
-
-XmlNode *TlenXmlCreateNode(char *name)
-{
- XmlNode *n;
-
- if (name == nullptr)
- return nullptr;
-
- n = (XmlNode *) mir_alloc(sizeof(XmlNode));
- memset(n, 0, sizeof(XmlNode));
- n->name = mir_strdup(name);
- return n;
-}
-
-void TlenXmlAddAttr(XmlNode *n, char *name, char *value)
-{
- int i;
-
- if (n == nullptr || name == nullptr || value == nullptr)
- return;
-
- i = n->numAttr;
- (n->numAttr)++;
- n->attr = (XmlAttr **) mir_realloc(n->attr, sizeof(XmlAttr *) * n->numAttr);
- n->attr[i] = (XmlAttr *) mir_alloc(sizeof(XmlAttr));
- n->attr[i]->name = mir_strdup(name);
- n->attr[i]->value = mir_strdup(value);
-}
-
-XmlNode *TlenXmlAddChild(XmlNode *n, char *name)
-{
- int i;
-
- if (n == nullptr || name == nullptr)
- return nullptr;
-
- i = n->numChild;
- n->numChild++;
- n->child = (XmlNode **) mir_realloc(n->child, sizeof(XmlNode *) * n->numChild);
- n->child[i] = (XmlNode *) mir_alloc(sizeof(XmlNode));
- memset(n->child[i], 0, sizeof(XmlNode));
- n->child[i]->name = mir_strdup(name);
- return n->child[i];
-}
-
-void TlenXmlAddText(XmlNode *n, char *text)
-{
- if (n != nullptr && text != nullptr) {
- if (n->text) mir_free(n->text);
- n->text = mir_strdup(text);
- }
-}
-
diff --git a/protocols/Tlen/src/tlen_xml.h b/protocols/Tlen/src/tlen_xml.h
deleted file mode 100644
index 9b5a078149..0000000000
--- a/protocols/Tlen/src/tlen_xml.h
+++ /dev/null
@@ -1,79 +0,0 @@
-/*
-
-Jabber Protocol Plugin for Miranda IM
-Tlen Protocol Plugin for Miranda NG
-Copyright (C) 2002-2004 Santithorn Bunchua
-Copyright (C) 2004-2007 Piotr Piastucki
-
-This program is free software; you can redistribute it and/or
-modify it under the terms of the GNU General Public License
-as published by the Free Software Foundation; either version 2
-of the License, or (at your option) any later version.
-
-This program 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 General Public License for more details.
-
-You should have received a copy of the GNU General Public License
-along with this program; if not, write to the Free Software
-Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
-
-*/
-
-#ifndef _TLEN_XML_H_
-#define _TLEN_XML_H_
-
-typedef enum { ELEM_OPEN, ELEM_CLOSE, ELEM_OPENCLOSE, ELEM_TEXT } XmlElemType;
-typedef enum { NODE_OPEN, NODE_CLOSE } XmlNodeType;
-
-typedef struct tagXmlAttr {
- char *name;
- char *value;
-} XmlAttr;
-
-typedef struct tagXmlNode {
- int depth; // depth of the current node (1=root)
- char *name; // tag name of the current node
- int numAttr; // number of attributes
- int maxNumAttr; // internal use (num of slots currently allocated to attr)
- XmlAttr **attr; // attribute list
- int numChild; // number of direct child nodes
- int maxNumChild; // internal use (num of slots currently allocated to child)
- struct tagXmlNode **child; // child node list
- char *text;
- XmlNodeType state; // internal use by parser
-} XmlNode;
-
-
-typedef struct tagXmlState {
- XmlNode root; // root is the document (depth = 0);
- // callback for depth=n element on opening/closing
- void (*callback1_open)(XmlNode *,void *);
- void (*callback1_close)(XmlNode *,void *);
- void (*callback2_open)(XmlNode *,void *);
- void (*callback2_close)(XmlNode *,void *);
- void *userdata1_open;
- void *userdata1_close;
- void *userdata2_open;
- void *userdata2_close;
-} XmlState;
-
-void TlenXmlInitState(XmlState *xmlState);
-void TlenXmlDestroyState(XmlState *xmlState);
-BOOL TlenXmlSetCallback(XmlState *xmlState, int depth, XmlElemType type, void (*callback)(XmlNode*, void*), void *userdata);
-int TlenXmlParse(XmlState *xmlState, char *buffer, int datalen);
-char *TlenXmlGetAttrValue(XmlNode *node, char *key);
-XmlNode *TlenXmlGetChild(XmlNode *node, char *tag);
-XmlNode *TlenXmlGetNthChild(XmlNode *node, char *tag, int nth);
-XmlNode *TlenXmlGetChildWithGivenAttrValue(XmlNode *node, char *tag, char *attrKey, char *attrValue);
-void TlenXmlFreeNode(XmlNode *node);
-XmlNode *TlenXmlCopyNode(XmlNode *node);
-
-XmlNode *TlenXmlCreateNode(char *name);
-void TlenXmlAddAttr(XmlNode *n, char *name, char *value);
-XmlNode *TlenXmlAddChild(XmlNode *n, char *name);
-void TlenXmlAddText(XmlNode *n, char *text);
-
-#endif
-
diff --git a/protocols/Tlen/src/version.h b/protocols/Tlen/src/version.h
deleted file mode 100644
index ca7a54d409..0000000000
--- a/protocols/Tlen/src/version.h
+++ /dev/null
@@ -1,17 +0,0 @@
-#define __MAJOR_VERSION 2
-#define __MINOR_VERSION 1
-#define __RELEASE_NUM 0
-#define __BUILD_NUM 0
-
-#include <stdver.h>
-
-#define TLENMUCC_VERSION_STRING "2.1.0.0"
-#define TLEN_VERSION_STRING TLENMUCC_VERSION_STRING
-
-#define __PLUGIN_NAME "Tlen protocol"
-#define __FILENAME "Tlen.dll"
-#define __DESCRIPTION "Tlen protocol support for Miranda NG."
-#define __AUTHOR "Santithorn Bunchua, Adam Strzelecki, Piotr Piastucki"
-#define __AUTHOREMAIL "the_leech@users.berlios.de"
-#define __AUTHORWEB "https://miranda-ng.org/p/Tlen/"
-#define __COPYRIGHT "© 2002-2012 Santithorn Bunchua, Piotr Piastucki"