diff --git a/app/models/channel.rb b/app/models/channel.rb index cf2ed60fa..d74ac21fe 100644 --- a/app/models/channel.rb +++ b/app/models/channel.rb @@ -13,6 +13,7 @@ class Channel < ApplicationModel puts "can't use " + 'Channel::' + channel[:adapter] puts e.inspect puts e.backtrace + c.disconnect end } end diff --git a/app/models/channel/facebook.rb b/app/models/channel/facebook.rb index 7fdddca9d..476c565c8 100644 --- a/app/models/channel/facebook.rb +++ b/app/models/channel/facebook.rb @@ -9,6 +9,12 @@ class Channel::Facebook end + + def disconnect + + + end + def send logger.debug('face!!!!!!!!!!!!!!') diff --git a/app/models/channel/imap.rb b/app/models/channel/imap.rb index 30816d504..a0c7aa8e2 100644 --- a/app/models/channel/imap.rb +++ b/app/models/channel/imap.rb @@ -14,42 +14,49 @@ class Channel::IMAP < Channel::EmailParser puts "fetching imap (#{channel[:options][:host]}/#{channel[:options][:user]} port=#{port},ssl=#{ssl})" - imap = Net::IMAP.new( channel[:options][:host], port, ssl, nil, false ) + @imap = Net::IMAP.new( channel[:options][:host], port, ssl, nil, false ) # try LOGIN, if not - try plain begin - imap.authenticate( 'LOGIN', channel[:options][:user], channel[:options][:password] ) + @imap.authenticate( 'LOGIN', channel[:options][:user], channel[:options][:password] ) rescue Exception => e if e.to_s !~ /unsupported\sauthentication\smechanism/i raise e end - imap.login( channel[:options][:user], channel[:options][:password] ) + @imap.login( channel[:options][:user], channel[:options][:password] ) end if !channel[:options][:folder] || channel[:options][:folder].empty? - imap.select('INBOX') + @imap.select('INBOX') else - imap.select( channel[:options][:folder] ) + @imap.select( channel[:options][:folder] ) end count = 0 - count_all = imap.search(['ALL']).count - imap.search(['ALL']).each do |message_id| + count_all = @imap.search(['ALL']).count + @imap.search(['ALL']).each do |message_id| count += 1 puts " - message #{count.to_s}/#{count_all.to_s}" - msg = imap.fetch(message_id,'RFC822')[0].attr['RFC822'] + msg = @imap.fetch(message_id,'RFC822')[0].attr['RFC822'] # puts msg.to_s # delete email from server after article was created if process(channel, msg) - imap.store(message_id, "+FLAGS", [:Deleted]) + @imap.store(message_id, "+FLAGS", [:Deleted]) end end - imap.expunge() - imap.disconnect() + @imap.expunge() + disconnect if count == 0 puts " - no message" end puts "done" end + + def disconnect + if @imap + @imap.disconnect() + end + end + def send(attr, notification = false) channel = Channel.where( :area => 'Email::Outbound', :active => true ).first begin diff --git a/app/models/channel/pop3.rb b/app/models/channel/pop3.rb index 0a71e6358..88f091160 100644 --- a/app/models/channel/pop3.rb +++ b/app/models/channel/pop3.rb @@ -14,14 +14,14 @@ class Channel::POP3 < Channel::EmailParser puts "fetching pop3 (#{channel[:options][:host]}/#{channel[:options][:user]} port=#{port},ssl=#{ssl})" - pop = Net::POP3.new( channel[:options][:host], port ) + @pop = Net::POP3.new( channel[:options][:host], port ) if ssl - pop.enable_ssl + @pop.enable_ssl end - pop.start( channel[:options][:user], channel[:options][:password] ) + @pop.start( channel[:options][:user], channel[:options][:password] ) count = 0 - count_all = pop.mails.size - pop.each_mail do |m| + count_all = @pop.mails.size + @pop.each_mail do |m| count += 1 puts " - message #{count.to_s}/#{count_all.to_s}" @@ -30,12 +30,19 @@ class Channel::POP3 < Channel::EmailParser m.delete end end - pop.finish + disconnect if count == 0 puts " - no message" end puts "done" end + + def disconnect + if @pop + @pop.finish + end + end + def send(attr, notification = false) channel = Channel.where( :area => 'Email::Outbound', :active => true ).first begin diff --git a/app/models/channel/twitter2.rb b/app/models/channel/twitter2.rb index 260f7db24..ad65f2c84 100644 --- a/app/models/channel/twitter2.rb +++ b/app/models/channel/twitter2.rb @@ -12,6 +12,12 @@ class Channel::Twitter2 end end + def disconnect + if @client + @client = nil + end + end + def fetch (channel) puts "fetching tweets (oauth_token#{channel[:options][:oauth_token]})" @@ -46,6 +52,7 @@ class Channel::Twitter2 fetch_loop( tweets, channel, channel[:options][:direct_messages][:group] ) end puts 'done' + disconnect end def fetch_loop( tweets, channel, group )