Subversion Repositories Kolibri OS

Rev

Rev 5770 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;;                                                                 ;;
  3. ;; Copyright (C) KolibriOS team 2014-2017. All rights reserved.    ;;
  4. ;; Distributed under terms of the GNU General Public License       ;;
  5. ;;                                                                 ;;
  6. ;;  pasta.asm - Paste text to dpaste.com from a file or from       ;;
  7. ;;              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_free_clip
  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.         cmp     eax, 1
  79.         je      error
  80.  
  81. ; Verify if we can work with it
  82.         mov     [clipboard_data], eax
  83.         cmp     dword[eax + 4], 0               ; text ?
  84.         jne     error_free_clip
  85.  
  86. ; Save length in [clipboard_data_length]
  87.         mov     ecx, dword[eax]
  88.         sub     ecx, 12
  89.         mov     [clipboard_data_length], ecx
  90.  
  91. ; Skip clipboard containter params for escape proc
  92.         add     eax, 12
  93.  
  94. escape:
  95. ; Escape all characters that need escaping
  96.         invoke  HTTP_escape, eax
  97.         test    eax, eax
  98.         jz      error_free_clip
  99.         mov     [clipboard_data_length], ebx
  100.  
  101.         push    eax
  102.         mcall   68, 13, [clipboard_data]
  103.         pop     [clipboard_data]
  104.  
  105. ; Post to the server
  106.         mov     ecx, [clipboard_data_length]
  107.         add     ecx, sz_paste_head.length
  108.         invoke  HTTP_post, sz_url, 0, FLAG_BLOCK, 0, sz_ctype, ecx
  109.         test    eax, eax
  110.         jz      error_free_clip_disconnect
  111.         mov     [identifier], eax
  112.  
  113. ; Send the data to the server
  114.         invoke  HTTP_send, [identifier], sz_paste_head, sz_paste_head.length
  115.  
  116.         push    [clipboard_data]
  117.         pop     [send_ptr]
  118.   .again:
  119.         invoke  HTTP_send, [identifier], [send_ptr], [clipboard_data_length]
  120.         cmp     eax, -1
  121.         je      error_free_clip_disconnect
  122.         test    eax, eax
  123.         jz      error_free_clip_disconnect
  124.         add     [send_ptr], eax
  125.         sub     [clipboard_data_length], eax
  126.         ja      .again
  127.  
  128. ; Free the data
  129.         mcall   68, 13, [clipboard_data]
  130.  
  131.   .again2:
  132.         invoke  HTTP_receive, [identifier]
  133.         test    eax, eax
  134.         jnz     .again2
  135.  
  136.         invoke  HTTP_disconnect, [identifier]
  137.  
  138.         mov     ebp, [identifier]
  139.         test    [ebp + http_msg.flags], 0xffff0000             ; Test error bits
  140.         jnz     error_free_http
  141.         test    [ebp + http_msg.flags], FLAG_GOT_HEADER
  142.         jz      error_free_http
  143.         cmp     [ebp + http_msg.status], 201    ; created
  144.         jne     error_http_code
  145.  
  146.         invoke  HTTP_find_header_field, [identifier], sz_location
  147.         test    eax, eax
  148.         jz      error_free_http
  149.  
  150.         mov     esi, eax
  151.         call    notify
  152.  
  153.         mcall   54, 3                   ; Delete last slot in the clipboard
  154.  
  155.         mov     dword[notify_msg-4], ecx
  156.         mov     dword[notify_msg+0], 0   ; Text
  157.         mov     dword[notify_msg+4], 1   ; cp0866
  158.         mcall   54, 2, , notify_msg-4    ; Write URL to the clipboard
  159.  
  160.         invoke  HTTP_free, [identifier]
  161.         mcall   -1
  162.  
  163. error_http_code:
  164.         lea     esi, [ebp + http_msg.http_header]
  165.         call    notify
  166. error_free_http:
  167.         invoke  HTTP_free, [identifier]
  168.         jmp     error
  169. error_free_clip_disconnect:
  170.         invoke  HTTP_disconnect, [identifier]
  171.         invoke  HTTP_free, [identifier]
  172. error_free_clip:
  173.         mcall   68, 13, [clipboard_data]
  174. error:
  175.         mov     [notify_struct.msg], sz_failed
  176.         mcall   70, notify_struct
  177.  
  178.         mcall   -1
  179.  
  180.  
  181. notify:
  182.  
  183.         mov     edi, notify_msg.text
  184.   .msg_loop:
  185.         lodsb
  186.         stosb
  187.         cmp     al, 13
  188.         je      .msg_end
  189.         cmp     al, 10
  190.         je      .msg_end
  191.         cmp     al, 0
  192.         je      .msg_end
  193.         jmp     .msg_loop
  194.   .msg_end:
  195.         dec     edi
  196.         lea     ecx, [edi - notify_msg + 4]
  197.         mov     eax, '" -t'
  198.         stosd
  199.         mov     ax, 'O'
  200.         stosw
  201.         mov     [notify_struct.msg], notify_msg
  202.         mcall   70, notify_struct
  203.  
  204.         ret
  205.  
  206.  
  207. ;---------------------------------------------------------------------
  208. ; Data area
  209. ;-----------------------------------------------------------------------------
  210. align   4
  211. @IMPORT:
  212.  
  213. library lib_http,               'http.obj'
  214.  
  215. import  lib_http, \
  216.         HTTP_get,               'get', \
  217.         HTTP_post,              'post', \
  218.         HTTP_send,              'send', \
  219.         HTTP_receive,           'receive', \
  220.         HTTP_find_header_field, 'find_header_field', \
  221.         HTTP_free,              'free', \
  222.         HTTP_escape,            'escape', \
  223.         HTTP_disconnect,        'disconnect'
  224.  
  225. IM_END:
  226.  
  227. file_struct:
  228.         dd 0            ; read file
  229.         dd 0            ; offset
  230.         dd 0            ; reserved
  231.         dd 32768        ; max file size
  232.   .buf  dd 0            ; buffer ptr
  233.         db 0
  234.         dd param
  235.  
  236. notify_struct:
  237.         dd 7            ; run application
  238.         dd 0
  239.   .msg  dd notify_msg
  240.         dd 0
  241.         dd 0
  242.         db '/sys/@notify', 0
  243.  
  244. sz_url                  db 'http://dpaste.com/api/v2/', 0
  245. sz_location             db 'location', 0
  246. sz_ctype                db 'application/x-www-form-urlencoded', 0
  247. sz_failed               db '"Pasta!',10,'Paste failed!" -E', 0
  248.  
  249. sz_paste_head           db 'content='
  250. .length = $ - sz_paste_head
  251.  
  252. notify_msg              db '"Pasta!',10
  253.   .text                 rb 1024
  254.  
  255. param                   rb 1024
  256.  
  257. identifier              dd 0
  258. clipboard_data          dd 0
  259. clipboard_data_length   dd 0
  260. send_ptr                dd ?
  261.  
  262. I_END:
  263.  
  264.  
  265.  
  266.