Added a EditorSocial.
[UnsignedByte.git] / src / Core / Editors / EditorNewAccount.cpp
bloba375debceb0511346d88346f716edf55a35991a0
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 "Account.h"
22 #include "AccountManager.h"
23 #include "Channel.h"
24 #include "DatabaseMgr.h"
25 #include "EditorAccount.h"
26 #include "EditorNewAccount.h"
27 #include "FieldImpls.h"
28 #include "Global.h"
29 #include "Managers.h"
30 #include "UBSocket.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 Assert(m_state != m_state);
57 break;
58 }/* default */
60 case M_FIRST:
62 m_state++;
63 // fallthrough
64 } /* case M_FIRST: */
66 case M_NAME:
68 if(line.size() == 0)
70 m_sock->Send("Enter a name for your account please: \n");
71 return;
74 if(mud::Managers::Get()->Account->IllegalName(line))
76 m_sock->Sendf("You cannot use the name %s, please pick another name.\n", line.c_str());
77 OnLine(Global::Get()->EmptyString);
78 return;
81 m_name = line;
82 m_state++;
83 OnLine(Global::Get()->EmptyString);
84 break;
85 } /* case M_NAME: */
87 case M_NAMECONFIRM:
89 if(line.size() == 0)
91 m_sock->Sendf("Create an account named %s?\n", m_name.c_str());
92 return;
95 if(!line.compare("n") || !line.compare("no"))
97 m_sock->Send("Let's try again then.\n");
98 m_state = M_FIRST;
99 OnLine(Global::Get()->EmptyString);
100 return;
103 if(line.compare("y") && line.compare("yes"))
105 OnLine(Global::Get()->EmptyString);
106 m_sock->Send("yes/no?\n");
107 return;
110 m_state++;
111 OnLine(Global::Get()->EmptyString);
112 return;
113 } /* case M_NAMECONFIRM: */
115 case M_PASSWORD:
117 if(line.size() == 0)
119 m_sock->Send("Please choose a password: \n");
120 return;
123 m_password = line;
124 m_state++;
125 OnLine(Global::Get()->EmptyString);
126 return;
127 } /* case M_PASSWORD: */
129 case M_PASSWORDCONFIRM:
131 if(line.size() == 0)
133 m_sock->Send("Please confirm your password: \n");
134 return;
137 if(m_password.compare(line))
139 m_sock->Send("Passwords do not match, let's try again!\n");
140 m_state = M_PASSWORD;
141 OnLine(Global::Get()->EmptyString);
142 return;
145 m_sock->Sendf("Your password is set.\n", line.c_str());
146 m_sock->Send("Please hit enter to continue.\n");
147 m_state++;
148 return;
149 } /* case M_PASSWORDCONFIRM: */
151 case M_DONE:
153 if(line.size() != 0)
155 m_sock->Send("Please hit enter to continue.\n");
156 return;
159 KeysPtr newaccountkeys = mud::Managers::Get()->Account->Add();
160 value_type id = newaccountkeys->first()->getIntegerValue();
161 if(id == 0)
163 m_sock->Send("Could not create a new account.\n");
164 m_sock->SetCloseAndDelete();
165 return;
168 mud::AccountPtr Acc = mud::Managers::Get()->Account->GetByKey(id);
169 Acc->setName(m_name);
170 Acc->setPassword(m_password);
171 Acc->Save();
173 m_sock->Sendf("Account %s created, enjoy!\n", m_name.c_str());
174 m_sock->SetAccount(Acc);
175 m_sock->SetEditor(new EditorAccount(m_sock), true);
176 return;
177 } /* case M_DONE: */
179 } /* switch(state) */
183 void EditorNewAccount::OnEmptyLine()
185 OnLine(Global::Get()->EmptyString);
189 bool EditorNewAccount::canReceiveChannel(mud::ChannelPtr channel) const
191 if(!channel->needLogin())
192 return true;
194 return false;
197 bool EditorNewAccount::supportPrefixes() const
199 return false;