2016-08-04 22:18:42 +02:00
|
|
|
$agent = Mechanize.new
|
2016-05-05 04:25:01 +02:00
|
|
|
|
2020-05-13 07:51:51 +02:00
|
|
|
$apiURL = ENV['API_URL'] || 'http://localhost:8080'
|
|
|
|
|
2017-02-15 07:08:48 +01:00
|
|
|
def plainRequest(path, data = {}, method = 'POST')
|
2020-05-13 07:51:51 +02:00
|
|
|
uri = $apiURL + path
|
2017-01-14 01:58:59 +01:00
|
|
|
|
2017-02-15 07:08:48 +01:00
|
|
|
if method == 'POST'
|
|
|
|
@response = $agent.post(uri, data)
|
|
|
|
else
|
|
|
|
@response = $agent.get(uri, data)
|
|
|
|
end
|
|
|
|
|
|
|
|
return @response
|
2017-01-14 01:58:59 +01:00
|
|
|
end
|
|
|
|
|
2016-07-27 05:44:07 +02:00
|
|
|
def request(path, data = {})
|
2020-05-13 07:51:51 +02:00
|
|
|
uri = $apiURL + path
|
2016-08-04 22:18:42 +02:00
|
|
|
response = $agent.post(uri, data)
|
2016-05-05 04:25:01 +02:00
|
|
|
|
|
|
|
return JSON.parse(response.body)
|
|
|
|
end
|
|
|
|
|
2016-05-11 03:35:36 +02:00
|
|
|
class Database
|
|
|
|
def initialize()
|
2018-04-27 01:01:12 +02:00
|
|
|
mysqlHost = ENV['MYSQL_HOST'] || 'localhost'
|
|
|
|
mysqlPort = ENV['MYSQL_PORT'] || '3306'
|
2016-07-27 05:44:07 +02:00
|
|
|
mysqlUser = ENV['MYSQL_USER'] || 'root'
|
|
|
|
mysqlPass = ENV['MYSQL_PASSWORD'] || ''
|
2020-05-13 07:51:51 +02:00
|
|
|
@connection = Mysql2::Client.new(
|
|
|
|
host: mysqlHost,
|
|
|
|
username: mysqlUser,
|
|
|
|
password: mysqlPass,
|
|
|
|
port: mysqlPort.to_i,
|
|
|
|
database: 'development'
|
|
|
|
)
|
2016-05-11 03:35:36 +02:00
|
|
|
end
|
2016-05-05 04:25:01 +02:00
|
|
|
|
2016-05-11 03:35:36 +02:00
|
|
|
def close()
|
|
|
|
@connection.close
|
|
|
|
end
|
2016-05-05 04:25:01 +02:00
|
|
|
|
2016-05-11 03:35:36 +02:00
|
|
|
def getRow(table, value, field = 'id')
|
|
|
|
queryResponse = @connection.query("select * from #{table} where #{field}='#{value.to_s}'")
|
|
|
|
|
2020-05-13 07:51:51 +02:00
|
|
|
return queryResponse.first
|
2016-05-11 03:35:36 +02:00
|
|
|
end
|
2016-12-29 21:25:45 +01:00
|
|
|
|
|
|
|
def getLastRow(table)
|
|
|
|
queryResponse = @connection.query("select * from #{table} order by id desc limit 1")
|
|
|
|
|
2020-05-13 07:51:51 +02:00
|
|
|
return queryResponse.first
|
2016-12-29 21:25:45 +01:00
|
|
|
end
|
2017-01-12 20:30:58 +01:00
|
|
|
|
|
|
|
def query(query_string)
|
2021-11-11 21:17:39 +01:00
|
|
|
return @connection.query(query_string)
|
2017-01-12 20:30:58 +01:00
|
|
|
end
|
2016-05-05 04:25:01 +02:00
|
|
|
end
|
2016-05-11 03:35:36 +02:00
|
|
|
|
2018-12-24 01:44:59 +01:00
|
|
|
class MailServer
|
|
|
|
def initialize
|
|
|
|
@smtp_server = ENV['OPENSUPPORTS_SMTP_SERVER']
|
|
|
|
@smtp_port = ENV['OPENSUPPORTS_SMTP_PORT']
|
|
|
|
|
|
|
|
@imap_server = ENV['OPENSUPPORTS_IMAP_SERVER']
|
|
|
|
@imap_port = ENV['OPENSUPPORTS_IMAP_PORT']
|
|
|
|
|
|
|
|
@admin_user = ENV['OPENSUPPORTS_EMAIL_ADMIN_USERNAME']
|
|
|
|
@admin_password = ENV['OPENSUPPORTS_EMAIL_ADMIN_PASSWORD']
|
|
|
|
|
|
|
|
@client_user = ENV['OPENSUPPORTS_EMAIL_CLIENT_USERNAME']
|
|
|
|
@client_password = ENV['OPENSUPPORTS_EMAIL_CLIENT_PASSWORD']
|
|
|
|
|
2019-01-12 04:38:33 +01:00
|
|
|
@admin_imap = Net::IMAP.new(@imap_server, {port: @imap_port})
|
|
|
|
@client_imap = Net::IMAP.new(@imap_server, {port: @imap_port})
|
2018-12-24 01:44:59 +01:00
|
|
|
|
2019-01-12 04:38:33 +01:00
|
|
|
@admin_imap.authenticate('LOGIN', @admin_user, @admin_password)
|
|
|
|
@client_imap.authenticate('LOGIN', @client_user, @client_password)
|
2018-12-24 01:44:59 +01:00
|
|
|
|
2019-01-12 04:38:33 +01:00
|
|
|
@client_stmp = Net::SMTP.start(@smtp_server, @smtp_port, @smtp_server, @client_user, @client_password, :plain)
|
2018-12-24 01:44:59 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def clear_mails
|
|
|
|
self.clear_admin_mails
|
|
|
|
self.clear_client_mails
|
|
|
|
end
|
|
|
|
|
|
|
|
def clear_admin_mails
|
2019-01-12 04:38:33 +01:00
|
|
|
@admin_imap.examine('INBOX')
|
|
|
|
puts @admin_imap.list("", "*")
|
|
|
|
|
|
|
|
@admin_imap.store(2, "+FLAGS", [:Deleted])
|
|
|
|
@admin_imap.uid_search(['NOT','DELETED']).each do |uid|
|
|
|
|
puts "deleteing #{uid}"
|
|
|
|
@admin_imap.uid_copy(uid, "INBOX.Trash")
|
|
|
|
@admin_imap.uid_store(uid, "+FLAGS", [:Deleted])
|
|
|
|
puts @admin_imap.uid_fetch(uid, ['FLAGS', 'UID'])[0]
|
|
|
|
end
|
|
|
|
|
|
|
|
puts @admin_imap.expunge
|
|
|
|
@admin_imap.logout
|
|
|
|
@admin_imap.disconnect
|
2018-12-24 01:44:59 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def clear_client_mails
|
2019-01-12 04:38:33 +01:00
|
|
|
@client_imap.examine('INBOX')
|
|
|
|
|
|
|
|
@client_imap.search(['NOT','DELETED']).each do |message_id|
|
|
|
|
@client_imap.copy(message_id, "INBOX.Trash")
|
|
|
|
@client_imap.store(message_id, "+FLAGS", [:Deleted])
|
|
|
|
end
|
|
|
|
|
|
|
|
@client_imap.expunge
|
2018-12-24 01:44:59 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def send_mail(subject, text, file = nil)
|
2019-01-12 04:38:33 +01:00
|
|
|
# message = MailFactory.new
|
|
|
|
# message.to = @admin_user
|
|
|
|
# message.from = @client_user
|
|
|
|
# message.subject = subject
|
|
|
|
# message.html = text
|
|
|
|
message = <<MESSAGE_END
|
|
|
|
From: Client <#{@client_user}>
|
|
|
|
To: Support <#{@admin_user}>
|
|
|
|
Subject: #{subject}
|
|
|
|
|
|
|
|
#{text}
|
|
|
|
MESSAGE_END
|
|
|
|
|
|
|
|
# unless file.nil?
|
|
|
|
# message.attach(file)
|
|
|
|
# end
|
|
|
|
|
|
|
|
@client_stmp.send_message(message, @client_user, @admin_user)
|
|
|
|
end
|
2018-12-24 01:44:59 +01:00
|
|
|
|
2019-01-12 04:38:33 +01:00
|
|
|
def check
|
|
|
|
puts 'checking...'
|
|
|
|
@admin_imap.examine('INBOX')
|
|
|
|
@admin_imap.search(['NOT','DELETED']).each do |message_id|
|
|
|
|
envelope = @admin_imap.fetch(message_id, "ENVELOPE")[0].attr["ENVELOPE"]
|
|
|
|
# puts "#{envelope.from[0].name}: \t#{envelope.subject}"
|
|
|
|
puts envelope.subject
|
|
|
|
end
|
2018-12-24 01:44:59 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-01-12 04:38:33 +01:00
|
|
|
# $mail_server = MailServer.new
|
2016-05-11 03:35:36 +02:00
|
|
|
$database = Database.new
|
2016-09-25 06:16:10 +02:00
|
|
|
|
2019-01-12 04:38:33 +01:00
|
|
|
# $mail_server.clear_mails
|
2019-02-13 03:37:27 +01:00
|
|
|
# $mail_server.send_mail('support message 1', 'texttexttext tex')
|
2019-01-12 04:38:33 +01:00
|
|
|
# $mail_server.check
|
|
|
|
|
2016-09-25 06:16:10 +02:00
|
|
|
$staff = {
|
2021-11-11 21:17:39 +01:00
|
|
|
:email => 'admin@opensupports.com',
|
|
|
|
:password => 'admin22'
|
2016-09-25 06:16:10 +02:00
|
|
|
}
|