Module: Texmailer
- Defined in:
- lib/texmailer/send.rb,
lib/texmailer/wapp.rb,
lib/texmailer/config.rb,
lib/texmailer/drafts.rb
Defined Under Namespace
Classes: DontCache
Instance Method Summary collapse
- #discarddraft(mailid) ⇒ Object
- #getdrafts ⇒ Object
- #getpvloc(mailid) ⇒ Object
- #htm2mail(thehtml_in, thetex) ⇒ Object
- #loaddraft(mailid) ⇒ Object
- #loadparams ⇒ Object
- #makerc ⇒ Object
- #msmtp(req, message) ⇒ Object
- #preview(mailid, req) ⇒ Object
- #savedraft(mailid, req) ⇒ Object
- #saveparams(req) ⇒ Object
- #sendmail(mailid, req) ⇒ Object
- #stripcss(html) ⇒ Object
- #webapp ⇒ Object
Instance Method Details
#discarddraft(mailid) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/texmailer/drafts.rb', line 59 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 end |
#getdrafts ⇒ Object
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 |
# File 'lib/texmailer/drafts.rb', line 75 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(mailto),CGI.escapeHTML(subject),drtime] end } s.compact! s.sort! {|a,b| b[3] <=> a[3]} return s end |
#getpvloc(mailid) ⇒ Object
15 16 17 18 19 |
# File 'lib/texmailer/drafts.rb', line 15 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 |
# 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 #{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
49 50 51 52 53 54 55 56 57 |
# File 'lib/texmailer/drafts.rb', line 49 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 |
#loadparams ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/texmailer/config.rb', line 3 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 p.inspect 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
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/texmailer/config.rb', line 35 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 |
#msmtp(req, message) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/texmailer/config.rb', line 57 def msmtp(req,) fnm = makerc pr = loadparams 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 } 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 |
#preview(mailid, req) ⇒ Object
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 |
# File 'lib/texmailer/drafts.rb', line 21 def preview(mailid,req) pvdir = $userdir + '/preview/' + mailid if ! File.directory? pvdir Dir.mkdir(pvdir) end s = <<EOF \\documentclass{article} \\usepackage{amsmath,amssymb} \\setlength{\\parindent}{0in} \\begin{document} #{req["body"]} \\end{document} EOF 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') 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
5 6 7 8 9 10 11 12 13 |
# File 'lib/texmailer/drafts.rb', line 5 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] } } end |
#saveparams(req) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/texmailer/config.rb', line 20 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
138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/texmailer/send.rb', line 138 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 |
#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 |
# 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 == '/' Process.exit!(0) if location == '/die' 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] else [404, {"Content-Type" => "text/plain"}, "File not found: "+location] end } end } end |