Subversion Repositories Kolibri OS

Rev

Rev 5541 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
4832 hidnplayr 1
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2
;;                                                                 ;;
5541 hidnplayr 3
;; Copyright (C) KolibriOS team 2014-2015. All rights reserved.    ;;
4832 hidnplayr 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
4833 hidnplayr 58
        jne     error_free_clip
4832 hidnplayr 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
5770 hidnplayr 78
        cmp     eax, 1
79
        je      error
4832 hidnplayr 80
 
81
; Verify if we can work with it
4833 hidnplayr 82
        mov     [clipboard_data], eax
4832 hidnplayr 83
        cmp     dword[eax + 4], 0               ; text ?
4833 hidnplayr 84
        jne     error_free_clip
85
 
86
; Save length in [clipboard_data_length]
4832 hidnplayr 87
        mov     ecx, dword[eax]
4834 hidnplayr 88
        sub     ecx, 12
4832 hidnplayr 89
        mov     [clipboard_data_length], ecx
90
 
4833 hidnplayr 91
; Skip clipboard containter params for escape proc
4832 hidnplayr 92
        add     eax, 12
93
 
94
escape:
95
; Escape all characters that need escaping
96
        invoke  HTTP_escape, eax
97
        test    eax, eax
4833 hidnplayr 98
        jz      error_free_clip
4832 hidnplayr 99
        mov     [clipboard_data_length], ebx
100
 
101
        push    eax
102
        mcall   68, 13, [clipboard_data]
103
        pop     [clipboard_data]
104
 
105
; Connect to the server
5770 hidnplayr 106
        invoke  HTTP_get, sz_url, 0, FLAG_BLOCK or FLAG_KEEPALIVE, 0
4832 hidnplayr 107
        test    eax, eax
4833 hidnplayr 108
        jz      error_free_clip
4832 hidnplayr 109
        mov     [identifier], eax
110
 
111
  .again:
5541 hidnplayr 112
        invoke  HTTP_receive, [identifier]
4832 hidnplayr 113
        test    eax, eax
114
        jnz     .again
115
 
116
        invoke  HTTP_find_header_field, [identifier], sz_set_cookie
117
        mov     edi, cookie
118
        test    eax, eax
119
        jz      .no_cookie
120
 
121
        mov     esi, eax
122
  .cookie_loop:
123
        lodsb
124
        stosb
125
        cmp     al, 13
126
        je      .cookie_end
127
        cmp     al, 10
128
        je      .cookie_end
129
        cmp     al, 0
130
        je      .cookie_end
131
        jmp     .cookie_loop
132
  .cookie_end:
133
        dec     edi
134
  .no_cookie:
135
        mov     ax, 0x0a0d
136
        stosw
137
        xor     al, al
138
        stosb
139
 
140
        mov     ecx, [clipboard_data_length]
141
        add     ecx, sz_paste_head.length + sz_paste_tail.length
5770 hidnplayr 142
        invoke  HTTP_post, sz_url, [identifier], FLAG_BLOCK, sz_cookie, sz_ctype, ecx
4832 hidnplayr 143
        test    eax, eax
5770 hidnplayr 144
        jz      error_free_clip_disconnect
4832 hidnplayr 145
 
4833 hidnplayr 146
; Send the data to the server
4832 hidnplayr 147
        mov     ecx, [eax + http_msg.socket]
148
        mcall   75, 6, , sz_paste_head, sz_paste_head.length, 0
149
        mcall   75, 6, , [clipboard_data], [clipboard_data_length], 0
150
        mcall   75, 6, , sz_paste_tail, sz_paste_tail.length, 0
151
 
4833 hidnplayr 152
; Free the data
153
        mcall   68, 13, [clipboard_data]
154
 
4832 hidnplayr 155
  .again2:
5541 hidnplayr 156
        invoke  HTTP_receive, [identifier]
4832 hidnplayr 157
        test    eax, eax
158
        jnz     .again2
159
 
5770 hidnplayr 160
        invoke  HTTP_disconnect, [identifier]
161
 
4832 hidnplayr 162
        mov     ebp, [identifier]
163
        cmp     [ebp + http_msg.status], 302    ; found
4833 hidnplayr 164
        jne     error_free_http
4832 hidnplayr 165
 
166
        invoke  HTTP_find_header_field, [identifier], sz_location
167
        test    eax, eax
4833 hidnplayr 168
        jz      error_free_http
4832 hidnplayr 169
 
170
        push    eax
171
        mov     esi, sz_failed
172
        mov     edi, paste_url
173
        mov     ecx, 2
174
        rep movsd
175
        pop     esi
176
  .url_loop:
177
        lodsb
178
        stosb
179
        cmp     al, 13
180
        je      .url_end
181
        cmp     al, 10
182
        je      .url_end
183
        cmp     al, 0
184
        je      .url_end
185
        jmp     .url_loop
186
  .url_end:
187
        dec     edi
188
        lea     ecx, [edi - paste_url + 4]
189
        mov     eax, '" -t'
190
        stosd
191
        mov     ax, 'O'
192
        stosw
193
        mov     [notify_struct.msg], paste_url
194
        mcall   70, notify_struct
195
 
196
        mcall   54, 3                   ; Delete last slot in the clipboard
197
 
198
        mov     dword[paste_url-4], ecx
199
        mov     dword[paste_url+0], 0   ; Text
200
        mov     dword[paste_url+4], 1   ; cp0866
201
        mcall   54, 2, , paste_url-4    ; Write URL to the clipboard
202
 
203
        invoke  HTTP_free, [identifier]
204
        mcall   -1
205
 
4833 hidnplayr 206
error_free_http:
207
        invoke  HTTP_free, [identifier]
208
        jmp     error
5770 hidnplayr 209
error_free_clip_disconnect:
210
        invoke  HTTP_disconnect, [identifier]
211
        invoke  HTTP_free, [identifier]
4833 hidnplayr 212
error_free_clip:
213
        mcall   68, 13, [clipboard_data]
4832 hidnplayr 214
error:
215
        mov     [notify_struct.msg], sz_failed
216
        mcall   70, notify_struct
217
 
218
        mcall   -1
219
 
220
 
221
;---------------------------------------------------------------------
222
; Data area
223
;-----------------------------------------------------------------------------
224
align   4
225
@IMPORT:
226
 
227
library lib_http,               'http.obj'
228
 
229
import  lib_http, \
230
        HTTP_get,               'get', \
231
        HTTP_post,              'post', \
5541 hidnplayr 232
        HTTP_receive,           'receive', \
4832 hidnplayr 233
        HTTP_find_header_field, 'find_header_field', \
5541 hidnplayr 234
        HTTP_free,              'free', \
5770 hidnplayr 235
        HTTP_escape,            'escape', \
236
        HTTP_disconnect,        'disconnect'
4832 hidnplayr 237
 
238
IM_END:
239
 
240
file_struct:
241
        dd 0            ; read file
242
        dd 0            ; offset
243
        dd 0            ; reserved
244
        dd 32768        ; max file size
245
  .buf  dd 0            ; buffer ptr
246
        db 0
247
        dd param
248
 
249
notify_struct:
250
        dd 7            ; run application
251
        dd 0
252
  .msg  dd paste_url
253
        dd 0
254
        dd 0
255
        db '/sys/@notify', 0
256
 
257
sz_url                  db 'http://paste.kolibrios.org/', 0
258
sz_set_cookie           db 'set-cookie', 0
259
sz_location             db 'location', 0
260
sz_ctype                db 'application/x-www-form-urlencoded', 0
4834 hidnplayr 261
sz_failed               db '"Pasta!',10,'Paste failed!" -E', 0
4832 hidnplayr 262
 
263
sz_paste_head           db 'code='
264
.length = $ - sz_paste_head
265
sz_paste_tail           db '%0D%0A&language=text&webpage='
266
.length = $ - sz_paste_tail
267
 
268
sz_cookie               db 'Cookie: '
269
cookie                  db 0
270
                        rb 1024
271
 
272
paste_url               rb 1024
273
param                   rb 1024
274
 
275
identifier              dd 0
276
clipboard_data          dd 0
277
clipboard_data_length   dd 0
278
 
279
I_END:
280