summaryrefslogtreecommitdiff
path: root/MirOTR/svcs_menu.cpp
blob: 47900f21e5bcf683444ca691be35474d8c61fc50 (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
#include "stdafx.h"
#include "otr.h"

HANDLE hMenuBuildEvent, hStopItem, hStartItem;

////////////////////////////////
///////// Menu Services
///////////////////////
int StartOTR(HANDLE hContact) {
	
	const char *proto = contact_get_proto(hContact);
	if(!proto) return 1; // error
	char *uname = contact_get_id(hContact);
	if(!uname) return 1; // error
	WORD pol = DBGetContactSettingWord(hContact, MODULENAME, "Policy", CONTACT_DEFAULT_POLICY);
	if(pol == CONTACT_DEFAULT_POLICY) pol = options.default_policy;
	
	lib_cs_lock();
    char *msg = otrl_proto_default_query_msg(MODULENAME, pol);
    otr_gui_inject_message(hContact, proto, proto, uname, msg ? msg : "?OTRv2?");
	lib_cs_unlock();
    otrl_message_free(msg);
	mir_free(uname);
	return 0;
}
int SVC_StartOTR(WPARAM wParam, LPARAM lParam) {
	HANDLE hContact = (HANDLE)wParam, hSub;
	if(options.bHaveMetaContacts && (hSub = (HANDLE)CallService(MS_MC_GETMOSTONLINECONTACT, (WPARAM)hContact, 0)) != 0) {
		hContact = hSub;
	}

	if ( options.bHaveSecureIM && CallService("SecureIM/IsContactSecured", (WPARAM)hContact, 0) != 0 ) {
		TCHAR msg[512];
		mir_sntprintf(msg, 512, TranslateT(LANG_OTR_SECUREIM_STARTED), contact_get_nameT(hContact));
		ShowError(msg);
		return 0;
	}
	
	TCHAR buff[512];
	mir_sntprintf(buff, 512, TranslateT(LANG_SESSION_REQUEST_OTR), contact_get_nameT(hContact));
	ShowMessage(hContact, buff);

	int res = StartOTR(hContact);
	if (res) return res;

	return 0;
}
int SVC_RefreshOTR(WPARAM wParam, LPARAM lParam) {
	HANDLE hContact = (HANDLE)wParam, hSub;
	if(options.bHaveMetaContacts && (hSub = (HANDLE)CallService(MS_MC_GETMOSTONLINECONTACT, (WPARAM)hContact, 0)) != 0) {
		hContact = hSub;
	}

	if ( options.bHaveSecureIM && CallService("SecureIM/IsContactSecured", (WPARAM)hContact, 0) != 0 ) {
		TCHAR msg[512];
		mir_sntprintf(msg, 512, TranslateT(LANG_OTR_SECUREIM_STARTED), contact_get_nameT(hContact));
		ShowError(msg);
		return 0;
	}
	
	TCHAR buff[512];
	mir_sntprintf(buff, 512, TranslateT(LANG_SESSION_TRY_CONTINUE_OTR), contact_get_nameT(hContact));
	ShowMessage(hContact, buff);

	int res = StartOTR(hContact);
	if (res) return res;

	return 0;
}
int otr_disconnect_contact(HANDLE hContact) {
	HANDLE hSub;
	if(ServiceExists(MS_MC_GETMOSTONLINECONTACT) && (hSub = (HANDLE)CallService(MS_MC_GETMOSTONLINECONTACT, (WPARAM)hContact, 0)) != 0) {
		hContact = hSub;
	}
	
	const char *proto = contact_get_proto(hContact);
	if(!proto) return 1; // error
	char *uname = contact_get_id(hContact);
	if(!uname) return 1; // error

	lib_cs_lock();
	otrl_message_disconnect(otr_user_state, &ops, hContact, proto, proto, uname);
	lib_cs_unlock();
	mir_free(uname);
	return 0;
}

int SVC_StopOTR(WPARAM wParam, LPARAM lParam) {
	HANDLE hContact = (HANDLE)wParam;
	
	// prevent this filter from acting on injeceted messages for metas, when they are passed though the subcontact's proto send chain
	if (otr_disconnect_contact(hContact)) return 0;

	SetEncryptionStatus(hContact, TRUST_NOT_PRIVATE);

	TCHAR buff[512];
	mir_sntprintf(buff, 512, TranslateT(LANG_SESSION_TERMINATED_OTR), contact_get_nameT(hContact));
	ShowMessage(hContact, buff);
	
	return 0;
}

int SVC_VerifyOTR(WPARAM wParam, LPARAM lParam) {
	HANDLE hContact = (HANDLE)wParam;
	ConnContext *context = otrl_context_find_miranda(otr_user_state, (HANDLE)wParam);
	if (!context) return 1;
	//VerifyContextDialog(context);	
	SMPInitDialog(context);
	return 0;
}

void InitMenu() {
	CLISTMENUITEM mi = {0};
	mi.cbSize = sizeof(mi);
	mi.flags = CMIF_NOTOFFLINE | CMIF_TCHAR | CMIF_ICONFROMICOLIB;
	mi.position = -400000;

	mi.ptszName = _T(LANG_STOP_OTR);
	mi.pszService = MS_OTR_MENUSTOP;
	mi.icolibItem = GetIconHandle(ICON_PRIVATE);

	hStopItem = (HANDLE)CallService(MS_CLIST_ADDCONTACTMENUITEM, 0, (LPARAM)&mi);
	
	mi.ptszName = _T(LANG_START_OTR);
	mi.pszService = MS_OTR_MENUSTART;
	mi.icolibItem = GetIconHandle(ICON_NOT_PRIVATE);

	hStartItem = (HANDLE)CallService(MS_CLIST_ADDCONTACTMENUITEM, 0, (LPARAM)&mi);

	hMenuBuildEvent = HookEvent(ME_CLIST_PREBUILDCONTACTMENU, SVC_PrebuildContactMenu);

}

void DeinitMenu() {
	UnhookEvent(hMenuBuildEvent);
}

int SVC_PrebuildContactMenu(WPARAM wParam, LPARAM lParam) {
	HANDLE hContact = (HANDLE)wParam;
	
	CLISTMENUITEM mi = {0};
	mi.cbSize = sizeof(mi);
	mi.flags = CMIM_FLAGS | CMIF_NOTOFFLINE | CMIF_TCHAR | CMIF_ICONFROMICOLIB;
	
	const char *proto = contact_get_proto(hContact);
	WORD pol = CONTACT_DEFAULT_POLICY;
	
	if(!proto || DBGetContactSettingByte(hContact, proto, "ChatRoom", 0) == 1) {
		goto hide_all;
	}
	
	if(proto && g_metaproto && strcmp(proto, g_metaproto) == 0) {
		// make menu act as per most online subcontact
		hContact = (HANDLE)CallService(MS_MC_GETMOSTONLINECONTACT, (WPARAM)hContact, 0);
		if(!hContact)
			goto hide_all;
		proto = contact_get_proto(hContact);
	}

	pol = DBGetContactSettingWord(hContact, MODULENAME, "Policy", CONTACT_DEFAULT_POLICY);
	if(pol == CONTACT_DEFAULT_POLICY) pol = options.default_policy;
	
	if(pol == OTRL_POLICY_NEVER || pol == OTRL_POLICY_ALWAYS) {
		goto hide_all;
	}

	{
		ConnContext *context = otrl_context_find_miranda(otr_user_state, hContact);	
		TrustLevel encrypted = otr_context_get_trust(context);
		if(encrypted != TRUST_NOT_PRIVATE) {
			CallService(MS_CLIST_MODIFYMENUITEM, (WPARAM)hStopItem, (LPARAM)&mi);
			mi.flags |= CMIF_HIDDEN;
			CallService(MS_CLIST_MODIFYMENUITEM, (WPARAM)hStartItem, (LPARAM)&mi);
		} else {
			//if(pol == OTRL_POLICY_MANUAL_MOD) {
				CallService(MS_CLIST_MODIFYMENUITEM, (WPARAM)hStartItem, (LPARAM)&mi);
				mi.flags |= CMIF_HIDDEN;
				CallService(MS_CLIST_MODIFYMENUITEM, (WPARAM)hStopItem, (LPARAM)&mi);
			//} else { // should only be 'opportunistic'
			//	goto hide_all;
			//}
		}	
	}
	
	return 0;

hide_all:
	mi.flags |= CMIF_HIDDEN;
	CallService(MS_CLIST_MODIFYMENUITEM, (WPARAM)hStopItem, (LPARAM)&mi);
	CallService(MS_CLIST_MODIFYMENUITEM, (WPARAM)hStartItem, (LPARAM)&mi);
	return 0;
}