Module: Texmailer
- Defined in:
- lib/texmailer/send.rb,
lib/texmailer/wapp.rb,
lib/texmailer/iview.rb,
lib/texmailer/adbook.rb,
lib/texmailer/config.rb,
lib/texmailer/drafts.rb
Defined Under Namespace
Classes: Adbook, DontCache, Iview
Constant Summary collapse
- CACHEVERSION =
'0.1'
- ABVERSION =
'0.1'
Instance Method Summary collapse
- #discarddraft(mailid) ⇒ Object
- #getdrafts ⇒ Object
- #getlist(flist) ⇒ Object
- #getpvloc(mailid) ⇒ Object
- #htm2mail(thehtml_in, thetex) ⇒ Object
- #loaddraft(mailid) ⇒ Object
-
#loadflist ⇒ Object
From list.
-
#loadiparams ⇒ Object
IMAP Parameters.
-
#loadparams ⇒ Object
SMTP Parameters.
- #makerc ⇒ Object
- #makereply(cache, mailid) ⇒ Object
- #msmtp(req, message) ⇒ Object
- #nicecache(cache) ⇒ Object
- #preview(mailid, req) ⇒ Object
- #savedraft(mailid, req) ⇒ Object
- #saveflist(req) ⇒ Object
- #saveiparams(req) ⇒ Object
- #saveparams(req) ⇒ Object
- #sendmail(mailid, req) ⇒ Object
- #shortem(emlst) ⇒ Object
- #stripcss(html) ⇒ Object
- #webapp ⇒ Object
Instance Method Details
#discarddraft(mailid) ⇒ Object
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/texmailer/drafts.rb', line 185 def discarddraft(mailid) svdir = $userdir + '/drafts/'+mailid if File.directory? svdir lst = Dir[svdir+'/*']; lst.each {|f| File.delete(f)} Dir.rmdir(svdir) end svdir = $userdir + '/preview/'+mailid if File.directory? svdir lst = Dir[svdir+'/*']; lst.each {|f| File.delete(f)} Dir.rmdir(svdir) end Dir.chdir($userdir) end |
#getdrafts ⇒ Object
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 |
# File 'lib/texmailer/drafts.rb', line 202 def getdrafts ids = Dir.glob($userdir + '/drafts/*',File::FNM_PATHNAME) s = ids.collect { |i| if ! File.directory? i nil else mid = File.basename i mailto = File.read(i + '/mailto') if File.readable? i + '/mailto' drtime = File.mtime(i + '/mailto') if File.readable? i + '/mailto' mailto ||= '' mailto = '(No Recipient)' if mailto.length == 0 drtime ||= File.mtime(i) subject = File.read(i + '/subject') if File.readable? i + '/subject' subject ||= '' subject = '(No Subject)' if subject.length == 0 [mid,CGI.escapeHTML(shortem(mailto)),CGI.escapeHTML(subject),drtime] end } s.compact! s.sort! {|a,b| b[3] <=> a[3]} return s end |
#getlist(flist) ⇒ Object
10 11 12 13 14 15 16 17 18 |
# File 'lib/texmailer/config.rb', line 10 def getlist flist return [] if ! flist flist.strip! return [] if flist.length == 0 a = flist.split(",") a.collect! {|x| x.gsub(/\s/,'').gsub(/[\<\>]/,'') } end |
#getpvloc(mailid) ⇒ Object
111 112 113 114 115 |
# File 'lib/texmailer/drafts.rb', line 111 def getpvloc(mailid) pvlnk = '/preview/' + mailid + '/t.html' return pvlnk if File.readable? $userdir+pvlnk return '/blankprev.html' end |
#htm2mail(thehtml_in, thetex) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/texmailer/send.rb', line 57 def htm2mail(thehtml_in,thetex) rstring = (0...15).map{ ('A'..'Z').to_a[rand(26)] }.join; rstring2 = (0...15).map{ ('A'..'Z').to_a[rand(26)] }.join; marker = rstring + "TEXMARKERSHOULDBEUNIQUE12375689" marker2 = rstring2 + "TEXMARKERSHOULDBEUNIQUE12375689" body =<<EOF MIME-Version: 1.0 Content-type: multipart/mixed; boundary=#{marker} --#{marker} EOF # Everything's in the body thehtml_in = stripcss thehtml_in thehtml_out = "" # Find attachments r = /(src)="(.*?)"/ attachs = Array.new mimetypes = Array.new cid = 0 while (m = r.match(thehtml_in)) thehtml_out = thehtml_out + m.pre_match if File.readable?(m[2]) then f = File.read(m[2]) thehtml_out = thehtml_out + m[1] + "=\"cid:#{rstring}%04d\"" % cid attachs[cid] = [f].pack("m") mimetypes[cid] = 'image/' + case m[2][(m[2].rindex('.')+1)..-1] when 'gif' then 'jpeg' when 'png' then 'png' else 'jpeg' end cid = cid + 1 else thehtml_out = thehtml_out + m[0] end thehtml_in = m.post_match end thehtml_out = thehtml_out + thehtml_in thehtml_out = [thehtml_out].pack("M") # Add html part body = body + <<EOF Content-Type: multipart/alternative; boundary=#{marker2} --#{marker2} Content-Type: text/plain Content-Transfer-Encoding: 8bit Content-Description: TeXMailer #{TMVERSION} #{thetex} --#{marker2} Content-Type: text/html Content-Transfer-Encoding: quoted-printable #{thehtml_out} --#{marker2}-- EOF # Add attachments for i in (0..(attachs.length-1)) cid = "<#{rstring}%04d>" % i; data = attachs[i] body = body + <<EOF2 --#{marker} Content-Disposition: inline Content-Type: #{mimetypes[i]} Content-Transfer-Encoding: base64 Content-Id: #{cid} #{data} EOF2 end body = body + <<EOF3 --#{marker}-- EOF3 return body end |
#loaddraft(mailid) ⇒ Object
175 176 177 178 179 180 181 182 183 |
# File 'lib/texmailer/drafts.rb', line 175 def loaddraft(mailid) svdir = $userdir + '/drafts/'+mailid a = Array.new ["mailto","mailcc","mailbcc","subject","body"].each { |s| fname = svdir+'/'+s a.push File.readable?(fname) ? File.read(fname) : "" } return a[0],a[1],a[2],a[3],a[4] end |
#loadflist ⇒ Object
From list
4 5 6 7 8 |
# File 'lib/texmailer/config.rb', line 4 def loadflist f = $userdir + "/flist.cfg" return '' if ! File.readable? f return File.read(f) end |
#loadiparams ⇒ Object
IMAP Parameters
26 27 28 29 30 31 32 33 34 35 |
# File 'lib/texmailer/config.rb', line 26 def loadiparams p = $iv.getsettings ['host','port','ssl','igcert','user','password'].each { |x| p[x] = String(p[x]) if p[x] p[x] ||= 'false' if x == 'ssl' || x == 'igcert' p[x] ||= '' } return p end |
#loadparams ⇒ Object
SMTP Parameters
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/texmailer/config.rb', line 51 def loadparams p = Hash.new params = [['from',0],['mailfrom', 0],['host', 0],['port', 0],['ssl', 1],['igcert', 1],['auth', 1],['user', 0],['pass', 0]] fnm = $userdir + '/smtp.cfg' if File.readable? fnm lns = IO.readlines(fnm) (1..((lns.length < params.length) ? lns.length : params.length)).each {|i| p[params[i-1][0]] = lns[i-1].sub(/\n/m,'') } end params.each { |x| p[x[0]] ||= '' } params.each { |x| p[x[0]] == 'false' if x[1] == 1 && p[x[0]] != 'true' } return p end |
#makerc ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/texmailer/config.rb', line 83 def makerc p = loadparams fnm = $userdir + '/msmtprc' File.open(fnm,'w',0600) { |f| f.write "account default\ntimeout 15\nauto_from off\n" f.write "from \"#{p["mailfrom"]}\"\n" f.write "host #{p["host"]}\n" f.write "port #{p["port"]}\n" f.write "auth on\nuser #{p["user"]}\npassword #{p["pass"]}\n" if p["auth"] == "true" if p["ssl"] == "true" then f.write "tls on\n" if p["igcert"] == "true" then f.write "tls_certcheck off\n" else f.write "tls_certcheck on\n" f.write "tls_trust_file #{$basedir}/ca-bundle.crt\n" end end } return fnm end |
#makereply(cache, mailid) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/texmailer/drafts.rb', line 30 def makereply(cache,mailid) p = loadparams mfrom = p["mailfrom"] mfrom = [mfrom] + getlist(loadflist) evl = cache[0] to = [evl.reply_to[0]] if evl.reply_to && evl.reply_to.length > 0 to ||= [evl.from[0]] if evl.from to ||= [] cc = evl.cc cc ||= [] if to.length == 0 to = to + evl.to if evl.to else cc = cc + evl.to if evl.to end [to,cc].each { |em| em.collect! { |x| addr = x.mailbox+"@"+x.host mine = false mfrom.each {|y| mine = true if y == addr} if mine nil else if x.name && x.name.length > 0 && x.name[0..0] != '=' "\"#{x.name}\" <#{addr}>" else addr end end } em.compact! } if evl.from x = evl.from[0] addr = x.mailbox+"@"+x.host addr.gsub!('_','\\_') sender = if x.name && x.name.length > 0 && x.name[0..0] != '=' "#{x.name} ``#{addr}\"" else addr end end sender ||= 'No Name' if evl.subject && evl.subject.length > 0 subj = evl.subject subj = "Re: "+subj if subj.length < 3 || subj[0..2].upcase != 'RE:' else subj = "Re: your mail" end exhdr = "In-Reply-To: " + evl. if evl. svdir = $userdir + '/drafts/'+mailid if ! File.directory? svdir Dir.mkdir(svdir) end File.open(svdir+"/mailto","w") {|f| f.write to.join(",")} if to.length > 0 File.open(svdir+"/mailcc","w") {|f| f.write cc.join(",")} if cc.length > 0 File.open(svdir+"/subject","w") {|f| f.write subj} File.open(svdir+"/exhdr","w") {|f| f.write exhdr} if evl. if cache[1].length > 0 dstr = 'On ' + evl.date.to_s + ' ' if evl.date dstr ||= '' qtext = "\\reply{\n" + dstr + sender + " wrote:\n\n" if ! cache[1][0] qtext = qtext + "\\begin{verbatim}\n"+cache[1][1]+"\n\\end{verbatim}\n}\n" else qtext = qtext + cache[1][1] + "\n}\n" end File.open(svdir+"/body","w") {|f| f.write "\n\n"+qtext} end end |
#msmtp(req, message) ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/texmailer/config.rb', line 105 def msmtp(req,) fnm = makerc pr = loadparams svdir = $userdir+"/drafts/"+req["mailid"] IO.popen("msmtp -C #{fnm} -t 2> #{$userdir}/msmtplog","w") { |p| [ ["To:","mailto"],["CC:","mailcc"],["BCC:","mailbcc"],["Subject:","subject"] ].each { |x| if req[x[1]] && req[x[1]].length > 0 then p.write x[0] + " " + req[x[1]].gsub(/\n/m,'') + "\n" end } p.write(File.read(svdir+"/exhdr") + "\n") if File.readable? svdir+"/exhdr" if pr["from"].length > 0 then p.write "From: #{pr["from"]} <#{pr["mailfrom"]}>\n" end p.write } return true,"E-mail Sent" if $? == 0 return false, "<pre>\n"+File.read($userdir+'/msmtplog')+"\n</pre>" if File.readable? $userdir+'/msmtplog' return false, 'Error running msmtp.' end |
#nicecache(cache) ⇒ Object
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 |
# File 'lib/texmailer/drafts.rb', line 229 def nicecache cache msgs = cache.sort { |m,l| Time.parse(l[1][0].date) <=> Time.parse(m[1][0].date) } msgs.collect! { |x| uid = x[0] evl = x[1][0] bdy = x[1][1][1] from = evl.from[0].name if evl.from[0].name && evl.from[0].name.length > 0 && evl.from[0].name[0..0] != '=' from ||= (evl.from[0].mailbox + "@" + evl.from[0].host) if evl.from[0].mailbox && evl.from[0].host from ||= "Unknown Sender" if from.length > 26 from = from.split(/[@ ]/)[0] end maxsb = 95 subject = evl.subject.gsub(/[\s]+/m, ' ').strip if evl.subject subject ||= '(no subject)' bdy.gsub!(/^\>.*/,'') if bdy.length > 0 && subject.length < maxsb && m = /[A-Za-z]/.match(bdy) bdy = m[0] + m.post_match bdy = bdy.gsub!(/\s+/m,' ').strip if bdy.length > maxsb-subject.length bdy = bdy[0..(maxsb-subject.length-1)] end subject = CGI.escapeHTML(subject)+'<span style="color:#999999">: '+CGI.escapeHTML(bdy)+"</span>" else subject = CGI.escapeHTML(subject) end sdate = Time.parse(evl.date).to_nice_s [uid,from,subject,sdate] } end |
#preview(mailid, req) ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/texmailer/drafts.rb', line 117 def preview(mailid,req) pvdir = $userdir + '/preview/' + mailid if ! File.directory? pvdir Dir.mkdir(pvdir) end body = req["body"] s = <<EOF \\documentclass[10pt]{article} \\usepackage{amsmath,amssymb} \\setlength{\\parindent}{0in} \\newcommand{\\reply}[1]{\\begin{quotation} \\setlength{\\parindent}{0in} #1 \\end{quotation} } \\begin{document} #{body} \\end{document} EOF vblocks = [] s2 = '' while m = /\\begin\{verbatim\}/.match(s) rstring = (0...15).map{ ('A'..'Z').to_a[rand(26)] }.join + "\n"; s2 = s2 + m.pre_match + rstring s = m.post_match if m2 = /\\end\{verbatim\}/.match(s) s = m2.post_match vstring = m2.pre_match else vstring = s s = '' end vstring = CGI.escapeHTML(vstring).gsub("\n","<br>\n") vblocks = vblocks + [ [rstring,vstring]] end s = s2 + s File.open(pvdir + '/t.tex','w') { |f| f.write s} Dir.chdir(pvdir); if system('latex -interaction=nonstopmode t > ltxrun.txt 2>/dev/null') system('htlatex t > /dev/null 2>/dev/null') html = File.read(pvdir+'/t.html') html.gsub!(/\<body.*?>/m) { |m| m + '<div style="font-family: Helvetica, Arial, sans-serif; font-size: 14px">' } html.gsub!('</body>','</body></div>') vblocks.each { |vb| html.gsub!(vb[0],vb[1]) } File.open(pvdir+'/t.html','w') { |f| f.write html} return true else emsg = File.read(pvdir+'/ltxrun.txt'); s = "<html><body><h1>Latex Error</h1><br><pre>\n" + emsg + "\n</pre></body></html>"; File.open(pvdir + '/t.html','w') {|f| f.write s} return false end end |
#savedraft(mailid, req) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/texmailer/drafts.rb', line 17 def savedraft(mailid,req) svdir = $userdir + '/drafts/'+mailid if ! File.directory? svdir Dir.mkdir(svdir) end ["mailto","mailcc","mailbcc","subject","body"].each { |s| File.open(svdir+'/'+s,"w") { |f| f.write req[s] } } ["mailto","mailcc","mailbcc"].each { |s| $ab.uptolist req[s] } end |
#saveflist(req) ⇒ Object
20 21 22 23 |
# File 'lib/texmailer/config.rb', line 20 def saveflist req g = getlist req["flist"] File.open($userdir + "/flist.cfg","w",0600) { |f| f.write g.join(', ')} end |
#saveiparams(req) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/texmailer/config.rb', line 37 def saveiparams req p = Hash.new ['host','user','password'].each { |x| p[x] = req[x] if req[x] p[x] ||= '' } p['port'] = Integer(req['port']) rescue p['port'] ||= 143 p['ssl'] = req['ssl'] ? true : false p['igcert'] = req['igcert'] ? true : false $iv.savesettings p end |
#saveparams(req) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/texmailer/config.rb', line 68 def saveparams req fnm = $userdir + '/smtp.cfg' params = [['from',0],['mailfrom', 0],['host', 0],['port', 0],['ssl', 1],['igcert', 1],['auth', 1],['user', 0],['pass', 0]] File.open(fnm,'w',0600) { |f| params.each { |x| d = req[x[0]] if x[1] == 0 && req[x[0]] d = 'true' if x[1] == 1 && req[x[0]] d ||= '' if x[1] == 0 d ||= 'false' f.write d+"\n" } } end |
#sendmail(mailid, req) ⇒ Object
139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/texmailer/send.rb', line 139 def sendmail(mailid,req) pvdir = $userdir + '/preview/' + mailid if ! File.directory? pvdir return false,"Internal Error!" end Dir.chdir(pvdir); return false,"Internal Error!" if ! File.readable?(pvdir+'/t.html') return false,"Internal Error!" if ! File.readable?(pvdir+'/t.tex') html = File.read(pvdir+'/t.html') return true, htm2mail(html,req["body"]) end |
#shortem(emlst) ⇒ Object
8 9 10 11 12 13 14 15 |
# File 'lib/texmailer/drafts.rb', line 8 def shortem(emlst) emlst = emlst.gsub(/\s+/,' ') r = /(\".*?\")\s*\<([^\>]*)\>/ a = emlst.gsub(r) { Regexp.last_match[1].gsub('"','') } return a end |
#stripcss(html) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/texmailer/send.rb', line 5 def stripcss(html) css = nil r = /\<link[^\>]*href="(.*\.css)"[\>]*\>/m html.gsub!(r) { |g| m = Regexp.last_match if ! File.readable? m[1] rep = "" else css = File.read(m[1]) rep = <<SCPT <style type="text/css"> #{css} </style> SCPT end rep } return html if ! css r = /\/\*.*?\*\//m css.gsub!(r,'') styles = Array.new r = /(.*?)\{(.*?)\}/ while m = r.match(css) cls = m[1].strip stlf = '' s = m[2] r2 = /font.*?\:.*?\;/ while m2 = r2.match(s) stlf = stlf + m2[0] s = m2.post_match end styles << [cls,stlf] if stlf.length > 0 css = m.post_match end r = /\<(.*?)class="(.*?)"(.*?)\>/m html.gsub!(r) { m = Regexp.last_match clid = m[2] stl = "" styles.each { |x| stl = stl + x[1] if x[0].index(clid) } stl = " style=\""+stl+"\" " if stl.length > 0 "<"+m[1]+"class=\""+m[2]+"\"" + stl + m[3]+">" } return html end |
#webapp ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/texmailer/wapp.rb', line 14 def webapp return Rack::Builder.new { use DontCache use Rack::Static, :urls => ["/preview"], :root => $userdir map '/' do run Proc.new {|env| location = env["PATH_INFO"] location = "/index.html" if location == '/' if location == '/die' $iv.stop Process.exit!(0) end if File.file? $basedir+"/rhtml"+location then code = 200 header = {"Content-Type" => "text/html"} req = Rack::Request.new(env) data = ERB.new(File.read($basedir+"/rhtml"+location)).result(binding) [code, header, data] elsif File.file? $basedir+"/js"+location then code = 200 header = {"Content-Type" => "text/javascript"} data = File.read($basedir+"/js"+location) [code, header, data] else [404, {"Content-Type" => "text/plain"}, "File not found: "+location] end } end } end |