Added count functionality in SavableManager.
[UnsignedByte.git] / src / Core / EditorNewAccount.cpp
blob599cad2cd64d3f709d93b195bf6140622ae712ac
1 /***************************************************************************
2 * Copyright (C) 2008 by Sverre Rabbelier *
3 * sverre@rabbelier.nl *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 3 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
21 #include "EditorNewAccount.h"
22 #include "EditorAccount.h"
23 #include "UBSocket.h"
24 #include "Account.h"
25 #include "AccountManager.h"
26 #include "Global.h"
27 #include "DatabaseMgr.h"
28 #include "FieldImpls.h"
29 #include "Channel.h"
31 EditorNewAccount::EditorNewAccount(UBSocket* sock) :
32 Editor(sock),
33 m_state(0)
35 OnLine(Global::Get()->EmptyString);
38 EditorNewAccount::~EditorNewAccount(void)
42 void EditorNewAccount::OnLine(const std::string &line)
44 if(!line.compare("quit"))
46 m_sock->Send("Allright, quitting!\n");
47 m_sock->SetCloseAndDelete();
48 return;
51 switch(m_state)
53 default:
55 Global::Get()->bug("EditorNewAccount::OnLine(), default m_state!\n");
56 m_sock->Send("BUG! Disconnecting you now...\n");
57 m_sock->SetCloseAndDelete();
58 return;
59 } /* default */
61 case M_FIRST:
63 m_state++;
64 // fallthrough
65 } /* case M_FIRST: */
67 case M_NAME:
69 if(line.size() == 0)
71 m_sock->Send("Enter a name for your account please: \n");
72 return;
75 if(mud::AccountManager::Get()->IllegalName(line))
77 m_sock->Sendf("You cannot use the name %s, please pick another name.\n", line.c_str());
78 OnLine(Global::Get()->EmptyString);
79 return;
82 m_name = line;
83 m_state++;
84 OnLine(Global::Get()->EmptyString);
85 break;
86 } /* case M_NAME: */
88 case M_NAMECONFIRM:
90 if(line.size() == 0)
92 m_sock->Sendf("Create an account named %s?\n", m_name.c_str());
93 return;
96 if(!line.compare("n") || !line.compare("no"))
98 m_sock->Send("Let's try again then.\n");
99 m_state = M_FIRST;
100 OnLine(Global::Get()->EmptyString);
101 return;
104 if(line.compare("y") && line.compare("yes"))
106 OnLine(Global::Get()->EmptyString);
107 m_sock->Send("yes/no?\n");
108 return;
111 m_state++;
112 OnLine(Global::Get()->EmptyString);
113 return;
114 } /* case M_NAMECONFIRM: */
116 case M_PASSWORD:
118 if(line.size() == 0)
120 m_sock->Send("Please choose a password: \n");
121 return;
124 m_password = line;
125 m_state++;
126 OnLine(Global::Get()->EmptyString);
127 return;
128 } /* case M_PASSWORD: */
130 case M_PASSWORDCONFIRM:
132 if(line.size() == 0)
134 m_sock->Send("Please confirm your password: \n");
135 return;
138 if(m_password.compare(line))
140 m_sock->Send("Passwords do not match, let's try again!\n");
141 m_state = M_PASSWORD;
142 OnLine(Global::Get()->EmptyString);
143 return;
146 m_sock->Sendf("Your password is set.\n", line.c_str());
147 m_sock->Send("Please hit enter to continue.\n");
148 m_state++;
149 return;
150 } /* case M_PASSWORDCONFIRM: */
152 case M_DONE:
154 if(line.size() != 0)
156 m_sock->Send("Please hit enter to continue.\n");
157 return;
160 KeysPtr newaccountkeys = mud::AccountManager::Get()->Add();
161 value_type id = newaccountkeys->first()->getIntegerValue();
162 if(id == 0)
164 m_sock->Send("Could not create a new account.\n");
165 m_sock->SetCloseAndDelete();
166 return;
169 mud::AccountPtr Acc = mud::AccountManager::Get()->GetByKey(id);
170 Acc->setName(m_name);
171 Acc->setPassword(m_password);
172 Acc->Save();
174 m_sock->Sendf("Account %s created, enjoy!\n", m_name.c_str());
175 m_sock->SetAccount(Acc);
176 m_sock->SetEditor(new EditorAccount(m_sock), true);
177 return;
178 } /* case M_DONE: */
180 } /* switch(state) */
184 void EditorNewAccount::OnEmptyLine()
186 OnLine(Global::Get()->EmptyString);
190 bool EditorNewAccount::canReceiveChannel(mud::ChannelPtr channel) const
192 if(!channel->needLogin())
193 return true;
195 return false;
198 bool EditorNewAccount::supportPrefixes() const
200 return false;