-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpost_form.py
214 lines (167 loc) · 7.39 KB
/
post_form.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import maxminddb
import npyscreen
import hashlib
from config import *
from utils import *
class MaxTitleTextfield(npyscreen.Textfield):
def __init__(self, *args, **keywords):
self.max_length = int(keywords['max_length'])
super(MaxTitleTextfield, self).__init__(*args, **keywords)
def h_addch(self, inp):
if len(self.value) < self.max_length:
return super(MaxTitleTextfield, self).h_addch(inp)
return
class MaxTitleText(npyscreen.TitleText):
_entry_type = MaxTitleTextfield
class MaxMultiLineEdit(npyscreen.MultiLineEdit):
def __init__(self, *args, **keywords):
self.max_length = int(keywords['max_length'])
self.max_lines = int(keywords['max_lines'])
super(MaxMultiLineEdit, self).__init__(*args, **keywords)
self.handlers.update({
curses.KEY_END: self.h_end,
curses.KEY_HOME: self.h_home})
def h_home(self, inp):
index = self.cursor_position -1
while(index > 0):
if self.value[index] == "\n":
index += 1
break
index -= 1
self.cursor_position = index
def h_end(self, inp):
self.cursor_position = self.value.find('\n', self.cursor_position)
if self.cursor_position == -1:
self.cursor_position = len(self.value)
def h_addch(self, inp):
if len(self.value) < self.max_length and self.value.count('\n') < self.max_lines:
return super(MaxMultiLineEdit, self).h_addch(inp)
return
def h_delete_right(self, inp):
super(MaxMultiLineEdit, self).h_delete_right(inp)
self.reformat_preserve_nl()
return
def h_delete_left(self, inp):
super(MaxMultiLineEdit, self).h_delete_left(inp)
self.reformat_preserve_nl()
return
def reformat_preserve_nl(self, *ignorethese):
width = self.maximum_display_width
text = self.value
lines = []
overflow = []
for former_line in text.split('\n'):
line = []
len_line = 0
if len(overflow) > 0:
actual_line = ' '.join(overflow) + ' ' + former_line
overflow = []
else:
actual_line = former_line
for word in actual_line.split(' '):
len_word = len(word)
if len_line + len_word <= width:
line.append(word)
len_line += len_word + 1
else:
#Once we had an overflow we stay overflowing
len_line += width
overflow.append(word)
lines.append(' '.join(line))
#Overlow pushed into a new line
if former_line == "" and len(line) > 1:
lines.append('')
if len(overflow) > 0:
lines.append(' '.join(overflow))
self.value = '\n'.join(lines)
return self.value
class BoxedMaxMultiLineEdit(npyscreen.BoxTitle):
_contained_widget = MaxMultiLineEdit
class PostForm(npyscreen.ActionPopup):
CANCEL_BUTTON_BR_OFFSET = (2, 73)
OK_BUTTON_TEXT = "Post!"
CANCEL_BUTTON_TEXT = "Cancel"
DEFAULT_LINES = 34
DEFAULT_COLUMNS = 80
def create(self):
self.value = None
self.wgHeadline = self.add(npyscreen.FixedText, max_height=1)
self.wgHeadline.editable = False
self.wgHeadline.syntax_highlighting = True
self.wgHeadline._highlightingdata = [curses.A_BOLD | self.theme_manager.findPair(self, 'DANGER')]*80
self.wgTitle = self.add(
MaxTitleText, name="Title:", max_length=MAX_CHARS_TITLE)
self.wgName = self.add(MaxTitleText, name="Name:",
max_length=MAX_CHARS_NAME)
self.wgContent = self.add(BoxedMaxMultiLineEdit, name="Post", max_height=23, contained_widget_arguments={
'max_length': MAX_CHARS_POST, 'max_lines': MAX_LINES_POST})
self.wgFooter = self.add(npyscreen.FixedText)
self.wgFooter.editable = False
self.wgFiles = self.add(npyscreen.FormControlCheckbox, name="Enable Files", width= 20)
self.nextrely -= 1
self.nextrelx += 20
self.wgPassword = self.add(MaxTitleText, name="PASSWORD:", max_length=MAX_CHARS_TITLE)
self.nextrelx -= 20
self.wgFiles.addVisibleWhenSelected(self.wgPassword)
self.nextrely += 1
self.wgCancel = self.add(npyscreen.MiniButtonPress,
name="Hide", when_pressed_function=self.on_hide)
def beforeEditing(self):
self.wgTitle.value = ""
self.wgContent.value = ""
self.wgName.value = ANON_NAME
self.wgFooter.value = ""
self.wgHeadline.value = " Create Thread"
self.wgCancel.hidden = True
if self.parentApp._FORM_VISIT_LIST[-2] == "THREAD":
self.wgHeadline.value = " Post Reply"
self.wgFooter.value = " Hiding will keep the form content in the thread"
self.wgCancel.hidden = False
if self.parentApp.reply_to:
self.wgContent.value = ">>"+str(self.parentApp.reply_to)
self.parentApp.reply_to = None
if self.parentApp.myThreadTitle:
self.wgTitle.value = self.parentApp.myThreadTitle
if self.parentApp.myThreadContent:
self.wgContent.value = self.parentApp.myThreadContent
def on_ok(self):
if len(self.wgContent.value) < MIN_CHARS_POST:
npyscreen.notify_wait("Not enough Content", title='You failed')
return
self.parentApp.myThreadTitle = None
self.parentApp.myThreadContent = None
country = ""
if self.parentApp.authenticated():
country = "##BOT##"
elif self.parentApp.myDatabase.board_has_country_balls(self.parentApp.myBoardId):
ip = os.getenv("SSH_CLIENT")
if not ip:
country = "Onion"
else:
ip = ip.split(" ")[0]
r = maxminddb.open_database(MAXMIND_DATABASE)
country = r.get(ip)['registered_country']['names']['en']
hashphrase = None
salt = None
if self.wgFiles.value:
salt = os.urandom(32)
hashphrase = hashlib.pbkdf2_hmac('sha256', self.wgPassword.value.encode("utf-8"), salt, 100000)
threadid = self.parentApp.myDatabase.post(self.parentApp.myBoardId, self.parentApp.myThreadId,
self.wgName.value, self.wgTitle.value, self.wgContent.value, country, hashphrase, salt)
if SFTP_INTEGRATION:
thread_path = get_local_path(self.parentApp.myDatabase, self.parentApp.myBoardId, threadid)
if not os.path.isdir(thread_path):
os.makedirs(thread_path)
self.parentApp.switchFormPrevious()
def on_cancel(self):
self.parentApp.myThreadTitle = None
self.parentApp.myThreadContent = None
self.parentApp.switchFormPrevious()
def on_hide(self):
if self.parentApp._FORM_VISIT_LIST[-2] == "THREAD":
self.parentApp.myThreadTitle = self.wgTitle.value
self.parentApp.myThreadContent = self.wgContent.value
else:
self.parentApp.myThreadTitle = None
self.parentApp.myThreadContent = None
self.parentApp.switchFormPrevious()