summaryrefslogtreecommitdiff
path: root/plugins/MirOTR/src/svcs_srmm.cpp
blob: 34053374b24af2a0225822721f6eae11b5e0814a (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
#include "stdafx.h"

HICON hIconNotSecure, hIconFinished, hIconPrivate, hIconUnverified;
BBButton OTRButton;

int SVC_IconPressed(WPARAM hContact, LPARAM lParam)
{
	StatusIconClickData *sicd = (StatusIconClickData *)lParam;

	if (mir_strcmp(sicd->szModule, MODULENAME) == 0) {
		char *proto = GetContactProto(hContact);
		if (proto && db_get_b(hContact, proto, "ChatRoom", 0))
			return 0;
		ShowOTRMenu(hContact, sicd->clickLocation);
	}

	return 0;
}

// set SRMM icon status, if applicable
void SetEncryptionStatus(MCONTACT hContact, TrustLevel level)
{
	char *proto = GetContactProto(hContact);
	bool chat_room = (proto && db_get_b(hContact, proto, "ChatRoom", 0));

	BBButton button = OTRButton;

	StatusIconData sid = {}, sid2 = {};
	sid.szModule = MODULENAME;
	sid.dwId = 0;
	sid.flags = MBF_HIDDEN;

	sid2.szModule = MODULENAME;
	sid2.dwId = 1;
	sid2.flags = MBF_HIDDEN;

	if (!chat_room) {
		switch (level) {
		case TRUST_FINISHED:
			sid.flags = 0;
			button.pwszTooltip = TranslateW(LANG_STATUS_FINISHED);
			button.hIcon = IcoLib_GetIconHandle(ICON_FINISHED);
			break;
		case TRUST_UNVERIFIED:
			sid2.flags = MBF_DISABLED;
			button.pwszTooltip = TranslateW(LANG_STATUS_UNVERIFIED);
			button.hIcon = IcoLib_GetIconHandle(ICON_UNVERIFIED);
			break;
		case TRUST_PRIVATE:
			sid2.flags = 0;
			button.pwszTooltip = TranslateW(LANG_STATUS_PRIVATE);
			button.hIcon = IcoLib_GetIconHandle(ICON_PRIVATE);
			break;
		default:
			sid.flags = MBF_DISABLED;
			button.pwszTooltip = TranslateW(LANG_STATUS_DISABLED);
			button.hIcon = IcoLib_GetIconHandle(ICON_NOT_PRIVATE);
			break;
		}
		button.bbbFlags = 0;
	}
	else button.bbbFlags = BBSF_HIDDEN;

	Srmm_ModifyIcon(hContact, &sid);
	Srmm_ModifyIcon(hContact, &sid2);
	Srmm_SetButtonState(hContact, &button);

	g_plugin.setDword(hContact, "TrustLevel", level);

	if (!chat_room) {
		MCONTACT hMeta = db_mc_getMeta(hContact);
		if(hMeta){
			MCONTACT hMostOnline = db_mc_getMostOnline(hMeta);
			if(hContact == hMostOnline) {
				SetEncryptionStatus(hMeta, level);
			} else {
				/* 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);
			}
		}
	}
}

int SVC_ButtonsBarLoaded(WPARAM, LPARAM)
{
	Srmm_AddButton(&OTRButton, &g_plugin);
	return 0;
}

int SVC_ButtonsBarPressed(WPARAM w, LPARAM l)
{
	CustomButtonClickData *cbcd = (CustomButtonClickData *)l;
	if (cbcd->dwButtonId == 0 && !mir_strcmp(cbcd->pszModule, MODULENAME)) {
		MCONTACT hContact = (MCONTACT)w;
	
		char *proto = GetContactProto(hContact);
		if(proto && db_get_b(hContact, proto, "ChatRoom", 0))
			return 0;
		ShowOTRMenu(hContact, cbcd->pt);

	}
	return 0;
}

void InitSRMM()
{
	// add icon to srmm status icons
	InitMirOTRMenu();

	hIconNotSecure = IcoLib_GetIcon(ICON_NOT_PRIVATE, 0);
	hIconFinished = IcoLib_GetIcon(ICON_FINISHED, 0);
	hIconPrivate = IcoLib_GetIcon(ICON_PRIVATE, 0);
	hIconUnverified = IcoLib_GetIcon(ICON_UNVERIFIED, 0);

	StatusIconData sid = {};
	sid.szModule = MODULENAME;
	sid.hIcon = hIconFinished;
	sid.hIconDisabled = hIconNotSecure;
	sid.flags = MBF_DISABLED | MBF_HIDDEN;
	sid.szTooltip = LANG_OTR_TOOLTIP;
	Srmm_AddIcon(&sid, &g_plugin);

	sid.dwId = 1;
	sid.hIcon = hIconPrivate;
	sid.hIconDisabled = hIconUnverified;
	Srmm_AddIcon(&sid, &g_plugin);
		
	// hook the window events so that we can can change the status of the icon
	HookEvent(ME_MSG_ICONPRESSED, SVC_IconPressed);

	memset(&OTRButton, 0, sizeof(OTRButton));
	OTRButton.pszModuleName = MODULENAME;
	OTRButton.dwDefPos = 200;
	OTRButton.bbbFlags = BBBF_ISRSIDEBUTTON | BBBF_CANBEHIDDEN | BBBF_ISIMBUTTON;
	OTRButton.pwszTooltip = TranslateT(LANG_OTR_TOOLTIP);
	OTRButton.hIcon = IcoLib_GetIconHandle(ICON_NOT_PRIVATE);

	HookEvent(ME_MSG_BUTTONPRESSED, SVC_ButtonsBarPressed);

	HookTemporaryEvent(ME_MSG_TOOLBARLOADED, SVC_ButtonsBarLoaded);
}

void DeinitSRMM()
{
	IcoLib_Release(ICON_NOT_PRIVATE, 0);
	IcoLib_Release(ICON_FINISHED, 0);
	IcoLib_Release(ICON_PRIVATE, 0);
	IcoLib_Release(ICON_UNVERIFIED, 0);
	hIconNotSecure = hIconFinished = hIconPrivate = hIconUnverified =nullptr;

	UninitMirOTRMenu();
}