ip_in_range 0.9 → 1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/ip_in_range.gemspec CHANGED
@@ -7,13 +7,14 @@ Gem::Specification.new do |s|
7
7
  s.date = Date.today.strftime('%F')
8
8
  s.summary = SUMMARY
9
9
  s.description = "Verify that an IP is in a range"
10
- s.authors = AUTHORS
11
- s.email = EMAIL
12
- s.files = %w~ip_in_range~.collect{|f| 'bin/' << f} + %w~log.conf ip_range.rb version.rb file_checking.rb logging.rb email.rb~.collect{|f| 'lib/' << f} + ['doc/license.txt','ip_in_range.gemspec']
10
+ s.authors = AUTHORS.join ','
11
+ s.email = AUTHORS_MAIL.join ','
12
+ s.files = %w~ip_in_range~.collect{|f| 'bin/' << f} + %w~ip_range.rb version.rb file_checking.rb basic_logging.rb email.rb~.collect{|f| 'lib/' << f} + ['doc/license.txt','ip_in_range.gemspec'] + ["README.md"]
13
13
  s.homepage = ''
14
- #s.requirements = ''
15
- #s.add_runtime_dependency ''
16
14
  s.executables = 'ip_in_range'
17
- s.license = 'GPL-3.0'
18
- s.required_ruby_version = '>= 2.7'
15
+ s.license = 'Nonstandard'
16
+ s.required_ruby_version = '>= 3.0'
17
+ s.requirements = 'mail'
18
+ s.add_runtime_dependency 'mail', '~> 2.8', '>= 2.8.1'
19
+
19
20
  end
@@ -0,0 +1,203 @@
1
+ #!/bin/env ruby
2
+ #encoding: UTF-8
3
+ =begin
4
+ /***************************************************************************
5
+ * 2023-2024, Michael Uplawski <michael.uplawski@uplawski.eu> *
6
+ * This program is free software; you can redistribute it and/or modify *
7
+ * it under the terms of the WTFPL 2.0 or later, see *
8
+ * http://www.wtfpl.net/about/ *
9
+ * *
10
+ * This program is distributed in the hope that it will be useful, *
11
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
13
+ * *
14
+ ***************************************************************************/
15
+ =end
16
+
17
+ #
18
+ # Simplified logging.
19
+ # See example code at the bottom of this file.
20
+ # Execute this file to see the output.
21
+ module BasicLogging
22
+
23
+ DEBUG = 0
24
+ INFO = 1
25
+ WARN = 2
26
+ ERROR = 3
27
+ FATAL = 4
28
+ UNKNOWN = nil
29
+
30
+ # this is mainly for the translation of method calls into log levels
31
+ Levels = {:debug => DEBUG, :info => INFO, :warn => WARN, :error => ERROR,
32
+ :fatal => FATAL, :unknown => UNKNOWN}
33
+
34
+ @@log_level = UNKNOWN
35
+ @@target = STDOUT
36
+ @@muted = []
37
+
38
+ # do not log, if caller is obj (class or instance)
39
+ def self.mute(obj)
40
+ name = obj.class == Class ? obj.name.dup : obj.class.name
41
+ @@muted << name
42
+ end
43
+
44
+ def self.is_muted?(obj)
45
+ name = obj.class == Class ? obj.name.dup : obj.class.name
46
+ @@muted.include?(name)
47
+ end
48
+
49
+ # set the log level
50
+ def set_level(lv)
51
+ if lv.respond_to?(:to_str) && Levels.keys.include?(lv.strip.to_sym)
52
+ lv = Levels[lv.to_sym]
53
+ elsif lv.respond_to?(:to_sym) && Levels.keys.include?(lv)
54
+ lv = Levels[lv]
55
+ end
56
+
57
+ if(!lv || (lv.respond_to?(:to_int) && lv >= DEBUG && lv <= FATAL) )
58
+ @@log_level = lv
59
+ else
60
+ msg = __FILE__.dup << ": ERROR : invalid log level \"" << lv.to_s << "\""
61
+ msg << "\n" << "Keepinng old log level " << Levels.keys.detect {| k| Levels[k] == @@log_level}.to_s
62
+ STDERR.puts msg
63
+ puts msg
64
+ end
65
+ end
66
+
67
+ # set the log target
68
+ def set_target(tg)
69
+ if tg.respond_to?(:to_io)
70
+ @@target = tg
71
+ elsif(!File::exist?(tg) || ( File.file?(tg) && File.writable?(tg) ) )
72
+ @@target = File.open(tg, 'w+')
73
+ elsif !tg || tg.respond_to?(:to_str) && tg.strip.empty?
74
+ @@target = nil
75
+ else
76
+ STDERR.puts __FILE__.dup << ': ERROR : target ' << tg << ' cannot be set'
77
+ STDERR.puts "Keeping old target " << @@target.inspect
78
+ return
79
+ end
80
+ end
81
+
82
+ # Output of log messages, depending on the log level set for the calling class
83
+ # and the name of the alias method which is actually called.
84
+ def log(message)
85
+ if !BasicLogging.is_muted?(self)
86
+ # how has this method been called?
87
+ mlevel = __callee__
88
+ if Levels.has_key?(mlevel) && Levels[mlevel] <= FATAL
89
+ # output only for levels equal or above the value that corresponds to
90
+ # the calling alias.
91
+ format_log( message, mlevel) if @@log_level && Levels[mlevel] >= @@log_level
92
+ else
93
+ STDERR.puts __FILE__.dup << ": ERROR : invalid log level \"" << mlevel.to_s << "\""
94
+ end
95
+ end
96
+ end
97
+
98
+ def target
99
+ @@target.path if @@target
100
+ end
101
+
102
+ def level
103
+ @@log_level.to_s if @@log_level
104
+ end
105
+
106
+ # Clear the log (-file)
107
+ def clear_log
108
+ if @@target && @@target.respond_to?(:truncate)
109
+ lock_target{ @@target.truncate(0) }
110
+ end
111
+ end
112
+
113
+ alias :debug :log
114
+ alias :info :log
115
+ alias :warn :log
116
+ alias :error :log
117
+ alias :fatal :log
118
+
119
+
120
+ private
121
+
122
+ def lock_target(&block)
123
+ begin
124
+ if @@target.respond_to?(:flock)
125
+ @@target.flock(File::LOCK_EX)
126
+ block.call
127
+ @@target.flock(File::LOCK_UN)
128
+ elsif @@target.respond_to?(:to_io)
129
+ block.call
130
+ end
131
+ rescue => ex
132
+ STDERR.puts __FILE__.dup << ": ERROR : cannot lock target (" << ex.message << ")"
133
+ end
134
+ end
135
+
136
+ # 1 format_log for all loggers.
137
+ def format_log(message, mlevel)
138
+ if @@target
139
+ clname = self.class.name
140
+ # indicate if a registered class or the registered object of a class is calling.
141
+ name = self.class == Class ? self.name.dup << ' [class]' : (clname != 'Object' ? clname : 'Top-Level')
142
+ lock_target{@@target.puts '' << name << ' ' << mlevel.to_s << ' ' << Time.now.strftime("%H:%M:%S:%6N") << ': ' << message.gsub("\n", "\n |")}
143
+ end
144
+ end
145
+ end
146
+ #---------test: execute file----------
147
+ if $0 == __FILE__
148
+ Array.extend(BasicLogging)
149
+ Array.set_level(BasicLogging::INFO)
150
+ Array.info('TEST')
151
+ ar = Array.new
152
+ ar.extend(BasicLogging)
153
+ # --- no output :
154
+ l = __LINE__
155
+ ar.debug(l.next.to_s << ': debug-test 0')
156
+ # output
157
+ ar.set_level(BasicLogging::DEBUG)
158
+ l = __LINE__
159
+ ar.debug(l.next.to_s << ': debug-test 1')
160
+
161
+ obj = Object.new
162
+ obj.extend(BasicLogging)
163
+ obj.set_level(BasicLogging::DEBUG)
164
+ puts "--------debug-----------"
165
+ obj.debug('debug')
166
+ obj.info('info')
167
+ obj.warn('warn')
168
+ obj.error('error')
169
+ obj.fatal('fatal')
170
+ puts "--------info-----------"
171
+ obj.set_level("info")
172
+ obj.debug('debug')
173
+ obj.info('info')
174
+ obj.warn('warn')
175
+ obj.error('error')
176
+ obj.fatal('fatal')
177
+ puts "--------fatal-----------"
178
+ obj.set_level("fatal")
179
+ obj.debug('debug')
180
+ obj.info('info')
181
+ obj.warn('warn')
182
+ obj.error('error')
183
+ obj.fatal('fatal')
184
+ puts "--------UNKNOWN-----------"
185
+ obj.set_level(nil)
186
+ obj.debug('debug')
187
+ obj.info('info')
188
+ obj.warn('warn')
189
+ obj.error('error')
190
+ obj.fatal('fatal')
191
+ puts " ------ Output into file ----"
192
+ obj.set_target "/tmp/test_log.log"
193
+ puts " ------ INFO -----------"
194
+ obj.set_level BasicLogging::INFO
195
+ obj.info('info output')
196
+
197
+ obj.info('info output 2')
198
+ puts "---------- invalid -------"
199
+ obj.set_target "/dev/sr0"
200
+ obj.set_level "power"
201
+ end
202
+
203
+ # EOF
data/lib/email.rb CHANGED
@@ -2,40 +2,32 @@
2
2
  #encoding: UTF-8
3
3
  =begin
4
4
  /***************************************************************************
5
- * ©2021-2021, Michael Uplawski <michael.uplawski@uplawski.eu> *
5
+ * ©2021-2024, Michael Uplawski <michael.uplawski@uplawski.eu> *
6
6
  * *
7
7
  * This program is free software; you can redistribute it and/or modify *
8
- * it under the terms of the GNU General Public License as published by *
9
- * the Free Software Foundation; either version 3 of the License, or *
10
- * (at your option) any later version. *
8
+ * it under the terms of the WTFPL 2.0 or later, see *
9
+ * http://www.wtfpl.net/about/ *
11
10
  * *
12
11
  * This program is distributed in the hope that it will be useful, *
13
12
  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15
- * GNU General Public License for more details. *
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
16
14
  * *
17
- * You should have received a copy of the GNU General Public License *
18
- * along with this program; if not, write to the *
19
- * Free Software Foundation, Inc., *
20
- * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
21
15
  ***************************************************************************/
22
16
  =end
23
17
 
24
- require_relative 'logging'
18
+ require_relative 'basic_logging'
25
19
  require 'mail'
26
20
 
27
21
  class FMail
28
- extend Logging
29
- @@log = init_logger(STDOUT)
22
+ include BasicLogging
30
23
 
31
24
  def initialize(mail_text)
32
- @log = @@log
33
25
  if !mail_text || mail_text.empty?
34
- @log.error('No mail-text provided. Aborting!')
26
+ error('No mail-text provided. Aborting!')
35
27
  exit false
36
28
  end
37
29
  @mail_text = mail_text
38
- # @log.debug('mail text is ' << @mail_text)
30
+ # debug('mail text is ' << @mail_text)
39
31
  extr_received
40
32
  end
41
33
 
@@ -47,13 +39,13 @@ class FMail
47
39
  begin
48
40
  @mail = Mail::read_from_string(@mail_text)
49
41
  rescue Exception => ex
50
- @log.error("cannot analyze this mail: " << ex.message)
51
- @log.error("Aborting!")
42
+ error("cannot analyze this mail: " << ex.message)
43
+ error("Aborting!")
52
44
  exit false;
53
45
  end
54
46
  headers = @mail.header_fields
55
47
  @received = headers.filter{|h| h.name == 'Received'}.collect{|h| h.unparsed_value}
56
- @log.debug("found received-headers: " << @received.to_s)
48
+ debug("found received-headers: " << @received.to_s)
57
49
  end
58
50
 
59
51
  end
data/lib/file_checking.rb CHANGED
@@ -1,26 +1,20 @@
1
1
  #encoding: UTF-8
2
2
  =begin
3
3
  /***************************************************************************
4
- * ©2011-2021 Michael Uplawski <michael.uplawski@uplawski.eu> *
4
+ * ©2021-2024, Michael Uplawski <michael.uplawski@uplawski.eu> *
5
5
  * *
6
6
  * This program is free software; you can redistribute it and/or modify *
7
- * it under the terms of the GNU General Public License as published by *
8
- * the Free Software Foundation; either version 3 of the License, or *
9
- * (at your option) any later version. *
7
+ * it under the terms of the WTFPL 2.0 or later, see *
8
+ * http://www.wtfpl.net/about/ *
10
9
  * *
11
10
  * This program is distributed in the hope that it will be useful, *
12
11
  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14
- * GNU General Public License for more details. *
12
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
15
13
  * *
16
- * You should have received a copy of the GNU General Public License *
17
- * along with this program; if not, write to the *
18
- * Free Software Foundation, Inc., *
19
- * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
20
14
  ***************************************************************************/
21
15
  =end
22
16
 
23
- require 'filemagic'
17
+ # require 'filemagic'
24
18
 
25
19
  =begin
26
20
  A module to facilitate frequently occuring checks on
@@ -92,7 +86,7 @@ module File_Checking
92
86
  end
93
87
  return nil
94
88
  end
95
-
89
+ =begin
96
90
  def self.magic_check(file, magic)
97
91
  fm = FileMagic.fm
98
92
  fd = fm.fd(File.new(file) ).split(';')[0]
@@ -101,7 +95,7 @@ module File_Checking
101
95
  end
102
96
  return nil
103
97
  end
104
-
98
+ =end
105
99
  end # module
106
100
 
107
101
  =begin
@@ -113,7 +107,6 @@ msg = file_check('some_file.txt', [:exist?, :readable?, 'writable'])
113
107
  # msg = file_check('some_file.txt', [:exist, :readable, 'writable?'])
114
108
 
115
109
  msg ||= magic_check('some_file.txt', [:type?], 'OpenDocument Text'
116
- msg ||= mime_check('some_file.txt', [:mime?], 'application/vnd.oasis.opendocument.text'
117
110
  puts msg if msg
118
111
  =end
119
112
  # E O F
data/lib/ip_range.rb CHANGED
@@ -2,29 +2,23 @@
2
2
  #encoding: UTF-8
3
3
  =begin
4
4
  /***************************************************************************
5
- * ©2021-2021, Michael Uplawski <michael.uplawski@uplawski.eu> *
5
+ * ©2021-2024, Michael Uplawski <michael.uplawski@uplawski.eu> *
6
6
  * *
7
7
  * This program is free software; you can redistribute it and/or modify *
8
- * it under the terms of the GNU General Public License as published by *
9
- * the Free Software Foundation; either version 3 of the License, or *
10
- * (at your option) any later version. *
8
+ * it under the terms of the WTFPL 2.0 or later, see *
9
+ * http://www.wtfpl.net/about/ *
11
10
  * *
12
11
  * This program is distributed in the hope that it will be useful, *
13
12
  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15
- * GNU General Public License for more details. *
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
16
14
  * *
17
- * You should have received a copy of the GNU General Public License *
18
- * along with this program; if not, write to the *
19
- * Free Software Foundation, Inc., *
20
- * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
21
15
  ***************************************************************************/
22
16
  =end
23
17
 
24
- require_relative 'logging'
18
+
19
+ require_relative 'basic_logging'
25
20
  class IPRange
26
- extend Logging
27
- @@log = init_logger(STDOUT)
21
+ include BasicLogging
28
22
 
29
23
  private
30
24
 
@@ -35,7 +29,7 @@ class IPRange
35
29
  if nibbles.size == 4
36
30
  ( (nibbles[0] * 256 + nibbles[1] ) * 256 + nibbles[2] ) * 256 + nibbles[3]
37
31
  else
38
- @log.error(ip.dup << " is not an IP-address!")
32
+ error(ip.dup << " is not an IP-address!")
39
33
  return nil
40
34
  end
41
35
  end
@@ -53,7 +47,6 @@ class IPRange
53
47
 
54
48
  # create an IP-range
55
49
  def initialize(args)
56
- @log = @@log
57
50
  if args && args.length >= 2
58
51
 
59
52
  #first IP
@@ -61,7 +54,8 @@ class IPRange
61
54
  #last IP
62
55
  @last = ip_to_number args[1]
63
56
  if @last && @first && ( @last < @first )
64
- @log.error('ERROR! Last IP is smaller than first. Aborting!')
57
+ error('ERROR! Last IP is smaller than the first (' << @first.to_s << ' - ' << @last.to_s << ')')
58
+ error('Aborting!')
65
59
  exit false
66
60
  end
67
61
  @vrange = args[0,2]
@@ -73,6 +67,8 @@ end
73
67
  ######### TEST #############
74
68
  if __FILE__ == $0
75
69
  rg = IPRange.new(['192.168.2.33', '192.168.255.150'])
70
+ #### ERROR :
71
+ # rg = IPRange.new(['23.122.9.11', '23.122.8.11'])
76
72
  puts rg.first
77
73
  puts rg.last
78
74
  puts rg.in_range? '192.168.1.35'
data/lib/version.rb CHANGED
@@ -1,4 +1,21 @@
1
- VERSION = 0.9
2
- SUMMARY="Ranges and text can be commented with a leading '#' "
1
+ =begin
2
+ /***************************************************************************
3
+ * ©2021-2024, Michael Uplawski <michael.uplawski@uplawski.eu> *
4
+ * *
5
+ * This program is free software; you can redistribute it and/or modify *
6
+ * it under the terms of the WTFPL 2.0 or later, see *
7
+ * http://www.wtfpl.net/about/ *
8
+ * *
9
+ * This program is distributed in the hope that it will be useful, *
10
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
12
+ * *
13
+ ***************************************************************************/
14
+ =end
15
+
16
+ VERSION = 1.10
17
+ SUMMARY="Ruby v. 4.01"
3
18
  AUTHORS=["Michael Uplawski@uplawski.eu"]
4
19
  EMAIL="michael.uplawski@uplawski.eu"
20
+ AUTHORS_MAIL=["<michael.uplawski@uplawski.eu>"]
21
+ YEARS= "2021-2026"
metadata CHANGED
@@ -1,36 +1,54 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ip_in_range
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.9'
4
+ version: '1.1'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Uplawski@uplawski.eu
8
- autorequire:
9
8
  bindir: bin
10
9
  cert_chain: []
11
- date: 2023-01-07 00:00:00.000000000 Z
12
- dependencies: []
10
+ date: 2026-01-25 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: mail
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - "~>"
17
+ - !ruby/object:Gem::Version
18
+ version: '2.8'
19
+ - - ">="
20
+ - !ruby/object:Gem::Version
21
+ version: 2.8.1
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - "~>"
27
+ - !ruby/object:Gem::Version
28
+ version: '2.8'
29
+ - - ">="
30
+ - !ruby/object:Gem::Version
31
+ version: 2.8.1
13
32
  description: Verify that an IP is in a range
14
- email: michael.uplawski@uplawski.eu
33
+ email: "<michael.uplawski@uplawski.eu>"
15
34
  executables:
16
35
  - ip_in_range
17
36
  extensions: []
18
37
  extra_rdoc_files: []
19
38
  files:
39
+ - README.md
20
40
  - bin/ip_in_range
21
41
  - doc/license.txt
22
42
  - ip_in_range.gemspec
43
+ - lib/basic_logging.rb
23
44
  - lib/email.rb
24
45
  - lib/file_checking.rb
25
46
  - lib/ip_range.rb
26
- - lib/log.conf
27
- - lib/logging.rb
28
47
  - lib/version.rb
29
48
  homepage: ''
30
49
  licenses:
31
- - GPL-3.0
50
+ - Nonstandard
32
51
  metadata: {}
33
- post_install_message:
34
52
  rdoc_options: []
35
53
  require_paths:
36
54
  - lib
@@ -38,15 +56,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
38
56
  requirements:
39
57
  - - ">="
40
58
  - !ruby/object:Gem::Version
41
- version: '2.7'
59
+ version: '3.0'
42
60
  required_rubygems_version: !ruby/object:Gem::Requirement
43
61
  requirements:
44
62
  - - ">="
45
63
  - !ruby/object:Gem::Version
46
64
  version: '0'
47
- requirements: []
48
- rubygems_version: 3.3.15
49
- signing_key:
65
+ requirements:
66
+ - mail
67
+ rubygems_version: 4.0.4
50
68
  specification_version: 4
51
- summary: Ranges and text can be commented with a leading '#'
69
+ summary: Ruby v. 4.01
52
70
  test_files: []
data/lib/log.conf DELETED
@@ -1,56 +0,0 @@
1
- #encoding: UTF-8
2
- =begin
3
- /***************************************************************************
4
- * ©2013 - 2019 Michael Uplawski <michael.uplawski@uplawski.eu> *
5
- * *
6
- * This program is free software; you can redistribute it and/or modify *
7
- * it under the terms of the GNU General Public License as published by *
8
- * the Free Software Foundation; either version 3 of the License, or *
9
- * (at your option) any later version. *
10
- * *
11
- * This program is distributed in the hope that it will be useful, *
12
- * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14
- * GNU General Public License for more details. *
15
- * *
16
- * You should have received a copy of the GNU General Public License *
17
- * along with this program; if not, write to the *
18
- * Free Software Foundation, Inc., *
19
- * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
20
- ***************************************************************************/
21
-
22
- A simplified logger configuration. Set the level for each individual logger
23
- below. Choose a different log-device or log-file if you like. Keep the
24
- formatting intact. Do not change other sections of this file.
25
- =end
26
-
27
- # Do not touch from here ----->
28
- require 'logger'
29
-
30
- debug = Logger::DEBUG
31
- info = Logger::INFO
32
- error = Logger::ERROR
33
- fatal = Logger::FATAL
34
- warn = Logger::WARN
35
- unknown = Logger::UNKNOWN
36
- {
37
- # <---------------- to here !
38
-
39
- # Enter your settings here, but take into consideration that not all
40
- # the named classes will really produce readable output. Well, you can
41
- # always try... Either name just the log-level or make the log-level
42
- # precede the output-device or output-file like in the examples.
43
-
44
- # Example: naming a log-file
45
- #
46
- # :HtmlBuilder => [info, 'C:\temp\htmlbuilder.log'],
47
- #
48
- # :HtmlBuilder => [debug, '/tmp/htmlbuilder.log'],
49
-
50
- :FMail => info,
51
- :IPRange => info,
52
- :TopLevel => debug,
53
-
54
- # And ignore the remainder, too.
55
- }
56
- #eof