Only allow this language: ((n|s)!(w|e)!(u|d)!;)+ for directions.
[UnsignedByte.git] / src / Core / Editors / EditorNewAccount.cpp
blobe8b569655cd05ccda326de78e3ef3dc071194569
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"
30 #include "Managers.h"
32 EditorNewAccount::EditorNewAccount(UBSocket* sock) :
33 Editor(sock),
34 m_state(0)
36 OnLine(Global::Get()->EmptyString);
39 EditorNewAccount::~EditorNewAccount(void)
43 void EditorNewAccount::OnLine(const std::string &line)
45 if(!line.compare("quit"))
47 m_sock->Send("Allright, quitting!\n");
48 m_sock->SetCloseAndDelete();
49 return;
52 switch(m_state)
54 default:
56 Global::Get()->bug("EditorNewAccount::OnLine(), default m_state!\n");
57 m_sock->Send("BUG! Disconnecting you now...\n");
58 m_sock->SetCloseAndDelete();
59 return;
60 } /* default */
62 case M_FIRST:
64 m_state++;
65 // fallthrough
66 } /* case M_FIRST: */
68 case M_NAME:
70 if(line.size() == 0)
72 m_sock->Send("Enter a name for your account please: \n");
73 return;
76 if(mud::Managers::Get()->Account->IllegalName(line))
78 m_sock->Sendf("You cannot use the name %s, please pick another name.\n", line.c_str());
79 OnLine(Global::Get()->EmptyString);
80 return;
83 m_name = line;
84 m_state++;
85 OnLine(Global::Get()->EmptyString);
86 break;
87 } /* case M_NAME: */
89 case M_NAMECONFIRM:
91 if(line.size() == 0)
93 m_sock->Sendf("Create an account named %s?\n", m_name.c_str());
94 return;
97 if(!line.compare("n") || !line.compare("no"))
99 m_sock->Send("Let's try again then.\n");
100 m_state = M_FIRST;
101 OnLine(Global::Get()->EmptyString);
102 return;
105 if(line.compare("y") && line.compare("yes"))
107 OnLine(Global::Get()->EmptyString);
108 m_sock->Send("yes/no?\n");
109 return;
112 m_state++;
113 OnLine(Global::Get()->EmptyString);
114 return;
115 } /* case M_NAMECONFIRM: */
117 case M_PASSWORD:
119 if(line.size() == 0)
121 m_sock->Send("Please choose a password: \n");
122 return;
125 m_password = line;
126 m_state++;
127 OnLine(Global::Get()->EmptyString);
128 return;
129 } /* case M_PASSWORD: */
131 case M_PASSWORDCONFIRM:
133 if(line.size() == 0)
135 m_sock->Send("Please confirm your password: \n");
136 return;
139 if(m_password.compare(line))
141 m_sock->Send("Passwords do not match, let's try again!\n");
142 m_state = M_PASSWORD;
143 OnLine(Global::Get()->EmptyString);
144 return;
147 m_sock->Sendf("Your password is set.\n", line.c_str());
148 m_sock->Send("Please hit enter to continue.\n");
149 m_state++;
150 return;
151 } /* case M_PASSWORDCONFIRM: */
153 case M_DONE:
155 if(line.size() != 0)
157 m_sock->Send("Please hit enter to continue.\n");
158 return;
161 KeysPtr newaccountkeys = mud::Managers::Get()->Account->Add();
162 value_type id = newaccountkeys->first()->getIntegerValue();
163 if(id == 0)
165 m_sock->Send("Could not create a new account.\n");
166 m_sock->SetCloseAndDelete();
167 return;
170 mud::AccountPtr Acc = mud::Managers::Get()->Account->GetByKey(id);
171 Acc->setName(m_name);
172 Acc->setPassword(m_password);
173 Acc->Save();
175 m_sock->Sendf("Account %s created, enjoy!\n", m_name.c_str());
176 m_sock->SetAccount(Acc);
177 m_sock->SetEditor(new EditorAccount(m_sock), true);
178 return;
179 } /* case M_DONE: */
181 } /* switch(state) */
185 void EditorNewAccount::OnEmptyLine()
187 OnLine(Global::Get()->EmptyString);
191 bool EditorNewAccount::canReceiveChannel(mud::ChannelPtr channel) const
193 if(!channel->needLogin())
194 return true;
196 return false;
199 bool EditorNewAccount::supportPrefixes() const
201 return false;