1
0
mirror of https://github.com/SpaceVim/SpaceVim.git synced 2025-02-04 02:10:06 +08:00
SpaceVim/bundle/vim-mail/autoload/mail/client.vim

120 lines
3.4 KiB
VimL
Raw Normal View History

2021-10-23 15:34:04 +08:00
let s:job_id = 0
let s:job_noop_timer = ''
let s:JOB = SpaceVim#api#import('job')
function! mail#client#connect(ip, port)
2021-10-24 11:56:24 +08:00
if has('nvim')
let s:job_id = sockconnect('tcp', a:ip . ':' . a:port,
\ {
\ 'on_data' : function('s:on_stdout'),
\ }
\ )
call mail#logger#info('mail client job id:' . s:job_id)
else
let s:job_channel = ch_open(a:ip . ':' . a:port,
\ {
\ 'callback' : function('s:data_handle'),
\ }
\ )
call mail#logger#info('mail client job channel:' . s:job_channel)
endif
2021-10-23 15:34:04 +08:00
endfunction
" Wed, 06 Sep 2017 02:55:41 +0000 ===> 2017-09-06
let s:__months = {
2021-10-23 16:32:42 +08:00
\ 'Sep' : 9,
\ }
2021-10-23 15:34:04 +08:00
function! s:convert(date) abort
2021-10-23 16:32:42 +08:00
let info = split(a:date, ' ')
let year = info[3]
let m = get(s:__months, info[2], 00)
let day = len(info[1]) == 1 ? '0' . info[1] : info[1]
return join([year, m, day], '-')
2021-10-23 15:34:04 +08:00
endfunction
function! s:noop(id) abort
2021-10-23 16:32:42 +08:00
call mail#client#send(mail#command#noop())
2021-10-23 15:34:04 +08:00
endfunction
let s:_mail_id = -1
let s:_mail_date = ''
let s:_mail_from = ''
let s:_mail_subject = ''
let s:mail_unseen = 0
2021-10-24 11:56:24 +08:00
function! s:data_handle(...) abort
call s:on_stdout(1,1, [a:1])
2021-10-23 15:34:04 +08:00
endfunction
function! s:on_stdout(id, data, event) abort
2021-10-24 11:56:24 +08:00
for data in a:data
call mail#logger#info('STDOUT: ' . data)
if data =~ '^\* \d\+ FETCH '
let s:_mail_id = matchstr(data, '\d\+')
elseif data =~ '^From: '
let s:_mail_from = substitute(data, '^From: ', '', 'g')
let s:_mail_from .= repeat(' ', 50 - len(s:_mail_from))
elseif data =~ '^Date: '
let s:_mail_date = s:convert(substitute(data, '^Date: ', '', 'g'))
elseif data =~ '^Subject: '
let s:_mail_subject = substitute(data, '^Subject: ', '', 'g')
call mail#client#mailbox#updatedir(s:_mail_id, s:_mail_from, s:_mail_date, s:_mail_subject, mail#client#win#currentDir())
elseif data =~ '* STATUS INBOX'
let s:mail_unseen = matchstr(data, '\d\+')
elseif data =~# '* OK Coremail System IMap Server Ready'
call mail#client#send(mail#command#login(g:mail_imap_login, g:mail_imap_password))
call mail#client#send(mail#command#select(mail#client#win#currentDir()))
call mail#client#send(mail#command#fetch('1:15', 'BODY[HEADER.FIELDS ("DATE" "FROM" "SUBJECT")]'))
call mail#client#send(mail#command#status('INBOX', '["RECENT"]'))
let s:job_noop_timer = timer_start(20000, function('s:noop'), {'repeat' : -1})
endif
endfor
2021-10-23 15:34:04 +08:00
endfunction
function! s:on_stderr(id, data, event) abort
2021-10-23 16:32:42 +08:00
for data in a:data
2021-10-24 11:56:24 +08:00
call mail#logger#error('STDERR: ' . data)
2021-10-23 16:32:42 +08:00
endfor
2021-10-23 15:34:04 +08:00
endfunction
function! s:on_exit(id, data, event) abort
2021-10-23 16:32:42 +08:00
let s:job_id = 0
if !empty(s:job_noop_timer)
call timer_stop(s:job_noop_timer)
let s:job_noop_timer = ''
endif
2021-10-23 15:34:04 +08:00
endfunction
function! mail#client#send(command)
2021-10-24 11:56:24 +08:00
call mail#logger#info('Send command: ' . a:command)
if has('nvim')
if s:job_id >= 0
call chansend(s:job_id, [a:command, ''])
else
call mail#logger#info('skipped!, job id is:' . s:job_id)
endif
2021-10-23 16:32:42 +08:00
else
2021-10-24 11:56:24 +08:00
call ch_sendraw(s:job_channel, a:command . "\n")
2021-10-23 16:32:42 +08:00
endif
2021-10-23 15:34:04 +08:00
endfunction
function! mail#client#open()
2021-10-23 16:32:42 +08:00
if s:job_id == 0
if !empty(g:mail_imap_login) && !empty(g:mail_imap_password)
call mail#client#connect(g:mail_imap_host, g:mail_imap_port)
2021-10-23 15:34:04 +08:00
endif
2021-10-23 16:32:42 +08:00
endif
call mail#client#win#open()
2021-10-23 15:34:04 +08:00
endfunction
function! mail#client#unseen()
2021-10-23 16:32:42 +08:00
return s:mail_unseen
2021-10-23 15:34:04 +08:00
endfunction