D Paste by WasserDragoon
Description: None
Hide line numbers

Create new paste
Post a reply
View replies

Paste:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
module main;

import tango.core.Thread;
import tango.io.Stdout;
import tango.net.Socket;
import tango.text.Util;

Socket    sock;
Thread    recv;
char[][]  args;
char[]    botnick = "WasserBot12345";
char[]    server  = "irc.freenode.net";
const int PORT    = 6667;

void receive()
{
    char[2048] buf;
    uint       len;
    
    len = sock.receive(buf);
    
    if (len)
    {
        foreach (char[] b; split(buf[0..len], "\n"))
        {
            char[] msg = trim(b);
            
            if (msg == "")
            {
                continue;
            }
            
            char[][] com;
            char[]   nickfull;
            char[]   nick;
            
            com = split(msg, " ");
            
            if (com[0][0] == ':')
            {
                nickfull = com[0][1..$];
                nick     = split(nickfull, "!")[0];
                com      = com[1..$];
            }
            
            if (com.length >= 2)
            {
                foreach (int i, char[] c; com[1..$])
                {
                    if (c.length > 0 && c[0] == ':')
                    {
                        try
                        {
                            com[i+1] = join(com[i+1..$], " ")[1..$];
                        }
                        catch {}
                        
                        com.length = i+2;
                    }
                }
            }
            
            if (com.length >= 1)
            {
                if (com[0] == "PING" && com.length == 2)
                {
                    sock.send("PONG :" ~ com[1] ~ "\n");
                }
                
                if (com[0] == "PRIVMSG" && com.length == 3)
                {
                    if (com[2] == "satz")
                    {
                        if (com[1] == botnick)
                        {
                            Stdout(" -- only to ")(nick)("\n")("a test 1");
                            sock.send("PRIVMSG " ~ nick ~ " :a test 1\n");
                        }
                        else
                        {
                            Stdout(" -- ")(nick)("\n")("a test 2");
                            sock.send("PRIVMSG #" ~ args[3] ~ " :a test 2\n");
                        }
                    }
                }
                
                if (com[0] == "ERROR" && com.length == 2)
                    Stdout("Got ERROR: ")(com[1]);
                
                if (com[0] == "JOIN" && com.length == 2)
                {
                    if (nick == botnick)
                        sock.send("PRIVMSG #" ~ args[3] ~ " :hi all\n");
                    else
                        sock.send("PRIVMSG #" ~ args[3] ~ " :@" ~ nick ~ " hi " ~ nick ~ "!\n");
                }
            }
        }
    }
}

void main(char[][] _args)
{
    args = _args;
    
    Stdout("connecting to server...");
    try
    {
        sock = new Socket(AddressFamily.INET, SocketType.STREAM, ProtocolType.TCP);
        sock.connect(new IPv4Address(server, PORT));
        Stdout("ok").newline;
    }
    catch
    {
        Stdout("failed").newline;
    }
    
    recv = new Thread(
        delegate void ()
        {
            while (true)
                receive();
        }
    );
    
    recv.start();
    
    Stdout("setting nick to: ")(botnick)("...");
    sock.send("NICK " ~ botnick ~ "\n");
    Stdout("ok").newline;
    
    Stdout("joining to channel...");
    sock.send("JOIN #d.tango\n");
    Stdout("ok").newline;
    
    // phobos: recv.wait();
    recv.join();
}

Replies:

    (some replies deleted)