/** * @author diogenes */ function local_messages() { this.dbGears = null; this.localServer = null; this.store = null; this.fileSubmitter = null; } local_messages.prototype.init_local_messages = function(){ if(this.dbGears == null) this.dbGears = google.gears.factory.create('beta.database'); if(this.localServer==null) this.localServer = google.gears.factory.create('beta.localserver'); if(this.store==null) this.store = this.localServer.createStore('store-expresso'); if(this.fileSubmitter == null) this.fileSubmitter = this.store.createFileSubmitter(); try { this.dbGears.open('database-expresso'); this.dbGears.execute('create table if not exists folder (folder text,uid_usuario int,unique(folder,uid_usuario))'); // inclusao do campo msg_source; // inclusao da constraint unique(id_folder), que permite gravar a mesma mensagem em diferentes pastas locais // (Rommel Cysne - rommel.cysne@serpro.gov.br - em 09/01/2009); this.dbGears.execute('create table if not exists mail' + ' (mail blob,original_id int,original_folder text,header blob,timestamp int,uid_usuario int,unseen int,id_folder int,' + //Codigo esta comentado para verificar a possibilidade de uso futuro (em 26/01/2009 - Rommel Cysne) // ' ffrom text, subject text, fto text, cc text, body text,unique (original_id,original_folder,uid_usuario,id_folder))'); ' ffrom text, subject text, fto text, cc text, body text, size int, unique (original_id,original_folder,uid_usuario,id_folder))'); this.dbGears.execute('create table if not exists anexo' + ' (id_mail int,nome_anexo text,url text,pid int)'); }catch(error) { //alert(error); } //some people that used old version of local messages could not have the size column. If it's the first version //with local messages you're using in expresso, this part of code can be removed try { this.dbGears.execute('alter table mail add column size int'); }catch(Exception) { } var rs = this.dbGears.execute('select rowid,header from mail where size is null'); while(rs.isValidRow()) { if(rs.field(1)!='N;') { var temp = connector.unserialize(rs.field(1)); this.dbGears.execute('update mail set size='+temp.Size+' where rowid='+rs.field(0)); } rs.next(); } //end of temporary code //trecho abaixo atualiza as bases locais antigas para permitir a gravacao do mesmo e-mail em diferentes pastas //de arquivamento local; var rs = this.dbGears.execute('select tbl_name from sqlite_master where tbl_name="mail" and sql like "%unique (original_id,original_folder,uid_usuario,id_folder)%"'); if(!rs.isValidRow()){ try{ this.dbGears.execute(' create table if not exists mailTmp (mail blob,original_id int,original_folder text,header blob,timestamp int,uid_usuario int,unseen int,id_folder int, ffrom text, subject text, fto text, cc text, body text, size int, unique (original_id,original_folder,uid_usuario,id_folder));'); this.dbGears.execute('begin transaction;'); this.dbGears.execute(' insert into mailTmp (mail,original_id,original_folder,header,timestamp,uid_usuario,unseen,id_folder,ffrom,subject,fto,cc,body,size) select mail,original_id,original_folder,header,timestamp,uid_usuario,unseen,id_folder,ffrom,subject,fto,cc,body,size from mail;'); this.dbGears.execute(' commit;'); this.dbGears.execute(' alter table mail rename to mailOriginal;'); this.dbGears.execute(' alter table mailTmp rename to mail;'); } catch(error){ this.dbGears.execute('rollback transaction;') ; alert(error); } } } /********************************** //Rommel Cysne local_messages.prototype.migra_db = function() { this.init_local_messages(); var rs = this.dbGears.execute('select tbl_name from sqlite_master where tbl_name="mail" and sql like "%unique (original_id,original_folder,uid_usuario,id_folder)%"'); if(!rs.isValidRow()){ try{ this.dbGears.execute(' create table if not exists mailTmp (mail blob,original_id int,original_folder text,header blob,timestamp int,uid_usuario int,unseen int,id_folder int, ffrom text, subject text, fto text, cc text, body text, size int, unique (original_id,original_folder,uid_usuario,id_folder));'); this.dbGears.execute('begin transaction;'); this.dbGears.execute(' insert into mailTmp (mail,original_id,original_folder,header,timestamp,uid_usuario,unseen,id_folder,ffrom,subject,fto,cc,body,size) select mail,original_id,original_folder,header,timestamp,uid_usuario,unseen,id_folder,ffrom,subject,fto,cc,body,size from mail;'); this.dbGears.execute(' commit;'); this.dbGears.execute(' alter table mail rename to mailOriginal;'); this.dbGears.execute(' alter table mailTmp rename to mail;'); } catch(error){ this.dbGears.execute('rollback transaction;') ; alert(error); } } this.finalize(); } /**********************************/ local_messages.prototype.drop_tables = function() { this.init_local_messages(); var rs = this.dbGears.execute('select url from anexo'); while(rs.isValidRow()) { this.store.remove(rs.field(0)); rs.next(); } this.dbGears.execute('drop table folder'); this.dbGears.execute('drop table mail'); this.dbGears.execute('drop table anexo'); this.finalize(); } local_messages.prototype.insere_email = function(msg_info,msg_header,anexos,folder) { this.init_local_messages(); var unseen; var login = msg_info.login; var original_id = msg_info.msg_number; var original_folder = msg_info.msg_folder; //Os campos abaixo precisam estar isolados para busca de mensagens em cima deles. var from = connector.serialize(msg_info.from); var subject = msg_info.subject; var body = msg_info.body; var to = connector.serialize(msg_info.toaddress2); var cc = connector.serialize(msg_info.cc); //var msg_source = msg_info.msg_source; var size = msg_header.Size; //Zero os campos que foram isolados para n�o ter informa��es duplicadas msg_info.from = null; msg_info.subject = null; msg_info.body = null; msg_info.to = null; msg_info.cc = null; //msg_header.Size=null; //If the mail was archieved in the same date the user received it, the date cames with the time. //here I solved it if(msg_header.udate.indexOf(":")!=-1) { msg_header.udate = msg_header.aux_date; } /** * The importance attribute can be empty, and javascript consider as null causing nullpointer. */ // if((msg_header.Importance == null) || (msg_header.Importance == "")) // msg_header.Importance = "Normal"; msg_header.aux_date = null; var mail = connector.serialize(msg_info); var header = connector.serialize(msg_header); var timestamp = msg_info.timestamp; var id_folder; var thumbs = msg_info.thumbs; if((folder==null) || (folder=="local_root")) folder = "Inbox"; else folder = folder.substr(6);//Retira a palavra "local_" var rs = this.dbGears.execute("select rowid from folder where folder=? and uid_usuario=?",[folder,account_id]); if(rs.isValidRow()) id_folder=rs.field(0); else { this.dbGears.execute("insert into folder (folder,uid_usuario) values (?,?)",["Inbox",account_id]); id_folder = this.dbGears.lastInsertRowId; } if(msg_info.Unseen=="U") { unseen = "1"; } var cb = function(url,success,captureId) { // alert("Capturado: " + url); } this.store.capture(msg_info.url_export_file,cb); var id_mail = this.dbGears.lastInsertRowId; try { for (i in anexos) { if(anexos[i]['name'].match('jpg')||anexos[i]['name'].match('gif')||anexos[i]['name'].match('png')) { var er_imagens = new RegExp("\\.\\/inc\\/show_embedded_attach.php\\?msg_folder=[\\w/]+\\&msg_num=[0-9]+\\&msg_part="+anexos[i]['pid']); // alert(er_imagens.source); var Result_imagens = er_imagens.exec(body); // alert(Result_imagens+anexos[i]['url']); body = body.replace(Result_imagens,anexos[i]['url']); // alert(body); } } this.dbGears.execute("insert into mail (mail,original_id,original_folder,header,timestamp,uid_usuario,unseen,id_folder,ffrom,subject,fto,cc,body,size) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?)",[mail,original_id,original_folder,header,timestamp,login,unseen,id_folder,from,subject,to,cc,body,size]); } catch (error) { //sucess = false; //alert("O e-mail de numero "+msg_info.msg_number+" foi anteriormente arquivado, se mais de um foi selecionado, o expresso ir� tentar arquivar os outros"); //throw error; //alert(error); this.finalize(); return false; } var rsUnseen = this.dbGears.execute("select header,unseen from mail where original_id=" + original_id); header = connector.unserialize(rsUnseen.field(0)); naoLida = header["Unseen"]; if( (naoLida == " ") && (unseen == "1") ) { try { header["Unseen"] = "U"; this.dbGears.execute("update mail set header='"+connector.serialize(header)+"',unseen="+unseen+" where original_id="+original_id); }catch(error) { this.finalize(); return false; } } var id_mail = this.dbGears.lastInsertRowId; this.insere_anexos(id_mail,anexos); this.finalize(); return true; } /*************************************************************************/ /* Funcao usada para exportar mensagens arquivadas localmente. * Rommel de Brito Cysne (rommel.cysne@serpro.gov.br) * em 22/12/2008. */ local_messages.prototype.local_messages_to_export = function(){ var msgs_to_export_id = get_selected_messages(); this.init_local_messages(); var handler_local_mesgs_to_export = function(data){ //alert("data - " + data + " - tipo - " + typeof(data)); download_attachments(null, null, data, null,null,'mensagens.zip'); } if(msgs_to_export_id){ //var msgs_to_export = msgs_to_export_id.split(","); var l_msg = "t"; var mesgs = new Array(); var subjects = new Array(); var hoje = new Date(); for(var i in msgs_to_export_id[0]){ var rs = this.dbGears.execute("select mail,subject from mail where rowid in ("+msgs_to_export_id[0][i]+")"); if(rs.isValidRow()){ mail = connector.unserialize(rs.field(0)); msg=mail['msg_source']; subject = rs.field(1); mesgs += msg; mesgs += "@@"; subjects += subject; subjects += "@@"; rs.next(); } else{ alert("erro"); } } source = escape(mesgs); subjects = escape(subjects); params = "folder="+msgs_to_export_id[1]+"&subjects="+subjects+"&mesgs="+source+"&l_msg="+l_msg+"&msgs_to_export="+msgs_to_export_id[0]; cExecute ("$this.exporteml.makeAll&", handler_local_mesgs_to_export, params); } return true; } /*************************************************************************/ local_messages.prototype.get_all_local_folder_messages= function(folder_name){ var mesgs = new Array(); var subjects = new Array(); var hoje = new Date(); var msgs_to_export = new Array(); var l_msg="t"; this.init_local_messages(); var query = "select mail,subject,mail.rowid from mail inner join folder on (mail.id_folder=folder.rowid) where folder.folder='" + folder_name + "'"; var rs = this.dbGears.execute(" "+query) var handler_local_mesgs_to_export = function(data){ //alert("data - " + data + " - tipo - " + typeof(data)); download_attachments(null, null, data, null,null,'mensagens.zip'); } var j=0; while (rs.isValidRow()){ msgs_to_export[j]=rs.field(2) mail = connector.unserialize(rs.field(0)); msg=mail['msg_source']; subject = rs.field(1); mesgs += msg; mesgs += "@@"; subjects += subject; subjects += "@@"; rs.next(); j++; } source = escape(mesgs); subjects = escape(subjects); params = "folder="+folder_name+"&subjects="+subjects+"&mesgs="+source+"&l_msg="+l_msg+"&msgs_to_export="+msgs_to_export; cExecute ("$this.exporteml.makeAll&", handler_local_mesgs_to_export, params); rs.close } local_messages.prototype.get_local_mail = function(id_mail) { this.init_local_messages(); var rs = this.dbGears.execute("select mail.rowid,mail.mail,mail.ffrom,mail.subject,mail.body,mail.fto,mail.cc,folder.folder,mail.original_id from mail inner join folder on mail.id_folder=folder.rowid where mail.rowid="+id_mail); var retorno = null; if(rs.isValidRow()) { retorno = rs.field(1); } retorno = connector.unserialize(retorno); if (retorno.source) { retorno.msg_number=rs.field(8); retorno.msg_folder=rs.field(7); show_msg(retorno); } else { retorno['fulldate'] = retorno['fulldate'].substr(0,16); retorno['from'] = connector.unserialize(rs.field(2)); retorno['subject'] = rs.field(3); retorno['body'] = rs.field(4); retorno['to'] = connector.unserialize(rs.field(5)); retorno['cc'] = connector.unserialize(rs.field(6)); retorno['local_message'] = true; retorno['msg_folder'] = "local_"+rs.field(7); //A pasta agora � local retorno['msg_number'] = rs.field(0); //O n�mero da mensagem � o rowid, pois a mensagem agora � local retorno['signature'] = ''; this.finalize(); return retorno; } } local_messages.prototype.insere_anexos = function(id_msg,anexos) { //N�o preciso iniciar nem fechar o gears, isso � tarefa de insere_email... for (var i = 0; i < anexos.length; i++) { this.dbGears.execute("insert into anexo (id_mail,nome_anexo,url,pid) values (?,?,?,?)", [id_msg, anexos[i]['name'],anexos[i]['url'],anexos[i]['pid']]); this.save_anexos(anexos[i]['url']); } } local_messages.prototype.save_anexos = function (url) { //N�o preciso iniciar nem fechar o gears, isso � tarefa de insere_email... var call_back = function(url,success,captureId) { // alert("Capturado: " + url); } this.store.capture(url,call_back); } local_messages.prototype.get_local_range_msgs = function(folder,msg_range_begin,emails_per_page,sort,sort_reverse,search) { this.init_local_messages(); var retorno = new Array(); msg_range_begin--; filter = " "; if(search=="FLAGGED") { filter = "and (header like '%\"Flagged\";s:1:\"F%' or header like '%\"Importance\";s:5:\"High%') "; } if(search=="UNSEEN") { filter = "and unseen = 1 "; } if(search=="SEEN") { filter = "and unseen = 0 "; } if (search=="ANSWERED") { filter = "and header like '%\"Answered\";s:1:\"A%' "; } sql = 'select rowid,header,size,timestamp,unseen from ('; sql += 'select mail.rowid as rowid,mail.header as header,mail.size as size,mail.timestamp as timestamp,mail.unseen as unseen,ltrim(ltrim(substr(UPPER(ffrom),21,length(ffrom)),\':\'),\'"\') as order_from,mail.subject from mail inner join folder on mail.id_folder=folder.rowid where mail.uid_usuario=? and folder.folder=? and lower(mail.ffrom) not like lower(?) '; sql += filter; sql += 'union '; sql += 'select mail.rowid as rowid,mail.header as header,mail.size as size,mail.timestamp as timestamp,mail.unseen as unseen,ltrim(ltrim(substr(UPPER(fto),7,length(fto)),\':\'),\'"\') as order_from,mail.subject from mail inner join folder on mail.id_folder=folder.rowid where mail.uid_usuario=? and folder.folder=? and lower(mail.ffrom) like lower(?) '; sql += filter; sql += ') order by ' if(sort == 'SORTFROM') { sql += 'order_from '; } if(sort == 'SORTARRIVAL') { sql += 'timestamp '; } if(sort == 'SORTSIZE') { sql += 'size '; } if(sort == 'SORTSUBJECT') { sql += 'UPPER(subject) '; } sql+= sort_reverse==0?"ASC ":"DESC "; sql +='limit ?,? '; var rs = this.dbGears.execute(sql,[account_id,folder,'%'+Element("user_email").value+'%',account_id,folder,'%'+Element("user_email").value+'%',msg_range_begin,emails_per_page]); var cont = 0; var rs3 = this.dbGears.execute('select count(*) from mail inner join folder on mail.id_folder=folder.rowid where mail.uid_usuario=? and folder.folder=?'+filter,[account_id,folder]); while (rs.isValidRow()) { //var email = rs.field(1); var head = rs.field(1); // alert(head); var codigoMail = rs.field(0); // alert(codigoMail); var rs2 = this.dbGears.execute('select count(*) from anexo where id_mail = '+codigoMail); var head_unserialized = connector.unserialize(head); head_unserialized.Size = rs.field(2); if(rs.field(4)==1) head_unserialized.Unseen = 'U'; //var email_unserialized = connector.unserialize(email); retorno[cont] = head_unserialized; if(retorno[cont]['subject']==null) retorno[cont]['subject']=''; retorno[cont]['msg_number'] = codigoMail; // retorno[cont]['Unseen'] = codigoMail; // alert(retorno[cont]); cont++; rs.next(); } retorno['num_msgs'] = rs3.field(0); rs3.close(); rs.close(); if(cont>0) rs2.close(); this.finalize(); return retorno; } local_messages.prototype.get_url_anexo = function(msg_number,pid) { this.init_local_messages(); /*var rs = this.dbGears.execute('select * from anexo'); while(rs.isValidRow()) { alert(rs.field(0)); alert(rs.field(1)); alert(rs.field(2)); alert(rs.field(3)); rs.next(); } alert('msg_number:'+msg_number); alert('pid:'+pid); */ var retorno; var rs = this.dbGears.execute('select url from anexo where id_mail='+msg_number+' and pid = '+"'"+pid+"'"); retorno = rs.field(0) this.finalize(); return retorno; } local_messages.prototype.getInputFileFromAnexo = function (element,url) { this.init_local_messages(); this.fileSubmitter.setFileInputElement(element,url); this.finalize(); } local_messages.prototype.finalize = function() { this.dbGears.close(); this.dbGears = null; } local_messages.prototype.delete_msgs = function(msgs_number,border_ID,folder) { this.init_local_messages(); var rs = this.dbGears.execute("select url from anexo where id_mail in ("+msgs_number+")"); while(rs.isValidRow()) { this.store.remove(rs.field(0)); rs.next(); } this.dbGears.execute("delete from anexo where id_mail in ("+msgs_number+")"); this.dbGears.execute("delete from mail where rowid in ("+msgs_number+")"); this.finalize(); if (msgs_number.length == 1) write_msg(get_lang("The message was deleted.")); else write_msg(get_lang("The messages were deleted.")); try { msgs_exploded = msgs_number.split(","); }catch(error) { msgs_exploded = msgs_number; } for (var i=0; i"); var friendly_filters = new Array(); if (filters[0].indexOf('ALL') != -1) { //Todos os filtros... friendly_filters.push("SUBJECT"); friendly_filters.push(filters[1]); friendly_filters.push("BODY"); friendly_filters.push(filters[1]); friendly_filters.push("FROM"); friendly_filters.push(filters[1]); friendly_filters.push("TO"); friendly_filters.push(filters[1]); friendly_filters.push("CC"); friendly_filters.push(filters[1]); } else { friendly_filters[0] = filters[0]; for (var i in filters) { if (i != 0) { var temp = filters[i].split(" "); friendly_filters.push(temp[0]); friendly_filters.push(temp[1]); } } } var sql = "select mail.header,folder.folder,mail.rowid,mail.timestamp from mail inner join folder on mail.id_folder=folder.rowid where mail.uid_usuario="+account_id + " and folder.folder in ("; for(var fnum in folders) { sql+="'"+folders[fnum]+"'"; if(fnum= 1){ return tmp + " kb"; }else{ return size + " b"; } } local_messages.prototype.aux_convert_filter_field = function(filter) { if((filter=="SUBJECT ") || (filter=="SUBJECT")) return "subject"; else if((filter=="BODY ") || (filter=="BODY")) return "body"; else if((filter=="FROM ") || (filter=="FROM")) return "ffrom"; else if((filter=="TO ") || (filter=="TO")) return "fto"; else if((filter=="CC ") || (filter=="CC")) return "cc"; } local_messages.prototype.has_local_mails = function() { this.init_local_messages(); var rs = this.dbGears.execute("select rowid from folder limit 0,1"); var retorno; if(rs.isValidRow()) retorno = true; else retorno = false; this.finalize(); return retorno; } local_messages.prototype.unarchive_msgs = function (folder,new_folder,msgs_number){ if(!new_folder) new_folder='INBOX'; this.init_local_messages(); var handler_unarchive = function(data) { if(data.error == '') write_msg(get_lang('All messages are successfully unarchived')); else alert(data.error); } if(msgs_number =='selected' || !msgs_number) { msgs_number = get_selected_messages() if (!msgs_number){ write_msg(get_lang('No selected message.')); return; } var rs = this.dbGears.execute("select mail,timestamp from mail where rowid in ("+msgs_number[0]+")"); var source=""; var timestamp=""; while(rs.isValidRow()) { mail=connector.unserialize(rs.field(0)); source_tmp=escape(mail.msg_source); source+="#@#@#@"+source_tmp; timestamp+="#@#@#@"+rs.field(1); rs.next(); } } else { var rs = this.dbGears.execute("select mail,timestamp from mail where rowid="+msgs_number[0]); mail=connector.unserialize(rs.field(0)); source=escape(mail.msg_source); timestamp=rs.field(1); } params="&folder="+new_folder+"&source="+source+"×tamp="+timestamp; cExecute ("$this.imap_functions.unarchive_mail&", handler_unarchive, params); } local_messages.prototype.get_msg_date = function (original_id){ this.init_local_messages(); //alert("select header from mail where original_id="+'"'+original_id+'"'); var rs = this.dbGears.execute("select mail from mail where original_id="+original_id); var tmp = connector.unserialize(rs.field(0)); var ret = new Array(); ret.fulldate = tmp.fulldate.substr(0,16); ret.smalldate = tmp.msg_day; ret.msg_day = tmp.msg_day; ret.msg_hour = tmp.msg_day; return ret; } local_messages.prototype.download_all_local_attachments = function(folder,id){ this.init_local_messages(); var rs = this.dbGears.execute("select mail from mail where rowid="+id); var tmp = connector.unserialize(rs.field(0)); source = tmp.msg_source; source = escape(source); var handler_source = function(data){ download_attachments(null, null, data, null,null,'anexos.zip'); } cExecute("$this.imap_functions.download_all_local_attachments",handler_source,"source="+source); } var expresso_local_messages; expresso_local_messages = new local_messages();