[C++] IRCBot unter Windows kompilieren?

byte_head

Captain
Registriert
Jan. 2005
Beiträge
3.674
Hallo,
ein Freund von mir hat einen IRCBot gecodet, welcher sich zwar unter Linux kompilieren lässt, jedoch unter Windows nicht, das Connect Grundgerüst für Windows hat er von einem Bekannten welcher nun jedoch 2 Wochen im Urlaub ist, aber der meinte, dass es so ohne probleme unter Windows lief, da weder ich noch mein Freund von Sockets unter Windows Ahnung haben würde ich euch bitten uns zu helfen und uns zu erklären wie wir diesen Source auch unter Windows kompilieren können, unter Linux läuft der Bot nämlich 1a.

Code:
// RFC zu IRC: http://www.ietf.org/rfc/rfc1459.txt

#include <iostream>    /* std::cout, std::cerr, std::endl */
#include <string>    /* std::string */
#include <cstdlib>    /* exit */
#include <cstdio>    /* perror */
#include <cstring>    /* memcpy, memset */
#include <fstream>    /*ofstream, ifstream */

#ifdef WIN32
#include <winsock2.h>
#else
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#endif

using namespace std;

const unsigned int MAX_LINE = 1024; // Groesse des Empfangspuffers

    const int PORT = 6667;
    const char *HOST = "your.server.net";
    string chan = "#channel";
    string nick = "Bot";
    string pass = "password";
    string message = "Hello!"; 
    string quit="Leaving.";

#ifdef WIN32
SOCKET sockfd;
#else
int sockfd;
#endif

void s2u(const char *msg) { //send to uplink
    send(sockfd, msg, strlen(msg), 0);
}

void irc_disconnect() {
#ifdef WIN32
    // Windows-Socket freigeben
    closesocket(sockfd);
    WSACleanup();
#else

    close(sockfd);
#endif
}



void irc_connect() {
#ifdef WIN32
    // Windows-Socket initialisieren
    WSADATA wsa;
    if(WSAStartup(MAKEWORD(2,0),&wsa)!=0)
        exit(1);
#endif


    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if(static_cast<int>(sockfd) < 0) {
        perror("socket()");
        irc_disconnect();
        exit(1);
    }

    hostent *hp = gethostbyname(HOST);
    if(!hp) {
        cerr << "gethostbyname()" << endl;
        irc_disconnect();
        exit(1);
    }

    sockaddr_in sin;
    memset((char*)&sin, 0, sizeof(sin));
    sin.sin_family = AF_INET;
    memcpy((char*)&sin.sin_addr, hp->h_addr, hp->h_length);
    sin.sin_port = htons(PORT);
    memset(&(sin.sin_zero), 0, 8*sizeof(char));

    if(connect(sockfd, (sockaddr*) &sin, sizeof(sin))==-1) {
        perror("connect()");
        irc_disconnect();
        exit(1);
    }
}

void irc_identify() {
    s2u(("NICK "+nick+"\r\n").c_str());                            // Nickname
    s2u("USER Bot 0 0 :Bot\r\n");                                // Userdaten
    s2u(("PRIVMSG NickServ IDENTIFY "+pass+"\r\n").c_str());                // Identifizieren
    s2u(("JOIN "+chan+"\r\n").c_str());                            // Channel betreten
    s2u(("PRIVMSG "+chan+" :"+message+"\r\n").c_str());                    // Begruessungsnachricht
}

void ping_parse(const string &buffer) {
    size_t pingPos = buffer.find("PING");
    if(pingPos!=string::npos) {
        string pong("PONG"+buffer.substr(pingPos+4)+"\r\n");
        cout << pong;
        s2u(pong.c_str());
    }
}

void bot_functions(const string &buffer) {
    size_t pos = 0;
    if((pos=buffer.find(":say "))!=string::npos) {
        s2u(("PRIVMSG "+chan+" :"+buffer.substr(pos+5)+"\r\n").c_str());
    }
    //else if(buffer.find(":"+owner+"!")==0 && buffer.find(":exit")!=string::npos){     
    else if((pos=buffer.find(":botexit"))!=string::npos){
        s2u(("PRIVMSG "+chan+" :Cya\r\n").c_str());
        s2u(("QUIT :"+quit+"\r\n").c_str());
        irc_disconnect();
        exit(0);
    }
    else if((pos=buffer.find(":me "))!=string::npos){
        s2u(("PRIVMSG "+chan+" :\001ACTION "+buffer.substr(pos+4)+"\001\r\n").c_str());
    }
    else if((pos=buffer.find(":wiki "))!=string::npos){
        s2u(("PRIVMSG "+chan+" :http://de.wikipedia.org/wiki/Spezial:Suche?search="+buffer.substr(pos+6)+"&go=Artikel\r\n").c_str());
    }
    else if((pos=buffer.find(":help"))!=string::npos){
        s2u(("PRIVMSG "+chan+" :IRCBot\r\n").c_str());
        s2u(("PRIVMSG "+chan+" :Commands:\r\n").c_str());
        s2u(("PRIVMSG "+chan+" ::say Text\r\n").c_str());
        s2u(("PRIVMSG "+chan+" ::wiki Text\r\n").c_str());
        s2u(("PRIVMSG "+chan+" ::me Text\r\n").c_str());
    }
}

void irc_parse(string buffer) {
    if(buffer.find("\r\n") == buffer.length()-2)
        buffer.erase(buffer.length()-2);
    ping_parse(buffer);
    bot_functions(buffer);
}

void settings()
{
    ofstream ofs;
    ifstream ifs;
    int mls=80;
    char hst[mls],chn[mls],bnm[mls],bpw[mls],msg[mls],qut[mls];
    cout<<"Server: ";
    cin>>hst;
    cout<<"Channel: ";
    cin>>chn;
    cout<<"Botname: ";
    cin>>bnm;
    cout<<"Bot Nickserv Password: ";
    cin>>bpw;
    cout<<"Join Message: ";
    cin>>msg;
    cout<<"Quit Message: ";
    cin>>qut;    

    ofs.open("settings");
    ofs<<hst<<endl;
    ofs<<chn<<endl;
    ofs<<bnm<<endl;
    ofs<<bpw<<endl;
    ofs<<msg<<endl;
    ofs.close();

}    

int main(int argc,char* argv[]) {

    ifstream ifs;
    ifs.open("settings");
    if(ifs.fail()||argc==2)
    {
         ifs.close();
        settings();
        ifs.open("settings");
    }

    int mls=80;
    char hst[mls],chn[mls],bnm[mls],bpw[mls],msg[mls],qut[mls];
    ifs.getline(hst,mls);
    ifs.getline(chn,mls);
    ifs.getline(bnm,mls);
    ifs.getline(bpw,mls);
    ifs.getline(msg,mls);
    ifs.getline(qut,mls);
    ifs.close();

    HOST=hst;

    chan=chn;
    nick=bnm;
    pass=bpw;
    message=msg;
    quit=qut;


    irc_connect();
    irc_identify();
    for(;;) {
        char buffer[MAX_LINE+1] = {0};
        if(recv(sockfd, buffer, MAX_LINE*sizeof(char), 0)<0) {
            perror("recv()");
            irc_disconnect();
            exit(1);
        }
        cout << buffer;
        irc_parse(buffer);
    }
    irc_disconnect();
}





grüße
byte_head
 
Hat sich erledigt, man kann ihn mit Visual Studio 2005 Pro und dem Plattform Sdk kompilieren.*sich extra die 2,56 Gb große 90 Tage Trial ziehn musste -.-*
Warum das nicht mit der Express ging ist ziemlich verwunderlich.
 
Das ist schwachsinn.

Es hätte auch MinGW getan, und es ist nicht Gigabyte groß: g++ -o ircbot.exe ircbot.cpp -lwsock32 -lw2_32 -Wall
 
Zurück
Oben