Skip to content
Browse files

rastafactoring :D

  • Loading branch information...
1 parent 2aa3651 commit 163e1064aca2bfcf74e739d5cd0729c15b0d8ee1 @rokk0 committed Apr 18, 2012
Showing with 55 additions and 31 deletions.
  1. +20 −13 app/controllers/bots_controller.rb
  2. +24 −3 app/helpers/bots_helper.rb
  3. +5 −1 lib/bots/discussion.rb
  4. +5 −1 lib/bots/group.rb
  5. +1 −13 lib/core/vk.rb
View
33 app/controllers/bots_controller.rb
@@ -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
@@ -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
@@ -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 } } }
@@ -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
View
27 app/helpers/bots_helper.rb
@@ -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
@@ -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
View
6 lib/bots/discussion.rb
@@ -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
View
6 lib/bots/group.rb
@@ -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
View
14 lib/core/vk.rb
@@ -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

0 comments on commit 163e106

Please sign in to comment.
Something went wrong with that request. Please try again.