1 /***************************************************************************
2 * Copyright (C) 2008 by Sverre Rabbelier *
3 * sverre@rabbelier.nl *
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. *
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. *
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 ***************************************************************************/
24 * This file contains the UBSocket class.
29 #include "SavableHeaders.h"
30 #include "SavableTypes.h"
34 #include <TcpSocket.h>
35 #include <ISocketHandler.h>
38 typedef std::list
<Editor
*> EditorList
; /**< The type of multiple Editors. */
41 * This class handles all data from and to the user.
43 * When accepting an connection it will set the login editor.
44 * All lines received are processed by calling the topmost editor's OnLine function.
46 class UBSocket
: public TcpSocket
49 /** Constructs a UBSocket using the specified socket handler. */
50 UBSocket(ISocketHandler
& h
);
52 /** Destructor, deletes all editors.*/
56 /** Accept a new connection. */
59 /** Accept the next line of user input. */
60 void OnLine(const std::string
& line
);
63 /** Send text to the user. */
64 void Send(const std::string
& msg
);
66 /** Send formatted text to the user. */
67 void Sendf(const char* format
, ...);
70 /** Whether this socket already has an account associated with it. */
71 bool hasAccount() const;
73 /** Returns the account associated with this socket, this account is asserted to have been assigned. */
74 mud::AccountPtr
GetAccount() const;
76 /** Whether the user was forced to send their latest line. */
77 bool hasForcer() const;
79 /** Returns the socket that forced this user to send their latest line, it is asserted that there is a forcer. */
80 UBSocket
* GetForcer() const;
82 /** Whether the current force is from an Admin. */
83 bool isHighForced() const { return m_highforced
; }
85 /** Whether the current force is from a Forcer. */
86 bool isForced() const { return m_forced
; }
88 /** Whether the current force is from another player. */
89 bool isLowForced() const { return m_lowforced
; }
91 /** Is the user in a state capable of receiving the specified channel. */
92 bool canReceiveChannel(mud::ChannelPtr channel
);
95 /** Sets the account associated with this user. */
96 void SetAccount(mud::AccountPtr account
) { m_account
= account
; }
99 * Set the specified editor as the current editor
101 * The specified editor does not replace any previous editors.
102 * Instead it is prepended to a list of editors.
103 * This allows for 'stacked' editors.
105 * @param editor The editor that should be prepended to the editor list.
106 * @param popLast Remove the most recent editor before adding the current editor.
108 void SetEditor(Editor
* editor
, bool popLast
= false);
110 /** Return to the previous editor, it is asserted that the user is never left with an empty editor list. */
114 * Sets the forces of the next commands.
116 * The forcer remains in place untill <code>EndForce</code> is called.
117 * The optional parameters describe what authorization the forcer has.
119 * @param forcer The socket that will force the next commands.
120 * @param weakForced Whether the force is done by another user.
121 * @param forced Whether the force is done by a Forcer.
122 * @param highForced Whether the force is done by an Admin.
126 void SetForcer(UBSocket
* forcer
, bool weakForced
= true, bool forced
= false, bool highForced
= false);
128 /** End the force. */
132 * This will switch editors.
134 * The <code>SetEditor</code> and <code>PopEditor</code> methods do not actually change editors.
135 * Instead they 'queue' editor changing actions till the end of the current 'loop', when this function is called.
136 * Each editor will therefore return from the function that called <code>SetEditor</code> or <code>PopEditor</code>.
137 * As such, editors do not have to worry about being deleted after calling either of those functions.
142 void SwitchEditors();
145 * This is an utility function that will cast the specified socket to an UBSocket.
147 * @param sock The socket to cast from.
148 * @param error Whether we should throw an exception when the socket could not be casted.
150 static UBSocket
* Cast(Socket
* sock
, bool error
= true);
153 /** Hide the copy constructor. */
154 UBSocket(const UBSocket
& rhs
);
156 /** Hide the assignment operator. */
157 UBSocket
operator=(const UBSocket
& rhs
);
159 void SetPrompt(const std::string
& prompt
= "");
161 bool handlePrefixes(const std::string
& line
);
162 void handleLine(const std::string
& line
);
164 std::string m_prompt
; /**< Current prompt. */
165 mud::AccountPtr m_account
; /**< Current account. */
166 bool m_popeditor
; /**< Whether we should delete the top editor next loop. */
167 bool m_popLast
; /**< Whether we should delete the top editor when adding new one next loop. */
168 EditorList m_editors
; /**< The list of editors. */
169 Editor
* m_nexteditor
; /**< The Editor to prepend to the list next loop. */
170 UBSocket
* m_forcer
; /**< The socket that is current forcing the user. */
171 bool m_lowforced
; /**< Whether the user is forced by another user. */
172 bool m_forced
; /**< Whether the user is forced by a Forcer. */
173 bool m_highforced
; /**< Whether the user is forced by an Admin. */
174 bool m_hascolor
; /**< Whether the user has colour enabled. */
175 char m_colorcode
; /**< The character the user uses to indicate a colourcode. */