Guest User

Untitled

a guest
Jul 5th, 2013
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 4.57 KB | None | 0 0
  1. #!/usr/bin/ruby
  2.  
  3. require "pathname"
  4. Dir.chdir(Pathname.new(__FILE__).dirname.to_s)
  5.  
  6. require "mail"
  7.  
  8. mail_size_limit= 16*1024
  9. user_size_limit= 1024**2
  10. users_dir= Pathname.new("user")
  11.  
  12. raw_incoming_mail= STDIN.read(mail_size_limit)
  13. incoming_mail= Mail.new(raw_incoming_mail)
  14.  
  15. user= [incoming_mail.from].flatten[0].gsub('"', "")
  16. exit 1 unless user
  17. exit 1 unless user=~ /@/
  18. subject= incoming_mail.subject
  19. exit 1 unless subject
  20. user_dir= users_dir + user.split("@", 2).reverse.join("___")
  21. size_file= user_dir + ".size"
  22. tmp_size_file= user_dir + ".size_tmp"
  23.  
  24. def send_response(original_mail, response_string, attachment= nil, response_subject=nil)
  25.     Mail.deliver do |mail|
  26.         to original_mail.from
  27.         from original_mail.to
  28.         subject response_subject || "Re: #{original_mail.subject}"
  29.         add_file attachment if attachment
  30.         body <<EOF
  31. #{response_string}
  32.  
  33. --------
  34. available commands:
  35. signup
  36. list
  37. put
  38. get <filename>
  39. delete <filename>
  40. share <filename> <user>
  41. EOF
  42.     end
  43. end
  44.  
  45. def send_error(original_mail, error_string)
  46.     Mail.deliver do |mail|
  47.         to original_mail.from
  48.         from original_mail.to
  49.         subject "error Re: #{original_mail.subject}"
  50.         body <<EOF
  51. I am sorry to inform you, that your requested command could not be executed.
  52. The reason is:
  53.  
  54. #{error_string}
  55. EOF
  56.     end
  57. end
  58.  
  59. case subject
  60. when "signup"
  61.     if user_dir.directory?
  62.         send_error(incoming_mail, "your are already signed up")
  63.         exit
  64.     end
  65.     unless (user_dir+"../.signup_allowed").file?
  66.         send_error(incoming_mail, "signup is currently disabled")
  67.         exit
  68.     end
  69.     user_dir.mkdir
  70.     size_file.open("w") { |f| f.puts 0 }
  71.     send_response(incoming_mail, "signup successfull")
  72. when "list"
  73.     unless user_dir.directory?
  74.         send_error(incoming_mail, "you are not signed up")
  75.         exit
  76.     end
  77.     file_listing= "your_files:\n" +
  78.     user_dir.children.select do |file|
  79.         file.basename.to_s[0] != ?.
  80.     end.collect do |file|
  81.         "#{file.basename} #{file.size/1024.0}Kb"
  82.     end.join("\n")
  83.     send_response(incoming_mail, file_listing)
  84. when /\Aget ([A-Za-z0-9_-]+(\.[a-z0-9]+)?)\Z/
  85.     file_name= $1
  86.     file_path= user_dir+file_name
  87.     unless user_dir.directory?
  88.         send_error(incoming_mail, "you are not signed up")
  89.         exit
  90.     end
  91.     unless file_path.file?
  92.         send_error(incoming_mail, "the requested file does not exist")
  93.         exit
  94.     end
  95.     send_response(incoming_mail, "here is your requested file", file_path.to_s)
  96. when /\Ashare ([A-Za-z0-9_-]+(\.[a-z0-9]+)?) ([A-Za-z0-9][A-Za-z0-9._-]*@([A-Za-z0-9-]+\.)+[A-Za-z]+)\Z/
  97.     file_name= $1
  98.     second_user= $3
  99.     file_path= user_dir+file_name
  100.     unless user_dir.directory?
  101.         send_error(incoming_mail, "you are not signed up")
  102.         exit
  103.     end
  104.     unless file_path.file?
  105.         send_error(incoming_mail, "the requested file does not exist")
  106.         exit
  107.     end
  108.     second_user_dir= users_dir + second_user.split("@", 2).reverse.join("___")
  109.     second_size_file= second_user_dir + ".size"
  110.     second_file_path= second_user_dir + file_name
  111.     unless second_size_file.file?
  112.         send_error(incoming_mail, "the given user is not signed up")
  113.         exit
  114.     end
  115.     if second_file_path.exist?
  116.         send_error(incoming_mail, "file cannot be shared for unknown reasons")
  117.         exit
  118.     end
  119.     second_file_path.make_symlink(file_path.to_s.sub("user/", "../"))
  120.     send_response(incoming_mail, "file shared", file_path.to_s)
  121. when /\Adelete ([A-Za-z0-9_-]+(\.[a-z0-9]+)?)\Z/
  122.     file_name= $1
  123.     file_path= user_dir+file_name
  124.     user_size= begin
  125.         size_file.read.to_i
  126.     rescue Errno::ENOENT
  127.         send_error(incoming_mail, "you are not signed up")
  128.         exit
  129.     end
  130.     unless file_name[0] != ?. and file_path.file?
  131.         send_error(incoming_mail, "the requested file does not exist")
  132.         exit
  133.     end
  134.     user_size-= file_path.size
  135.     file_path.unlink
  136.     tmp_size_file.open("w") { |f| f.puts user_size }
  137.     tmp_size_file.rename(size_file)
  138.     send_response(incoming_mail, "file deleted")
  139. when "put"
  140.     user_size= begin
  141.         size_file.read.to_i
  142.     rescue Errno::ENOENT
  143.         send_error(incoming_mail, "you are not signed up")
  144.         exit
  145.     end
  146.     attachment= incoming_mail.attachments[0]
  147.     unless attachment and attachment.filename=~ /\A([A-Za-z0-9_-]+(\.[a-z0-9]+)?)\Z/
  148.         send_error(incoming_mail, "no valid attachment found")
  149.         exit
  150.     end
  151.     file_path= user_dir+attachment.filename
  152.     if file_path.exist?
  153.         send_error(incoming_mail, "file already exists")
  154.         exit
  155.     end
  156.     attachement_body= attachment.body.decoded
  157.     user_size+= attachement_body.size
  158.     if user_size > user_size_limit
  159.         send_error(incoming_mail, "you have no space left")
  160.         exit
  161.     end
  162.     tmp_size_file.open("w") { |f| f.puts user_size }
  163.     tmp_size_file.rename(size_file)
  164.     file_path.open("w") { |f| f.write attachement_body }
  165.     send_response(incoming_mail, "file saved")
  166. end
Advertisement
Add Comment
Please, Sign In to add comment