| 1 | " A Twitter client plugin for Vim |
|---|
| 2 | " Maintainer: Yuki Mitsui <silphire@gmail.com> |
|---|
| 3 | " License: distributed under the same terms of Vim itself |
|---|
| 4 | " Created: 2008-01-20 |
|---|
| 5 | " Version: 0.2 |
|---|
| 6 | " |
|---|
| 7 | |
|---|
| 8 | " if !has('ruby') |
|---|
| 9 | " echoerr 'missing Ruby interface.' |
|---|
| 10 | " exit |
|---|
| 11 | " endif |
|---|
| 12 | |
|---|
| 13 | command! -nargs=* Tw :call s:Twitter(<q-args>) |
|---|
| 14 | |
|---|
| 15 | " |
|---|
| 16 | function! s:UrlEscape(str) |
|---|
| 17 | " convert to UTF-8 |
|---|
| 18 | let l:u8str = a:str |
|---|
| 19 | if &encoding != 'utf-8' |
|---|
| 20 | if has('iconv') |
|---|
| 21 | let l:u8str = iconv(a:str, &encoding, 'utf-8') |
|---|
| 22 | else |
|---|
| 23 | echoerr 'missing iconv interface. compile with iconv or use UTF-8 encoding.' |
|---|
| 24 | endif |
|---|
| 25 | endif |
|---|
| 26 | let l:escstr = '' |
|---|
| 27 | let l:i = 0 |
|---|
| 28 | while l:i < strlen(l:u8str) |
|---|
| 29 | if l:u8str[l:i] =~# '[0-9A-Za-z_\-\.]' |
|---|
| 30 | let l:escstr .= l:u8str[l:i] |
|---|
| 31 | elseif l:u8str[l:i] == ' ' |
|---|
| 32 | let l:escstr .= '+' |
|---|
| 33 | elseif l:u8str[l:i] == '+' |
|---|
| 34 | let l:escstr .= '%2B' |
|---|
| 35 | else |
|---|
| 36 | let l:byte = char2nr(l:u8str[l:i]) |
|---|
| 37 | let l:escstr .= "%" |
|---|
| 38 | let l:escstr .= '0123456789ABCDEF'[l:byte / 0x10] |
|---|
| 39 | let l:escstr .= '0123456789ABCDEF'[l:byte % 0x10] |
|---|
| 40 | endif |
|---|
| 41 | let l:i += 1 |
|---|
| 42 | endwhile |
|---|
| 43 | return l:escstr |
|---|
| 44 | endfunction |
|---|
| 45 | |
|---|
| 46 | " |
|---|
| 47 | function! s:UrlCharUnEscape(ch) |
|---|
| 48 | endfunction |
|---|
| 49 | |
|---|
| 50 | " |
|---|
| 51 | function! s:UrlUnEscape(str) |
|---|
| 52 | endfunction |
|---|
| 53 | |
|---|
| 54 | " |
|---|
| 55 | function! s:GetMail() |
|---|
| 56 | if !exists("g:tvitter_mail") || strlen(g:tvitter_mail) == 0 |
|---|
| 57 | let g:tvitter_mail = input("mail: ") |
|---|
| 58 | endif |
|---|
| 59 | endfunction |
|---|
| 60 | |
|---|
| 61 | " |
|---|
| 62 | function! s:GetPassword() |
|---|
| 63 | if !exists("g:tvitter_pass") || strlen(g:tvitter_pass) == 0 |
|---|
| 64 | let g:tvitter_pass = inputsecret("password: ") |
|---|
| 65 | endif |
|---|
| 66 | endfunction |
|---|
| 67 | |
|---|
| 68 | " |
|---|
| 69 | function! s:CheckLength(msg) |
|---|
| 70 | let l:len = strlen(a:msg) |
|---|
| 71 | if l:len > 140 |
|---|
| 72 | let l:answer = input("Overlong update (".l:len." bytes). Really send? ") |
|---|
| 73 | if(strlen(l:answer) > 0 && l:answer[0] == 'y') |
|---|
| 74 | return 1 |
|---|
| 75 | else |
|---|
| 76 | return 0 |
|---|
| 77 | endif |
|---|
| 78 | endif |
|---|
| 79 | return 1 |
|---|
| 80 | endfunction |
|---|
| 81 | |
|---|
| 82 | " |
|---|
| 83 | function! s:ReflectResult(response) |
|---|
| 84 | echo a:response |
|---|
| 85 | if split(a:response, ' ')[1] == '200' |
|---|
| 86 | return 1 |
|---|
| 87 | else |
|---|
| 88 | unlet g:tvitter_mail |
|---|
| 89 | unlet g:tvitter_pass |
|---|
| 90 | return 0 |
|---|
| 91 | endif |
|---|
| 92 | endfunction |
|---|
| 93 | |
|---|
| 94 | " |
|---|
| 95 | function! s:UpdateStatus(msg) |
|---|
| 96 | call s:GetMail() |
|---|
| 97 | call s:GetPassword() |
|---|
| 98 | |
|---|
| 99 | if !s:CheckLength(a:msg) |
|---|
| 100 | return |
|---|
| 101 | endif |
|---|
| 102 | |
|---|
| 103 | let l:status = s:UrlEscape(a:msg) |
|---|
| 104 | let l:result = '' |
|---|
| 105 | if executable('curl') |
|---|
| 106 | let l:result = system('curl -s -D- -u '.g:tvitter_mail.':'.g:tvitter_pass.' http://twitter.com/statuses/update.json -d "status='.l:status.'"') |
|---|
| 107 | elseif executable('wget') |
|---|
| 108 | let l:result = system('wget -q -O- --save-headers --user='.g:tvitter_mail.' --password='.g:tvitter_pass.' http://twitter.com/statuses/update.json --post-data="status='.l:status.'"') |
|---|
| 109 | else |
|---|
| 110 | echoerr "either curl or wget is required" |
|---|
| 111 | endif |
|---|
| 112 | |
|---|
| 113 | call s:ReflectResult(split(l:result, '\n')[0]) |
|---|
| 114 | endfunction |
|---|
| 115 | |
|---|
| 116 | " |
|---|
| 117 | function! s:ReadFriendStatus() |
|---|
| 118 | call s:GetMail() |
|---|
| 119 | call s:GetPassword() |
|---|
| 120 | |
|---|
| 121 | if executable('curl') |
|---|
| 122 | let l:result = system('curl -s -D- -u '.g:tvitter_mail.':'.g:tvitter_pass.' http://twitter.com/statuses/friends_timeline.json') |
|---|
| 123 | elseif executable('wget') |
|---|
| 124 | let l:result = system('wget -q -O- --save-headers --user='.g:tvitter_mail.' --password='.g:tvitter_pass.' http://twitter.com/statuses/friends_timeline.json') |
|---|
| 125 | else |
|---|
| 126 | echoerr "either curl or wget is needed" |
|---|
| 127 | endif |
|---|
| 128 | let l:elems = split(l:result, '\n') |
|---|
| 129 | if s:ReflectResult(l:elems[0]) |
|---|
| 130 | let null = 'null' |
|---|
| 131 | let true = 'true' |
|---|
| 132 | let false = 'false' |
|---|
| 133 | execute 'let l:status='.l:elems[-1] |
|---|
| 134 | unlet null, true, false |
|---|
| 135 | endif |
|---|
| 136 | endfunction |
|---|
| 137 | |
|---|
| 138 | " Post an entry to Twitter |
|---|
| 139 | function! s:Twitter(msg) |
|---|
| 140 | if strlen(a:msg) == 0 |
|---|
| 141 | " entry list |
|---|
| 142 | call s:ReadFriendStatus() |
|---|
| 143 | else |
|---|
| 144 | " post a comment |
|---|
| 145 | call s:UpdateStatus(a:msg) |
|---|
| 146 | endif |
|---|
| 147 | endfunction |
|---|
| 148 | |
|---|