summaryrefslogtreecommitdiff
path: root/plugins/SMS/src/receive.cpp
blob: b201f82db924fb81e53d87426790b0a3eb13c78f (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
293
294
295
296
297
298
299
/*
Miranda-IM SMS Plugin
Copyright (C) 2001-2  Richard Hughes
Copyright (C) 2007-2009  Rozhuk Ivan

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
---------------------------------------------------------------------------

This was the original words.
This plugin was modified by Ariel Shulman (NuKe007).
For any comments, problems, etc. contact me at Miranda-IM forums or E-Mail or ICQ.
All the information needed you can find at www.nuke007.tk
Enjoy the code and use it smartly!
*/

#include "common.h"

//This function handles the ACK received from that hooked.
int handleAckSMS(WPARAM wParam,LPARAM lParam)
{
	if (lParam)
	if (((ACKDATA*)lParam)->type==ICQACKTYPE_SMS)
	{
		char szPhone[MAX_PHONE_LEN]={0};
		WCHAR wszPhone[MAX_PHONE_LEN]={0};
		LPSTR lpszXML=(LPSTR)((ACKDATA*)lParam)->lParam,lpszData,lpszPhone;
		SIZE_T dwXMLSize=lstrlenA(lpszXML),dwDataSize,dwPhoneSize;
		ACKDATA *ack=((ACKDATA*)lParam);

		if (GetXMLFieldEx(lpszXML,dwXMLSize,&lpszData,&dwDataSize,"sms_message","text",NULL))
		{
			if (GetXMLFieldEx(lpszXML,dwXMLSize,&lpszPhone,&dwPhoneSize,"sms_message","sender",NULL))
			{
				LPSTR lpszMessageUTF;
				LPWSTR lpwszMessageXMLEncoded,lpwszMessageXMLDecoded;
				SIZE_T dwBuffLen,dwMessageXMLEncodedSize,dwMessageXMLDecodedSize;
				HANDLE hContact;
				DBEVENTINFO dbei={0};

				dwBuffLen=(dwDataSize+MAX_PATH);
				dbei.pBlob=(LPBYTE)MEMALLOC((dwBuffLen+dwPhoneSize));
				lpwszMessageXMLEncoded=(LPWSTR)MEMALLOC((dwBuffLen*sizeof(WCHAR)));
				lpwszMessageXMLDecoded=(LPWSTR)MEMALLOC((dwBuffLen*sizeof(WCHAR)));
				if (dbei.pBlob && lpwszMessageXMLEncoded && lpwszMessageXMLDecoded)
				{
					dwMessageXMLEncodedSize=MultiByteToWideChar(CP_UTF8,0,lpszData,dwDataSize,lpwszMessageXMLEncoded,dwBuffLen);
					DecodeXML(lpwszMessageXMLEncoded,dwMessageXMLEncodedSize,lpwszMessageXMLDecoded,dwBuffLen,&dwMessageXMLDecodedSize);
					lpszMessageUTF=(LPSTR)lpwszMessageXMLEncoded;
					WideCharToMultiByte(CP_UTF8,0,lpwszMessageXMLDecoded,dwMessageXMLDecodedSize,lpszMessageUTF,dwBuffLen,NULL,NULL);

					dwPhoneSize=CopyNumberA(szPhone,lpszPhone,dwPhoneSize);
					dwPhoneSize=MultiByteToWideChar(CP_UTF8,0,szPhone,dwPhoneSize,wszPhone,MAX_PHONE_LEN);
					hContact=HContactFromPhone(wszPhone,dwPhoneSize);

					dbei.cbSize=sizeof(dbei);
					dbei.szModule=GetModuleName(hContact);
					dbei.timestamp=time(NULL);
					dbei.flags=(DBEF_UTF);
					dbei.eventType=ICQEVENTTYPE_SMS;
					dbei.cbBlob=(mir_snprintf((LPSTR)dbei.pBlob,((dwBuffLen+dwPhoneSize)),"SMS From: +%s\r\n%s",szPhone,lpszMessageUTF)+sizeof(DWORD));
					//dbei.pBlob=(LPBYTE)lpszBuff;
					(*((DWORD*)(dbei.pBlob+(dbei.cbBlob-sizeof(DWORD)))))=0;
					CallService(MS_DB_EVENT_ADD,(WPARAM)hContact,(LPARAM)&dbei);
					if (hContact==NULL)
					{	
						if (RecvSMSWindowAdd(NULL,ICQEVENTTYPE_SMS,wszPhone,dwPhoneSize,(LPSTR)dbei.pBlob,dbei.cbBlob))
						{
							CallService(MS_DB_EVENT_MARKREAD,(WPARAM)hContact,(LPARAM)&dbei);
							SkinPlaySound("RecvSMSMsg");
						}
					}
				}
				MEMFREE(lpwszMessageXMLDecoded);
				MEMFREE(lpwszMessageXMLEncoded);
				MEMFREE(dbei.pBlob);
			}
		}else
		if (GetXMLFieldEx(lpszXML,dwXMLSize,&lpszData,&dwDataSize,"sms_delivery_receipt","delivered",NULL))
		{
			if (GetXMLFieldEx(lpszXML,dwXMLSize,&lpszPhone,&dwPhoneSize,"sms_delivery_receipt","destination",NULL))
			{
				HANDLE hContact;
				DBEVENTINFO dbei={0};

				dwPhoneSize=CopyNumberA(szPhone,lpszPhone,dwPhoneSize);
				dwPhoneSize=MultiByteToWideChar(CP_UTF8,0,szPhone,dwPhoneSize,wszPhone,MAX_PHONE_LEN);
				hContact=HContactFromPhone(wszPhone,dwPhoneSize);

				dbei.cbSize=sizeof(dbei);
				dbei.szModule=GetModuleName(hContact);
				dbei.timestamp=time(NULL);
				dbei.eventType=ICQEVENTTYPE_SMSCONFIRMATION;
				if (CompareStringA(MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_US),NORM_IGNORECASE,lpszData,dwDataSize,"yes",3)==CSTR_EQUAL)
				{
					dbei.cbBlob=(MAX_PHONE_LEN+MAX_PATH);
					dbei.pBlob=(PBYTE)MEMALLOC(dbei.cbBlob);
					if (dbei.pBlob) dbei.cbBlob=(mir_snprintf((LPSTR)dbei.pBlob,dbei.cbBlob,"SMS Confirmation From: +%s\r\nSMS was sent succesfully",szPhone)+4);
				}else{
					if (GetXMLFieldEx(lpszXML,dwXMLSize,&lpszData,&dwDataSize,"sms_delivery_receipt","error","params","param",NULL)==FALSE)
					{
						lpszData="";
						dwDataSize=0;
					}
					dbei.cbBlob=(MAX_PHONE_LEN+MAX_PATH+dwDataSize);
					dbei.pBlob=(PBYTE)MEMALLOC(dbei.cbBlob);
					if (dbei.pBlob)
					{
						dbei.cbBlob=mir_snprintf((LPSTR)dbei.pBlob,dbei.cbBlob,"SMS Confirmation From: +%s\r\nSMS was not sent succesfully: ",szPhone);
						CopyMemory((dbei.pBlob+dbei.cbBlob),lpszData,dwDataSize);
						dbei.cbBlob+=(dwDataSize+sizeof(DWORD));
						(*((DWORD*)(dbei.pBlob+(dbei.cbBlob-sizeof(DWORD)))))=0;
					}
				}

				if (dbei.pBlob)
				{
					if (hContact)
					{
						CallService(MS_DB_EVENT_ADD,(WPARAM)hContact,(LPARAM)&dbei);
					}else{
						RecvSMSWindowAdd(NULL,ICQEVENTTYPE_SMSCONFIRMATION,wszPhone,dwPhoneSize,(LPSTR)dbei.pBlob,dbei.cbBlob);
					}
					MEMFREE(dbei.pBlob);
				}
			}
		}else
		if ((ack->result==ACKRESULT_FAILED) || GetXMLFieldEx(lpszXML,dwXMLSize,&lpszData,&dwDataSize,"sms_response","deliverable",NULL))
		{
			HWND hWndDlg=SendSMSWindowHwndByHProcessGet(ack->hProcess);
			if (hWndDlg) {
				char szNetwork[MAX_PATH];

				KillTimer(hWndDlg,wParam);
				GetXMLFieldExBuff(lpszXML,dwXMLSize,szNetwork,sizeof(szNetwork),NULL,"sms_response","network",NULL);

				if (ack->result==ACKRESULT_FAILED || CompareStringA(MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_US),NORM_IGNORECASE,lpszData,dwDataSize,"no",2)==CSTR_EQUAL) {	
					HWND hwndTimeOut;
					char szBuff[1024];
					WCHAR wszErrorMessage[1028];
					LPSTR lpszErrorDescription;

					if (SendSMSWindowMultipleGet(hWndDlg)) {
						TVITEM tvi;
						tvi.mask=TVIF_TEXT;
						tvi.hItem=SendSMSWindowHItemSendGet(hWndDlg);
						tvi.pszText=wszPhone;
						tvi.cchTextMax=SIZEOF(wszPhone);
						TreeView_GetItem(GetDlgItem(hWndDlg,IDC_NUMBERSLIST),&tvi);
					}
					else GET_DLG_ITEM_TEXTW(hWndDlg,IDC_ADDRESS,wszPhone,SIZEOF(szPhone));

					if (ack->result == ACKRESULT_FAILED)
						lpszErrorDescription=lpszXML;
					else {
						lpszErrorDescription=szBuff;
						GetXMLFieldExBuff(lpszXML,dwXMLSize,szBuff,sizeof(szBuff),NULL,"sms_response","error","params","param",NULL);
					}

					mir_sntprintf(wszErrorMessage,SIZEOF(wszErrorMessage),TranslateT("SMS message didn't send by %S to %s because: %S"),szNetwork,wszPhone,lpszErrorDescription);
					ShowWindow(hWndDlg,SW_SHOWNORMAL);
					EnableWindow(hWndDlg,FALSE);
					hwndTimeOut=CreateDialog(ssSMSSettings.hInstance,MAKEINTRESOURCE(IDD_SENDSMSTIMEDOUT),hWndDlg,SMSTimedOutDlgProc);
					SET_DLG_ITEM_TEXTW(hwndTimeOut,IDC_STATUS,wszErrorMessage);
				}
				else {
					SendSMSWindowDBAdd(hWndDlg);
					if ( SendSMSWindowMultipleGet(hWndDlg)) {
						if ( SendSMSWindowNextHItemGet(hWndDlg,SendSMSWindowHItemSendGet(hWndDlg))) {
							SendSMSWindowAsSentSet(hWndDlg);
							SendSMSWindowHItemSendSet(hWndDlg,SendSMSWindowNextHItemGet(hWndDlg,SendSMSWindowHItemSendGet(hWndDlg)));
							SendSMSWindowNext(hWndDlg);
						}
						else SendSMSWindowRemove(hWndDlg);
					}
					else {
						if ( CompareStringA(MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_US),NORM_IGNORECASE,lpszData,dwDataSize,"yes",3)==CSTR_EQUAL ||
							  CompareStringA(MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_US),NORM_IGNORECASE,lpszData,dwDataSize,"smtp",4)==CSTR_EQUAL) {
							char szSource[MAX_PATH],szMessageID[MAX_PATH];

							if (DB_SMS_GetByte(NULL,"ShowACK",SMS_DEFAULT_SHOWACK)) {	
								HWND hwndAccepted=CreateDialog(ssSMSSettings.hInstance,MAKEINTRESOURCE(IDD_SENDSMSACCEPT),hWndDlg,SMSAcceptedDlgProc);
								if (CompareStringA(MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_US),NORM_IGNORECASE,lpszData,dwDataSize,"yes",3)==CSTR_EQUAL) {
									GetXMLFieldExBuff(lpszXML,dwXMLSize,szSource,sizeof(szSource),NULL,"sms_response","source",NULL);
									GetXMLFieldExBuff(lpszXML,dwXMLSize,szMessageID,sizeof(szMessageID),NULL,"sms_response","message_id",NULL);
								}
								else {
									SET_DLG_ITEM_TEXTW(hwndAccepted,IDC_ST_SOURCE,TranslateT("From:"));
									SET_DLG_ITEM_TEXTW(hwndAccepted,IDC_ST_MESSAGEID,TranslateT("To:"));
									GetXMLFieldExBuff(lpszXML,dwXMLSize,szSource,sizeof(szSource),NULL,"sms_response","from",NULL);
									GetXMLFieldExBuff(lpszXML,dwXMLSize,szMessageID,sizeof(szMessageID),NULL,"sms_response","to",NULL);
								}
								SET_DLG_ITEM_TEXTA(hwndAccepted,IDC_NETWORK,szNetwork);
								SET_DLG_ITEM_TEXTA(hwndAccepted,IDC_SOURCE,szSource);
								SET_DLG_ITEM_TEXTA(hwndAccepted,IDC_MESSAGEID,szMessageID);
							}
							else SendSMSWindowRemove(hWndDlg);
						}
						else SendSMSWindowRemove(hWndDlg);
					}
				}
			}
		}
	}
	return 0;
}

//Handles new SMS messages added to the database
int handleNewMessage(WPARAM wParam,LPARAM lParam)
{
	CHAR szServiceFunction[MAX_PATH],*pszServiceFunctionName;
	WCHAR szToolTip[MAX_PATH];
	HANDLE hContact=(HANDLE)wParam,hDbEvent=(HANDLE)lParam;
	CLISTEVENT cle={0};
	DBEVENTINFO dbei={0};

	dbei.cbSize=sizeof(dbei);
	if ((dbei.cbBlob=CallService(MS_DB_EVENT_GETBLOBSIZE,lParam,0))!=-1)
	{
		dbei.pBlob=(PBYTE)MEMALLOC(dbei.cbBlob);
		if (dbei.pBlob)
		{
			CopyMemory(szServiceFunction,PROTOCOL_NAMEA,PROTOCOL_NAME_SIZE);
			pszServiceFunctionName=szServiceFunction+PROTOCOL_NAME_LEN;

			if (CallService(MS_DB_EVENT_GET,lParam,(LPARAM)&dbei)==0)
			if ((dbei.flags&DBEF_SENT)==0)
			if (dbei.eventType==ICQEVENTTYPE_SMS)
			{
				if (dbei.cbBlob>MIN_SMS_DBEVENT_LEN)
				{
					SkinPlaySound("RecvSMSMsg");
					if (DB_SMS_GetByte(NULL,"AutoPopup",0))
					{
						if (RecvSMSWindowAdd(hContact,ICQEVENTTYPE_SMS,NULL,0,(LPSTR)dbei.pBlob,dbei.cbBlob))
						{
							CallService(MS_DB_EVENT_MARKREAD,(WPARAM)hContact,(LPARAM)&dbei);
						}
					}else{
						CopyMemory(pszServiceFunctionName,SMS_READ,sizeof(SMS_READ));

						cle.cbSize=sizeof(cle);
						cle.flags=CLEF_TCHAR;
						cle.hContact=hContact;
						cle.hDbEvent=hDbEvent;
						cle.hIcon=LoadSkinnedIcon(SKINICON_OTHER_SMS);
						cle.pszService=szServiceFunction;
						mir_sntprintf(szToolTip,SIZEOF(szToolTip),TranslateT("SMS Message from %s"),GetContactNameW(hContact));
						cle.ptszTooltip=szToolTip;
						CallService(MS_CLIST_ADDEVENT,0,(LPARAM)&cle);
					}
				}
			}else
			if (dbei.eventType==ICQEVENTTYPE_SMSCONFIRMATION)
			{
				SkinPlaySound("RecvSMSConfirmation");
				if (DB_SMS_GetByte(NULL,"AutoPopup",0)) 
				{
					if (RecvSMSWindowAdd(hContact,ICQEVENTTYPE_SMSCONFIRMATION,NULL,0,(LPSTR)dbei.pBlob,dbei.cbBlob))
					{
						CallService(MS_DB_EVENT_DELETE,(WPARAM)hContact,(LPARAM)&dbei);
					}
				}else{
					UINT iIcon;

					if (GetDataFromMessage((LPSTR)dbei.pBlob,dbei.cbBlob,NULL,NULL,0,NULL,&iIcon))
					{
						CopyMemory(pszServiceFunctionName,SMS_READ_ACK,sizeof(SMS_READ_ACK));

						cle.cbSize=sizeof(cle);
						cle.flags=CLEF_TCHAR;
						cle.hContact=hContact;
						cle.hDbEvent=hDbEvent;
						cle.hIcon=(HICON)LoadImage(ssSMSSettings.hInstance,MAKEINTRESOURCE(iIcon),IMAGE_ICON,0,0,LR_SHARED);
						cle.pszService=szServiceFunction;
						mir_sntprintf(szToolTip,SIZEOF(szToolTip),TranslateT("SMS Confirmation from %s"),GetContactNameW(hContact));
						cle.ptszTooltip=szToolTip;
						CallService(MS_CLIST_ADDEVENT,0,(LPARAM)&cle);
					}
				}
			}
			MEMFREE(dbei.pBlob);
		}
	}
	return 0;
}