-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtelnet-imap.rb
105 lines (89 loc) · 2.91 KB
/
telnet-imap.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# Copyright 2008 the original author or authors.
#
# http://www.gnu.org/licenses/gpl.txt
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
require 'net/telnet'
require 'logger'
class ExchangeCleaner
def initialize
@log = Logger.new(STDOUT); @log.level = Logger::DEBUG
end
def delete_pesky_mails(server,username,password,dry_run)
folders_to_check = ['INBOX', 'Calendar']
connect_to_exchange server,username, password
folders_to_check.each { |b| examine_and_delete_messages_in b,dry_run }
logout_and_close
end
private
def select_box box
send_command "? SELECT \"#{box}\"";
end
def logout_and_close
send_command "? logout"
@client.close
end
def examine_and_delete_messages_in box, dry_run
@log.debug("examining folder #{box}")
text = select_box box
number_of_messages = 0
if text.include? 'EXISTS'
array = text.split('*')
number_of_messages = array[1].gsub(/EXISTS/,'').strip.to_i
end
if number_of_messages > 0
(1...number_of_messages).each do |i|
compatible = is_rfc_822_compatible? box,i
@log.debug("#{box}-message-#{i}-rfc-compatible?-#{compatible}")
if !compatible
if dry_run.eql?('y')
puts "I could have deleted this message you chicken"
else
delete_message box,i
end
end
end
end
end
def delete_message box, message_number
@log.info("Deleting message #{box}-#{message_number}")
send_command "? store #{message_number} +FLAGS (\\Deleted)"
send_command "? expunge"
end
def is_rfc_822_compatible? box,message_number
@log.debug("Checking#{box}, #{message_number}")
text = send_command "? FETCH #{message_number} rfc822.text"
!text.include? 'NO The requested message could not be converted to an RFC-822 compatible format'
end
def send_command command
text = ''
begin
@client.cmd(command) { |c| @log.debug(c.chomp) ; text << c }
rescue Exception=>e
end
text
end
def connect_to_exchange server, username, password
@client = Net::Telnet.new('Host' => server, 'Port' => '143',"Prompt" => /[#>:]/n, 'Timeout' => 15 )
login username, password
end
def login username, password
send_command "? LOGIN #{username} #{password}";
end
end
if ARGV.length != 4
puts "please provide server, username, password and dry run (y/n) "
exit -1
end
e = ExchangeCleaner.new
e.delete_pesky_mails ARGV[0], ARGV[1], ARGV[2], ARGV[3]