Mega Code Archive

 
Categories / Delphi / Examples
 

Sending ICQ Pages

Title: Sending ICQ Pages Question: Ever wanted your application to be able to notify an ICQ client when a task was completed ? Here's how to do it: Answer: {There is ONE problem with this code: It does not resolve the wwp.mirabilis.com hostname, it simply uses a hard coded IP} unit unitICQPage; interface uses Windows, Classes, SysUtils, Winsock; Function ICQPage(dwUIN: DWORD; szFrom, szFromEmail, szSubject, szMsg: String): Integer; implementation Function ICQPage(dwUIN: DWORD; szFrom, szFromEmail, szSubject, szMsg: String): Integer; {This procedure sends an ICQ page. It uses a raw winsock interface to do this Return values: 0 = success 1 = WSA Error (WSAGetLastError) } Const ERR_SUCCESS = 0; ERR_WSA = 1; Var MyWSA: WSAData; SIN: TSockAddr; hSocket: TSocket; sRequest: String; Begin If WSAStartup(MakeWord(1,1), MyWSA) 0 Then Begin //There's nothing to do, it failed! ICQPage := ERR_WSA; //ERROR CODE WSACleanup; Exit; end; hSocket := socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); If hSocket = INVALID_SOCKET Then Begin //Unable to create socket :( ICQPage := ERR_WSA; WSACleanup; Exit; End; {Setup "target" information} SIN.sin_family := AF_INET; SIN.sin_port := htons(80); //Port 80 is default HTTP (And ofcource the one mirabilis use} {NOTE: The hostname wwp.mirabilis.com should be resolved in code} // SIN.sin_addr.S_addr := inet_addr(PChar('127.0.0.1')); SIN.sin_addr.S_addr := inet_addr(PChar('205.188.252.120')); If connect(hSocket, SIN, SizeOf(SIN)) = SOCKET_ERROR Then Begin //Error while connecting... {Hmm, Now the program should attempt to resolve the hostname wwp.mirabilis.com and attempt to reconnect to that IP But since I do not know how to resolve hostnames:} ICQPage := ERR_WSA; CloseSocket(hSocket); WSACleanup; Exit; end; //The socket has connected! Sleep(30); //dunno, I just feel like it ;-) sRequest := 'from='+szFrom+'&'+ 'fromemail='+szFromEmail+'&'+ 'subject='+szSubject+'&'+ 'body='+szMsg+'&'+ 'to='+IntToStr(dwUIN)+ '&Send=Send+Message'; sRequest := 'POST /scripts/WWPMsg.dll HTTP/1.1'+#13#10+ 'Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/msword, */*'+#13#10+ 'Accept-Language: en'+#13#10+ 'Content-Type: application/x-www-form-urlencoded'+#13#10+ 'Accept-Encoding: gzip, deflate'+#13#10+ 'User-Agent: Mozilla/4.0 (compatible; MSIE 5.01; Windows 98)'+#13#10+ 'Host: 127.0.0.1'+#13#10+ 'Content-Length: '+IntToStr(Length(sRequest))+#13#10+ 'Connection: Keep-Alive'+#13#10+ ''+#13#10+ sRequest; If Send(hSocket, sRequest[1], Length(sRequest), 0) = SOCKET_ERROR Then Begin //unable to send ICQPage := ERR_WSA; CloseSocket(hSocket); WSACleanup; Exit; End; //Page sent! CloseSocket(hSocket); WSACleanup; ICQPage := ERR_SUCCESS; end; //ICQPage end.