-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser.cpp
More file actions
185 lines (148 loc) · 4.03 KB
/
user.cpp
File metadata and controls
185 lines (148 loc) · 4.03 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#include <iostream>
#include <algorithm>
#include "user.hpp"
#include "server.hpp"
/* Constructors and Deconstructors */
//===================================================//
User::User(pollfd &client, char* host, char* service, Server* server) : replyMessage(), _server(server), _channelList(), _nick(""), _fd(), _host(host), _service(service), _isRegistered(false), _username(), _realname(), _client(client) {
_fd = _client.fd;
/* _username = "";
_realname = "IRC User";
_isRegistered = false;
_creationTime = std::time(NULL);
_channelList = std::vector<Channel*>(); */
std::cout << "Default user constructor called with Client Input" << std::endl;
}
User &User::operator=(const User &src) {
this->_fd = src.getFd();
this->_nick = src.getNick();
return (*this);
}
User::~User() {
std::cout << "User removed" << std::endl;
}
/* Getters and Setters */
//===================================================//
int User::getFd() const {
return (this->_fd);
}
void User::setFd(int new_fd) {
this->_fd = new_fd;
}
std::string User::getNick() const {
return (this->_nick);
}
void User::setNick(std::string nick) {
std::transform(nick.begin(), nick.end(), nick.begin(), ::tolower);
_nick = nick;
}
std::string User::getUsername() const {
return (this->_username);
}
void User::setUsername(std::string username) {
this->_username = username;
}
std::string User::getRealname() const {
return (this->_realname);
}
void User::setRealname(std::string realname) {
this->_username = realname;
}
void User::setRegistered() {
this->_isRegistered = true;
}
bool User::isRegistered() {
return (this->_isRegistered);
}
std::vector<Channel *> User::getChannels()
{
return (this->_channelList);
}
/* time_t User::getTime() {
return (std::time(NULL));
} */
/*
time_t User::getCreationTime() {
return (this->_creationTime);
} */
char** User::getHost() {
return (&this->_host);
}
void User::setHost(char* host) {
this->_host = host;
}
void User::setClient(pollfd &client) {
this->_client = client;
}
void User::setService(char* service) {
this->_service = service;
}
void User::deleteChannel(std::string channel) {
// Protection: Check if the _channelList is empty
if (this->_channelList.size() == 0)
{
return ;
}
std::vector<Channel*>::iterator itr;
for (itr=this->_channelList.begin(); itr != this->_channelList.end(); itr++)
{
if(channel == (*itr)->getName())
{
_channelList.erase(itr);
break ;
}
}
}
/* Deletes a Channel Obj Pointer from the _channelUserList */
void User::delete_Channel(Channel* channel)
{
// Protection: Check if the _channelList is empty
if (this->_channelList.size() == 0)
{
return ;
}
std::vector<Channel*>::iterator itr;
for (itr=this->_channelList.begin(); itr != this->_channelList.end(); itr++)
{
if(channel == *itr) {
std::cout << "Reached in delete_Channel" << std::endl;
_channelList.erase(itr);
break ;
}
}
}
/* Checkers */
//===================================================//
Channel* User::findUserinChannel(std::string name)
{
// Protection: Check if the _channelList is empty
if (this->_channelList.size() == 0)
{
return (NULL);
}
std::vector<Channel*>::iterator itr;
for (itr=this->_channelList.begin(); itr != this->_channelList.end(); itr++) {
if (name == (*itr)->getName())
return (*itr);
}
std::cerr << "Channel not found by name" << std::endl;
return (NULL);
}
Channel* User::get_channel_if_in(const std::string& channel_name)
{
std::cout << "DEBUG: Inside get_channel_if_in() nr 1" << std::endl;
if (!this->_channelList.empty()) {
std::cout << "DEBUG: Inside get_channel_if_in() nr 2" << std::endl;
std::vector<Channel*>::iterator itro;
for (itro = this->_channelList.begin(); itro != this->_channelList.end(); itro++)
{
std::cout << "DEBUG: Inside get_channel_if_in() nr 3; this->_channelList.size() is " << this->_channelList.size() << std::endl;
if (channel_name == (*itro)->getName())
{
std::cout << "DEBUG: Inside get_channel_if_in() nr 4, before the return" << std::endl;
return ((*itro));
}
}
}
return (NULL);
}