Skip to content
Low-level programming · Higher-level thinking
Login Register
Forum / Samples

NMDC Bot

A community discussion post from the original code:zIRON forum.

Colin admin
Posts
928
Threads
49
Location
Bulgaria

NMDC Bot

i've put together a quick DC bot in around 116 lines (include white spaced new lines but not including includes)

//
// Copyright (c) OverHertz Ltd.
// Permission is granted to modify and use this code with your projects.
// Compile Compatibility. Ziron 1.1.28.1+
//

program WIN32CUI 'DC-Bot';

#include 'ch.zir';
#include 'wsock32.zir';
#include 'dclib/dcutils.zir';

//consts (settings)
const BOTNAME = 'ZIRON';
const DC_TAG = '<ApexDC++ V:1.5.1,M:A,H:0/1/7,S:1>';
const HUBIP = '127.0.0.1';
const HUBPORT = 411;


////////////////////////////////////////////////////////

//useful macros
inline function strCompare(s1, s2) {
	strCmp($s1, $s2);
	$return eax;
}

//globals
WSADATA wsa_Data;
WSOCKET sock;
SOCKADDR_IN sin;

char buf1[2048], buf2[1024];


//code
sin.sin_addr = inet_addr(HUBIP);
sin.sin_family = AF_INET;
sin.sin_port = htons(HUBPORT);

WSAStartup(WSOCKVER2_2, @wsa_Data);
sock = socket(AF_INET, SOCK_STREAM, 0);

connect(sock, @sin, sizeof SOCKADDR_IN);

repeat {
	buf1 = 0;
	
	eax = 2048;
	edi = @buf1;
	
	@fill:
	
	//begin receiving
	eax = recv(sock, edi, eax, 0);
	if (eax == -1) {
		print('Connection closed\r\n');
		break;
	} elseif (eax == 0) {
		print('Connection closed\r\n');
		break;
	}
	char[edi+eax] = 0;
	edi = @buf1;
	
	eax = strLen(@buf1);
	
	dl = [edi+eax-1];
	if (dl <> "|") {
		edi += eax;	

		edx = 2048;
		edx -= eax;
		eax = edx;
		
		goto @fill;
	}	
	
	if (char*[edi] == "$Loc" and word[edi+4] == "k ") {
		edi += 6;
	
		eax = strOffset(edi, '|');
		[eax] = 0;
	
		eax = DC_LockToKey(@buf1, @buf2);
		push eax
		send(sock, '$Supports UserCommand UserIP2 TTHSearch ZPipe0 GetZBlock |', sizeof '$Supports UserCommand UserIP2 TTHSearch ZPipe0 GetZBlock |', 0);	
		pop eax
		send(sock, @buf2, eax, 0);
		send(sock, '$ValidateNick '+BOTNAME+'|', sizeof '$ValidateNick '+BOTNAME+'|', 0);
	} elseif (char*[edi] == "$Sup" and char*[edi+4] == "port" and word[edi+8] == "s ") {
		send(sock, '$Version 1,0091|$GetNickList|$MyINFO $ALL '+BOTNAME+' '+DC_TAG+'$ $100?$$0$|', sizeof '$Version 1,0091|$GetNickList|$MyINFO $ALL '+BOTNAME+' '+DC_TAG+'$ $100?$$0$|', 0);
	} elseif (char[edi] == "<") {
		print(edi:string, '\r\n');
		
		//chat message
		edi = strOffset(@buf1, '>');
		edi += 2;
		
		if (char*[edi] == "zir ") {
			if (char*[edi+4] == "say ") {
				eax = strSub(@buf2, edi, 8, 1024);
				push eax
				send(sock, '<'+BOTNAME+'> ', sizeof '<'+BOTNAME+'> ', 0);
				pop eax
				send(sock, @buf2, eax, 0);
			} elseif (char*[edi+4] == "quit") {
				send(sock, '<'+BOTNAME+'> Goodbyte!|', sizeof '<'+BOTNAME+'> Goodbyte!|', 0);
				break;
			}
		}
	}
};

closesocket(sock);
WSACleanup();

wait_key(nil);
ExitProcess(0);

The code was written very quickly, is not optimized, but is good as a starter code, or even for learning a little...Settings are at the top, including ip of the server and port, the bot will connect and has 2 valid commands.

"zir say text here" - will send "text here" chat line.

and

"zir quit" - will quit from the server and prepare to close.

let me know what you think, and if you make any improvements or make any bots using this code, please post and let us know :)

Replies

Molotov member
Posts
49
Threads
21
Location
Behind Keyboard

nice bot,,

got a quick q though

wouldn´t

 eax = recv(sock, edi, eax, 0);
  if (eax == -1) {
    print('Connection closed\r\n');
    break;
  } elseif (eax == 0) {
    print('Connection closed\r\n');
    break;
  }

be better as?

 eax = recv(sock, edi, eax, 0);
  if (eax < 1) {
    print('Connection closed\r\n');
    break;
  }

since you have the same action in both brackets

"Lerning new things is what life is all about, if you are always doing the same old things day in and day out you might aswell just kill yourself." - ME d^_^b

Emil_halim member
Posts
639
Threads
104
Location
Alex, Egypt

i think that will work if you are sure that ziron will treats eax as singed integer in this

expression

 if (eax < 1) 

http://www.freewebs.com/ogremagic/index.htm

Molotov member
Posts
49
Threads
21
Location
Behind Keyboard

hence posted it as a question.. wanted to know why he didn´t used it. since i just started coding zir/asm. :)

"Lerning new things is what life is all about, if you are always doing the same old things day in and day out you might aswell just kill yourself." - ME d^_^b

Colin admin
Posts
928
Threads
49
Location
Bulgaria

yes and no.. it is possible to make ziron treat it as signed via a typecast

if ((int32)eax < 1) {

however the idea was to have different error codes at some point... recv returning 0 = gracefully closed.. if -1 then there was an error get last error and print something... but that is for later :)

Colin admin
Posts
928
Threads
49
Location
Bulgaria

I decided to have a play around with this and try make it easier for others to understand, also i've made it process lines in order... since the previous version expected data sent in a specific order, this version is a little better and makes use of some functions to help keep things simple.

It will also print the unknown commands in red, i did not re-add the commands in this version, that is your task :)

You require the latest beta or newer of (Ziron 1.1.28.1) for this update with latest updated includes. (Which i have updated at usual beta download link)

//
// Copyright (c) OverHertz Ltd.
// Permission is granted to modify and use this code with your projects.
// Compile Compatibility. Ziron 1.1.28.1+
//

program WIN32CUI 'DC-Bot';

#include 'ch.zir';
#include 'wsock32.zir';
#include 'dclib/dcutils.zir';
#include 'colors.zir';

//consts (settings)
const BOTNAME = 'ZIRON';
const DC_TAG = '<ApexDC++ V:1.5.1,M:A,H:0/1/7,S:1>';
const HUBIP = '127.0.0.1';
const HUBPORT = 411;
const BUFFERSIZE = 2048;

////////////////////////////////////////////////////////


//useful macros
inline function strCompare(s1, s2) {
	strCmp($s1, $s2);
	$return eax;
}

//globals
WSADATA wsa_Data;
SOCKADDR_IN sin;
WSOCKET sock;

char buf1[BUFFERSIZE], buf2[1024];

function InitClient() {
	eax = WSAStartup(WSOCKVER2_2, @wsa_Data);
	if (eax != 0) {
		print('wsastartup failed\r\n');
		return false;
	}

	//code
	sin.sin_addr = inet_addr(HUBIP);
	sin.sin_family = AF_INET;
	sin.sin_port = htons(HUBPORT);

	sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	if (sock == INVALID_SOCKET) {
		print('invalid socket\r\n');
		return false;
	}

	eax = connect(sock, @sin, sizeof SOCKADDR_IN);
	if (eax == SOCKET_ERROR) {
		eax = WSAGetLastError();
	
		print('socket connect error (',eax:int,')\r\n');
		return false;
	}
	
	eax = true;
}

function ReceiveDCData(DWord ofs) {
	uses esi edi;
	esi = @buf1;
	add esi, ofs
	
	edi = BUFFERSIZE;
	repeat {
		if (edi < 1) {
			//data is too long we will scrap the current buffer and try begin receiving again.
			esi = @buf1;
			edi = BUFFERSIZE;
		}
		eax = recv(sock, esi, edi, 0);
	
		if (eax == -1) {
			print('socket error\r\n');
			return false;
		} elseif (eax == 0) {
			print('socket gracefully closed\r\n');
			return false;
		}
		char[esi+eax] = 0;
		esi += eax;
		edi -= eax;
		
		eax = strIndexOf(@buf1, '|');
	} until (eax != -1);
	
	esi = @buf1;
	//seal the buffer and return the offset
	char[esi+eax] = 0;
	
	eax++;
}

procedure EchoMessage(char* msg; DWord col) {
	SetConsoleTextAttribute(hStdOut, col);
	print(msg, '\r\n');
}

function ProcessBuffer(DWord len) {
	uses esi;	
	esi = @buf1;
	
	repeat {
	
		//process the line....	
		if (char*[esi] == "$Loc" and word[esi+4] == "k ") {		
			send(sock, '$Supports UserCommand UserIP2 TTHSearch ZPipe0 GetZBlock |', sizeof '$Supports UserCommand UserIP2 TTHSearch ZPipe0 GetZBlock |', 0);			
			eax = DC_LockToKey(esi, @buf2);
			send(sock, @buf2, eax, 0);
			send(sock, '$ValidateNick '+BOTNAME+'|', sizeof '$ValidateNick '+BOTNAME+'|', 0);
			
		} elseif (char*[esi] == "$Sup" and char*[esi+4] == "port" and word[esi+8] == "s ") {
			eax = send(sock, '$Version 1,0091|$GetNickList|$MyINFO $ALL '+BOTNAME+' '+DC_TAG+'$ $100?$$0$|', sizeof '$Version 1,0091|$GetNickList|$MyINFO $ALL '+BOTNAME+' '+DC_TAG+'$ $100?$$0$|', 0);			
			
		} elseif (char*[esi] == "$Hub" and char*[esi+4] == "Name" and char[esi+8] == " ") {
			lea eax, [esi+9];			
			SetConsoleTitle(eax);
			
		} elseif (char[esi] == "$") {
			EchoMessage(esi:string, FOREGROUND_RED);
			
		} else {
			//text message
			EchoMessage(esi:string, CONSOLE_DEFAULT);
		}
						
		//
		
		//fold the string
		eax = strFold(esi, len);
		eax = strIndexOf(esi, '|');
		
		if (eax == -1) {
			break;
		}
		
		//seal the |
		char[esi+eax] = 0;
		eax++;
		
		//update length
		len = eax;
	};
	
	//return what is left of the buffer.
	eax = strLen(@buf1);
}


eax = InitClient();

if (eax == true) {
	eax = xor;
	repeat {
		eax = ReceiveDCData(eax);
		if (eax == false) {
			break;
		}
	
		eax = ProcessBuffer(eax);
	};
	
	shutdown(sock, SD_BOTH);	
}
closesocket(sock);
WSACleanup();

wait_key(nil);
ExitProcess(0);

let me know your thoughts. :)