-- ************************************************************************** -- -- FreePOPs @abv.bg webmail interface -- -- Released under the GNU/GPL license -- Written by Georgi Saev <georgi_saev@abv.bg> -- ************************************************************************** --
-- Globals -- PLUGIN_VERSION = "0.1.2" PLUGIN_NAME = "abv.bg" PLUGIN_LICENSE = "GNU/GPL" PLUGIN_DOMAINS = {"@abv.bg","@gyuvetch.bg","@gbg.bg"} -- -------------------------------------------------------------------------- -- foo_globals= {
username="nothing", hostname="nothing", password="nothing", session_id="nothing", browser="nothing", stat_done=false, stat_fixed_id="nothing" } -- -------------------------------------------------------------------------- -- function init(pstate) freepops.export(pop3server) log.dbg("FreePOPs plugin '".. PLUGIN_NAME.."' version '"..PLUGIN_VERSION.."' started!\n")
-- the serialization module if freepops.dofile("serialize.lua") == nil then return POPSERVER_ERR_UNKNOWN end -- the browser module if freepops.dofile("browser.lua") == nil then return POPSERVER_ERR_UNKNOWN end -- the common module if freepops.dofile("common.lua") == nil then return POPSERVER_ERR_UNKNOWN end -- checks on globals freepops.set_sanity_checks() return POPSERVER_ERR_OK end --
-- -------------------------------------------------------------------------- -- -- Must save the mailbox name
function user(pstate,username1) local at = string.find(username1, "@") local username = string.sub(username1,0,at-1) local hostname = string.sub(username1,at+1) foo_globals.username = username foo_globals.hostname = hostname print("*** the user wants to login as "..foo_globals.username.."@"..foo_globals.hostname.."'") return POPSERVER_ERR_OK end -- -------------------------------------------------------------------------- -- -- Must login
function pass(pstate,password) foo_globals.password = password -- create a new browser local b = browser.new() -- store the browser object in globals foo_globals.browser = b -- create the data to post local post_data = string.format("username=%s&password=%s&hostname=%s",foo_globals.username,foo_globals.password,foo_globals.hostname) -- the uri to post to local post_uri = "http://mail05.abv.bg/app/servlet/bg.abv.mail.Login" -- post it local file,err = nil, nil file,err = b:post_uri(post_uri,post_data) -- search the session ID local _,_,id = string.find(file,"jsessionid=(%w+)") if id == nil then print("*** exiting") return POPSERVER_ERR_AUTH end foo_globals.session_id = id print("*** the user has logged in as "..foo_globals.username.."@"..foo_globals.hostname.."")
return POPSERVER_ERR_OK end
-- -------------------------------------------------------------------------- -- -- Fill the number of messages and their size function fix_no_sessid(pstate) print("*** WARNING: stat can't get sessid fixing") print("*** re-loging in") pass(nil,foo_globals.password) print("*** is it ok ?") local file,err = nil, nil local b = foo_globals.browser local session_id = foo_globals.session_id -- print("*** foo_globals.session_id= "..foo_globals.session_id.."") file,err = b:get_uri("http://mail05.abv.bg/app/j/box.jsp;jsessionid="..session_id.."?fid=10") local _,_,id = string.find(file,"jsessionid=(%w+)") if id == nil then -- print("*** this is err :") -- print(err) -- print("*** this is file :") -- print(file) -- print("*** it's not ok ?") return fix_no_sessid() else print("*** fixed") return id,file end end function stat(pstate) if foo_globals.stat_done == true then return POPSERVER_ERR_OK end local file,err = nil, nil local b = foo_globals.browser local session_id = foo_globals.session_id local j=0 file,err = b:get_uri("http://mail05.abv.bg/app/j/box.jsp;jsessionid="..session_id.."?fid=10") local _,_,id = string.find(file,"jsessionid=(%w+)") local _,_,num_pages = string.find(file,"Стр. 1 от (%w+)") if num_pages == nil then num_pages=1 end if id == nil then id,file =fix_no_sessid() end
local nummesg = 0 for tmp = 1,num_pages do file,err = b:get_uri("http://mail05.abv.bg/app/j/box.jsp;jsessionid="..session_id.."?fid=10&pid="..tmp.."") local _,_,id = string.find(file,"jsessionid=(%w+)") if id == nil then id,file =fix_no_sessid() end local e = ".*<tr>.*<td>.*<input>.*</td>.*<td>.*<img>.*</td>.*<td>[.*]{img}.*</td>.*<td>[.*]{img}.*</td>.*<td>[.*]{img}.*</td>.*<td>.*<a>.*</a>.*</td>.*<td>[.*]{a}[.*]{/a}.*</td>.*<td>.*</td>.*<td>.*</td>.*</tr>" local g = "O<O>O<O>O<X>O<O>O<O>O<O>O<O>O<O>[O]{O}O<O>O<O>[O]{O}O<O>O<O>[O]{O}O<O>O<O>O<O>O<O>O<O>O<O>[O]{O}[O]{O}O<O>O<O>O<O>O<O>X<O>O<O>" local x = mlex.match(file,e,g) --debug print x:print() nummesg = nummesg + x:count()
end print("*** "..nummesg.." pisma ob6to r1") set_popstate_nummesg(pstate,nummesg)
for tmp = 1,num_pages do file,err = b:get_uri("http://mail05.abv.bg/app/j/box.jsp;jsessionid="..session_id.."?fid=10&pid="..tmp.."") local _,_,id = string.find(file,"jsessionid=(%w+)") if id == nil then id,file =fix_no_sessid() end local e = ".*<tr>.*<td>.*<input>.*</td>.*<td>.*<img>.*</td>.*<td>[.*]{img}.*</td>.*<td>[.*]{img}.*</td>.*<td>[.*]{img}.*</td>.*<td>.*<a>.*</a>.*</td>.*<td>[.*]{a}[.*]{/a}.*</td>.*<td>.*</td>.*<td>.*</td>.*</tr>" local g = "O<O>O<O>O<X>O<O>O<O>O<O>O<O>O<O>[O]{O}O<O>O<O>[O]{O}O<O>O<O>[O]{O}O<O>O<O>O<O>O<O>O<O>O<O>[O]{O}[O]{O}O<O>O<O>O<O>O<O>X<O>O<O>" local x = mlex.match(file,e,g) --debug print x:print()
print("*** Getting page "..tmp.." out of "..num_pages.."")
for i=1,x:count() do local _,_,size = string.find(x:get(1,i-1),"(%d+)") local _,_,size_mult_k = string.find(x:get(1,i-1),"([Kk][Bb])") local _,_,size_mult_m = string.find(x:get(1,i-1),"([Mm][Bb])") local _,_,uidl = string.find(x:get(0,i-1),"value=\"(%d+)\"") if size_mult_k ~= nil then size = size * 1024 end if size_mult_m ~= nil then size = size * 1024 * 1024 end j=j+1 set_mailmessage_size(pstate,j,size) set_mailmessage_uidl(pstate,j,uidl) print("*** "..i.." pisma so far") end end
print("*** "..j.." pisma") foo_globals.stat_done = true return POPSERVER_ERR_OK end -- -------------------------------------------------------------------------- -- -- Fill msg uidl field function uidl(pstate,msg) return common.uidl(pstate,msg) end -- -------------------------------------------------------------------------- -- -- Fill all messages uidl field function uidl_all(pstate) return common.uidl_all(pstate) end -- -------------------------------------------------------------------------- -- -- Fill msg size function list(pstate,msg) return common.list(pstate,msg) end -- -------------------------------------------------------------------------- -- -- Fill all messages size function list_all(pstate) return common.list_all(pstate) end -- -------------------------------------------------------------------------- -- -- Unflag each message marked for deletion function rset(pstate) return common.rset(pstate) end -- -------------------------------------------------------------------------- -- -- Mark msg for deletion function dele(pstate,msg) return common.dele(pstate,msg) end -- -------------------------------------------------------------------------- -- -- Do nothing function noop(pstate) return common.noop(pstate) end --------------------------------------------------------------------------------
-- The callback factory for retr -- function retr_cb(data) local a = stringhack.new() return function(s,len) s = a:dothack(s).."\0" popserver_callback(s,data) return len,nil end end -- -------------------------------------------------------------------------- -- -- Get message msg, must call -- popserver_callback to send the data function retr(pstate,msg,pdata) local data=pdata -- we need the stat local st = stat(pstate) if st ~= POPSERVER_ERR_OK then return st end -- the callback local cb = retr_cb(data) -- some local stuff local session_id = foo_globals.session_id local b = foo_globals.browser local uri ="http://mail05.abv.bg/app/servlet/bg.abv.mail.GetData;jsessionid="..session_id.."?fid=10&mid="..get_mailmessage_uidl(pstate,msg).."&nid=0&eid=-1&charset=Cp1251&ac=d" -- tell the browser to pipe the uri using cb local f,rc = b:pipe_uri(uri,cb) if not f then log.error_print("Asking for "..uri.."\n") log.error_print(rc.."\n") return POPSERVER_ERR_NETWORK end return POPSERVER_ERR_OK end
-- -------------------------------------------------------------------------- -- -- Must quit without updating
function quit(pstate) return POPSERVER_ERR_OK end
|