-
Notifications
You must be signed in to change notification settings - Fork 0
/
Channel.cpp
76 lines (61 loc) · 2.12 KB
/
Channel.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
//
// Author: Jakub Fajkus
// Project: ISA IRC bot
// Last revision: 17.11.2017
//
#include "Channel.h"
Channel::Channel(string &name) : name(name) {}
void Channel::add_user(string nickname) {
//check if the user is already there
//if it is, do not add him again
for (const LoggedUser &user : this->users) {
if (user.nickname == nickname) {
return;
}
}
//the user is not in the logged users
//so, add it there
LoggedUser logged_user(nickname);
this->users.emplace_back(logged_user);
}
void Channel::remove_user(string nickname) {
//look for the nickname in all the users
for (int j = 0; j < this->users.size(); ++j) {
LoggedUser &logged_user = this->users[j];
//if the nickname is the same
if (logged_user.nickname == nickname) {
//remove the j-th element, which is the nickname we are looking for
this->users.erase(this->users.begin() + j);
}
}
}
bool Channel::is_logged(string user) {
//get user if is logged or null, if not
for (const LoggedUser &user_object : this->users) {
if (user_object.nickname == user) {
return true;
}
}
return false;
}
void Channel::add_message(string user, string message) {
this->messages.emplace_back(UserMessage(user, message));
}
vector<string> Channel::get_messages_for_user(string user) {
//set up the vector for the messages
vector<string> unsent_messages;
//get all the messages for the user and remove them
for (int j = 0; j < this->messages.size(); ++j) {
UserMessage &message = this->messages[j];
//if the message is for the user
if (message.user == user) {
//add it to the list which will be returned
unsent_messages.emplace_back(message.message);
//remove the j-th element, which is the message added to the vector
this->messages.erase(this->messages.begin() + j);
//because of the message erasing, the j-th element must be processed once again as the array was shifted
--j;
}
}
return unsent_messages;
}