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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
|
#include "messaging.h"
#include <map>
#include <queue>
typedef std::queue<std::string> MessageQueue;
typedef std::map<HANDLE, MessageQueue> ContactMessageQueue;
ContactMessageQueue contact_message_queue;
CRITICAL_SECTION q_cs;
mwServiceIm *service_im = 0;
void mwIm_conversation_opened(mwConversation *conv) {
mwIdBlock *idb = mwConversation_getTarget(conv);
HANDLE hContact = FindContactByUserId(idb->user);
if(!hContact) {
mwSametimeList *user_list = mwSametimeList_new();
mwSametimeGroup *stgroup = mwSametimeGroup_new(user_list, mwSametimeGroup_NORMAL, Translate("None"));
mwSametimeUser *stuser = mwSametimeUser_new(stgroup, mwSametimeUser_NORMAL, idb);
AddContact(stuser, (options.add_contacts ? false : true));
GetMoreDetails(idb->user);
}
ContactMessageQueue::iterator i;
EnterCriticalSection(&q_cs);
if((i = contact_message_queue.find(hContact)) != contact_message_queue.end()) {
while(i->second.size()) {
mwConversation_send(conv, mwImSend_PLAIN, (gconstpointer)i->second.front().c_str());
i->second.pop();
}
contact_message_queue.erase(i);
}
LeaveCriticalSection(&q_cs);
// gives linker error 'unresolved external symbol' :( So instead we will either add ciphers to the session or not (see session.cpp)
//mwConversation_setEncrypted(conv, options.encrypt_session);
}
/** A conversation has been closed */
void mwIm_conversation_closed(mwConversation *conv, guint32 err) {
if(err & ERR_FAILURE && err != CONNECTION_RESET) {
char *msg = mwError(err);
TCHAR *ts = u2t(msg);
//MessageBox(0, ts, TranslateT("Sametime Error"), MB_OK | MB_ICONWARNING);
ShowError(TranslateTS(ts));
g_free(msg);
free(ts);
}
mwIdBlock *idb = mwConversation_getTarget(conv);
HANDLE hContact = FindContactByUserId(idb->user);
if(hContact) {
ContactMessageQueue::iterator i;
EnterCriticalSection(&q_cs);
if((i = contact_message_queue.find(hContact)) != contact_message_queue.end()) {
contact_message_queue.erase(i);
}
LeaveCriticalSection(&q_cs);
}
}
/** A message has been received on a conversation */
void mwIm_conversation_recv(mwConversation *conv, mwImSendType type, gconstpointer msg) {
mwIdBlock *idb = mwConversation_getTarget(conv);
HANDLE hContact = FindContactByUserId(idb->user);
if(type == mwImSend_TYPING) {
CallService(MS_PROTO_CONTACTISTYPING, (WPARAM)hContact, (LPARAM)(GPOINTER_TO_UINT(msg) == 0 ? 0 : 2));
return;
}
if(type != mwImSend_PLAIN) return;
wchar_t temp[MAX_MESSAGE_SIZE];
char text[MAX_MESSAGE_SIZE];
MultiByteToWideChar(CP_UTF8, 0, (const char *)msg, -1, temp, MAX_MESSAGE_SIZE);
WideCharToMultiByte(CallService(MS_LANGPACK_GETCODEPAGE, 0, 0), WC_COMPOSITECHECK, temp, -1, text, MAX_MESSAGE_SIZE * sizeof(char), 0, 0);
CCSDATA ccs = {0};
PROTORECVEVENT pre = {0};
ccs.hContact = hContact;
ccs.szProtoService = PSR_MESSAGE;
ccs.wParam = 0;
ccs.lParam = (LPARAM)⪯
pre.timestamp = (DWORD)time(0);
int tMsgBodyLen = strlen(text);
int tRealBodyLen = wcslen(temp);
int tMsgBufLen = (tMsgBodyLen+1) * sizeof(char) + (tRealBodyLen+1)*sizeof( wchar_t );
char* tMsgBuf = ( char* )malloc( tMsgBufLen );
char* p = tMsgBuf;
strcpy( p, text );
p += (tMsgBodyLen+1);
if ( tRealBodyLen != 0 ) {
wcscpy((wchar_t *)p, temp);
pre.flags = PREF_UNICODE;
}
pre.szMessage = tMsgBuf;
DBDeleteContactSetting(hContact, "CList", "Hidden");
CallService(MS_PROTO_CHAINRECV, 0, (LPARAM)&ccs);
free(tMsgBuf);
}
mwImHandler mwIm_handler = {
mwIm_conversation_opened,
mwIm_conversation_closed,
mwIm_conversation_recv
};
HANDLE SendMessageToUser(HANDLE hContact, char *msg) {
mwIdBlock idb;
mwAwareIdBlock id_block;
wchar_t temp[MAX_MESSAGE_SIZE];
char text[MAX_MESSAGE_SIZE];
MultiByteToWideChar(CallService(MS_LANGPACK_GETCODEPAGE, 0, 0), MB_PRECOMPOSED, msg, -1, temp, MAX_MESSAGE_SIZE);
WideCharToMultiByte(CP_UTF8, 0, temp, -1, text, MAX_MESSAGE_SIZE * sizeof(char), 0, 0);
if(GetAwareIdFromContact(hContact, &id_block)) {
idb.user = id_block.user;
idb.community = id_block.community;
mwConversation *conv = mwServiceIm_getConversation(service_im, &idb);
if(conv) {
if(!mwConversation_isOpen(conv)) {
EnterCriticalSection(&q_cs);
contact_message_queue[hContact].push(text);
LeaveCriticalSection(&q_cs);
mwConversation_open(conv);
} else
mwConversation_send(conv, mwImSend_PLAIN, (gconstpointer)msg);
free(id_block.user);
return (HANDLE)conv;
}
free(id_block.user);
}
return 0;
}
HANDLE SendMessageToUserW(HANDLE hContact, wchar_t *msg) {
mwIdBlock idb;
mwAwareIdBlock id_block;
char text[MAX_MESSAGE_SIZE];
WideCharToMultiByte(CP_UTF8, 0, msg, -1, text, MAX_MESSAGE_SIZE * sizeof(char), 0, 0);
if(GetAwareIdFromContact(hContact, &id_block)) {
idb.user = id_block.user;
idb.community = id_block.community;
mwConversation *conv = mwServiceIm_getConversation(service_im, &idb);
if(conv) {
if(!mwConversation_isOpen(conv)) {
EnterCriticalSection(&q_cs);
contact_message_queue[hContact].push(text);
LeaveCriticalSection(&q_cs);
mwConversation_open(conv);
} else
mwConversation_send(conv, mwImSend_PLAIN, (gconstpointer)text);
free(id_block.user);
return (HANDLE)conv;
}
free(id_block.user);
}
return 0;
}
void SendTyping(HANDLE hContact, bool typing) {
mwIdBlock idb;
mwAwareIdBlock id_block;
if(GetAwareIdFromContact(hContact, &id_block)) {
idb.user = id_block.user;
idb.community = id_block.community;
mwConversation *conv = mwServiceIm_getConversation(service_im, &idb);
if(conv) {
if(mwConversation_isOpen(conv))
mwConversation_send(conv, mwImSend_TYPING, (gconstpointer)GUINT_TO_POINTER(typing ? 1 : 0));
}
free(id_block.user);
}
}
void CloseIm(HANDLE hContact) {
mwIdBlock idb;
mwAwareIdBlock id_block;
if(GetAwareIdFromContact(hContact, &id_block)) {
idb.user = id_block.user;
idb.community = id_block.community;
mwConversation *conv = mwServiceIm_getConversation(service_im, &idb);
if(conv) {
if(mwConversation_isOpen(conv))
mwConversation_close(conv, 0);
}
free(id_block.user);
}
}
void InitMessaging(mwSession *session) {
InitializeCriticalSection(&q_cs);
mwSession_addService(session, (mwService *)(service_im = mwServiceIm_new(session, &mwIm_handler)));
mwServiceIm_setClientType(service_im, mwImClient_PLAIN);
}
void DeinitMessaging(mwSession *session) {
mwSession_removeService(session, mwService_IM);
mwService_free((mwService *)service_im);
service_im = 0;
DeleteCriticalSection(&q_cs);
}
|