Imported from archive.
[mudpy.git] / lib / muff / muffuser.py
1 """User objects for the MUFF Engine"""
2
3 # Copyright (c) 2005 mudpy, Jeremy Stanley <fungi@yuggoth.org>
4 # All rights reserved.
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
8 # are met:
9 #
10 #    - Redistributions of source code must retain the above copyright
11 #      notice, this list of conditions and the following disclaimer.
12 #    - Redistributions in binary form must reproduce the above
13 #      copyright notice, this list of conditions and the following
14 #      disclaimer in the documentation and/or other materials provided
15 #      with the distribution.
16 #
17 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21 # COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 # POSSIBILITY OF SUCH DAMAGE.
29
30 import ConfigParser
31 import string
32
33 import muff
34 for module in muff.__all__:
35         exec("import " + module)
36
37 class User:
38         def __init__(self):
39                 self.name = ""
40                 self.passhash = ""
41                 self.address = ""
42                 self.connection = None
43                 self.authenticated = False
44                 self.password_tries = 1
45                 self.state = "entering account name"
46                 self.menu_seen = False
47                 self.error = ""
48                 self.input_queue = []
49                 self.output_queue = []
50                 self.partial_input = ""
51                 self.echoing = True
52                 self.record = ConfigParser.SafeConfigParser()
53         def load(self):
54                 filename = muffconf.config_data.get("general", "account_path") + "/" + self.name
55                 try:
56                         self.record.read(filename)
57                         self.passhash = self.record.get("account", "passhash")
58                         self.last_address = self.record.get("account", "last_address", self.address)
59                 except:
60                         pass
61         def get_passhash(self):
62                 filename = muffconf.config_data.get("general", "account_path") + "/" + self.proposed_name
63                 temporary_record = ConfigParser.SafeConfigParser()
64                 try:
65                         temporary_record.read(filename)
66                         self.passhash = temporary_record.get("account", "passhash")
67                 except:
68                         self.passhash = ""
69         def save(self):
70                 if self.authenticated:
71                         filename = muffconf.config_data.get("general", "account_path") + "/" + self.name.lower()
72                         if not self.record.has_section("account"):
73                                 self.record.add_section("account")
74                         self.record.set("account", "name", self.name)
75                         self.record.set("account", "passhash", self.passhash)
76                         self.record.set("account", "last_address", self.address)
77                         record_file = file(filename, "w")
78                         self.record.write(record_file)
79                         record_file.close()
80         def show_menu(self):
81                 self.send(muffmenu.get_menu(self))
82         def remove(self):
83                 muffvars.userlist.remove(self)
84         def send(self, output, eol="$(eol)"):
85                 if output:
86                         output = "$(eol)" + output + eol
87                         output = string.replace(output, "$(eol)", "\r\n")
88                         output = string.replace(output, "$(div)", "\r\n\r\n")
89                         output = string.replace(output, "$(bld)", chr(27)+"[1m")
90                         output = string.replace(output, "$(nrm)", chr(27)+"[0m")
91                         output = string.replace(output, "$(blk)", chr(27)+"[30m")
92                         output = string.replace(output, "$(grn)", chr(27)+"[32m")
93                         output = string.replace(output, "$(red)", chr(27)+"[31m")
94                         output = string.replace(output, "$(account)", self.name)
95                         output = muffmisc.wrap_ansi_text(output, 80)
96                         self.output_queue.append(output)
97                         try:
98                                 self.connection.send(self.output_queue[0])
99                                 self.output_queue.remove(self.output_queue[0])
100                                 self.menu_seen = False
101                         except:
102                                 pass
103