fix
[libotrd.git] / client.d
blob882d9f4d8dd9f6698386ca6d3510413abf2993a4
1 module client;
3 import std.string;
5 import otr;
8 ////////////////////////////////////////////////////////////////////////////////
9 struct MessageBuf {
10 char*[] data;
11 char*[] sender;
13 @property size_t length () @safe pure nothrow @nogc { return data.length; }
15 void push (const(char)* aSender, const(char)* aData) {
16 data ~= otrl_strdup(aData);
17 sender ~= otrl_strdup(aSender);
20 void popFirst () {
21 otrl_mfree(data[0]);
22 otrl_mfree(sender[0]);
23 foreach (immutable idx; 1..data.length) {
24 data[idx-1] = data[idx];
25 sender[idx-1] = sender[idx];
27 data.length -= 1;
28 sender.length -= 1;
32 ////////////////////////////////////////////////////////////////////////////////
33 class Client {
34 OtrlUserState mOTRState;
35 char* accountz; // asciiz
36 MessageBuf mbuf;
37 // user-defined fields
38 int inOTR; // !0: in OTR mode
39 int verified;
41 this (const(char)* acc) {
42 accountz = otrl_strdup(acc);
43 mOTRState = otrl_userstate_create();
46 ~this () {
47 if (mOTRState !is null) {
48 otrl_userstate_free(mOTRState);
49 mOTRState = null;
51 otrl_mfree(accountz);
52 accountz = null;
55 @property const(char)* account () { return accountz; }
59 ////////////////////////////////////////////////////////////////////////////////
60 private Client[] clientList;
63 void clientListClear () {
64 clientList.length = 0;
67 Client clientFind (const(char)* account) {
68 import core.stdc.string : strcmp;
70 if (account is null) return null;
71 foreach (Client cl; clientList) {
72 if (strcmp(cl.accountz, account) == 0) return cl;
74 return null;
78 void clientRemove (const(char)* account) {
79 import core.stdc.string : strcmp;
81 if (account is null) return;
82 foreach (immutable idx, Client cl; clientList) {
83 if (strcmp(cl.accountz, account) == 0) {
84 foreach (immutable x; idx+1..clientList.length) clientList[x-1] = clientList[x];
85 clientList.length -= 1;
86 return;
92 Client clientAdd (const(char)* account) {
93 if (account is null) return null;
94 auto cl = clientFind(account);
95 if (cl) throw new Error("duplicate account");
96 cl = new Client(account);
97 clientList ~= cl;
98 return cl;