summaryrefslogtreecommitdiff
path: root/plugins/MirLua/src/m_message.cpp
blob: c02adb04787a58101dbd766197241ed325e855ca (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
#include "stdafx.h"

static int message_Paste(lua_State *L)
{
	MCONTACT hContact = luaL_checkinteger(L, 1);
	ptrW text(mir_utf8decodeW(luaL_checkstring(L, 2)));

	MessageWindowData mwd;
	INT_PTR res = Srmm_GetWindowData(hContact, mwd);
	lua_pushinteger(L, res);
	if (res)
		return 1;

	HWND hEdit = GetDlgItem(mwd.hwndWindow, 1002 /*IDC_MESSAGE*/);
	if (!hEdit) hEdit = GetDlgItem(mwd.hwndWindow, 1009 /*IDC_CHATMESSAGE*/);

	SendMessage(hEdit, EM_REPLACESEL, TRUE, (LPARAM)text);

	return 1;
}

static int message_Send(lua_State *L)
{
	MCONTACT hContact = luaL_checkinteger(L, 1);
	const char *message = luaL_checkstring(L, 2);

	INT_PTR res = 1;

	const char *szProto = GetContactProto(hContact);
	if (db_get_b(hContact, szProto, "ChatRoom", 0) == TRUE)
	{
		ptrW wszChatRoom(db_get_wsa(hContact, szProto, "ChatRoomID"));
		ptrW wszMessage(mir_utf8decodeW(message));
		res = Chat_SendUserMessage(szProto, wszChatRoom, wszMessage);
		lua_pushinteger(L, res);
	}
	else if ((res = ProtoChainSend(hContact, PSS_MESSAGE, 0, (LPARAM)message)) != ACKRESULT_FAILED)
	{
		DBEVENTINFO dbei = {};
		dbei.szModule = MODULE;
		dbei.timestamp = time(nullptr);
		dbei.eventType = EVENTTYPE_MESSAGE;
		dbei.cbBlob = (DWORD)mir_strlen(message);
		dbei.pBlob = (PBYTE)mir_strdup(message);
		dbei.flags = DBEF_UTF | DBEF_SENT;
		db_event_add(hContact, &dbei);

		lua_pushinteger(L, res);
		return 1;
	}

	lua_pushinteger(L, res);

	return 1;
}

static luaL_Reg messageApi[] =
{
	{ "Paste", message_Paste },
	{ "Send", message_Send },

	{ nullptr, nullptr }
};

LUAMOD_API int luaopen_m_message(lua_State *L)
{
	luaL_newlib(L, messageApi);

	MT<MessageWindowEventData>(L, "MessageWindowEventData")
		.Field(&MessageWindowEventData::uType, "Type", LUA_TINTEGER)
		.Field(&MessageWindowEventData::hContact, "hContact", LUA_TINTEGER)
		.Field(&MessageWindowEventData::uFlags, "Flags", LUA_TINTEGER);

	return 1;
}