<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
	<title type="html"><![CDATA[Серый форум &mdash; JS: пример бота ICQ]]></title>
	<link rel="self" href="https://forum.script-coding.com/extern.php?action=feed&amp;tid=7319&amp;type=atom" />
	<updated>2012-07-02T13:47:48Z</updated>
	<generator>PunBB</generator>
	<id>https://forum.script-coding.com/viewtopic.php?id=7319</id>
		<entry>
			<title type="html"><![CDATA[JS: пример бота ICQ]]></title>
			<link rel="alternate" href="https://forum.script-coding.com/viewtopic.php?pid=61334#p61334" />
			<content type="html"><![CDATA[<p>Приветствую. Хочу поделиться примером работы с ICQ на JScript. </p><div class="codebox"><pre><code>function callback(uin, msg)
{
	this.SendMessage(uin, &quot;OK&quot;);
	if (msg==&quot;close&quot;) 
	{
		this.SendMessage(uin, &quot;bye&quot;);
		this.Close();
		WScript.Quit();
	}
}

var bot = new ICQ(&quot;9166196&quot;, &quot;***&quot;);

bot.AddOnMsgListener(callback);
bot.Start();

while(1) WScript.Sleep(50);</code></pre></div><div class="quotebox"><blockquote><p>Методы объекта ICQ</p><p>AddOnMsgListener(callback) - добавление обработчиков события получения сообщения.<br />Start() - старт клиента<br />SendMessage(uin, text) - отправка сообщения<br />Close() - закрыть клиент</p></blockquote></div><p>Разумеется на чистом JS такое не получится (хотя ведется некоторая работа и в этом направлении по использованию WebAPI от ICQ). Поэтому придется с собой тянуть еще пару файлов: IcqBot.exe и IcqMod.dll.&nbsp; IcqBot.exe представляет собой скомпилированный код AutoIt. Для начала работы&nbsp; запустите файл JS.</p><p><a href="http://zalil.ru/33433072">Ссылка на закачку</a> <em>исправленная версия</em></p><p>Код скрипта<br /></p><div class="codebox"><pre><code>
var WshShell = new ActiveXObject(&quot;WScript.Shell&quot;);

function GlobalObject(Name)
{    
    if(!Name)Name=&quot;GlobalObject&quot;;
    if (this.Window = this.FindWindow(Name))null;else
    {
        this.Window = GetObject(&quot;new:{C08AFD90-F2A1-11D1-8455-00A0C91F3880}&quot;);
        this.Window.StatusText = Name;
    }
}

GlobalObject.prototype = 
{
    FindWindow : function (Name)
    {
        var  ShellWindows=(new ActiveXObject(&quot;Shell.Application&quot;)).Windows();
        for (var i=ShellWindows.Count; --i&gt;=0;)
        if (ShellWindows.Item(i).StatusText &amp;&amp; ShellWindows.Item(i).StatusText.indexOf(Name)!=-1) 
        return ShellWindows.Item(i);
    },
    
    GetProperty: function (Name)
    {
        return this.Window.GetProperty(Name);
    },
    
    SetProperty: function (Name, Value)
    {
        this.Window.PutProperty(Name, Value);
    }
}

function TerminateProcessByName(Name)
{

var objWMIService, colitems, e;
	objWMIService = GetObject(&quot;winmgmts:{impersonationlevel=impersonate}!\\\\.\\root\\cimv2&quot;);
	colitems = objWMIService.ExecQuery(&quot;SELECT * FROM Win32_Process WHERE name = &#039;&quot;+Name+&quot;&#039;&quot;);
	e = new Enumerator(colitems);
	for (; !e.atEnd(); e.moveNext())
	e.item().Terminate();
}

function ICQ(uin, pass)
{
	this.__callbacks=[];
	this.g = new GlobalObject(&quot;ICQ&quot;);
	this.g.SetProperty(&quot;login&quot;, uin);
	this.g.SetProperty(&quot;pass&quot;, pass);
	this.g.SetProperty(&quot;quit&quot;, &quot;0&quot;);
	this.g.SetProperty(&quot;scriptbot&quot;, this);
}

ICQ.prototype.SendMessage = function (To, Text)
{
	this.g.SetProperty(&quot;text&quot;, Text);
	this.g.SetProperty(&quot;uin&quot;, To);
}

ICQ.prototype.__OnMessageFireEvent = function (uin, msg) 
{
	for (var i=0, l=this.__callbacks.length; i&lt;l; i++)
	this.__callbacks[i].call(this, uin, msg);
}

ICQ.prototype.AddOnMsgListener = function (callback)
{
	this.__callbacks.push(callback);
}

ICQ.prototype.Start=function ()
{
	WshShell.Run(&quot;IcqBot&quot;);
}

ICQ.prototype.Close = function () 
{
	this.g.SetProperty(&quot;quit&quot;, &quot;1&quot;);
	this.g.SetProperty(&quot;uin&quot;, 0);
	WScript.Sleep(1000);
	var e;
	try {
		TerminateProcessByName(&quot;IcqBot.exe&quot;)
		this.g.Quit();
	} catch (e) {}
	
}

//---------------------------------------------------------------

function callback(uin, msg)
{
	this.SendMessage(uin, &quot;OK&quot;);
	if (msg==&quot;close&quot;) 
	{
		this.SendMessage(uin, &quot;bye&quot;);
		this.Close();
		WScript.Quit();
	}
}

var bot = new ICQ(&quot;9166196&quot;, &quot;***&quot;);

bot.AddOnMsgListener(callback);
bot.Start();

while(1) WScript.Sleep(50);</code></pre></div><p>Вот код &quot;сервера&quot; на AutoIt<br /></p><div class="codebox"><pre><code>#NoTrayIcon 
#include &lt;IcqMod.au3&gt;

$oContainer = OpenContainer(&quot;ICQ&quot;)

Func OpenContainer($sName)
    Local $oShell, $oShellWindow, $oShellWindows
    $oShell = ObjCreate(&quot;Shell.Application&quot;)
    $oShellWindows = $oShell.Windows
    
    For $oShellWindow In $oShellWindows
        If StringInstr($oShellWindow.StatusText, $sName) Then
            Return $oShellWindow
        EndIf
    Next
    
    $oContainer = ObjGet(&quot;new:{C08AFD90-F2A1-11D1-8455-00A0C91F3880}&quot;)
    $oContainer.StatusText = $sName
    
    Return $oContainer
EndFunc

Func PutProperty($oContainer, $sName, $vValue)
    $oContainer.PutProperty($sName, $vValue)
EndFunc

Func GetProperty($oContainer, $sName)
    Return $oContainer.GetProperty($sName)
EndFunc

$icqlogin = GetProperty($oContainer, &quot;login&quot;)
$icqpass = GetProperty($oContainer, &quot;pass&quot;)
$scriptbot = GetProperty($oContainer, &quot;scriptbot&quot;)

Func Check()
   $uin = GetProperty($oContainer, &quot;uin&quot;)
   $text = GetProperty($oContainer, &quot;text&quot;)
   If ($uin&lt;&gt;0) Then 
	  ICQ_Send_Msg($uin, $text) 
      PutProperty($oContainer, &quot;uin&quot;, &quot;0&quot;)
   EndIf
   
   If GetProperty($oContainer, &quot;quit&quot;)=&quot;1&quot; Then 
	  ICQ_Close() 
	  Exit
   EndIf
EndFunc

if ICQ_Connect(&quot;login.icq.com&quot;, 5190, $icqlogin, $icqpass) = $ICQ_CONNECT_STATUS_OK	Then
	
	While DllStructGetData($ICQ_CLIENT, &quot;status&quot;) = $ICQ_CLIENT_STATUS_CONNECTED
		Sleep(100)
		Check()
		If ICQ_Read_Msg() = 1 Then
			$RecvMsg = DllStructGetData($RecvInfo, &quot;msg&quot;)
			$scriptbot.__OnMessageFireEvent(DllStructGetData($RecvInfo,&quot;uin&quot;), $RecvMsg)
		EndIf
	WEnd
Else
	MsgBox(0, &quot;Error&quot;, &quot;Connection error&quot;)
EndIf</code></pre></div>]]></content>
			<author>
				<name><![CDATA[JSmаn]]></name>
				<uri>https://forum.script-coding.com/profile.php?id=24434</uri>
			</author>
			<updated>2012-07-02T13:47:48Z</updated>
			<id>https://forum.script-coding.com/viewtopic.php?pid=61334#p61334</id>
		</entry>
</feed>
