-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrivmsg.cpp
118 lines (115 loc) · 3.77 KB
/
Privmsg.cpp
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
#include "Server.hpp"
#include "Client.hpp"
#include "Channel.hpp"
void Server::SendUserMessage(std::string message, Client *client)
{
send(client->get_fd(), message.c_str(), message.size(), 0);
}
void Server::SendChannelMessage(std::string message, Channel *channel, Client *sender)
{
std::vector<Client *> clients = channel->GetClients();
for (std::vector<Client *>::iterator it = clients.begin(); it != clients.end(); it++)
{
if (*it == sender)
continue;
send((*it)->get_fd(), message.c_str(), message.size(), 0);
}
}
void Server::Privmsg(std::vector<std::string> command, const char *buffer, Client *client)
{
std::string response;
if (command.size() < 3)
{
response = ":HALAL_TINDER 461 :Not enough parameters\r\n";
send(client->get_fd(), response.c_str(), response.size(), 0);
return;
}
if (command[2][0] != ':')
{
response = ":HALAL_TINDER 412 :No text to send\r\n";
send(client->get_fd(), response.c_str(), response.size(), 0);
return;
}
if (command[1][0] == '#')
{
Channel *channel = NULL;
for (std::vector<Channel *>::iterator it = channels.begin(); it != channels.end(); ++it)
{
if ((*it)->GetName() == command[1])
{
channel = *it;
break;
}
}
if (channel == NULL)
{
response = ":HALAL_TINDER 403 :No such channel\r\n";
send(client->get_fd(), response.c_str(), response.size(), 0);
return;
}
if (!channel->IsInChannel(client))
{
response = ":HALAL_TINDER 442 :You're not on that channel\r\n";
send(client->get_fd(), response.c_str(), response.size(), 0);
return;
}
std::string message = ":" + client->get_nick() + "!" + client->get_user() + "@HALAL_TINDER PRIVMSG " + channel->GetName() + " ";
for (size_t i = 2; i < command.size(); i++)
{
if (command[i][0] == ':')
{
std::string bufstr(buffer);
size_t idx = bufstr.find(':');
if (idx != std::string::npos)
{
message += bufstr.substr(idx);
message += "\r\n";
break;
}
}
message += command[i];
if (i + 1 != command.size())
message += " ";
}
message += "\r\n";
SendChannelMessage(message, channel, client);
}
else
{
Client *receiver = NULL;
for (std::map<int, Client>::iterator it = clients.begin(); it != clients.end(); ++it)
{
if (it->second.get_nick() == command[1])
{
receiver = &it->second;
break;
}
}
if (receiver == NULL)
{
response = ":HALAL_TINDER 401 :No such nick/channel\r\n";
send(client->get_fd(), response.c_str(), response.size(), 0);
return;
}
std::string message = ":" + client->get_nick() + "!" + client->get_user() + "@HALAL_TINDER PRIVMSG " + receiver->get_nick() + " ";
for (size_t i = 2; i < command.size(); i++)
{
if (command[i][0] == ':')
{
std::string bufstr(buffer);
size_t idx = bufstr.find(':');
if (idx != std::string::npos)
{
message += bufstr.substr(idx);
message += "\r\n";
break;
}
}
message += command[i];
if (i + 1 != command.size())
message += " ";
}
message += "\r\n";
SendUserMessage(message, receiver);
}
}