~vomun-developers/anonplus/vomun-trunk

« back to all changes in this revision

Viewing changes to src/uis/web/handler.py

  • Committer: AJ00200
  • Date: 2011-11-26 00:03:57 UTC
  • Revision ID: git-v1:f4f4352eb338a5f57a5a92e4bf23bec36d298d13
Blocks we receive are now stored in ~/.vomun/blocks.json. libs.globals and libs.config are now used directly after importing libs; they are set in libs/__init__.py now.

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
import hashlib
3
3
import time
4
4
 
 
5
import libs
5
6
import libs.events
6
 
import libs.globals
7
7
import libs.friends
8
8
import uis.web.content
9
9
import libs.encryption.rsa
19
19
                 'Welcome to Anon+.\nJust a warning, this system is not completely secure but it is probably better than the alternatives.'
20
20
            )
21
21
        ]
22
 
        
 
22
 
23
23
    def got_message(self, packet):
24
24
        contents = packet.message.split(',', 2)
25
25
        name = contents[0]
26
26
        sha256 = contents[1]
27
27
        post = contents[2]
28
 
        
 
28
 
29
29
        self.posts.insert(0, Post(name, sha256, post))
30
 
        
 
30
 
31
31
    def web_ui_request(self, path, connection):
32
32
        if path == '/':
33
33
            connection.send_response(200)
60
60
                    main = 'We are quitting now. Threads are being killed.',
61
61
                    sidecontent = 'Goodbye :)'
62
62
            ))
63
 
            libs.globals.global_vars['running'] = False
 
63
            libs.globals['running'] = False
64
64
            libs.threadmanager.killall()
65
65
            libs.threadmanager.close_sockets()
66
66
        elif path.startswith('/make_post.cgi?'):
69
69
                item = parameter.split('=')
70
70
                if item[0] == 'post':
71
71
                    post_contents = item[1] + ' (%s)' % time.ctime()
72
 
            for friend in libs.globals.global_vars['friends'].values():
 
72
            for friend in libs.globals['friends'].values():
73
73
                friend.send_message(','.join((
74
 
                        libs.globals.global_vars['config']['username'],
 
74
                        libs.config['username'],
75
75
                        hashlib.sha256(post_contents).hexdigest(),
76
76
                        post_contents)))
77
77
            self.posts.insert(0, Post(
78
 
                    libs.globals.global_vars['config']['username'],
 
78
                    libs.config['username'],
79
79
                    hashlib.sha256(post_contents).hexdigest(),
80
80
                    post_contents))
81
 
                        
 
81
 
82
82
            connection.send_response(301)
83
83
            connection.send_header('Location', 'http://localhost:7777/')
84
84
            connection.end_headers()
86
86
        elif path.startswith('/forward.cgi?'):
87
87
            parameters = unquote_plus(path.split('?')[-1])
88
88
            post_hash = parameters.split('=')[-1]
89
 
            
 
89
 
90
90
            for post in self.posts:
91
91
                print('Post: %s; FWD: %s' % (post.hash, post_hash))
92
92
                if post.hash == post_hash:
93
 
                    for friend in libs.globals.global_vars['friends'].values():
 
93
                    for friend in libs.globals['friends'].values():
94
94
                        friend.send_message(','.join((
95
95
                                post.name,
96
96
                                post.sha256,
97
97
                                post.body
98
98
                        )))
99
99
                    self.posts.insert(0, post)
100
 
                    break                 
101
 
            
 
100
                    break
 
101
 
102
102
            connection.send_response(301)
103
103
            connection.send_header('Location', 'http://localhost:7777/')
104
104
            connection.end_headers()
117
117
                    pagetitle = 'Friends',
118
118
                    main = uis.web.content.friend_page.format(
119
119
                            key = libs.encryption.rsa.export_key(
120
 
                                libs.globals.global_vars['config']['nodekey']
 
120
                                libs.config['nodekey']
121
121
                            )
122
122
                        ),
123
123
                    sidecontent = self.__friends2html()
127
127
            keydata = ''
128
128
            name = ''
129
129
            ip = ''
130
 
            
 
130
 
131
131
            # Parse parameters
132
132
            for parameter in parameters:
133
133
                item = parameter.split('=')
138
138
                    name = item[1]
139
139
                elif item[0] == 'ip':
140
140
                    ip = item[1]
141
 
                    
 
141
 
142
142
            # Add the friend object
143
143
            libs.friends.add_friend(result, ip = ip,
144
144
                                    port = 1337, name = name)
145
 
                
 
145
 
146
146
            # Notify the user of success
147
147
            connection.send_response(200)
148
148
            connection.send_header('Content-type', 'text/html')
163
163
                    main = uis.web.content.key_form,
164
164
                    sidecontent = self.__friends2html()
165
165
            ))
166
 
            
 
166
 
167
167
    def __friends2html(self):
168
168
        '''Convert our friends list into some nice HTML'''
169
169
        return uis.web.content.friends_box.format(
170
170
                friends = 'Our friend detector tells me you have no friends!'
171
171
        )
172
 
        
 
172
 
173
173
    def __generate_news_feed(self):
174
174
        '''Return the post elements in HTML'''
175
175
        content = ''
193
193
                        hash = post.hash
194
194
                )
195
195
        return content
196
 
    
 
196
 
197
197
class Post(object):
198
198
    def __init__(self, name, sha256, post):
199
199
        self.name = name
200
200
        self.sha256 = sha256
201
201
        self.hash = sha256[0:10]
202
202
        self.body = post
203
 
        
204
 
        if name == libs.globals.global_vars['config']['username']:
 
203
 
 
204
        if name == libs.config['username']:
205
205
            self.type = 'self'
206
 
        elif '@'+libs.globals.global_vars['config']['username'] in self.body:
 
206
        elif '@'+libs.config['username'] in self.body:
207
207
            self.type = 'mention'
208
208
        else:
209
209
            self.type = 'normal'
210
 
            
 
210
 
211
211
        # Sanity checks
212
212
        if not self.check_hash():
213
213
            self.body = 'This post failed the hash check.'
214
214
        self.sanatize()
215
 
            
 
215
 
216
216
    def check_hash(self):
217
217
        '''Check if the provided hash matches the informationw we were given.
218
218
        Return True if it does, otherwise False.
219
219
        '''
220
220
        return hashlib.sha256(self.body).hexdigest()[0:10] == self.hash
221
 
            
 
221
 
222
222
    def sanatize(self):
223
223
        '''Check the post options to make sure they do not contain anything
224
224
        that might act as a security risk or compromise an identity.'''
228
228
        self.body = self.body.replace('>', '>')
229
229
        self.name = self.name.replace('"', '"')
230
230
        self.body = self.body.replace('"', '"')
231
 
        
 
231
 
232
232
        # Features such as auto-linking and line breaks
233
233
        self.body = self.body.replace('\n', '<br />')