summaryrefslogtreecommitdiff
path: root/Applications/IRCClient/IRCClient.h
blob: 12f4fbe5ac7f0a3ff1ec3181deb610ff16977784 (plain)
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
#pragma once

#include <AK/AKString.h>
#include <AK/HashMap.h>
#include <AK/CircularQueue.h>
#include <AK/Function.h>

class IRCChannel;
class IRCQuery;
class GNotifier;

class IRCClient {
public:
    IRCClient(const String& address, int port = 6667);
    ~IRCClient();

    bool connect();

    String hostname() const { return m_hostname; }
    int port() const { return m_port; }

    String nickname() const { return m_nickname; }

    void join_channel(const String&);

    bool is_nick_prefix(char) const;

    Function<void()> on_connect;
    Function<void()> on_disconnect;
    Function<void(const String& channel)> on_channel_message;
    Function<void(const String& name)> on_query_message;
    Function<void()> on_server_message;

private:
    struct Message {
        String prefix;
        String command;
        Vector<String> arguments;
    };

    void receive_from_server();
    void send(const String&);
    void send_user();
    void send_nick();
    void send_pong(const String& server);
    void process_line();
    void handle_join(const Message&);
    void handle_ping(const Message&);
    void handle_namreply(const Message&);
    void handle_privmsg(const Message&);
    void handle(const Message&);
    IRCQuery& ensure_query(const String& name);

    String m_hostname;
    int m_port { 0 };
    int m_socket_fd { -1 };

    String m_nickname;
    Vector<char> m_line_buffer;
    OwnPtr<GNotifier> m_notifier;
    HashMap<String, RetainPtr<IRCChannel>> m_channels;
    HashMap<String, RetainPtr<IRCQuery>> m_queries;

    CircularQueue<String, 1024> m_server_messages;
};