Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/ruby
- require "pathname"
- Dir.chdir(Pathname.new(__FILE__).dirname.to_s)
- require "mail"
- mail_size_limit= 16*1024
- user_size_limit= 1024**2
- users_dir= Pathname.new("user")
- raw_incoming_mail= STDIN.read(mail_size_limit)
- incoming_mail= Mail.new(raw_incoming_mail)
- user= [incoming_mail.from].flatten[0].gsub('"', "")
- exit 1 unless user
- exit 1 unless user=~ /@/
- subject= incoming_mail.subject
- exit 1 unless subject
- user_dir= users_dir + user.split("@", 2).reverse.join("___")
- size_file= user_dir + ".size"
- tmp_size_file= user_dir + ".size_tmp"
- def send_response(original_mail, response_string, attachment= nil, response_subject=nil)
- Mail.deliver do |mail|
- to original_mail.from
- from original_mail.to
- subject response_subject || "Re: #{original_mail.subject}"
- add_file attachment if attachment
- body <<EOF
- #{response_string}
- --------
- available commands:
- signup
- list
- put
- get <filename>
- delete <filename>
- share <filename> <user>
- EOF
- end
- end
- def send_error(original_mail, error_string)
- Mail.deliver do |mail|
- to original_mail.from
- from original_mail.to
- subject "error Re: #{original_mail.subject}"
- body <<EOF
- I am sorry to inform you, that your requested command could not be executed.
- The reason is:
- #{error_string}
- EOF
- end
- end
- case subject
- when "signup"
- if user_dir.directory?
- send_error(incoming_mail, "your are already signed up")
- exit
- end
- unless (user_dir+"../.signup_allowed").file?
- send_error(incoming_mail, "signup is currently disabled")
- exit
- end
- user_dir.mkdir
- size_file.open("w") { |f| f.puts 0 }
- send_response(incoming_mail, "signup successfull")
- when "list"
- unless user_dir.directory?
- send_error(incoming_mail, "you are not signed up")
- exit
- end
- file_listing= "your_files:\n" +
- user_dir.children.select do |file|
- file.basename.to_s[0] != ?.
- end.collect do |file|
- "#{file.basename} #{file.size/1024.0}Kb"
- end.join("\n")
- send_response(incoming_mail, file_listing)
- when /\Aget ([A-Za-z0-9_-]+(\.[a-z0-9]+)?)\Z/
- file_name= $1
- file_path= user_dir+file_name
- unless user_dir.directory?
- send_error(incoming_mail, "you are not signed up")
- exit
- end
- unless file_path.file?
- send_error(incoming_mail, "the requested file does not exist")
- exit
- end
- send_response(incoming_mail, "here is your requested file", file_path.to_s)
- 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/
- file_name= $1
- second_user= $3
- file_path= user_dir+file_name
- unless user_dir.directory?
- send_error(incoming_mail, "you are not signed up")
- exit
- end
- unless file_path.file?
- send_error(incoming_mail, "the requested file does not exist")
- exit
- end
- second_user_dir= users_dir + second_user.split("@", 2).reverse.join("___")
- second_size_file= second_user_dir + ".size"
- second_file_path= second_user_dir + file_name
- unless second_size_file.file?
- send_error(incoming_mail, "the given user is not signed up")
- exit
- end
- if second_file_path.exist?
- send_error(incoming_mail, "file cannot be shared for unknown reasons")
- exit
- end
- second_file_path.make_symlink(file_path.to_s.sub("user/", "../"))
- send_response(incoming_mail, "file shared", file_path.to_s)
- when /\Adelete ([A-Za-z0-9_-]+(\.[a-z0-9]+)?)\Z/
- file_name= $1
- file_path= user_dir+file_name
- user_size= begin
- size_file.read.to_i
- rescue Errno::ENOENT
- send_error(incoming_mail, "you are not signed up")
- exit
- end
- unless file_name[0] != ?. and file_path.file?
- send_error(incoming_mail, "the requested file does not exist")
- exit
- end
- user_size-= file_path.size
- file_path.unlink
- tmp_size_file.open("w") { |f| f.puts user_size }
- tmp_size_file.rename(size_file)
- send_response(incoming_mail, "file deleted")
- when "put"
- user_size= begin
- size_file.read.to_i
- rescue Errno::ENOENT
- send_error(incoming_mail, "you are not signed up")
- exit
- end
- attachment= incoming_mail.attachments[0]
- unless attachment and attachment.filename=~ /\A([A-Za-z0-9_-]+(\.[a-z0-9]+)?)\Z/
- send_error(incoming_mail, "no valid attachment found")
- exit
- end
- file_path= user_dir+attachment.filename
- if file_path.exist?
- send_error(incoming_mail, "file already exists")
- exit
- end
- attachement_body= attachment.body.decoded
- user_size+= attachement_body.size
- if user_size > user_size_limit
- send_error(incoming_mail, "you have no space left")
- exit
- end
- tmp_size_file.open("w") { |f| f.puts user_size }
- tmp_size_file.rename(size_file)
- file_path.open("w") { |f| f.write attachement_body }
- send_response(incoming_mail, "file saved")
- end
Advertisement
Add Comment
Please, Sign In to add comment