summaryrefslogtreecommitdiff
path: root/Applications/IRCClient/IRCClient.h
blob: 02891a09ffaf7442c63b77dc491cb1e5a68f8e20 (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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#pragma once

#include "IRCLogBuffer.h"
#include "IRCWindow.h"
#include <AK/AKString.h>
#include <AK/CircularQueue.h>
#include <AK/Function.h>
#include <AK/HashMap.h>
#include <LibCore/CConfigFile.h>
#include <LibCore/CTCPSocket.h>

class IRCChannel;
class IRCQuery;
class IRCWindowListModel;
class CNotifier;

class IRCClient final : public CObject {
    C_OBJECT(IRCClient)
    friend class IRCChannel;
    friend class IRCQuery;
public:
    IRCClient();
    virtual ~IRCClient() override;

    void set_server(const String& hostname, int port = 6667);

    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&);
    void part_channel(const String&);
    void change_nick(const String&);

    bool is_nick_prefix(char) const;

    IRCWindow* current_window() { return aid_get_active_window(); }
    const IRCWindow* current_window() const { return aid_get_active_window(); }

    Function<void()> on_connect;
    Function<void()> on_disconnect;
    Function<void()> on_server_message;
    Function<void(const String&)> on_nickname_changed;
    Function<void(IRCChannel&)> on_part_from_channel;

    Function<IRCWindow*(void*, IRCWindow::Type, const String&)> aid_create_window;
    Function<IRCWindow*()> aid_get_active_window;
    Function<void()> aid_update_window_list;

    void register_subwindow(IRCWindow&);
    void unregister_subwindow(IRCWindow&);

    IRCWindowListModel* client_window_list_model() { return m_client_window_list_model.ptr(); }
    const IRCWindowListModel* client_window_list_model() const { return m_client_window_list_model.ptr(); }

    int window_count() const { return m_windows.size(); }
    const IRCWindow& window_at(int index) const { return *m_windows.at(index); }
    IRCWindow& window_at(int index) { return *m_windows.at(index); }

    int window_index(const IRCWindow& window) const
    {
        for (int i = 0; i < m_windows.size(); ++i) {
            if (m_windows[i] == &window)
                return i;
        }
        return -1;
    }

    void did_part_from_channel(Badge<IRCChannel>, IRCChannel&);

    void handle_user_input_in_channel(const String& channel_name, const String&);
    void handle_user_input_in_query(const String& query_name, const String&);
    void handle_user_input_in_server(const String&);

    void handle_whois_action(const String&);
    void handle_open_query_action(const String&);
    void handle_close_query_action(const String&);
    void handle_join_action(const String&);
    void handle_part_action(const String&);
    void handle_change_nick_action(const String&);

    IRCQuery& ensure_query(const String& name);
    IRCChannel& ensure_channel(const String& name);

    void add_server_message(const String&);

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

    enum class PrivmsgOrNotice {
        Privmsg,
        Notice,
    };

    void receive_from_server();
    void send(const String&);
    void send_user();
    void send_nick();
    void send_pong(const String& server);
    void send_privmsg(const String& target, const String&);
    void send_notice(const String& target, const String&);
    void send_whois(const String&);
    void process_line(ByteBuffer&&);
    void handle_join(const Message&);
    void handle_part(const Message&);
    void handle_ping(const Message&);
    void handle_topic(const Message&);
    void handle_rpl_topic(const Message&);
    void handle_rpl_whoisuser(const Message&);
    void handle_rpl_whoisserver(const Message&);
    void handle_rpl_whoisoperator(const Message&);
    void handle_rpl_whoisidle(const Message&);
    void handle_rpl_endofwhois(const Message&);
    void handle_rpl_whoischannels(const Message&);
    void handle_rpl_topicwhotime(const Message&);
    void handle_rpl_endofnames(const Message&);
    void handle_rpl_namreply(const Message&);
    void handle_privmsg_or_notice(const Message&, PrivmsgOrNotice);
    void handle_nick(const Message&);
    void handle(const Message&);
    void handle_user_command(const String&);
    void handle_ctcp_request(const StringView& peer, const StringView& payload);
    void handle_ctcp_response(const StringView& peer, const StringView& payload);
    void send_ctcp_response(const StringView& peer, const StringView& payload);

    void on_socket_connected();

    String m_hostname;
    int m_port { 6667 };

    CTCPSocket* m_socket { nullptr };

    String m_nickname;
    OwnPtr<CNotifier> m_notifier;
    HashMap<String, RefPtr<IRCChannel>, CaseInsensitiveStringTraits> m_channels;
    HashMap<String, RefPtr<IRCQuery>, CaseInsensitiveStringTraits> m_queries;

    Vector<IRCWindow*> m_windows;

    IRCWindow* m_server_subwindow { nullptr };

    NonnullRefPtr<IRCWindowListModel> m_client_window_list_model;
    NonnullRefPtr<IRCLogBuffer> m_log;
    NonnullRefPtr<CConfigFile> m_config;
};