summaryrefslogtreecommitdiff
path: root/plugins/SMS/src/SMS_svc.cpp
blob: cff9a72946c4685ddb1b0e4b89831e0fd3596084 (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
#include "common.h"

int LoadServices(void)
{
	CHAR szServiceFunction[MAX_PATH],*pszServiceFunctionName;

	CopyMemory(szServiceFunction,PROTOCOL_NAMEA,PROTOCOL_NAME_SIZE);
	pszServiceFunctionName=szServiceFunction+PROTOCOL_NAME_LEN;

	// Service creation
	for (SIZE_T i=0;i<SIZEOF(siPluginServices);i++)
	{
		CopyMemory(pszServiceFunctionName,siPluginServices[i].lpszName,(lstrlenA(siPluginServices[i].lpszName)+1));
		CreateServiceFunction(szServiceFunction,(MIRANDASERVICE)siPluginServices[i].lpFunc);
	}
	return 0;
}


int LoadModules(void)
{
	ssSMSSettings.hHookOptInitialize=HookEvent(ME_OPT_INITIALISE,OptInitialise);
	ssSMSSettings.hHookRebuildCMenu=HookEvent(ME_CLIST_PREBUILDCONTACTMENU,SmsRebuildContactMenu);
	ssSMSSettings.hHookProtoAck=HookEvent(ME_PROTO_ACK,handleAckSMS);
	ssSMSSettings.hHookDbAdd=HookEvent(ME_DB_EVENT_ADDED,handleNewMessage);
	ssSMSSettings.hHookAccListChanged=HookEvent(ME_PROTO_ACCLISTCHANGED,RefreshAccountList);
	

	CLISTMENUITEM mi={0};
	CHAR szServiceFunction[MAX_PATH];

	mir_snprintf(szServiceFunction,sizeof(szServiceFunction),"%s%s",PROTOCOL_NAMEA,SMS_SEND);

	mi.cbSize=sizeof(mi);
	mi.position=300050000;
	mi.hIcon=LoadSkinnedIcon(SKINICON_OTHER_SMS);
	mi.ptszName=SMS_SEND_STR;
	mi.pszService=szServiceFunction;
	mi.flags=CMIF_TCHAR;
	Menu_AddMainMenuItem(&mi);

	mi.position=-2000070000;
	mi.hIcon=LoadSkinnedIcon(SKINICON_OTHER_SMS);
	mi.ptszName=SMS_SEND_CM_STR;
	mi.pszService=szServiceFunction;
	mi.flags=CMIF_TCHAR;	
	ssSMSSettings.hContactMenuItems[0]=Menu_AddContactMenuItem(&mi);

	SkinAddNewSoundEx("RecvSMSMsg",PROTOCOL_NAMEA,LPGEN("Incoming SMS Message"));
	SkinAddNewSoundEx("RecvSMSConfirmation",PROTOCOL_NAMEA,LPGEN("Incoming SMS Confirmation"));


	RefreshAccountList(NULL,NULL);

	RestoreUnreadMessageAlerts();

	return 0;
}


void UnloadModules()
{
	// Main menu destroy
	//CListDestroyMenu(gdiMenuItems,SIZEOF(gdiMenuItems));
	//ZeroMemory(masMraSettings.hMainMenuItems,sizeof(masMraSettings.hMainMenuItems));

	// Contact menu destroy
	//CListDestroyMenu(gdiContactMenuItems,(SIZEOF(gdiContactMenuItems) - ((masMraSettings.heNudgeReceived==NULL)? 0:1)));
	//ZeroMemory(masMraSettings.hContactMenuItems,sizeof(masMraSettings.hContactMenuItems));

	if (ssSMSSettings.hHookAccListChanged)		{UnhookEvent(ssSMSSettings.hHookAccListChanged);		ssSMSSettings.hHookAccListChanged=NULL;}
	if (ssSMSSettings.hHookDbAdd)				{UnhookEvent(ssSMSSettings.hHookDbAdd);					ssSMSSettings.hHookDbAdd=NULL;}
	if (ssSMSSettings.hHookProtoAck)			{UnhookEvent(ssSMSSettings.hHookProtoAck);				ssSMSSettings.hHookProtoAck=NULL;}
	if (ssSMSSettings.hHookRebuildCMenu)		{UnhookEvent(ssSMSSettings.hHookRebuildCMenu);			ssSMSSettings.hHookRebuildCMenu=NULL;}
	if (ssSMSSettings.hHookOptInitialize)		{UnhookEvent(ssSMSSettings.hHookOptInitialize);			ssSMSSettings.hHookOptInitialize=NULL;}

	//IconsUnLoad();

}


void UnloadServices()
{
	CHAR szServiceFunction[MAX_PATH],*pszServiceFunctionName;

	CopyMemory(szServiceFunction,PROTOCOL_NAMEA,PROTOCOL_NAME_SIZE);
	pszServiceFunctionName=szServiceFunction+PROTOCOL_NAME_LEN;

	// destroy plugin services
	for (SIZE_T i=0;i<SIZEOF(siPluginServices);i++)
	{
		CopyMemory(pszServiceFunctionName,siPluginServices[i].lpszName,(lstrlenA(siPluginServices[i].lpszName)+1));
		DestroyServiceFunction(szServiceFunction);
	}
}



int SmsRebuildContactMenu(WPARAM wParam,LPARAM lParam)
{
	Menu_ShowItem(ssSMSSettings.hContactMenuItems[0], GetContactPhonesCount((HANDLE)wParam));
	return 0;
}


//This function called when user clicked Menu.
int SendSMSMenuCommand(WPARAM wParam,LPARAM lParam)
{
	HWND hwndSendSms;

	if (wParam)
	{// user clicked on the "SMS Message" on one of the users
		hwndSendSms=SendSMSWindowIsOtherInstanceHContact((HANDLE)wParam);
		if (hwndSendSms)
			SetFocus(hwndSendSms);
		else
			hwndSendSms=SendSMSWindowAdd((HANDLE)wParam);
	}
	else{// user clicked on the "SMS Send" in the Main Menu
		hwndSendSms=SendSMSWindowAdd(NULL);
		EnableWindow(GetDlgItem(hwndSendSms,IDC_NAME),TRUE);
		EnableWindow(GetDlgItem(hwndSendSms,IDC_SAVENUMBER),FALSE);

		for (HANDLE hContact = db_find_first(); hContact; hContact = db_find_next(hContact))
			if (GetContactPhonesCount(hContact)) {
				SEND_DLG_ITEM_MESSAGEW(hwndSendSms,IDC_NAME,CB_ADDSTRING,0,(LPARAM)GetContactNameW(hContact));
				SendSMSWindowSMSContactAdd(hwndSendSms,hContact);
			}
	}
	return 0;
}


//This function used to popup a read SMS window after the user clicked on the received SMS message.
int ReadMsgSMS(WPARAM wParam,LPARAM lParam)
{
	CLISTEVENT *cle = (CLISTEVENT*)lParam;

	DBEVENTINFO dbei = { sizeof(dbei) };
	if ((dbei.cbBlob = db_event_getBlobSize(((CLISTEVENT*)lParam)->hDbEvent)) != -1) {
		dbei.pBlob = (PBYTE)_alloca(dbei.cbBlob);

		if (db_event_get(cle->hDbEvent, &dbei) == 0)
		if (dbei.eventType == ICQEVENTTYPE_SMS || dbei.eventType == ICQEVENTTYPE_SMSCONFIRMATION)
		if (dbei.cbBlob > MIN_SMS_DBEVENT_LEN) {
			if (RecvSMSWindowAdd(cle->hContact,ICQEVENTTYPE_SMS,NULL,0,(LPSTR)dbei.pBlob,dbei.cbBlob)) {
				db_event_markRead(cle->hContact, cle->hDbEvent);
				return 0;
			}
		}
	}
	return 1;
}

//This function used to popup a read SMS window after the user clicked on the received SMS confirmation.
int ReadAckSMS(WPARAM wParam,LPARAM lParam)
{
	CLISTEVENT *cle = (CLISTEVENT*)lParam;

	DBEVENTINFO dbei = { sizeof(dbei) };
	if ((dbei.cbBlob = db_event_getBlobSize(cle->hDbEvent)) != -1) {
		dbei.pBlob = (PBYTE)_alloca(dbei.cbBlob);

		if (db_event_get(cle->hDbEvent, &dbei) == 0)
		if (dbei.eventType == ICQEVENTTYPE_SMS || dbei.eventType == ICQEVENTTYPE_SMSCONFIRMATION)
		if (dbei.cbBlob > MIN_SMS_DBEVENT_LEN) {
			if (RecvSMSWindowAdd(cle->hContact, ICQEVENTTYPE_SMSCONFIRMATION, NULL, 0, (LPSTR)dbei.pBlob, dbei.cbBlob)) {
				db_event_delete(cle->hContact, cle->hDbEvent);
				return 0;
			}
		}
	}
	return 1;
}

void RestoreUnreadMessageAlerts(void)
{
	DBEVENTINFO dbei = { sizeof(dbei) };

	for (HANDLE hContact = db_find_first(); hContact; hContact = db_find_next(hContact))
		for (HANDLE hDbEvent = db_event_firstUnread(hContact); hDbEvent; hDbEvent = db_event_next(hDbEvent)) {
			dbei.cbBlob=0;
			if (db_event_get(hDbEvent, &dbei) == 0)
			if ((dbei.flags & (DBEF_SENT|DBEF_READ))==0 && ((dbei.eventType==ICQEVENTTYPE_SMS) || (dbei.eventType==ICQEVENTTYPE_SMSCONFIRMATION)))
			if (dbei.cbBlob>MIN_SMS_DBEVENT_LEN)
				handleNewMessage((WPARAM)hContact,(LPARAM)hDbEvent);
		}

	for (HANDLE hDbEvent = db_event_firstUnread(NULL); hDbEvent; hDbEvent = db_event_next(hDbEvent)) {
		dbei.cbBlob=0;
		if (db_event_get(hDbEvent, &dbei) == 0)
		if ((dbei.flags & (DBEF_SENT|DBEF_READ))==0 && ((dbei.eventType==ICQEVENTTYPE_SMS) || (dbei.eventType==ICQEVENTTYPE_SMSCONFIRMATION)))
		if (dbei.cbBlob > MIN_SMS_DBEVENT_LEN)
			handleNewMessage(NULL, (LPARAM)hDbEvent);
	}
}