forked from codewhite7777/ft_irc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChatbot.cpp
127 lines (111 loc) · 3.42 KB
/
Chatbot.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Chatbot.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mgo <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/13 13:26:15 by hena #+# #+# */
/* Updated: 2022/10/13 13:29:44 by mgo ### ########.fr */
/* */
/* ************************************************************************** */
#include "Chatbot.hpp"
#include "Channel.hpp"
#include "Server.hpp"
#include "utils.hpp"
#include <map>
#include <iostream>
#include <string>
#include <time.h>
#include <vector>
std::string ChatBot::CommandList(void)
{
std::string list = "";
for (std::map< std::string, std::string>::iterator iter = chatbot_command_list.begin()
; iter != chatbot_command_list.end()
; ++iter)
{
list += iter->first + ' ';
}
return list;
}
void ChatBot::AddCommand(std::string &contents)
{
std::vector<std::string> v = split(contents, ',');
if (v[0][0] == '!' && v[0] != "!add" && v[0] != "!delete" \
&& v[0] != "!list" && v[0] != "!info")
{
chatbot_command_list[v[0]] = v[1];
}
}
void ChatBot::DeleteCommand(std::string &command)
{
if (command[0] == '!' && command != "!add" && command != "!delete" \
&& command != "!list" && command != "!info")
chatbot_command_list.erase(command);
}
std::string ChatBot::GetNowTime()
{
std::string now = "";
time_t timer;
timer = time(NULL);
now += ctime(&timer);
return now;
}
ChatBot::ChatBot(void)
{
// !hi
chatbot_command_list.insert(std::make_pair("!hi", "안녕하세요"));
// !now
chatbot_command_list.insert(std::make_pair("!now", GetNowTime()));
// !mgo
chatbot_command_list.insert(std::make_pair("!mgo", "나는 항상 배가 고프다"));
// !hena
chatbot_command_list.insert(std::make_pair("!hena", "뀨 저는 귀요미에욤"));
// !info
chatbot_command_list.insert(std::make_pair("!info", "기본 봇 명령어: !add, !delete, !list"));
}
bool ChatBot::CheckList(std::string param, std::string &ret)
{
std::map<std::string, std::string>::iterator
iter = chatbot_command_list.find(param);
if (iter != chatbot_command_list.end())
{
ret = chatbot_command_list[param];
return (true);
}
return (false);
}
bool ChatBot::ChatBotCommand(std::string& param, std::string &bot_msg)
{
unsigned int ret = param.find(' ');
if ((int)ret != -1)
{
std::string chat_command = param.substr(1, ret - 1);
std::string chat_param = param.substr(ret + 1);
if (chat_command == "!add")
AddCommand(chat_param);
else if (chat_command == "!delete")
DeleteCommand(chat_param);
return true;
}
else
{
std::string chat_command = param.substr(1);
if (chat_command == "!list")
bot_msg = CommandList();
else if (chat_command == "!now")
chatbot_command_list[chat_command] = GetNowTime();
CheckList(chat_command, bot_msg);
return true;
}
return false;
}
ChatBot::~ChatBot(void)
{
chatbot_command_list.clear();
}
std::string ChatBot::getPrefix(void)
{
return (":NIGHTBOT!night@localhost ");
}