Imported from archive.
[mudpy.git] / lib / muff / muffuser.py
diff --git a/lib/muff/muffuser.py b/lib/muff/muffuser.py
new file mode 100644 (file)
index 0000000..4ecdebf
--- /dev/null
@@ -0,0 +1,103 @@
+"""User objects for the MUFF Engine"""
+
+# Copyright (c) 2005 mudpy, Jeremy Stanley <fungi@yuggoth.org>
+# All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions
+# are met:
+#
+#    - Redistributions of source code must retain the above copyright
+#      notice, this list of conditions and the following disclaimer.
+#    - Redistributions in binary form must reproduce the above
+#      copyright notice, this list of conditions and the following
+#      disclaimer in the documentation and/or other materials provided
+#      with the distribution.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+# COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+# POSSIBILITY OF SUCH DAMAGE.
+
+import ConfigParser
+import string
+
+import muff
+for module in muff.__all__:
+       exec("import " + module)
+
+class User:
+       def __init__(self):
+               self.name = ""
+               self.passhash = ""
+               self.address = ""
+               self.connection = None
+               self.authenticated = False
+               self.password_tries = 1
+               self.state = "entering account name"
+               self.menu_seen = False
+               self.error = ""
+               self.input_queue = []
+               self.output_queue = []
+               self.partial_input = ""
+               self.echoing = True
+               self.record = ConfigParser.SafeConfigParser()
+       def load(self):
+               filename = muffconf.config_data.get("general", "account_path") + "/" + self.name
+               try:
+                       self.record.read(filename)
+                       self.passhash = self.record.get("account", "passhash")
+                       self.last_address = self.record.get("account", "last_address", self.address)
+               except:
+                       pass
+       def get_passhash(self):
+               filename = muffconf.config_data.get("general", "account_path") + "/" + self.proposed_name
+               temporary_record = ConfigParser.SafeConfigParser()
+               try:
+                       temporary_record.read(filename)
+                       self.passhash = temporary_record.get("account", "passhash")
+               except:
+                       self.passhash = ""
+       def save(self):
+               if self.authenticated:
+                       filename = muffconf.config_data.get("general", "account_path") + "/" + self.name.lower()
+                       if not self.record.has_section("account"):
+                               self.record.add_section("account")
+                       self.record.set("account", "name", self.name)
+                       self.record.set("account", "passhash", self.passhash)
+                       self.record.set("account", "last_address", self.address)
+                       record_file = file(filename, "w")
+                       self.record.write(record_file)
+                       record_file.close()
+       def show_menu(self):
+               self.send(muffmenu.get_menu(self))
+       def remove(self):
+               muffvars.userlist.remove(self)
+       def send(self, output, eol="$(eol)"):
+               if output:
+                       output = "$(eol)" + output + eol
+                       output = string.replace(output, "$(eol)", "\r\n")
+                       output = string.replace(output, "$(div)", "\r\n\r\n")
+                       output = string.replace(output, "$(bld)", chr(27)+"[1m")
+                       output = string.replace(output, "$(nrm)", chr(27)+"[0m")
+                       output = string.replace(output, "$(blk)", chr(27)+"[30m")
+                       output = string.replace(output, "$(grn)", chr(27)+"[32m")
+                       output = string.replace(output, "$(red)", chr(27)+"[31m")
+                       output = string.replace(output, "$(account)", self.name)
+                       output = muffmisc.wrap_ansi_text(output, 80)
+                       self.output_queue.append(output)
+                       try:
+                               self.connection.send(self.output_queue[0])
+                               self.output_queue.remove(self.output_queue[0])
+                               self.menu_seen = False
+                       except:
+                               pass
+