MessageSenderUnit.pas 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. unit MessageSenderUnit;
  2. {$WARN SYMBOL_PLATFORM OFF}
  3. interface
  4. uses
  5. ComObj, Dialogs,ActiveX, AxCtrls, Classes, RealOAMessenger_TLB, StdVcl, SysUtils,
  6. BlockingTCPClient, RealICQProxy, ASPTypeLibrary_TLB, MD5, EncdDecd;
  7. type
  8. TMessageSender = class(TAutoObject, IConnectionPointContainer, IMessageSender)
  9. private
  10. { Private declarations }
  11. FConnectionPoints: TConnectionPoints;
  12. FConnectionPoint: TConnectionPoint;
  13. FEvents: IMessageSenderEvents;
  14. { note: FEvents maintains a *single* event sink. For access to more
  15. than one event sink, use FConnectionPoint.SinkList, and iterate
  16. through the list of sinks. }
  17. FBlockingTCPClient: TBlockingTCPClient;
  18. FServerAddress: String;
  19. FServerPort: SYSINT;
  20. FProxyType: Shortint;
  21. FProxyAddress: String;
  22. FProxyPort: SYSINT;
  23. FProxyPassword: String;
  24. FProxyUsername: String;
  25. FProxyDomain: String;
  26. FUsername: String;
  27. FPassword: String;
  28. FMessageType: Integer;
  29. FAutoOpenWindow: Boolean;
  30. FPosition,
  31. FLeft,
  32. FTop,
  33. FWidth,
  34. FHeight: Integer;
  35. FTitle,
  36. FContent,
  37. FUrl: String;
  38. FAutoCloseTime: Integer;
  39. FUsers: String;
  40. m_scriptContext: IScriptingContext;
  41. procedure Connect;
  42. procedure Login;
  43. procedure Send;
  44. public
  45. procedure Initialize; override;
  46. protected
  47. { Protected declarations }
  48. property ConnectionPoints: TConnectionPoints read FConnectionPoints
  49. implements IConnectionPointContainer;
  50. procedure EventSinkChanged(const EventSink: IUnknown); override;
  51. procedure OnStartPage(const unk: IUnknown); safecall;
  52. procedure OnEndPage; safecall;
  53. function Get_ServerAddress: OleVariant; safecall;
  54. procedure Set_ServerAddress(Value: OleVariant); safecall;
  55. function Get_ServerPort: OleVariant; safecall;
  56. procedure Set_ServerPort(Value: OleVariant); safecall;
  57. function Get_ProxyType: OleVariant; safecall;
  58. procedure Set_ProxyType(Value: OleVariant); safecall;
  59. function Get_ProxyAddress: OleVariant; safecall;
  60. procedure Set_ProxyAddress(Value: OleVariant); safecall;
  61. function Get_ProxyPort: OleVariant; safecall;
  62. procedure Set_ProxyPort(Value: OleVariant); safecall;
  63. function Get_ProxyPassword: OleVariant; safecall;
  64. function Get_ProxyUsername: OleVariant; safecall;
  65. procedure Set_ProxyPassword(Value: OleVariant); safecall;
  66. procedure Set_ProxyUsername(Value: OleVariant); safecall;
  67. function Get_ProxyDomain: OleVariant; safecall;
  68. procedure Set_ProxyDomain(Value: OleVariant); safecall;
  69. function Get_Username: OleVariant; safecall;
  70. procedure Set_Username(Value: OleVariant); safecall;
  71. function Get_Password: OleVariant; safecall;
  72. procedure Set_Password(Value: OleVariant); safecall;
  73. procedure SendSystemMessage(MessageType, AutoOpenWindow, Position, Left,
  74. Top, Width, Height, Title, Content, Url, AutoCloseTime,
  75. Users: OleVariant); safecall;
  76. end;
  77. TRealICQLoginResultType = ( rtLoginOK = 0,
  78. rtCanUpdate = 1,
  79. rtMustUpdate = 2,
  80. rtVersionError = 3,
  81. rtAuthorizationError = 4,
  82. rtOther = 5);
  83. implementation
  84. uses ComServ, Windows;
  85. //------------------------------------------------------------------------------
  86. procedure TMessageSender.OnStartPage(const unk: IUnknown);
  87. begin
  88. m_scriptContext := unk as IScriptingContext;
  89. end;
  90. //------------------------------------------------------------------------------
  91. procedure TMessageSender.OnEndPage;
  92. begin
  93. m_scriptContext := nil;
  94. end;
  95. procedure TMessageSender.EventSinkChanged(const EventSink: IUnknown);
  96. begin
  97. FEvents := EventSink as IMessageSenderEvents;
  98. end;
  99. procedure TMessageSender.Initialize;
  100. begin
  101. inherited Initialize;
  102. FConnectionPoints := TConnectionPoints.Create(Self);
  103. if AutoFactory.EventTypeInfo <> nil then
  104. FConnectionPoint := FConnectionPoints.CreateConnectionPoint(
  105. AutoFactory.EventIID, ckSingle, EventConnect)
  106. else FConnectionPoint := nil;
  107. FServerAddress := '';
  108. FServerPort := 0;
  109. FProxyType := 0;
  110. FProxyAddress := '';
  111. FProxyPort := 0;
  112. FProxyPassword := '';
  113. FProxyUsername := '';
  114. FProxyDomain := '';
  115. FUsername := '';
  116. FPassword := '';
  117. end;
  118. function TMessageSender.Get_ServerAddress: OleVariant;
  119. begin
  120. Result := FServerAddress;
  121. end;
  122. procedure TMessageSender.Set_ServerAddress(Value: OleVariant);
  123. begin
  124. FServerAddress := Value;
  125. end;
  126. function TMessageSender.Get_ServerPort: OleVariant;
  127. begin
  128. Result := FServerPort;
  129. end;
  130. procedure TMessageSender.Set_ServerPort(Value: OleVariant);
  131. begin
  132. FServerPort := Value;
  133. end;
  134. function TMessageSender.Get_ProxyType: OleVariant;
  135. begin
  136. Result := FProxyType;
  137. end;
  138. procedure TMessageSender.Set_ProxyType(Value: OleVariant);
  139. begin
  140. FProxyType := Value;
  141. end;
  142. function TMessageSender.Get_ProxyAddress: OleVariant;
  143. begin
  144. Result := FProxyAddress;
  145. end;
  146. procedure TMessageSender.Set_ProxyAddress(Value: OleVariant);
  147. begin
  148. FProxyAddress := Value;
  149. end;
  150. function TMessageSender.Get_ProxyPort: OleVariant;
  151. begin
  152. Result := FProxyPort;
  153. end;
  154. procedure TMessageSender.Set_ProxyPort(Value: OleVariant);
  155. begin
  156. FProxyPort := Value;
  157. end;
  158. function TMessageSender.Get_ProxyPassword: OleVariant;
  159. begin
  160. Result := FProxyPassword;
  161. end;
  162. function TMessageSender.Get_ProxyUsername: OleVariant;
  163. begin
  164. Result := FProxyUsername;
  165. end;
  166. procedure TMessageSender.Set_ProxyPassword(Value: OleVariant);
  167. begin
  168. FProxyPassword := Value;
  169. end;
  170. procedure TMessageSender.Set_ProxyUsername(Value: OleVariant);
  171. begin
  172. FProxyUsername := Value;
  173. end;
  174. function TMessageSender.Get_ProxyDomain: OleVariant;
  175. begin
  176. Result := FProxyDomain;
  177. end;
  178. procedure TMessageSender.Set_ProxyDomain(Value: OleVariant);
  179. begin
  180. FProxyDomain := Value;
  181. end;
  182. function TMessageSender.Get_Username: OleVariant;
  183. begin
  184. Result := FUsername;
  185. end;
  186. procedure TMessageSender.Set_Username(Value: OleVariant);
  187. begin
  188. FUsername := Value;
  189. end;
  190. function TMessageSender.Get_Password: OleVariant;
  191. begin
  192. Result := FPassword;
  193. end;
  194. procedure TMessageSender.Set_Password(Value: OleVariant);
  195. begin
  196. FPassword := Value;
  197. end;
  198. procedure TMessageSender.Connect;
  199. begin
  200. if FBlockingTCPClient <> nil then FreeAndNil(FBlockingTCPClient);
  201. FBlockingTCPClient := TBlockingTCPClient.Create;
  202. FBlockingTCPClient.NoDelay := True;
  203. FBlockingTCPClient.RemoteAddress := FServerAddress;
  204. FBlockingTCPClient.RemotePort := FServerPort;
  205. FBlockingTCPClient.Proxy.ProxyType := TProxyType(FProxyType);
  206. FBlockingTCPClient.Proxy.Address := FProxyAddress;
  207. FBlockingTCPClient.Proxy.Port := FProxyPort;
  208. FBlockingTCPClient.Proxy.Username := FProxyUsername;
  209. FBlockingTCPClient.Proxy.Password := FProxyPassword;
  210. FBlockingTCPClient.Proxy.Domain := FProxyDomain;
  211. FBlockingTCPClient.Connect;
  212. end;
  213. procedure TMessageSender.Login;
  214. var
  215. EncryptedPassword: String;
  216. nIndex,
  217. UsernameLength,
  218. PasswordLength,
  219. BufferLength: SmallInt;
  220. InternalVersion: Word;
  221. SendBuffer: Array of Byte;
  222. begin
  223. UsernameLength := Length(FUsername);
  224. EncryptedPassword := UpperCase(MD5Print(MD5String(FPassword)));
  225. PasswordLength := Length(EncryptedPassword);
  226. BufferLength := 7 + UsernameLength + PasswordLength;
  227. SetLength(SendBuffer, BufferLength);
  228. nIndex := 0;
  229. //填充 (1)协议类型(0x01) 1byte
  230. SendBuffer[nIndex] := $01;
  231. Inc(nIndex, 1);
  232. //填充 (2)消息总长度 2byte
  233. CopyMemory(@SendBuffer[nIndex], @BufferLength, 2);
  234. Inc(nIndex, 2);
  235. //填充 (3)当前客户端的内部版本号 2byte
  236. InternalVersion := $01;
  237. CopyMemory(@SendBuffer[nIndex], @InternalVersion, 2);
  238. Inc(nIndex, 2);
  239. //填充 (4)用户名长度 1byte
  240. SendBuffer[nIndex] := Byte(UsernameLength);
  241. Inc(nIndex, 1);
  242. //填充 (5)用户名 动态长度,由(4)指定
  243. CopyMemory(@SendBuffer[nIndex], PChar(FUsername), UsernameLength);
  244. Inc(nIndex, UsernameLength);
  245. //填充 (6)密码长度 1byte
  246. SendBuffer[nIndex] := Byte(PasswordLength);
  247. Inc(nIndex, 1);
  248. //填充 (7)密码 动态长度,由(6)指定
  249. CopyMemory(@SendBuffer[nIndex], PChar(EncryptedPassword), PasswordLength);
  250. //Inc(nIndex, PasswordLength);
  251. FBlockingTCPClient.SendBuffer(SendBuffer[0], BufferLength);
  252. end;
  253. procedure TMessageSender.Send;
  254. var
  255. nIndex,
  256. BufferLength: SmallInt;
  257. UsersLength,
  258. SystemMessageStrLength: Word;
  259. SendBuffer: Array of Byte;
  260. SystemMessageStr,
  261. DisplayMode,
  262. Rectangle: String;
  263. begin
  264. BufferLength := 6;
  265. UsersLength := Length(FUsers);
  266. if UsersLength > 0 then
  267. begin
  268. Inc(BufferLength, 2);
  269. Inc(BufferLength, UsersLength);
  270. end;
  271. if FAutoOpenWindow then
  272. DisplayMode := '0'
  273. else
  274. DisplayMode := '1';
  275. if FPosition = 2 then
  276. Rectangle := '{' + IntToStr(FLeft) + ',' + IntToStr(FTop) + ',' + IntToStr(FWidth) + ',' + IntToStr(FHeight) + '}'
  277. else
  278. Rectangle := '{' + IntToStr(FWidth) + ',' + IntToStr(FHeight) + '}';
  279. SystemMessageStr := IntToStr(FMessageType) + Chr(9) +
  280. DisplayMode + Chr(9) +
  281. EncodeString(FUrl) + Chr(9) +
  282. IntToStr(FPosition) + Chr(9) +
  283. Rectangle + Chr(9) +
  284. EncodeString(FTitle) + Chr(9) +
  285. IntToStr(FAutoCloseTime) + Chr(9) +
  286. EncodeString(FContent);
  287. SystemMessageStrLength := Length(SystemMessageStr);
  288. Inc(BufferLength, SystemMessageStrLength);
  289. SetLength(SendBuffer, BufferLength);
  290. nIndex := 0;
  291. //填充 (1)协议类型(0x41) 1byte
  292. SendBuffer[nIndex] := $41;
  293. Inc(nIndex, 1);
  294. //填充 (2)消息总长度 2byte
  295. CopyMemory(@SendBuffer[nIndex], @BufferLength, 2);
  296. Inc(nIndex, 2);
  297. //填充 (3)用户类型 1byte
  298. if UsersLength > 0 then
  299. SendBuffer[nIndex] := $0
  300. else
  301. SendBuffer[nIndex] := $1;
  302. Inc(nIndex, 1);
  303. if UsersLength > 0 then
  304. begin
  305. //填充 (4)用户名长度 1byte
  306. CopyMemory(@SendBuffer[nIndex], @UsersLength, 2);
  307. Inc(nIndex, 2);
  308. //填充 (5)用户名 动态长度,由(4)指定
  309. CopyMemory(@SendBuffer[nIndex], PChar(FUsers), UsersLength);
  310. Inc(nIndex, UsersLength);
  311. end;
  312. //填充 (6)系统消息长度 2byte
  313. CopyMemory(@SendBuffer[nIndex], @SystemMessageStrLength, 2);
  314. Inc(nIndex, 2);
  315. //填充 (7)系统消息 动态长度
  316. CopyMemory(@SendBuffer[nIndex], PChar(SystemMessageStr), SystemMessageStrLength);
  317. //Inc(nIndex, SystemMessageStrLength);
  318. FBlockingTCPClient.SendBuffer(SendBuffer[0], BufferLength);
  319. end;
  320. procedure TMessageSender.SendSystemMessage(MessageType, AutoOpenWindow,
  321. Position, Left, Top, Width, Height, Title, Content, Url, AutoCloseTime,
  322. Users: OleVariant);
  323. begin
  324. FMessageType := MessageType;
  325. FAutoOpenWindow := AutoOpenWindow;
  326. FPosition := Position;
  327. FLeft := Left;
  328. FTop := Top;
  329. FWidth := Width;
  330. FHeight := Height;
  331. FTitle := Title;
  332. FContent := Content;
  333. FUrl := Url;
  334. FAutoCloseTime := AutoCloseTime;
  335. FUsers := Users;
  336. try
  337. Connect;
  338. Login;
  339. Sleep(200);
  340. Send;
  341. Sleep(500);
  342. finally
  343. if FBlockingTCPClient <> nil then FreeAndNil(FBlockingTCPClient);
  344. end;
  345. end;
  346. initialization
  347. TAutoObjectFactory.Create(ComServer, TMessageSender, Class_MessageSender,
  348. ciMultiInstance, tmApartment);
  349. end.