1
+ # include " Invite.hpp"
2
+ # include " Server.hpp"
3
+ # include " CommandHandler.hpp"
4
+
5
+ /* ************************************************************************** */
6
+ /* Defines */
7
+ /* ************************************************************************** */
8
+ #define ERR_WELCOMED " 462 PRIVMSG :You are not authenticated\r\n "
9
+ #define ERR_NEEDMOREPARAMS (nickname ) " 461 PRVMSG " + nickname + " INVITE :Not enough parameters\r\n "
10
+ #define ERR_NOSUCHCHANNEL (channel ) " 403 " + channel + " :No such channel\r\n "
11
+ #define ERR_NOTONCHANNEL (nickname, channel ) " 442 " + nickname + " " + channel + " :You're not on that channel\r\n "
12
+ #define ERR_CHANOPRIVSNEEDED (nickname, channel ) " 482 " + nickname + " " + channel + " :You're not channel operator\r\n "
13
+ #define ERR_USERNOTEXIST (user ) " 401 " + user + " :No such user in the database\r\n "
14
+ #define ERR_CANTINVITESELF " 437 :You can't invite yourself\r\n "
15
+ #define RPL_INVITING (nickname, nickname_invited, channel ) " :" + nickname + " INVITE " + nickname_invited + " " + channel + " \r\n "
16
+ /* ************************************************************************** */
17
+ /* Constructors and Destructors */
18
+ /* ************************************************************************** */
19
+ Invite::Invite () : ACommand(" INVITE" ) {}
20
+
21
+ Invite::~Invite () {}
22
+
23
+
24
+ /* ************************************************************************** */
25
+ /* Functions */
26
+ /* ************************************************************************** */
27
+ string Invite::executeCommand (Server *server) {
28
+ int &fd = server->getFds ()[server->getClientIndex ()].fd ;
29
+ list<string> &tokens = server->getCommandHandler ().getCommandTokens ();
30
+ string &nickname_invited = *tokens.begin ();
31
+ string &channel_token = *++tokens.begin ();
32
+ clientInfo &user_info = server->getUserDB ()[fd];
33
+ string &nickname = user_info._nickname ;
34
+
35
+ string error = parseFirstPart (server, tokens, channel_token);
36
+ if (!error.empty ())
37
+ return error;
38
+
39
+ Channel *channel = server->getChannel (channel_token);
40
+ map<int , int > &user_list = channel->getUserList ();
41
+ if (user_list.find (fd) == user_list.end ())
42
+ return ERR_NOTONCHANNEL (nickname, channel_token);
43
+
44
+ if (user_list[fd] != OPERATOR) // TODO ajuster quand il y aura des channels private avec les MOD?
45
+ return ERR_CHANOPRIVSNEEDED (nickname, channel_token);
46
+ int fd_invited = findClientToInvite (server, nickname_invited);
47
+ if (fd_invited == fd)
48
+ return ERR_CANTINVITESELF;
49
+ else if (fd_invited == 0 )
50
+ return ERR_USERNOTEXIST (nickname_invited);
51
+
52
+ channel->addUserToChannel (server, nickname_invited, fd_invited, 0 );
53
+ string message = RPL_INVITING (nickname, nickname_invited, channel_token);
54
+ channel->broadcastToAll (message);
55
+ return " " ;
56
+ }
57
+
58
+ int Invite::findClientToInvite (Server *server, const string &nickname_invited) {
59
+ map<int , clientInfo> &user_db = server->getUserDB ();
60
+ for (map<int , clientInfo>::const_iterator it = user_db.begin (); it != user_db.end (); ++it) {
61
+ if (it->second ._nickname == nickname_invited) {
62
+ return it->first ;
63
+ }
64
+ }
65
+ return 0 ;
66
+ }
67
+
68
+ string Invite::parseFirstPart (Server *server, const list<string> &tokens, const string &channel_token) {
69
+ int &fd = server->getFds ()[server->getClientIndex ()].fd ;
70
+ clientInfo &user_info = server->getUserDB ()[fd];
71
+ string &nickname = user_info._nickname ;
72
+
73
+ if (!user_info._welcomed )
74
+ return ERR_WELCOMED;
75
+ if (tokens.size () < 2 )
76
+ return ERR_NEEDMOREPARAMS (nickname);
77
+ if (!server->getChannel (channel_token))
78
+ return ERR_NOSUCHCHANNEL (channel_token);
79
+ return " " ;
80
+ }
0 commit comments