-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtoxreceiverstream.cpp
181 lines (168 loc) · 3.11 KB
/
toxreceiverstream.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#include "toxreceiverstream.h"
ToxReceiverStream::ToxReceiverStream
(
std::istream &i_stream,
std::ostream &o_stream,
std::ostream &e_stream,
TOX_MESSAGE_TYPE default_message_type
)
: istream(i_stream), ostream(o_stream), estream(e_stream), defaultMessageType(default_message_type)
{
estream << "Connecting.. ";
}
ToxReceiverStream::~ToxReceiverStream()
{
}
void ToxReceiverStream::onId
(
// Tox *tox,
ToxClient *toxclient,
const std::string &valuehex
)
{
estream << valuehex << std::endl;
}
void ToxReceiverStream::onConnectionStatus
(
// Tox *tox,
ToxClient *toxclient,
TOX_CONNECTION value
)
{
std::string s;
switch(value)
{
case TOX_CONNECTION_TCP:
s = "TCP connection established";
break;
case TOX_CONNECTION_UDP:
s = "UDP connection established";
break;
default:
s = "offline";
}
estream << s << std::endl;
}
void ToxReceiverStream::onMessage
(
ToxClient *toxclient,
TOX_MESSAGE_TYPE message_type,
uint32_t friend_number,
const std::string &value,
void *user_data
)
{
ostream << toxclient->getFriendName(friend_number) << std::endl << value << std::endl;
}
bool ToxReceiverStream::onFriendRequest
(
ToxClient *toxclient,
const uint8_t *key,
const std::string &name,
void *user_data
)
{
estream << "Friend request " << name << std::endl;
return true;
}
bool ToxReceiverStream::nextMessageTo
(
uint32_t *friend_number,
TOX_MESSAGE_TYPE *message_type,
std::string *text
)
{
bool r = messages.size();
if (r)
{
ToxMessage m = messages.front();
messages.pop();
*friend_number = m.friend_number;
*message_type = m.message_type;
*text = m.message;
}
return r;
}
void ToxReceiverStream::putMessage
(
ToxClient *toxclient,
const TOX_MESSAGE_TYPE message_type,
const uint32_t friend_number,
const std::string &value
)
{
}
void ToxReceiverStream::execCommand
(
std::ostream &strm,
ToxClient *toxclient,
const std::string &line
)
{
if (!toxclient)
return;
if (line == "/status")
{
strm << "Name\tStatus";
strm << std::endl;
int sz = toxclient->getFriendSize();
for (int i = 0; i < sz; i++)
{
std::string n = toxclient->getFriendName(i);
if (n.empty())
strm << i + 1;
else
strm << n;
strm << "\t";
TOX_USER_STATUS status = toxclient->getFriendStatus(i);
std::string sstatus;
switch(status)
{
case TOX_USER_STATUS_NONE:
sstatus = "online";
break;
case TOX_USER_STATUS_BUSY:
sstatus = "busy";
break;
case TOX_USER_STATUS_AWAY:
sstatus = "away";
break;
}
strm << (toxclient->getFriendIsConnected(i)? "connected" : "disconnected") << "\t"
<< sstatus << "\t"
<< toxclient->getFriendStatusString(i) << "\t";
strm << std::endl;
}
strm << std::endl;
}
}
bool ToxReceiverStream::isCommand
(
const std::string &line
)
{
bool r = false;
if (line == "/status")
r = true;
return r;
}
void ToxReceiverStream::readLine
(
ToxClient *toxclient
)
{
std::string line;
std::getline(istream, line);
if (isCommand(line))
{
execCommand(std::cout, toxclient, line);
}
else
{
ToxMessage m;
m.friend_number = 0;
m.message_type = defaultMessageType;
m.message = line;
messages.push(m);
}
}