-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmd_privmsg.cpp
107 lines (84 loc) · 2.87 KB
/
cmd_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
#include "Server.hpp"
bool ft_is_inchannel(Channel &ch, int fd_cli) {
if (ch._clients.count(fd_cli))
return (true);
return (false);
}
void ft_tochannel(Client &they, t_server &srv, std::string &target, std::string &msg) {
target.erase(target.begin());
if (!srv.channels.count(target)) {
msg = ":ircap 403 " + they.getNick() + " #" + target + " :No such channel\r\n";
send(they.getFd(), msg.c_str(), msg.length(), 0);
return ;
}
Channel *ch = &(srv.channels.find(target)->second);
if (!ft_is_inchannel(*ch, they.getFd())) {
msg = ":ircap 442 " + they.getNick() + " #" + target + " :You're not on that channel\r\n";
send(they.getFd(), msg.c_str(), msg.length(), 0);
return ;
}
msg = ":" + they.getNick() + "!" + " PRIVMSG #" + target + " " + msg + "\r\n";
std::set<int>::const_iterator it = ch->_clients.begin();
std::set<int>::const_iterator end = ch->_clients.end();
while (it != end) {
if ((*it) != they.getFd())
send((*it), msg.c_str(), msg.length(), 0);
++it;
}
}
void ft_oponly(Client &they, t_server &srv, std::string &target, std::string &msg) {
target.erase(target.begin());
if (!srv.channels.count(target)) {
msg = ":ircap 403 " + they.getNick() + " #" + target + " :No such channel\r\n";
send(they.getFd(), msg.c_str(), msg.length(), 0);
return ;
}
Channel *ch = &(srv.channels.find(target)->second);
if (!ft_is_inchannel(*ch, they.getFd())) {
msg = ":ircap 442 " + they.getNick() + " #" + target + " :You're not on that channel\r\n";
send(they.getFd(), msg.c_str(), msg.length(), 0);
return ;
}
msg = ":" + they.getNick() + "!" + " PRIVMSG #" + target + " " + msg + "\r\n";
std::set<int>::const_iterator it = ch->_clients.begin();
std::set<int>::const_iterator end = ch->_clients.end();
while (it != end) {
if (ch->_operators.count(srv.client_map[(*it)]->getNick()) && (*it) != they.getFd())
send((*it), msg.c_str(), msg.length(), 0);
++it;
}
}
void ft_touser(Client &they, t_server &srv, std::string &target, std::string &msg) {
msg = ":" + they.getNick() + "!" + " PRIVMSG " + target + " " + msg + "\r\n";
send(srv.nicknames[target], msg.c_str(), msg.length(), 0);
}
void Client::privmsg(t_server &srv)
{
std::istringstream iss(buf);
std::string tmp, target, msg;
std::getline(iss, tmp, ' ');
std::getline(iss, target, ' ');
std::getline(iss, msg, '\r');
if (msg.find(":DCC") != std::string::npos)
{
fileTransfer(srv);
return ;
}
if (target.empty() || msg.empty())
{
tmp = "ERROR :No target or message given\r\n";
send(this->_fd, tmp.c_str(), tmp.length(), 0);
return ;
}
if (target[0] == '@' && target[1] == '#')
ft_oponly(*this, srv, target, msg);
else if (target[0] == '#')
ft_tochannel(*this, srv, target, msg);
else if (target[0] != '@')
ft_touser(*this, srv, target, msg);
else {
tmp = "ERROR :No target or message given\r\n";
send(this->_fd, tmp.c_str(), tmp.length(), 0);
return ;
}
}