Register | Login
Forum Index > Samples > NMDC Bot
Author Message
Pages: 1
Admin
Site Admin

avatar

(send private message)

Posts: 933
Topics: 55

Location:
OverHertz Studio
[873] NMDC Bot - posted: 2011-11-27 15:09:03
i've put together a quick DC bot in around 116 lines (include white spaced new lines but not including includes)

Code:
//
// 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 smile

Download Ziron
Get free hosting for Ziron related fan-sites and Ziron projects, contact me in private message.
Molotov
Member
(send private message)

Posts: 49
Topics: 21

Location:
Behind Keyboard
[874] - posted: 2011-11-27 15:57:59
nice bot,,

got a quick q though

wouldn´t

Code:
 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?

Code:
 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
Ziron Beta Tester

avatar

(send private message)

Posts: 639
Topics: 104

Location:
Alex, Egypt
[876] - posted: 2011-11-27 17:45:16
i think that will work if you are sure that ziron will treats eax as singed integer in this
expression
Code:
 if (eax < 1) 



http://www.freewebs.com/ogremagic/index.htm
Molotov
Member
(send private message)

Posts: 49
Topics: 21

Location:
Behind Keyboard
[878] - posted: 2011-11-27 18:19:45
hence posted it as a question.. wanted to know why he didn´t used it. since i just started coding zir/asm. smile

"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
Admin
Site Admin

avatar

(send private message)

Posts: 933
Topics: 55

Location:
OverHertz Studio
[879] - posted: 2011-11-27 18:45:03
yes and no.. it is possible to make ziron treat it as signed via a typecast

Code:
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 smile


Download Ziron
Get free hosting for Ziron related fan-sites and Ziron projects, contact me in private message.
Admin
Site Admin

avatar

(send private message)

Posts: 933
Topics: 55

Location:
OverHertz Studio
[902] - posted: 2011-11-29 15:47:17
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 smile

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)

Code:
//
// 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. smile

Download Ziron
Get free hosting for Ziron related fan-sites and Ziron projects, contact me in private message.
Pages: 1
create new reply


Quick reply:

Message:



Currently Active Users:
There are currently 6 user(s) online. 0 member(s) and 6 guest(s)
Most users ever online was 1046, January 28, 2022, 2:08 pm.


Statistics:
Threads: 225 | Posts: 1848 | Members: 51 | Active Members: 51
Welcome to our newest member, yecate
const Copyright = '2011-2024 © OverHertz Ltd. All rights reserved.';
Web development by OverHertz Ltd