Subversion Repositories Kolibri OS

Rev

Rev 4833 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
4832 hidnplayr 1
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2
;;                                                                 ;;
3
;; Copyright (C) KolibriOS team 2014. All rights reserved.         ;;
4
;; Distributed under terms of the GNU General Public License       ;;
5
;;                                                                 ;;
6
;;  pasta.asm - Paste text to paste.kolibrios.org from a file or   ;;
7
;;              from clipboard.                                    ;;
8
;;                                                                 ;;
9
;;          GNU GENERAL PUBLIC LICENSE                             ;;
10
;;             Version 2, June 1991                                ;;
11
;;                                                                 ;;
12
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
13
 
14
format binary as ""
15
use32
16
        org     0x0
17
 
18
        db      'MENUET01'      ; header
19
        dd      0x01            ; header version
20
        dd      START           ; entry point
21
        dd      IM_END          ; image size
22
        dd      I_END+0x1000    ; required memory
23
        dd      I_END+0x1000    ; esp
24
        dd      param
25
        dd      0               ; I_Path
26
 
27
 
28
include '../../macros.inc'
29
include '../../proc32.inc'
30
include '../../dll.inc'
31
include '../../develop/libraries/http/http.inc'
32
 
33
virtual at 0
34
        http_msg http_msg
35
end virtual
36
 
37
 
38
START:
39
        mcall   68, 11                  ; init heap so we can allocate memory dynamically
40
 
41
; load libraries
42
        stdcall dll.Load, @IMPORT
43
        test    eax, eax
44
        jnz     error
45
 
46
; Got parameters?
47
        cmp     byte[param], 0
48
        je      clipboard
49
 
50
; Yep, try to read the file.
51
        mcall   68, 12, 32768
52
        test    eax, eax
53
        jz      error
54
        mov     [file_struct.buf], eax
55
        mov     [clipboard_data], eax
56
        mcall   70, file_struct
57
        cmp     eax, 6
58
        jne     error
59
        mov     [clipboard_data_length], ebx
60
        mov     eax, [clipboard_data]
61
 
62
        jmp     escape
63
 
64
clipboard:
65
 
66
; Get number of slots on the clipboard
67
        mcall   54, 0
68
        cmp     eax, -1
69
        je      error
70
 
71
; Get last item on clipboard
72
        mov     ecx, eax
73
        dec     ecx
74
        inc     ebx
75
        mcall   54
76
        cmp     eax, -1
77
        je      error
78
 
79
; Verify if we can work with it
80
        cmp     dword[eax + 4], 0               ; text ?
81
        jne     error
82
        mov     [clipboard_data], eax
83
        mov     ecx, dword[eax]
84
        sub     ecx, 8
85
        mov     [clipboard_data_length], ecx
86
 
87
        mov     eax, [clipboard_data]
88
        add     eax, 12
89
 
90
escape:
91
; Escape all characters that need escaping
92
        invoke  HTTP_escape, eax
93
        test    eax, eax
94
        jz      error
95
        mov     [clipboard_data_length], ebx
96
 
97
        push    eax
98
        mcall   68, 13, [clipboard_data]
99
        pop     [clipboard_data]
100
 
101
; Connect to the server
102
        invoke  HTTP_get, sz_url, 0
103
        test    eax, eax
104
        jz      error
105
        mov     [identifier], eax
106
 
107
  .again:
108
        invoke  HTTP_process, [identifier]
109
        test    eax, eax
110
        jnz     .again
111
 
112
        invoke  HTTP_find_header_field, [identifier], sz_set_cookie
113
        mov     edi, cookie
114
        test    eax, eax
115
        jz      .no_cookie
116
 
117
        mov     esi, eax
118
  .cookie_loop:
119
        lodsb
120
        stosb
121
        cmp     al, 13
122
        je      .cookie_end
123
        cmp     al, 10
124
        je      .cookie_end
125
        cmp     al, 0
126
        je      .cookie_end
127
        jmp     .cookie_loop
128
  .cookie_end:
129
        dec     edi
130
  .no_cookie:
131
        mov     ax, 0x0a0d
132
        stosw
133
        xor     al, al
134
        stosb
135
 
136
        invoke  HTTP_free, [identifier]
137
 
138
        mov     ecx, [clipboard_data_length]
139
        add     ecx, sz_paste_head.length + sz_paste_tail.length
140
        invoke  HTTP_post, sz_url, sz_cookie, sz_ctype, ecx
141
        test    eax, eax
142
        jz      error
143
        mov     [identifier], eax
144
 
145
        mov     ecx, [eax + http_msg.socket]
146
        mcall   75, 6, , sz_paste_head, sz_paste_head.length, 0
147
        mcall   75, 6, , [clipboard_data], [clipboard_data_length], 0
148
        mcall   75, 6, , sz_paste_tail, sz_paste_tail.length, 0
149
 
150
  .again2:
151
        invoke  HTTP_process, [identifier]
152
        test    eax, eax
153
        jnz     .again2
154
 
155
        mov     ebp, [identifier]
156
        cmp     [ebp + http_msg.status], 302    ; found
157
        jne     error
158
 
159
        invoke  HTTP_find_header_field, [identifier], sz_location
160
        test    eax, eax
161
        jz      error
162
 
163
        push    eax
164
        mov     esi, sz_failed
165
        mov     edi, paste_url
166
        mov     ecx, 2
167
        rep movsd
168
        pop     esi
169
  .url_loop:
170
        lodsb
171
        stosb
172
        cmp     al, 13
173
        je      .url_end
174
        cmp     al, 10
175
        je      .url_end
176
        cmp     al, 0
177
        je      .url_end
178
        jmp     .url_loop
179
  .url_end:
180
        dec     edi
181
        lea     ecx, [edi - paste_url + 4]
182
        mov     eax, '" -t'
183
        stosd
184
        mov     ax, 'O'
185
        stosw
186
        mov     [notify_struct.msg], paste_url
187
        mcall   70, notify_struct
188
 
189
        mcall   54, 3                   ; Delete last slot in the clipboard
190
 
191
        mov     dword[paste_url-4], ecx
192
        mov     dword[paste_url+0], 0   ; Text
193
        mov     dword[paste_url+4], 1   ; cp0866
194
        mcall   54, 2, , paste_url-4    ; Write URL to the clipboard
195
 
196
        invoke  HTTP_free, [identifier]
197
        mcall   -1
198
 
199
error:
200
        mov     [notify_struct.msg], sz_failed
201
        mcall   70, notify_struct
202
 
203
        mcall   -1
204
 
205
 
206
;---------------------------------------------------------------------
207
; Data area
208
;-----------------------------------------------------------------------------
209
align   4
210
@IMPORT:
211
 
212
library lib_http,               'http.obj'
213
 
214
import  lib_http, \
215
        HTTP_get,               'get', \
216
        HTTP_process,           'process', \
217
        HTTP_free,              'free', \
218
        HTTP_stop,              'stop', \
219
        HTTP_post,              'post', \
220
        HTTP_find_header_field, 'find_header_field', \
221
        HTTP_escape,            'escape'
222
 
223
IM_END:
224
 
225
file_struct:
226
        dd 0            ; read file
227
        dd 0            ; offset
228
        dd 0            ; reserved
229
        dd 32768        ; max file size
230
  .buf  dd 0            ; buffer ptr
231
        db 0
232
        dd param
233
 
234
notify_struct:
235
        dd 7            ; run application
236
        dd 0
237
  .msg  dd paste_url
238
        dd 0
239
        dd 0
240
        db '/sys/@notify', 0
241
 
242
sz_url                  db 'http://paste.kolibrios.org/', 0
243
sz_set_cookie           db 'set-cookie', 0
244
sz_location             db 'location', 0
245
sz_ctype                db 'application/x-www-form-urlencoded', 0
246
sz_failed               db '"Pasta',13,10,'Paste failed!" -E', 0
247
 
248
sz_paste_head           db 'code='
249
.length = $ - sz_paste_head
250
sz_paste_tail           db '%0D%0A&language=text&webpage='
251
.length = $ - sz_paste_tail
252
 
253
sz_cookie               db 'Cookie: '
254
cookie                  db 0
255
                        rb 1024
256
 
257
paste_url               rb 1024
258
param                   rb 1024
259
 
260
identifier              dd 0
261
clipboard_data          dd 0
262
clipboard_data_length   dd 0
263
 
264
I_END:
265