summaryrefslogtreecommitdiff
path: root/plugins/Ping/src/ping.cpp
blob: 01d99dedd4b71f9b5d846d17a8cac2979487cb47 (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
#include "stdafx.h"

CLIST_INTERFACE *pcli;
CMPlugin g_plugin;
int &hLangpack(g_plugin.m_hLang);

HNETLIBUSER hNetlibUser = nullptr;
HANDLE hFillListEvent = nullptr;

bool use_raw_ping = true;

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

PLUGININFOEX pluginInfo =
{
	sizeof(PLUGININFOEX),
	__PLUGIN_NAME,
	PLUGIN_MAKE_VERSION(__MAJOR_VERSION, __MINOR_VERSION, __RELEASE_NUM, __BUILD_NUM),
	__DESCRIPTION,
	__AUTHOR,
	__COPYRIGHT,
	__AUTHORWEB,
	UNICODE_AWARE,
	// {760EA901-C0C2-446C-8029-94C3BC47C45E}
	{ 0x760ea901, 0xc0c2, 0x446c, { 0x80, 0x29, 0x94, 0xc3, 0xbc, 0x47, 0xc4, 0x5e } }
};

extern "C" __declspec(dllexport) PLUGININFOEX* MirandaPluginInfoEx(DWORD)
{
	return &pluginInfo;
}

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

static void CreatePluginServices()
{
	// general
	CreateServiceFunction(PLUG "/Ping", PluginPing);
	CreateServiceFunction(PLUG "/DblClick", DblClick);

	// list
	CreateServiceFunction(PLUG "/ClearPingList", ClearPingList);
	CreateServiceFunction(PLUG "/GetPingList", GetPingList);
	CreateServiceFunction(PLUG "/SetPingList", SetPingList);
	CreateServiceFunction(PLUG "/SetAndSavePingList", SetAndSavePingList);
	CreateServiceFunction(PLUG "/LoadPingList", LoadPingList);
	CreateServiceFunction(PLUG "/SavePingList", SavePingList);

	reload_event_handle = CreateHookableEvent(PLUG "/ListReload");

	//log
	CreateServiceFunction(PLUG "/Log", Log);
	CreateServiceFunction(PLUG "/ViewLogData", ViewLogData);
	CreateServiceFunction(PLUG "/GetLogFilename", GetLogFilename);
	CreateServiceFunction(PLUG "/SetLogFilename", SetLogFilename);

	// menu
	CreateServiceFunction(PLUG "/DisableAll", PingDisableAll);
	CreateServiceFunction(PLUG "/EnableAll", PingEnableAll);
	CreateServiceFunction(PLUG "/ToggleEnabled", ToggleEnabled);
	CreateServiceFunction(PLUG "/ShowGraph", ShowGraph);
	CreateServiceFunction(PLUG "/Edit", EditContact);

}

static int OnShutdown(WPARAM, LPARAM)
{
	graphs_cleanup();

	UnhookEvent(hFillListEvent);

	DeinitList();

	if (use_raw_ping)
		cleanup_raw_ping();
	else
		ICMP::cleanup();

	return 0;
}

static int OnModulesLoaded(WPARAM, LPARAM)
{
	NETLIBUSER nl_user = {};
	nl_user.szSettingsModule = PLUG;
	nl_user.flags = NUF_OUTGOING | NUF_HTTPCONNS | NUF_UNICODE;
	nl_user.szDescriptiveName.w = TranslateT("Ping Plugin");
	hNetlibUser = Netlib_RegisterUser(&nl_user);

	InitUtils();
	InitMenus();

	hFillListEvent = HookEvent(PLUG "/ListReload", FillList);

	if (!db_get_b(0, PLUG, "PingPlugImport", 0)) {
		if (db_get_dw(0, "PingPlug", "NumEntries", 0)) {
			import_ping_addresses();
			db_set_b(0, PLUG, "PingPlugImport", 1);
		}
	}

	InitList();

	CallService(PLUG "/LoadPingList", 0, 0);

	graphs_init();

	if (options.logging)
		CallService(PLUG "/Log", (WPARAM)L"start", 0);

	return 0;
}

static IconItem iconList[] =
{
	{ LPGEN("Responding"), "ping_responding", IDI_ICON_RESPONDING },
	{ LPGEN("Not Responding"), "ping_not_responding", IDI_ICON_NOTRESPONDING },
	{ LPGEN("Testing"), "ping_testing", IDI_ICON_TESTING },
	{ LPGEN("Disabled"), "ping_disabled", IDI_ICON_DISABLED },
};

extern "C" __declspec(dllexport) int Load(void)
{
	mir_getLP(&pluginInfo);
	pcli = Clist_GetInterface();

	use_raw_ping = false;
	db_set_b(0, PLUG, "UsingRawSockets", (BYTE)use_raw_ping);

	DuplicateHandle(GetCurrentProcess(), GetCurrentThread(), GetCurrentProcess(), &mainThread, THREAD_SET_CONTEXT, FALSE, 0);
	hWakeEvent = CreateEvent(nullptr, FALSE, FALSE, L"Local\\ThreadWaitEvent");

	// create services before loading options - so we can have the 'getlogfilename' service!
	CreatePluginServices();

	LoadOptions();

	g_plugin.addSound("PingTimeout", LPGENW("Ping"), LPGENW("Timeout"));
	g_plugin.addSound("PingReply", LPGENW("Ping"), LPGENW("Reply"));

	HookEvent(ME_SYSTEM_MODULESLOADED, OnModulesLoaded);
	HookEvent(ME_OPT_INITIALISE, PingOptInit);
	HookEvent(ME_SYSTEM_PRESHUTDOWN, OnShutdown);
	HookEvent(ME_SKIN2_ICONSCHANGED, ReloadIcons);

	g_plugin.registerIcon(LPGEN("Ping"), iconList);
	return 0;
}

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

extern "C" __declspec(dllexport) int Unload(void)
{
	SavePingList(0, 0);

	CloseHandle(mainThread);

	if (options.logging)
		CallService(PLUG "/Log", (WPARAM)L"stop", 0);

	return 0;
}