|
2 | 2 | #include "../headers/Colors.hpp"
|
3 | 3 | #include "sstream"
|
4 | 4 |
|
5 |
| -int IrcServer::receiver(std::string _nickname) |
6 |
| -{ |
7 |
| - for (unsigned int i = 0 ; i < clients.size() ; i++) |
8 |
| - { |
9 |
| - if (_nickname == clients[i]->getNickname()) |
10 |
| - return (i); |
11 |
| - } |
12 |
| - return (-1); |
13 |
| -} |
14 |
| - |
15 | 5 | //remplacer nicknameAlreadyUsed par nicknameExist
|
16 | 6 |
|
17 |
| -// void IrcServer::checkCprivmsg(std::string _msg, int _pos) |
18 |
| -// { |
19 |
| -// std::istringstream split_msg(_msg); |
20 |
| -// std::string token; |
21 |
| -// std::string msg_send; |
22 |
| -// std::vector<std::string> command_msg; |
23 |
| - |
24 |
| -// while(split_msg >> token) |
25 |
| -// command_msg.push_back(token); |
26 |
| - |
27 |
| -// if (command_msg.size() < 3) |
28 |
| -// { |
29 |
| -// msg_send = RED ITALIC "[Error: Incomplete command: syntax = CPRIVMSG name :...]\n" END_STYLE; |
30 |
| -// send(fd_sockets[_pos].fd, msg_send.c_str(), msg_send.size(), 0); |
31 |
| -// return; |
32 |
| -// } |
33 |
| -// else |
34 |
| -// { |
35 |
| -// if (command_msg[1].find(':') != std::string::npos || command_msg[2].find(':') == std::string::npos) |
36 |
| -// { |
37 |
| -// msg_send = RED ITALIC "[Error: Incomplete command: syntax = CPRIVMSG name :...]\n" END_STYLE; |
38 |
| -// send(fd_sockets[_pos].fd, msg_send.c_str(), msg_send.size(), 0); |
39 |
| -// return; |
40 |
| -// } |
41 |
| -// else |
42 |
| -// { |
43 |
| -// if (_msg.size() == _msg.find(':')) |
44 |
| -// { |
45 |
| -// msg_send = RED ITALIC "[Error: Incomplete command: syntax = CPRIVMSG name :...]\n" END_STYLE; |
46 |
| -// send(fd_sockets[_pos].fd, msg_send.c_str(), msg_send.size(), 0); |
47 |
| -// return; |
48 |
| -// } |
49 |
| -// msg_send = _msg.substr(_msg.find(':') + 1, _msg.size()) + "\n"; |
50 |
| -// if (!find_channel(command_msg[1], msg_send, _pos)) |
51 |
| -// { |
52 |
| -// msg_send = RED ITALIC "[Error: This nickname does not match any channel]\n" END_STYLE; |
53 |
| -// send(fd_sockets[_pos].fd, msg_send.c_str(), msg_send.size(), 0); |
54 |
| -// } |
55 |
| -// } |
56 |
| -// } |
57 |
| -// } |
58 |
| - |
59 |
| -void IrcServer::checkPrivmsg(std::string _msg, int _pos) |
| 7 | +int IrcServer::splitPrivmsgCommand(int _pos, std::string _msg, std::vector<std::string>& _receivers) |
60 | 8 | {
|
61 | 9 | std::istringstream split_msg(_msg);
|
62 |
| - std::string token; |
63 |
| - std::string msg_send; |
64 |
| - std::vector<std::string> command_msg; |
| 10 | + std::string token, msg_send; |
| 11 | + std::vector<std::string> command; |
| 12 | + unsigned int i; |
65 | 13 |
|
66 | 14 | while(split_msg >> token)
|
67 |
| - command_msg.push_back(token); |
68 |
| - |
69 |
| - if (command_msg.size() < 3) |
| 15 | + command.push_back(token); |
| 16 | + |
| 17 | + if (command.size() < 3 || command[2].find(':') == std::string::npos || _msg.size() == _msg.find(':')) |
70 | 18 | {
|
71 |
| - msg_send = RED ITALIC "[Error: Incomplete command: syntax = PRIVMSG name :...]\n" END_STYLE; |
| 19 | + // ERR_NEEDMOREPARAMS |
| 20 | + msg_send = RED ITALIC "[Error: Invalid command: syntax = PRIVMSG name :message]\n" END_STYLE; |
72 | 21 | send(fd_sockets[_pos].fd, msg_send.c_str(), msg_send.size(), 0);
|
| 22 | + return (0); |
73 | 23 | }
|
74 |
| - else |
| 24 | + |
| 25 | + for (i = 0 ; i < command[1].size() ; i++) |
75 | 26 | {
|
76 |
| - if (command_msg[1].find(':') != std::string::npos || command_msg[2].find(':') == std::string::npos) |
| 27 | + std::string receiver; |
| 28 | + while (i < command[1].size() && command[1][i] != ',') |
| 29 | + receiver += command[1][i++]; |
| 30 | + _receivers.push_back(receiver); |
| 31 | + if (i == command[1].size()) |
| 32 | + break; |
| 33 | + } |
| 34 | + |
| 35 | + return (1); |
| 36 | +} |
| 37 | + |
| 38 | +void IrcServer::checkPrivmsg(std::string _msg, int _pos) |
| 39 | +{ |
| 40 | + std::string msg_send; |
| 41 | + std::vector<std::string> receivers; |
| 42 | + unsigned int i = 0; |
| 43 | + int user, channel; |
| 44 | + |
| 45 | + if (!splitPrivmsgCommand(_pos, _msg, receivers)) |
| 46 | + return ; |
| 47 | + while (i < receivers.size()) |
| 48 | + { |
| 49 | + user = userExist(receivers[i]); |
| 50 | + channel = channelExist(receivers[i]); |
| 51 | + |
| 52 | + msg_send = _msg.substr(_msg.find(':') + 1, _msg.size()) + "\n"; |
| 53 | + if (user == -1 && channel == -1) |
77 | 54 | {
|
78 |
| - msg_send = RED ITALIC "[Error: Incomplete command: syntax = PRIVMSG name :...]\n" END_STYLE; |
| 55 | + msg_send = RED ITALIC "[Error: This nickname does not match any user or channel]\n" END_STYLE; |
79 | 56 | send(fd_sockets[_pos].fd, msg_send.c_str(), msg_send.size(), 0);
|
80 | 57 | }
|
81 |
| - else |
| 58 | + else if (user != -1) |
82 | 59 | {
|
83 |
| - int dest = receiver(command_msg[1]); |
84 |
| - if (_msg.size() == _msg.find(':')) |
85 |
| - { |
86 |
| - msg_send = RED ITALIC "[Error: Incomplete command: syntax = PRIVMSG name :...]\n" END_STYLE; |
87 |
| - send(fd_sockets[_pos].fd, msg_send.c_str(), msg_send.size(), 0); |
88 |
| - return; |
89 |
| - } |
90 |
| - msg_send = _msg.substr(_msg.find(':') + 1, _msg.size()) + "\n"; |
91 |
| - if (dest != -1) |
92 |
| - { |
93 |
| - std::string temp = PURPLE "[From: " + clients[_pos - 1]->getNickname() + "] => " END_STYLE BOLD + msg_send + END_STYLE; |
94 |
| - send(clients[dest]->getFdClient().fd, temp.c_str(), temp.size(), 0); |
95 |
| - } |
96 |
| - if (find_channel(command_msg[1], msg_send, _pos)) |
97 |
| - return ; |
98 |
| - else |
99 |
| - { |
100 |
| - msg_send = RED ITALIC "[Error: This nickname does not match any user or channel]\n" END_STYLE; |
101 |
| - send(fd_sockets[_pos].fd, msg_send.c_str(), msg_send.size(), 0); |
102 |
| - } |
| 60 | + std::string temp = PURPLE "[From: " + clients[_pos - 1]->getNickname() + "] => " END_STYLE BOLD + msg_send + END_STYLE; |
| 61 | + send(clients[user]->getFdClient().fd, temp.c_str(), temp.size(), 0); |
103 | 62 | }
|
| 63 | + else if (channel != -1) |
| 64 | + find_channel(receivers[i], msg_send, _pos); |
| 65 | + i++; |
104 | 66 | }
|
105 | 67 | }
|
106 | 68 |
|
|
0 commit comments