summaryrefslogtreecommitdiff
path: root/plugins/MirOTR/MirOTR/src/svcs_srmm.cpp
blob: 8ad883bfd676d0472d4706c7720b8d7b7bbbff3e (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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#include "stdafx.h"
#include "mirotrmenu.h"
HANDLE hEventIconPressed;
HICON hIconNotSecure, hIconFinished, hIconPrivate, hIconUnverified;
BBButton OTRButton;

/* Plugin init and deinit */
// int WindowEvent(WPARAM wParam, LPARAM lParam) {
// 	MessageWindowEventData *mwd = (MessageWindowEventData *)lParam;
// 	
// 	if(mwd->uType == MSG_WINDOW_EVT_CLOSE && options.end_window_close) {
// 		FinishSession(mwd->hContact);
// 		return 0;
// 	}
// 
// 	if(mwd->uType != MSG_WINDOW_EVT_OPEN) return 0;
// 	if (!options.bHaveSRMMIcons) return 0;
// 	
// 	HANDLE hContact = mwd->hContact, hTemp;
// 	if(options.bHaveMetaContacts && (hTemp = (HANDLE)CallService(MS_MC_GETMOSTONLINECONTACT, (WPARAM)hContact, 0)) != 0)
// 		hContact = hTemp;
// 	
// 	if (!CallService(MS_PROTO_ISPROTOONCONTACT, (WPARAM)hContact, (LPARAM)MODULENAME))
// 		return 0;
// 	
// 	lib_cs_lock();
// 	ConnContext *context = otrl_context_find_miranda(otr_user_state, hContact);
// 	lib_cs_unlock();
// 	
// 	SetEncryptionStatus(hContact, otr_context_get_trust(context));
// 	
// 	return 0;
// }

int SVC_IconPressed(WPARAM wParam, LPARAM lParam) {
	HANDLE hContact = (HANDLE)wParam;
	StatusIconClickData *sicd = (StatusIconClickData *)lParam;
	if(sicd->cbSize < (int)sizeof(StatusIconClickData))
		return 0;

	if(strcmp(sicd->szModule, MODULENAME) == 0) {
		char *proto = GetContactProto(hContact);
		if(proto && DBGetContactSettingByte(hContact, proto, "ChatRoom", 0))
			return 0;
		ShowOTRMenu(hContact, sicd->clickLocation);
		/*
		HMENU hMenu = LoadMenu(hInst, MAKEINTRESOURCE(IDR_CONTEXT));
		CallService(MS_LANGPACK_TRANSLATEMENU, (WPARAM)hMenu, 0);
		TrustLevel level = (TrustLevel) db_get_dw(hContact, MODULENAME, "TrustLevel", TRUST_NOT_PRIVATE);
		MENUITEMINFO minfo = {0};
		minfo.cbSize = sizeof(MENUITEMINFO);
		minfo.fMask = MIIM_STRING|MIIM_STATE|MIIM_BITMAP;
		minfo.fState = MF_DISABLED;

		HWND wnd = GetFocus();
		HDC dc = GetDC(wnd), hdc = CreateCompatibleDC(dc);
		HBITMAP bmpIconPrivate = CreateCompatibleBitmap(dc, 16, 16);
		HGDIOBJ old = SelectObject(hdc, bmpIconPrivate);
		DrawIconEx(hdc, 0, 0, hIconPrivate,16, 16, 0, GetSysColorBrush(COLOR_MENU), DI_NORMAL);

		HBITMAP bmpIconUnverified = CreateCompatibleBitmap(dc, 16, 16);
		SelectObject(hdc, bmpIconUnverified);
		DrawIconEx(hdc, 0, 0, hIconUnverified,16, 16, 0, GetSysColorBrush(COLOR_MENU), DI_NORMAL);

		HBITMAP bmpIconFinished = CreateCompatibleBitmap(dc, 16, 16);
		SelectObject(hdc, bmpIconFinished);
		DrawIconEx(hdc, 0, 0, hIconFinished,16, 16, 0, GetSysColorBrush(COLOR_MENU), DI_NORMAL);

		HBITMAP bmpIconNotSecure = CreateCompatibleBitmap(dc, 16, 16);
		SelectObject(hdc, bmpIconNotSecure);
		DrawIconEx(hdc, 0, 0, hIconNotSecure,16, 16, 0, GetSysColorBrush(COLOR_MENU), DI_NORMAL);

		switch (level) {
			case TRUST_PRIVATE:
				minfo.hbmpItem = bmpIconPrivate;
				minfo.dwTypeData = TranslateT(LANG_STATUS_PRIVATE);
				break;
			case TRUST_UNVERIFIED:
				minfo.hbmpItem = bmpIconUnverified;
				minfo.dwTypeData = TranslateT(LANG_STATUS_UNVERIFIED);
				break;
			case TRUST_FINISHED:
				minfo.hbmpItem = bmpIconFinished;
				minfo.dwTypeData = TranslateT(LANG_STATUS_FINISHED);
				break;
			default:
				minfo.hbmpItem = bmpIconNotSecure;
				minfo.dwTypeData = TranslateT(LANG_STATUS_DISABLED);
		}

		SelectObject(hdc, old);
		DeleteDC(hdc);
		ReleaseDC(wnd, dc);

		SetMenuItemInfo(hMenu, IDM_OTR_STATUS, FALSE, &minfo);

			minfo.fMask = MIIM_STATE|MIIM_BITMAP;
			minfo.fState = (level==TRUST_NOT_PRIVATE)?MF_ENABLED:MF_GRAYED;
			minfo.hbmpItem = bmpIconUnverified;
			SetMenuItemInfo(hMenu, IDM_OTR_START, FALSE, &minfo);

			minfo.fState = (level==TRUST_NOT_PRIVATE)?MF_GRAYED:MF_ENABLED;
			minfo.hbmpItem = bmpIconNotSecure;
			SetMenuItemInfo(hMenu, IDM_OTR_STOP, FALSE, &minfo);

			minfo.fState = (level==TRUST_NOT_PRIVATE)?MF_GRAYED:MF_ENABLED;
			minfo.hbmpItem = bmpIconFinished;
			SetMenuItemInfo(hMenu, IDM_OTR_REFRESH, FALSE, &minfo);

			minfo.fState = (level==TRUST_NOT_PRIVATE||level==TRUST_FINISHED)?MF_GRAYED:MF_ENABLED;
			minfo.hbmpItem = bmpIconPrivate;
			SetMenuItemInfo(hMenu, IDM_OTR_VERIFY, FALSE, &minfo);

		switch ( (DWORD) TrackPopupMenu(GetSubMenu(hMenu, 0), TPM_NONOTIFY|TPM_RETURNCMD, sicd->clickLocation.x, sicd->clickLocation.y, 0, GetFocus(), 0) )
		{
			case IDM_OTR_START:
				SVC_StartOTR(wParam, 0);
				break;
			case IDM_OTR_REFRESH:
				SVC_RefreshOTR(wParam, 0);
				break;
			case IDM_OTR_STOP:
				SVC_StopOTR(wParam, 0);
				break;
			case IDM_OTR_VERIFY:
				ConnContext *context = otrl_context_find_miranda(otr_user_state, (HANDLE)wParam);
				if (context) VerifyContextDialog(context);
				break;
		}
		DeleteObject(bmpIconPrivate);
		DeleteObject(bmpIconFinished);
		DeleteObject(bmpIconNotSecure);
		DeleteObject(bmpIconUnverified);
		*/
	}
	
	return 0;
}

// set SRMM icon status, if applicable
void SetEncryptionStatus(HANDLE hContact, TrustLevel level) {
	//char dbg_msg[2048];
	//dbg_msg[0] = 0;

	//strcat(dbg_msg, "Set encyption status: ");
	//strcat(dbg_msg, (encrypted ? "true" : "false"));

	char *proto = GetContactProto(hContact);
	bool chat_room = (proto && DBGetContactSettingByte(hContact, proto, "ChatRoom", 0));

	// if (!chat_room) DBWriteContactSettingByte(hContact, MODULENAME, "Encrypted", (encrypted ? 1 : 0));

	if(options.bHaveSRMMIcons || options.bHaveButtonsBar) {
		//strcat(dbg_msg, "\nchanging icon");
		BBButton button = OTRButton;
		StatusIconData sid = {0}, sid2={0};
		sid.cbSize = sizeof(sid);
		sid.szModule = MODULENAME;
		sid.dwId = 0;
		sid.flags = MBF_HIDDEN;

		sid2.cbSize = sid.cbSize;
		sid2.szModule = MODULENAME;
		sid2.dwId = 1;
		sid2.flags = MBF_HIDDEN;
		if (!chat_room) {
			switch (level) {
				case TRUST_FINISHED:
					sid.flags = 0;
					button.ptszTooltip = TranslateT(LANG_STATUS_FINISHED);
					button.hIcon = GetIconHandle(ICON_FINISHED);
					break;
				case TRUST_UNVERIFIED:
					sid2.flags = MBF_DISABLED;
					button.ptszTooltip = TranslateT(LANG_STATUS_UNVERIFIED);
					button.hIcon = GetIconHandle(ICON_UNVERIFIED);
					break;
				case TRUST_PRIVATE:
					sid2.flags = 0;
					button.ptszTooltip = TranslateT(LANG_STATUS_PRIVATE);
					button.hIcon = GetIconHandle(ICON_PRIVATE);
					break;
				default:
					sid.flags = MBF_DISABLED;
					button.ptszTooltip = TranslateT(LANG_STATUS_DISABLED);
					button.hIcon = GetIconHandle(ICON_NOT_PRIVATE);
					break;
			}
			button.bbbFlags = 0;
		} else {
			button.bbbFlags = BBSF_HIDDEN;
		}
		if (options.bHaveSRMMIcons) {
			CallService(MS_MSG_MODIFYICON, (WPARAM)hContact, (LPARAM)&sid);
			CallService(MS_MSG_MODIFYICON, (WPARAM)hContact, (LPARAM)&sid2);
		}
		if (options.bHaveButtonsBar) CallService(MS_BB_SETBUTTONSTATE, (WPARAM)hContact, (LPARAM)&button);
		db_set_dw(hContact, MODULENAME, "TrustLevel", level);

		if (!chat_room && options.bHaveMetaContacts) {
			HANDLE hMeta = (HANDLE)CallService(MS_MC_GETMETACONTACT, (WPARAM)hContact, 0);
			HANDLE hMostOnline = (HANDLE)CallService(MS_MC_GETMOSTONLINECONTACT, (WPARAM)hMeta, 0);
			if(hMeta && hContact == hMostOnline) {
				//strcat(dbg_msg, "\nrecursing for meta");
				SetEncryptionStatus(hMeta, level);
			}
			else if(hMeta) {
			/* in case the new most online contact has changed
			   (e.g. when the otr subcontact goes offline) */
			ConnContext *context = otrl_context_find_miranda(otr_user_state, hMostOnline);
			TrustLevel encrypted = otr_context_get_trust(context);
			SetEncryptionStatus(hMeta, encrypted);
			}
		}
	}
	//PUShowMessage(dbg_msg, SM_NOTIFY);
}

int SVC_ButtonsBarLoaded(WPARAM, LPARAM) {
	CallService(MS_BB_ADDBUTTON, 0, (LPARAM)&OTRButton);
	return 0;
}
int SVC_ButtonsBarPressed(WPARAM w, LPARAM l) {
	CustomButtonClickData* cbcd = (CustomButtonClickData *)l;
	if (cbcd->cbSize == (int)sizeof(CustomButtonClickData) && cbcd->dwButtonId == 0 && strcmp(cbcd->pszModule, MODULENAME)==0) {
		HANDLE hContact = (HANDLE)w;
	
		char *proto = GetContactProto(hContact);
		if(proto && DBGetContactSettingByte(hContact, proto, "ChatRoom", 0))
			return 0;
		ShowOTRMenu(hContact, cbcd->pt);

	}
	return 0;
}

void InitSRMM() {
	// add icon to srmm status icons
	InitMirOTRMenu();
	if(options.bHaveSRMMIcons) {
		hIconNotSecure = LoadIcon(ICON_NOT_PRIVATE, 0);
		hIconFinished = LoadIcon(ICON_FINISHED, 0);
		hIconPrivate = LoadIcon(ICON_PRIVATE, 0);
		hIconUnverified = LoadIcon(ICON_UNVERIFIED, 0);

		StatusIconData sid = {0};
		sid.cbSize = sizeof(sid);
		sid.szModule = MODULENAME;
		sid.dwId = 0;
		sid.hIcon = hIconFinished;
		sid.hIconDisabled = hIconNotSecure;
		sid.flags = MBF_DISABLED | MBF_HIDDEN;
		sid.szTooltip = Translate(LANG_OTR_TOOLTIP);
		CallService(MS_MSG_ADDICON, 0, (LPARAM)&sid);

		sid.dwId = 1;
		sid.hIcon = hIconPrivate;
		sid.hIconDisabled = hIconUnverified;
		CallService(MS_MSG_ADDICON, 0, (LPARAM)&sid);
		
		// hook the window events so that we can can change the status of the icon
		
		hEventIconPressed = HookEvent(ME_MSG_ICONPRESSED, SVC_IconPressed);
	}
	if (options.bHaveButtonsBar) {
		ZeroMemory(&OTRButton, sizeof(OTRButton));
		OTRButton.cbSize = sizeof(OTRButton);
		OTRButton.dwButtonID = 0;
		OTRButton.pszModuleName = MODULENAME;
		OTRButton.dwDefPos = 200;
#ifdef _UNICODE
		OTRButton.bbbFlags = BBBF_ISRSIDEBUTTON|BBBF_CANBEHIDDEN|BBBF_ISIMBUTTON;
#else
		OTRButton.bbbFlags = BBBF_ISRSIDEBUTTON|BBBF_CANBEHIDDEN|BBBF_ISIMBUTTON|BBBF_ANSITOOLTIP;
#endif
		OTRButton.ptszTooltip = TranslateT(LANG_OTR_TOOLTIP);
		OTRButton.hIcon = GetIconHandle(ICON_NOT_PRIVATE);
		HookEvent(ME_MSG_TOOLBARLOADED, SVC_ButtonsBarLoaded);
		HookEvent(ME_MSG_BUTTONPRESSED, SVC_ButtonsBarPressed);
	}
}
void DeinitSRMM() {
	if(options.bHaveSRMMIcons) {
		UnhookEvent(hEventIconPressed);
		ReleaseIcon(ICON_NOT_PRIVATE, 0);
		ReleaseIcon(ICON_FINISHED, 0);
		ReleaseIcon(ICON_PRIVATE, 0);
		ReleaseIcon(ICON_UNVERIFIED, 0);
		hIconNotSecure = hIconFinished = hIconPrivate = hIconUnverified =0;
	}
	UninitMirOTRMenu();
}