summaryrefslogtreecommitdiff
path: root/protocols/Sametime/src/sametime.cpp
blob: ab7dce7818848fa042b6fb782519a6a55366c788 (plain)
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
#include "StdAfx.h"
#include "sametime.h"
#include "version.h"

// plugin stuff


PLUGININFOEX pluginInfoEx =
{
	sizeof(PLUGININFOEX),
	__PLUGIN_NAME,
	PLUGIN_MAKE_VERSION(__MAJOR_VERSION, __MINOR_VERSION, __RELEASE_NUM, __BUILD_NUM),
	__DESCRIPTION,
	__AUTHOR,
	__COPYRIGHT,
	__AUTHORWEB,
	UNICODE_AWARE,
	// {F1B0BA1B-0C91-4313-85EB-225069D44D01}
	{ 0xf1b0ba1b, 0xc91, 0x4313, { 0x85, 0xeb, 0x22, 0x50, 0x69, 0xd4, 0x4d, 0x1 } } 
};

CMPlugin::CMPlugin() :
	ACCPROTOPLUGIN<CSametimeProto>("Sametime", pluginInfoEx)
{
	SetUniqueId("stid");
}

/////////////////////////////////////////////////////////////////////////////////////////

CMPlugin g_plugin;

/////////////////////////////////////////////////////////////////////////////////////////

extern "C" __declspec(dllexport) const MUUID MirandaInterfaces[] = { MIID_PROTOCOL, MIID_LAST };

/////////////////////////////////////////////////////////////////////////////////////////
// protocol related services

/** Copy the name of the protocol into lParam
* @param wParam :	max size of the name
* @param lParam :	reference to a char *, which will hold the name
*/
INT_PTR CSametimeProto::GetName(WPARAM wParam, LPARAM lParam)
{
	strncpy((char*)lParam, m_szModuleName, wParam);
	return 0;
}


/** Loads the icon corresponding to the status
* Called by the CList when the status changes.
* @param wParam :	icon type
* @return			an \c HICON in which the icon has been loaded.
*/
INT_PTR CSametimeProto::SametimeLoadIcon(WPARAM wParam, LPARAM lParam)
{
	
	UINT id;
	switch (wParam & 0xFFFF) {
	case PLI_PROTOCOL:
		id = IDI_ICON_PROTO;
		break;
	default:
		return NULL;
	}

	return (INT_PTR) LoadImage(g_plugin.getInst(), MAKEINTRESOURCE(id), IMAGE_ICON,
						GetSystemMetrics(wParam & PLIF_SMALL ? SM_CXSMICON : SM_CXICON),
						GetSystemMetrics(wParam & PLIF_SMALL ? SM_CYSMICON : SM_CYICON), 0);
}


//icolib stuff
static IconItem iconList[] =
{
	{ LPGEN("Protocol icon"), "protoicon", IDI_ICON_PROTO, 0 },
	{ LPGEN("Start conference"), "leaveconference", IDI_ICON_INVITE, 0 },
	{ LPGEN("Leave conference"), "startconference", IDI_ICON_LEAVE, 0 },
	{ LPGEN("Announce"), "announce", IDI_ICON_ANNOUNCE, 0 },
	{ LPGEN("Notification"), "notify", IDI_ICON_NOTIFY, 0 },
	{ LPGEN("Error"), "error", IDI_ICON_ERROR, 0 }
};

void SametimeInitIcons(void)
{
	g_plugin.registerIcon("Protocols/Sametime", iconList, "SAMETIME");
}

HANDLE GetIconHandle(int iconId)
{
	for (int i = 0; i < _countof(iconList); i++)
		if (iconList[i].defIconID == iconId)
			return iconList[i].hIcolib;
	return nullptr;
}

HICON LoadIconEx(const char* name, bool big)
{
	char szSettingName[100];
	mir_snprintf(szSettingName, "%s_%s", "SAMETIME", name);
	return IcoLib_GetIcon(szSettingName, big);
}

void ReleaseIconEx(const char* name, bool big)
{
	char szSettingName[100];
	mir_snprintf(szSettingName, "%s_%s", "SAMETIME", name);
	IcoLib_Release(szSettingName, big);
}

// Copied from MSN plugin - sent acks need to be from different thread
void __cdecl sttFakeAckInfoSuccessThread(TFakeAckParams* tParam)
{
	CSametimeProto* proto = tParam->proto;
	proto->debugLogW(L"sttFakeAckInfoSuccessThread() start");

	Sleep(100);
	proto->ProtoBroadcastAck(tParam->hContact, ACKTYPE_GETINFO, ACKRESULT_SUCCESS, (HANDLE)1);

	proto->debugLogW(L"sttFakeAckInfoSuccessThread() end");
	mir_free(tParam);
}

void __cdecl sttFakeAckMessageSuccessThread(TFakeAckParams* tParam)
{
	CSametimeProto* proto = tParam->proto;
	proto->debugLogW(L"sttFakeAckMessageSuccessThread() start");

	Sleep(100);
	proto->ProtoBroadcastAck(tParam->hContact, ACKTYPE_MESSAGE, ACKRESULT_SUCCESS, (HANDLE)tParam->lParam, 0);

	proto->debugLogW(L"sttFakeAckMessageSuccessThread() end");
	mir_free(tParam);
}

void __cdecl sttFakeAckMessageFailedThread(TFakeAckParams* tParam)
{
	CSametimeProto* proto = tParam->proto;
	proto->debugLogW(L"sttFakeAckMessageFailedThread() start");

	Sleep(100);
	proto->ProtoBroadcastAck(tParam->hContact, ACKTYPE_MESSAGE, ACKRESULT_FAILED, nullptr, tParam->lParam); ///TODO tParam->lParam: add error message

	proto->debugLogW(L"sttFakeAckMessageFailedThread() end");
	mir_free(tParam);
}

void __cdecl sttRecvAwayThread(TFakeAckParams* tParam)
{
	CSametimeProto* proto = tParam->proto;
	proto->debugLogW(L"sttRecvAwayThread() start");

	Sleep(100);
	proto->UserRecvAwayMessage(tParam->hContact);

	proto->debugLogW(L"sttRecvAwayThread() end");
	free(tParam);
}

int CSametimeProto::OnWindowEvent(WPARAM, LPARAM lParam)
{
	MessageWindowEventData *mwed = (MessageWindowEventData*)lParam;

	if ((mwed == nullptr) || db_get_b(mwed->hContact, m_szModuleName, "ChatRoom", 0))
		return 0;

	if ((mwed->uType == MSG_WINDOW_EVT_CLOSING) || (mwed->uType == MSG_WINDOW_EVT_CLOSE))
		CloseIm(mwed->hContact);

	return 0;
}

int CSametimeProto::OnIdleChanged(WPARAM, LPARAM lParam)
{
	if (!(lParam & IDF_PRIVACY)) {
		is_idle = lParam & IDF_ISIDLE ? true : false;
		SetIdle(is_idle);
	}

	return 0;
}

void CSametimeProto::OnModulesLoaded()
{
	// register with chat module
	GCREGISTER gcr = {};
	gcr.pszModule = m_szModuleName;
	gcr.ptszDispName = m_tszUserName;
	gcr.iMaxText = MAX_MESSAGE_SIZE;
	Chat_Register(&gcr);
}

void CSametimeProto::OnShutdown()
{
	if (m_iStatus != ID_STATUS_OFFLINE)
		LogOut();
}

int CSametimeProto::OnSametimeContactDeleted(WPARAM hContact, LPARAM)
{
	ContactDeleted(hContact);
	ChatDeleted(hContact);
	return 0;
}

void CSametimeProto::SetAllOffline()
{
	debugLogW(L"SetAllOffline() start");

	for (auto &hContact : AccContacts()) {
		if (db_get_b(hContact, m_szModuleName, "ChatRoom", 0)) {
			db_delete_contact(hContact);
			continue;
		}

		db_set_w(hContact, m_szModuleName, "Status", ID_STATUS_OFFLINE);
		db_set_dw(hContact, m_szModuleName, "IdleTS", 0);
	}
}

void CSametimeProto::BroadcastNewStatus(int iNewStatus)
{
	if (m_iStatus == iNewStatus)
		return;

	debugLogW(L"BroadcastNewStatus() m_iStatus=[%d], iNewStatus=[%d]", m_iStatus, iNewStatus);

	previous_status = m_iStatus;
	m_iStatus = iNewStatus;
	ProtoBroadcastAck(NULL, ACKTYPE_STATUS, ACKRESULT_SUCCESS, (HANDLE)previous_status, m_iStatus);
}