Thread Rating:
  • 2 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
First C++ Program in a long time
#1
Code:
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#pragma comment(lib, "ws2_32.lib");

using namespace std;

typedef struct
{
    string Nick;
    string Host;
    string Message;
} IRC_Privmsg;

string ranstring(int len)
{
    string ListNew;
    for(int i = 0; i < len; i++)
    {
        int select = rand() % 2;
        char Value = (select ? (char)(rand() % 26 + 65):(char)(rand() % 10 + 48));
        ListNew.append(1, Value);
    }
    return ListNew;
}

int IRC_Send(SOCKET s, string buf)
{
    send(s, buf.c_str(), strlen(buf.c_str()), 0);
}

IRC_Privmsg privmsg_parse(string message)
{
    int pos = 1;
    IRC_Privmsg IRC_Parsed;
    for(int x = pos; x < message.length(); x++)
    {
        if(message[x]!='!')
        {
            IRC_Parsed.Nick.append(1, message[x]);
        }
        else
        {
            pos = x + 1;
            break;
        }
    }
    for(int x = pos; x < message.length(); x++)
    {
        if(message[x]!=' ')
        {
            IRC_Parsed.Host.append(1, message[x]);
        }
        else
        {
            pos = x;
            break;
        }
    }
    for(int x = pos ;x < message.length(); x++)
    {
        if(message[x]==':')
        {
            pos = x + 1;
            break;
        }
    }
    for(int x = pos; x < message.length(); x++)
    {
        IRC_Parsed.Message.append(1, message[x]);
    }
    return IRC_Parsed;
}

void IRC_Connect(string host, int port, string nick)
{
        SOCKET mySocket;
        WSAData wsaData;
        struct hostent *hoste;
        struct sockaddr_in in;
        int s;
        WSAStartup(MAKEWORD(2,2), &wsaData);
        s = socket(AF_INET, SOCK_STREAM, 0);
        hoste = gethostbyname(host.c_str());
        if (hoste == NULL)
        {
            IRC_Connect(host, port, nick);
        }

        in.sin_family = AF_INET;
        in.sin_port = htons((unsigned short)port);
        in.sin_addr.s_addr = *(unsigned long *) hoste->h_addr;
        if (connect(s, (struct sockaddr *) &in, sizeof(struct sockaddr_in)) == SOCKET_ERROR)
        {
            IRC_Connect(host, port, nick);
        }
        char recvBuf[4096];
        int byteRecv;
        int byteSent;
        cout << "Connected....\nSending headers...\n\n";
        IRC_Send(s, "USER "+nick+" 8 * :X\r\n");
        IRC_Send(s, "NICK "+nick+"\r\n");
        IRC_Send(s, "JOIN #botland\r\n");
        IRC_Privmsg PrivmsgHandler;
        while(byteRecv != SOCKET_ERROR)
        {
            memset(recvBuf, '\0', sizeof(recvBuf));
            byteRecv = recv(s, recvBuf, 4096, 0);
            if(byteRecv == 0 || byteRecv == WSAECONNRESET)
            {
                cout << "Conection Reset by host!\n";
                IRC_Connect(host, port, nick);
            }
            if(string(recvBuf).find("PING") != string::npos)
            {
                cout << "ping";
                IRC_Send(s, "PONG "+string(recvBuf).substr(20,9) + "\r\n");
            }
            if(string(recvBuf).find("PRIVMSG") != string::npos)
            {
                PrivmsgHandler = privmsg_parse(recvBuf);
                cout << PrivmsgHandler.Nick << endl;
                cout << PrivmsgHandler.Host << endl;
                cout << PrivmsgHandler.Message << endl;
            }
            cout << recvBuf <<endl;
        }
}

int main()
{
         string host = "irc.Malvager.com";
         int port = 6667;
         string ran = ranstring(7);
         string nick = "Bot|"+ran;
         IRC_Connect(host, port, nick);
}

Basic IRC type bot, uses a struct to handle/parse PRIVMSG's

includes a simple random string generation function for the nick

Auto reconnects, etc.
[Image: nv70ad.png]
Terrorcore, unleash, extermination
Hyper real, cold blood, determination
fudge them, I like this sensation
Incredible, I from the annihilation
Reply
#2
Nice work Fallen.
You going to continue laying off the C++ or start coding alot more?
Reply
#3
Glad to see you didn't give up, as daunting as C++ seemed at first. Good job, Fallen.
<Aoi-chan> everyone's first vi session. ^C^C^X^X^X^XquitqQ!qdammit[esc]qwertyuiopasdfghjkl;:xwhat
Reply
#4
Code:
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#pragma comment(lib, "ws2_32.lib");

using namespace std;

typedef struct
{
    string Nick;
    string Host;
    string Message;
} IRC_Privmsg;

string ranstring(int len)
{
    string ListNew;
    for(int i = 0; i < len; i++)
    {
        int select = rand() % 2;
        char Value = (select ? (char)(rand() % 26 + 65):(char)(rand() % 10 + 48));
        ListNew.append(1, Value);
    }
    return ListNew;
}

int IRC_Send(SOCKET s, string buf)
{
    send(s, buf.c_str(), strlen(buf.c_str()), 0);
}

IRC_Privmsg privmsg_parse(string message)
{
    int pos = 1;
    IRC_Privmsg IRC_Parsed;
    for(int x = pos; x < message.length(); x++)
    {
        if(message[x]!='!')
        {
            IRC_Parsed.Nick.append(1, message[x]);
        }
        else
        {
            pos = x + 1;
            break;
        }
    }
    for(int x = pos; x < message.length(); x++)
    {
        if(message[x]!=' ')
        {
            IRC_Parsed.Host.append(1, message[x]);
        }
        else
        {
            pos = x;
            break;
        }
    }
    for(int x = pos ;x < message.length(); x++)
    {
        if(message[x]==':')
        {
            pos = x + 1;
            break;
        }
    }
    for(int x = pos; x < message.length(); x++)
    {
        IRC_Parsed.Message.append(1, message[x]);
    }
    return IRC_Parsed;
}

void IRC_Connect(string host, int port, string nick)
{
        SOCKET mySocket;
        WSAData wsaData;
        struct hostent *hoste;
        struct sockaddr_in in;
        int s;
        WSAStartup(MAKEWORD(2,2), &wsaData);
        s = socket(AF_INET, SOCK_STREAM, 0);
        hoste = gethostbyname(host.c_str());
        if (hoste == NULL)
        {
            IRC_Connect(host, port, nick);
        }

        in.sin_family = AF_INET;
        in.sin_port = htons((unsigned short)port);
        in.sin_addr.s_addr = *(unsigned long *) hoste->h_addr;
        if (connect(s, (struct sockaddr *) &in, sizeof(struct sockaddr_in)) == SOCKET_ERROR)
        {
            IRC_Connect(host, port, nick);
        }
        char recvBuf[4096];
        int byteRecv;
        int byteSent;
        cout << "Connected....\nSending headers...\n\n";
        IRC_Send(s, "USER "+nick+" 8 * :X\r\n");
        IRC_Send(s, "NICK "+nick+"\r\n");
        IRC_Send(s, "JOIN #botland\r\n");
        IRC_Privmsg PrivmsgHandler;
        while(byteRecv != SOCKET_ERROR)
        {
            memset(recvBuf, '\0', sizeof(recvBuf));
            byteRecv = recv(s, recvBuf, 4096, 0);
            if(byteRecv == 0 || byteRecv == WSAECONNRESET)
            {
                cout << "Conection Reset by host!\n";
                IRC_Connect(host, port, nick);
            }
            if(string(recvBuf).find("PING") != string::npos)
            {
                cout << "ping";
                IRC_Send(s, "PONG "+string(recvBuf).substr(20,9) + "\r\n");
            }
            if(string(recvBuf).find("PRIVMSG") != string::npos)
            {
                PrivmsgHandler = privmsg_parse(recvBuf);
                cout << PrivmsgHandler.Nick << endl;
                cout << PrivmsgHandler.Host << endl;
                cout << PrivmsgHandler.Message << endl;
            }
            cout << recvBuf <<endl;
        }
}

int main()
{
         string host = "irc.Malvager.com";
         int port = 6667;
         string ran = ranstring(7);
         string nick = "Bot|"+ran;
         IRC_Connect(host, port, nick);
}

You pretty much copied this
http://www.rohitab.com/discuss/index.php...opic=22120

Just changed the variables and changed the sprintfs to C++
you should have written your own from scratch, its not that hard
Reply
#5
(04-09-2010, 05:14 PM)Moudi Wrote:
Code:
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#pragma comment(lib, "ws2_32.lib");

using namespace std;

typedef struct
{
    string Nick;
    string Host;
    string Message;
} IRC_Privmsg;

string ranstring(int len)
{
    string ListNew;
    for(int i = 0; i < len; i++)
    {
        int select = rand() % 2;
        char Value = (select ? (char)(rand() % 26 + 65):(char)(rand() % 10 + 48));
        ListNew.append(1, Value);
    }
    return ListNew;
}

int IRC_Send(SOCKET s, string buf)
{
    send(s, buf.c_str(), strlen(buf.c_str()), 0);
}

IRC_Privmsg privmsg_parse(string message)
{
    int pos = 1;
    IRC_Privmsg IRC_Parsed;
    for(int x = pos; x < message.length(); x++)
    {
        if(message[x]!='!')
        {
            IRC_Parsed.Nick.append(1, message[x]);
        }
        else
        {
            pos = x + 1;
            break;
        }
    }
    for(int x = pos; x < message.length(); x++)
    {
        if(message[x]!=' ')
        {
            IRC_Parsed.Host.append(1, message[x]);
        }
        else
        {
            pos = x;
            break;
        }
    }
    for(int x = pos ;x < message.length(); x++)
    {
        if(message[x]==':')
        {
            pos = x + 1;
            break;
        }
    }
    for(int x = pos; x < message.length(); x++)
    {
        IRC_Parsed.Message.append(1, message[x]);
    }
    return IRC_Parsed;
}

void IRC_Connect(string host, int port, string nick)
{
        SOCKET mySocket;
        WSAData wsaData;
        struct hostent *hoste;
        struct sockaddr_in in;
        int s;
        WSAStartup(MAKEWORD(2,2), &wsaData);
        s = socket(AF_INET, SOCK_STREAM, 0);
        hoste = gethostbyname(host.c_str());
        if (hoste == NULL)
        {
            IRC_Connect(host, port, nick);
        }

        in.sin_family = AF_INET;
        in.sin_port = htons((unsigned short)port);
        in.sin_addr.s_addr = *(unsigned long *) hoste->h_addr;
        if (connect(s, (struct sockaddr *) &in, sizeof(struct sockaddr_in)) == SOCKET_ERROR)
        {
            IRC_Connect(host, port, nick);
        }
        char recvBuf[4096];
        int byteRecv;
        int byteSent;
        cout << "Connected....\nSending headers...\n\n";
        IRC_Send(s, "USER "+nick+" 8 * :X\r\n");
        IRC_Send(s, "NICK "+nick+"\r\n");
        IRC_Send(s, "JOIN #botland\r\n");
        IRC_Privmsg PrivmsgHandler;
        while(byteRecv != SOCKET_ERROR)
        {
            memset(recvBuf, '\0', sizeof(recvBuf));
            byteRecv = recv(s, recvBuf, 4096, 0);
            if(byteRecv == 0 || byteRecv == WSAECONNRESET)
            {
                cout << "Conection Reset by host!\n";
                IRC_Connect(host, port, nick);
            }
            if(string(recvBuf).find("PING") != string::npos)
            {
                cout << "ping";
                IRC_Send(s, "PONG "+string(recvBuf).substr(20,9) + "\r\n");
            }
            if(string(recvBuf).find("PRIVMSG") != string::npos)
            {
                PrivmsgHandler = privmsg_parse(recvBuf);
                cout << PrivmsgHandler.Nick << endl;
                cout << PrivmsgHandler.Host << endl;
                cout << PrivmsgHandler.Message << endl;
            }
            cout << recvBuf <<endl;
        }
}

int main()
{
         string host = "irc.Malvager.com";
         int port = 6667;
         string ran = ranstring(7);
         string nick = "Bot|"+ran;
         IRC_Connect(host, port, nick);
}

You pretty much copied this
http://www.rohitab.com/discuss/index.php...opic=22120

Just changed the variables and changed the sprintfs to C++
you should have written your own from scratch, its not that hard

Yeah the socket part was mostly grabbed from another post, however you can ask p4bz, Kheldar, or Psycho Tongue We were fighting with socket related errors all night. God it was a mess lol
All the structs, etc are 100% mine. Almost every C/++ bot uses structs, its not that uncommon lol. I actually used another post than that one for the socket stuff actually.

Also, the privmsg_parse function, ranstring, etc are all mine (p4bz fixed the error in the one) so I dont see where you are going.

Actually, now that I look at the link you posted, ours are nothing alike really. You are either just butthurt over something, or a as shole. Either way, you are wrong.
[Image: nv70ad.png]
Terrorcore, unleash, extermination
Hyper real, cold blood, determination
fudge them, I like this sensation
Incredible, I from the annihilation
Reply
#6
(04-09-2010, 05:14 PM)Moudi Wrote: You pretty much copied this
http://www.rohitab.com/discuss/index.php...opic=22120

Just changed the variables and changed the sprintfs to C++
you should have written your own from scratch, its not that hard

No, what are you talking about? Have you ever written an IRC bot before? Have you ever done socket programming? You do realize it's a generic process don't you? Everyone's IRC bots are similar. They all connect in the same way, there's not that many ways to use a socket. You don't know what you're talking about. Even simple IRC bots have a generic process...you connect and loop indefinitely responding to pings from the server. The only similarity is the general idea. You could say the same thing to everyone who wrote their own IRC bot. I was there with p4bz, Kheldar and Fallen in IRC while Fallen worked on this...I would know too. This is like accusing someone of plagiarism for not using alternate words for "the," "and," "a," or "they" because someone else used them before. It's ridiculous that you have to try to drag someone's hard work down for such a stupid reason. Honestly, if I could give you a negative reputation point, I probably would, because you probably deserve it.


Moudi Wrote:Just changed the variables and changed the sprintfs to C++

lol?
<Aoi-chan> everyone's first vi session. ^C^C^X^X^X^XquitqQ!qdammit[esc]qwertyuiopasdfghjkl;:xwhat
Reply
#7
Some nice code you posted there Fallen. Will going to analyze it, because I'm learning C++ and just past the basic stuff, like conditions, loops, arrays, functions, ...

#DB
Reply
#8
Very nice share dude...thank's Oui
Reply
#9
Nice code Fallen.You rock!
Reply
#10
Good job, Nice share, Tnx Smile
[Image: ShuTdown.png]
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)