Skip to content

Commit

Permalink
rastafactoring :D
Browse files Browse the repository at this point in the history
  • Loading branch information
rokk0 committed Apr 18, 2012
1 parent 2aa3651 commit 163e106
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 31 deletions.
33 changes: 20 additions & 13 deletions app/controllers/bots_controller.rb
Expand Up @@ -7,9 +7,10 @@
class BotsController < ApplicationController
include BotsHelper

before_filter :user_access, :only => [:edit, :update, :show, :destroy]
before_filter :user_access_create, :only => [:new, :create]
before_filter :user_access_control, :only => [:run, :stop, :run_all]
before_filter :user_access, :only => [:edit, :update, :show, :destroy]
before_filter :user_access_create, :only => [:new, :create]
before_filter :user_access_control, :only => [:run, :stop]
before_filter :user_access_control_all, :only => [:run_all]

def index
if !current_user.admin? && current_user.id != params[:user_id].to_i
Expand Down Expand Up @@ -67,11 +68,7 @@ def destroy
end

def run
user_bot = init_bot(@bot)

user_bot.spam if user_bot.logged_in?

respond_to { |format| format.json { render :json => { 'state' => "##{@bot.id} - #{user_bot.login_state}" } } }
respond_to { |format| format.json { render :json => run_bot(@bot) } }
end

def stop
Expand All @@ -84,11 +81,7 @@ def run_all
states = []

User.find(params[:user_id].to_i).bots.each do |bot|
user_bot = init_bot(bot)

user_bot.spam if user_bot.logged_in?

states.push("##{bot.id} - #{user_bot.login_state}")
states.push(run_bot(bot))
end

respond_to { |format| format.json { render :json => { 'states' => states } } }
Expand All @@ -101,4 +94,18 @@ def init_bot(bot)
_bot = ('Bots::' + bot.bot_type.capitalize).constantize.new(bot.id, bot.email, bot.password, bot.page, bot.page_hash, bot.message, bot.count, bot.code)
end

# general method to run bot with hash check
def run_bot(bot)
_bot = init_bot(bot)

if _bot.logged_in?
if bot.page_hash.empty?
bot.update_attributes(:page_hash => _bot.get_hash(bot.page))
end
_bot.spam
end

return { 'state' => "##{bot.id} - #{_bot.login_state}" }
end

end
27 changes: 24 additions & 3 deletions app/helpers/bots_helper.rb
Expand Up @@ -5,6 +5,7 @@ module BotsHelper
# check user access to all information about bots except listing.
def user_access
@bot = current_user.admin? ? Bot.find(params[:id]) : current_user.bots.find(params[:id])

check_user_access(@bot.user_id)
rescue
flash_access_denied
Expand All @@ -18,21 +19,41 @@ def user_access_create
# check user access to run/stop bots.
def user_access_control
_bot = Bot.find(params[:id])

if current_user.admin?
@bot = !User.find(_bot.user_id).admin? ? _bot : current_user.bots.find(params[:id])
else
@bot = current_user.bots.find(params[:id])
end

rescue
respond_to { |format| format.json { render :json => { 'state' => 'access denied' } } }
response_access_denied
end

# check user access to run/stop all bots by user_id
def user_access_control_all
user = User.find(params[:user_id])

if (!current_user?(user) && !current_user.admin?) || (current_user.admin? && user.admin?)
response_access_denied
end

rescue
response_access_denied
end

# check user access to actions with bots, also return exception if user not found.
def check_user_access(user_id)
user = User.find(user_id.to_i)
flash_access_denied if !current_user?(user) && user.admin? && current_user.admin?
user = User.find(user_id)

flash_access_denied if (!current_user?(user) && !current_user.admin?) || (current_user.admin? && user.admin?)

rescue
flash_user_not_found
end

def response_access_denied
respond_to { |format| format.json { render :json => { 'state' => 'access denied' } } }
end

end
6 changes: 5 additions & 1 deletion lib/bots/discussion.rb
Expand Up @@ -11,8 +11,12 @@ def initialize(id, email, password, page, hash, message, count, code)
@msg_count = 0

@vk.login
end

def get_hash(page)
page = @@agent.get(page)

@hash = @vk.get_hash(id, /hash:\s'([^.]\w*)'/) if @hash.to_s.empty?
@hash = @vk.parse_page(page, /hash:\s'([^.]\w*)'/)
end

def spam
Expand Down
6 changes: 5 additions & 1 deletion lib/bots/group.rb
Expand Up @@ -11,8 +11,12 @@ def initialize(id, email, password, page, hash, message, count, code)
@msg_count = 0

@vk.login
end

def get_hash(page)
page = @@agent.get(page)

@hash = @vk.get_hash(id, /"post_hash":"([^.]\w*)"/) if @hash.to_s.empty?
@hash = @vk.parse_page(page, /"post_hash":"([^.]\w*)"/)
end

def spam
Expand Down
14 changes: 1 addition & 13 deletions lib/core/vk.rb
Expand Up @@ -78,24 +78,12 @@ def login_security
end
end

def get_hash(id, regexp)
bot = Bot.find(id)
page = @@agent.get(bot.page)

parse_page(page, regexp)

bot.update_attributes(:page_hash => @hash)

@hash
rescue
nil
end

def parse_page(page, regexp)
page.search('script').each do |script|
script.content.match(regexp)
@hash ||= $1
end
@hash
end

end
Expand Down

0 comments on commit 163e106

Please sign in to comment.