# GMail # supports receiving and sending of mail with attachments # # @Requires: 1.1 RC2 # @version: 1.4.2 # german texts (with MrPostman 1.2.2+) # @version: 1.4.1 # gmail attachments changed - corrected # @version: 1.4 # gmail login changed (2005-09-11) # @version: 1.3 # add the unread mail only option # @Version: 1.2.2 # google replaced gmail with mail in URLs (because of a german trade mark) # @Version: 1.2.1 # getting headers fixed after web change # @Version: 1.2 # some small fixes # @Version: 1.1 # webmail change: fixed by setting cookie mode sub getInfo local(info) info.name = "gmail" info.authors[0] = "Martin Vlcek " info.authors[1] = "Florent Kaisser " info.version = "1.4.2" info.updateService = "" info.documentationLink = "" info.options["maxNum"].order = 1 info.options["maxNum"].value = 1000 info.options["maxNum"].label = "Max Number of Messages" info.options["maxNum"].description = "The maximum number of messages to check for retrieval" info.options["onlyUnread"].order = 2 info.options["onlyUnread"].value = false info.options["onlyUnread"].label = "Unread mail only" info.options["onlyUnread"].description = "Get the unread mail only" #--- german texts info.options["maxNum"].de.label = "Max. Anzahl Nachrichten" info.options["maxNum"].de.description = "Die maximale Anzahl von Nachrichten, die überprüft bzw. heruntergeladen werden sollen" info.options["onlyUnread"].de.label = "Nur ungelesene Nachrichten" info.options["onlyUnread"].de.description = "Nur ungelesene Nachrichten herunterladen" return info end sub getExtensions return list("@gmail.com") end sub login(username,password) local(loginurl,params,status,page,found,loginaction) #--- only works with cookie policy in compatibility mode: setcookiemode("COMPATIBILITY") #--- no message sizes available, date is in correct format #--- let's log in: #--- (MVL) 20050911 login (url, ...) changed #status,page = get("https://mail.google.com/mail") #loginurl = "https://www.google.com/accounts/ServiceLoginBoxAuth?service=mail&continue=" & urlencode("http://mail.google.com/mail") & "&Email=" & urlencode(username) & "&Passwd=" & urlencode(password) #status,page = get(loginurl) #addcookie("GMAIL_RTT","541",".google.com","/") params["service"] = "mail" params["continue"] = "http://mail.google.com/mail" params["Email"] = username params["Passwd"] = password params["null"] = "Sign in" params["rm"] = "false" params["hl"] = "en" loginurl = "https://www.google.com/accounts/ServiceLoginAuth" status,page = post(loginurl,params) #--- maybe there is a better way, but the following calls seem to be necessary: #--- (MVL) 20050911 not needed anymore #found,url = match(page,"\"(CheckCookie\?[^\"]*)\"") #returnif(!found,false) #url = "https://www.google.com/accounts/" & url #status,page = get(url) found,url = match(page,"location.replace\(\"([^\"]*)\"") status,page = get(url) setbaseurl("http://mail.google.com/mail") #--- random number for the following calls to gmail: zx = getZx() return true end sub loginForReceive(username,password) local(ok,url,status,page,found,numfound) local(first,pagesize,number) local(pattern_msga,pattern_msgs,pattern_page,msga,msga1,ma,msgs1,ms) ok = login(username,password) returnif(!ok,false) url = "/mail?search=inbox&view=tl&start=0&init=1&zx=" & zx status,page = get(url) pattern_msga = "D\(\[\"t\",(.*?)\]\s*\);" pattern_page = "D\(\[\"ts\",(\d+),(\d+),(\d+)," #---get all message definition arrays D ... msga = list() found,first,pagesize,number = match(page,pattern_page) numfound,msga = matchall(page,pattern_msga) #---... on all pages while (integer(first) + integer(pagesize) < integer(number) and integer(first) + integer(pagesize) < integer(info.options["maxNum"].value)) url = "/mail?search=inbox&view=tl&start=" & (integer(first)+integer(pagesize)) & "&zx=" & zx status,page = get(url) found,first,pagesize,number = match(page,pattern_page) msga1 = list() numfound,msga1 = matchall(page,pattern_msga) msga = join(msga,msga1) end #---get all messages msgs = list() pattern_msgs = "\[\"([^\"]*)\",(?:[^\[\]]|\[[^\]]*\])*\]" if (info.options["onlyUnread"].value) pattern_msgs = "\[\"([^\"]*)\",0*1,(?:[^\[\]]|\[[^\]]*\])*\]" end foreach(msga,ma) msgs1 = list() numfound,msgs1[].id = matchall(ma,pattern_msgs) msgs = join(msgs,msgs1) end #---message sizes unknown - will automatically be set to 1kB return true end sub receive(msg) local(url,status,page,found,numfound,msg,dummy,ss_pat,sc_pat,scg_pat,slg_pat) local(pattern_header,pattern_body,pattern_attall,pattern_attach) local(bodyparts,atttexts) url = "/mail?view=cv&search=inbox&th=" & msg.id & "&zx=" & zx status,page = get(url,map(),map(),"UTF-8") #--- headers and body can be found in definition functions ss_pat = "\"[^\"]*\"" sc_pat = "\"(?:[^\"\\\\]|\\\\.)*\"" scg_pat = "\"((?:[^\"\\\\]|\\\\.)*)\"" slg_pat = "\[\s*((?:" & sc_pat & "(?:\s*,\s*" & sc_pat & ")*)?)\s*\]" pattern_header = "D\(\[\"mi\",[-\d]+,[-\d]+," & ss_pat & ",[-\d]+,\"[-\d]+\"," & sc_pat & "," & sc_pat & "," & scg_pat & "," & sc_pat & "," & sc_pat & "\s*,\s*" & slg_pat & "\s*,\s*" & slg_pat & "\s*,\s*" & slg_pat & "\s*,\s*" & slg_pat & "\s*,\s*" & scg_pat & "\s*,\s*" & scg_pat #pattern_header = "D\(\[\"mi\",[-\d]+,[-\d]+," & ss_pat & ",[-\d]+,\"[-\d]+\"," & sc_pat & "," & scg_pat & "," & sc_pat & "," & sc_pat & "\s*,\s*" & slg_pat & "\s*,\s*" & slg_pat & "\s*,\s*" & slg_pat & "\s*,\s*" & slg_pat & "\s*,\s*" & scg_pat & "\s*,\s*" & scg_pat #pattern_header = "D\(\[\"mi\",\"?-?\d+\"?,\"?-?\d+\"?,\"(?:[^\"\\\\]*|\\\\.)*?\",\"?-?\d+\"?,\"?-?\d+\"?,\"(?:[^\"\\\\]*|\\\\.)*?\",\"((?:[^\"\\\\]*|\\\\.)*?)\",\"(?:[^\"\\\\]*|\\\\.)*?\",\"(?:[^\"\\\\]*|\\\\.)*?\",\"((?:[^\"\\\\]*|\\\\.)*?)\",\"((?:[^\"\\\\]*|\\\\.)*?)\",\"(?:[^\"\\\\]*|\\\\.)*?\",\"(?:[^\"\\\\]*|\\\\.)*?\",\"((?:[^\"\\\\]*|\\\\.)*?)\",\"((?:[^\"\\\\]*|\\\\.)*?)\"," pattern_body = "D\(\[\"mb\",\"((?:[^\"\\\\]*|\\\\.)*?)\"" pattern_attall = "D\(\[\"ma\",(.*)" #pattern_attall = "D\(\[\"ma\",\"((?:[^\"\\\\]*|\\\\.)*?)\"" pattern_attach = "([^<]*?).*?href=\\\\+\"(/mail/\?view=att&disp=att[^\\\\]*)\\\\+\"" #pattern_attach = "([^<]*?).*?href=\\\\\"(/mail\?view=att&disp=[^\\\\]*)\\\\\"" #pattern_attach = "\\\\([^\\\\]*?)\\\\.*?href=\\\\\"(/mail\?view=att&disp=[^\\\\]*)\\\\\"" #pattern_attach = "D\(\[\"ma\",.*?\\\\([^\\\\]*?)\\\\.*?href=\\\\\"(/mail\?view=att&disp=attd[^\\\\]*)\\\\\"" #--- get header fields found,msg.from,msg.to,msg.cc,msg.bcc,dummy,msg.date,msg.subject = match(page,pattern_header) #--- remove all backslashes in subject, ... msg.subject = replaceall(msg.subject,"\\\\(.)","$1") msg.from = replaceall(msg.from,"\\\\(.)","$1") msg.to = string(getaddresses(msg.to),", ") msg.cc = string(getaddresses(msg.cc),", ") msg.bcc = string(getaddresses(msg.bcc),", ") #--- get body text numfound,bodyparts[] = matchall(page,pattern_body) msg.text = string(bodyparts,"") #--- replace line feed and tab and remove all backslashes msg.text = replaceall(msg.text,"\\\\r","") msg.text = replaceall(msg.text,"\\\\n","\n") msg.text = replaceall(msg.text,"\\\\t","\t") msg.text = replaceall(msg.text,"\\\\(.)","$1") #--- remove all javascript calls msg.text = replaceall(msg.text,"\son\w+=\"(?:[^\"\\\\]*|\\\\.)*\"","") msg.text = "\n" & msg.text & "\n" msg.mimetype = "text/html" msg.charset = "UTF-8" #--- get attachments #numfound,atttexts[] = matchall(page,pattern_attall) found,partwithatt = match(page,pattern_attall) if (found) numfound,msg.attachments[].text,msg.attachments[].link = matchall(partwithatt,pattern_attach,true) end return msg end sub getaddresses(liststr) local(scg_pat,numfound,sitems,sitems2,sitem) scg_pat = "(?:^\s*|,\s*)?\"((?:[^\"\\\\]|\\\\.)*)\"" numfound,sitems = matchall(liststr,scg_pat) sitems2 = list() foreach (sitems,sitem) sitems2[size(sitems2)] = replaceall(sitem,"\\\\(.)","$1") end return sitems2 end sub delete(msgs) local(params,status,page,msg) params["act"] = "tr" params["at"] = getcookie("GMAIL_AT") params["vp"] = "" foreach(msgs,msg) params["t#" & msg.id] = msg.id end status,page = post("/mail?search=inbox&view=tl&start=0",params) return (status == 200) end sub loginForSend(username,password) return login(username,password) end sub send(msg) local(params,i,fileparams,status,page) params["view"] = "sm" params["rm"] = "" params["th"] = "" params["draft"] = "" #params["cmid"] = ... params["at"] = getcookie("GMAIL_AT") params["to"] = msg.to params["cc"] = msg.cc params["bcc"] = msg.bcc params["subject"] = msg.subject params["msgbody"] = msg.text i = 0 fileparams = map() foreach(msg.attachments,att) fileparams["file" & i] = att.link i = i + 1 end status,page = postmultipart("/mail",fileparams,params) return (status == 200) end sub logout status,page = get("/mail?logout&zx=" & zx) return (status == 200) end sub getZx local(i,r,c) i = 0 r = "" c = list("0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f") while (i < 26) r = r & c[random(16)] i = i + 1 end return r end