myasteriskconf 0.1.0

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 2b63992f1eab01b269516d00d7e8e6d5e41a9b87f387edeebd143b21d212ee82
4
+ data.tar.gz: eadbaa1155d67056de4cf40032767310293bb797dd10fc9a7590750c75611b97
5
+ SHA512:
6
+ metadata.gz: 4998febbafe5f7a7f317d2587b3a1a353f594e37e3d0ac70bd4ab639b39504ceb50f0a9f1a75b970c1360d1363b5e5989650a0367b959c85646edd391b4975a4
7
+ data.tar.gz: 6f0e2a37dcecd86fccaaec64b7276a03dd46a5eeba78945b24f20864ce4420f181ba306ad38247ef32bddb9750e8e9d4215a06f8082450009ddbc1c79abb9476
checksums.yaml.gz.sig ADDED
Binary file
data.tar.gz.sig ADDED
Binary file
@@ -0,0 +1,213 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # file: myasteriskconf.rb
4
+
5
+ require 'polyrex-headings'
6
+
7
+
8
+ class MyAsteriskConf
9
+ using ColouredText
10
+
11
+ attr_reader :to_h
12
+
13
+ def initialize(raw_s, debug: false)
14
+
15
+ @debug = debug
16
+
17
+ contents = RXFHelper.read(raw_s).first
18
+ puts 'content: ' + contents.inspect if @debug
19
+
20
+ s = if contents =~ /^<\?ph / then
21
+ contents
22
+ else
23
+ '<?ph schema="sections[title,tags]/section[x]"?>
24
+ title: Asterisk Config
25
+ tags: asterisk
26
+
27
+ ' + contents
28
+
29
+ end
30
+
31
+ puts ('s: ' + s.inspect).debug if @debug
32
+ ph = PolyrexHeadings.new(s, debug: debug)
33
+
34
+ @to_h = @h = ph.to_h.first.last
35
+ puts @to_h.inspect
36
+
37
+ @sip = []
38
+ @extensions = []
39
+ build()
40
+
41
+ end
42
+
43
+ def to_extensions()
44
+ @extensions.join("\n")
45
+ end
46
+
47
+ def to_sip()
48
+ @sip.join("\n")
49
+ end
50
+
51
+ private
52
+
53
+ def build()
54
+
55
+ @sip << "[general]"
56
+ @sip << "localnet=" + @h[:sip][:localnet]
57
+ @sip << "externaddr = " + @h[:sip][:externaddr]
58
+
59
+ registers = @h[:sip][:register]
60
+ register = nil
61
+
62
+ if registers.any? then
63
+
64
+ register = registers.first
65
+ userid, reg_secret, sip_host = register.match(/(\w+):(\w+)@([^\/]+)/)\
66
+ .captures
67
+ reg_label = sip_host[/\w+/]
68
+
69
+
70
+ @sip << "register => " + register
71
+
72
+
73
+ reg_label = register[/(?<=@)\w+/]
74
+ provider = "
75
+ [#{reg_label}]
76
+ disable=all
77
+ type=peer
78
+ context=from-#{reg_label}
79
+ defaultuser=#{userid}
80
+ fromuser=#{userid}
81
+ authuser=#{userid}
82
+ secret=#{reg_secret}
83
+ host=#{sip_host}
84
+ fromdomain=#{sip_host}
85
+ dtmfmode=rfc2833
86
+ insecure=invite,port
87
+ qualify=yes
88
+ canreinvite=no
89
+ nat=force_rport,comedia
90
+ disallow=all
91
+ ;allow=ulaw
92
+ allow=alaw
93
+ allow=gsm
94
+ allow=g729
95
+ "
96
+
97
+ @sip << provider
98
+
99
+ end
100
+
101
+ extensions = []
102
+
103
+ phones = @h[:phones].map do |x|
104
+
105
+ puts 'x: ' + x.inspect if @debug
106
+ x.match(/^([^:]+):([^\/]+)\/([^$]+)/).captures # id, secret, ext
107
+
108
+ end
109
+
110
+ phones.each do |id, secret, ext|
111
+
112
+ @sip << sip_template(id.downcase, secret)
113
+ extensions << ext_template(ext, id)
114
+
115
+ end
116
+
117
+ @extensions << "[my-phones]"
118
+ @extensions.concat extensions
119
+
120
+ a = phones.map {|x| "SIP/" + x[0].downcase }
121
+ @extensions << "\nexten => 1009,1,Dial(%s,40)" % a.join('&')
122
+ @extensions << "exten => 1009,n,Hangup()"
123
+
124
+ # check for outbound extensions
125
+
126
+ outbound = @h.dig(*%i(extensions outbound))
127
+
128
+ =begin
129
+ Pattern matching help for variable extension numbers:
130
+
131
+ - X - any digit from 0-9
132
+ - Z - any digit from 1-9
133
+ - N - any digit from 2-9
134
+ - [12679] - any digit in the brakets (in the example: 1,2,6,7,9)
135
+ - . - (dot) wildcard, matches everything remaining
136
+ ( _1234. - matches anything strating with 1234 excluding 1234 itself).
137
+
138
+ source: https://www.asteriskguru.com/tutorials/extensions_conf.html
139
+ =end
140
+
141
+ outbound.each do |key, value|
142
+
143
+ r = key[/d\{(\d+)\}/,1]
144
+
145
+ dialout = value.sub(/\(EXTEN\)/,'${EXTEN}')
146
+
147
+ if r then
148
+
149
+ pattern = '_' + 'X' * r.to_i
150
+
151
+ elsif key[/^\d+$/]
152
+
153
+ pattern = key.to_s
154
+
155
+ else
156
+
157
+ dialout = value.sub(/\(EXTEN\)/,'${EXTEN}')
158
+ pattern = '_' + key.to_s
159
+
160
+ end
161
+
162
+ @extensions << "\nexten => %s,1,Dial(SIP/%s@%s,60,tr)" \
163
+ % [pattern, dialout, reg_label]
164
+ @extensions << "exten => %s,n,Playback(invalid)" % pattern
165
+ @extensions << "exten => %s,n,Hangup" % pattern
166
+
167
+ end
168
+
169
+ if register then
170
+
171
+ @extensions << "\n[from-#{reg_label}]"
172
+ reg_ext = register[/\d+$/]
173
+ @extensions << "exten => #{reg_ext},n,Goto(my-phones,1009,1)"
174
+ @extensions << "exten => #{reg_ext},n,Hangup()"
175
+
176
+ end
177
+
178
+ end
179
+
180
+ def ext_template(ext, deviceid)
181
+
182
+ "
183
+ exten => #{ext},1, Answer()
184
+ exten => #{ext},n,Dial(SIP/#{deviceid},40)
185
+ exten => #{ext},n,Hangup()"
186
+
187
+ end
188
+
189
+
190
+ def sip_template(deviceid, secret)
191
+
192
+ "
193
+ [#{deviceid}]
194
+ defaultuser=#{deviceid}
195
+ secret=#{secret}
196
+ type=friend
197
+ host=dynamic
198
+ qualify=yes
199
+ context=my-phones
200
+ insecure=invite,port
201
+ canreinvite=no
202
+ disallow=all ; better for custom-tunning codec selection
203
+ allow=ulaw
204
+ allow=alaw
205
+ allow=gsm"
206
+
207
+ end
208
+
209
+
210
+
211
+
212
+ end
213
+
metadata ADDED
@@ -0,0 +1,91 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: myasteriskconf
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - James Robertson
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain:
11
+ - |
12
+ -----BEGIN CERTIFICATE-----
13
+ MIIEXjCCAsagAwIBAgIBATANBgkqhkiG9w0BAQsFADAsMSowKAYDVQQDDCFnZW1t
14
+ YXN0ZXIvREM9amFtZXNyb2JlcnRzb24vREM9ZXUwHhcNMjAwMTAyMTA0NTA3WhcN
15
+ MjEwMTAxMTA0NTA3WjAsMSowKAYDVQQDDCFnZW1tYXN0ZXIvREM9amFtZXNyb2Jl
16
+ cnRzb24vREM9ZXUwggGiMA0GCSqGSIb3DQEBAQUAA4IBjwAwggGKAoIBgQC8ZWDZ
17
+ yeRcgxFRTR0vJ3pKrtyK37LIuc60J9scLMANo/v6+CxgxZS6TADWZnmVSNhOljQD
18
+ /NyyaFj0LdSdaAvbS9z/lcPtKnWqyNY61WaQQ0rODua2WTpkWq64OSGy9frwwyHV
19
+ RMHoIgo/b2Q43g1hWHOqkvX/dxY8Mh1RbvgBJnGl80QU9ksRDgor0Z7PPnKVruR0
20
+ 3tpDIy8HH/ziqf8wXGap6XMgoh6KuBCc3+yb3fcYBYmJYE1ugiQoNg9JJYt2LDgt
21
+ HLaVW2OuUmPE8psiQUFfndqkk3iMwxL2Jk2ExbMgyi2jf/UjxU9I1T0k6sAPDYOs
22
+ NwREFTogojl3hAcZ11zAS/pYXYf305DiK3vaVcOroAa5qGnhU2FXodTdCwdsmZF3
23
+ jsEyxJY7Vsxd0N0DC4RRM6kbQp+7K07aY/Z6qs3VOc+PmVzxE+yKoD9tTod2BVI1
24
+ OHaBnj0VSFs1MoAW14WNt+LV0weVkfRamKKAvb27jV/La+bfCUoR8R2wq/UCAwEA
25
+ AaOBijCBhzAJBgNVHRMEAjAAMAsGA1UdDwQEAwIEsDAdBgNVHQ4EFgQURMqAyIvX
26
+ 3YJzBATnTPslO4PQtyEwJgYDVR0RBB8wHYEbZ2VtbWFzdGVyQGphbWVzcm9iZXJ0
27
+ c29uLmV1MCYGA1UdEgQfMB2BG2dlbW1hc3RlckBqYW1lc3JvYmVydHNvbi5ldTAN
28
+ BgkqhkiG9w0BAQsFAAOCAYEAmSOGe2u+rYxAL3K1gHZgtnKb2BdjrXjUhg8YgJYY
29
+ Are9+c0vRfLd46BoLHMz+dnYHVIefu5vMRqqcLvfaRhiQxSBKtn4LcpMZyDCUP3N
30
+ qUxZMqCGajVieI8KfuoK4BXZgABtZDKGcqndvvRob0MA/Oxiq4zkH4KpWHC+26LG
31
+ z6aNADnmA+aWYTNK3zApbBupIv2KxyUzsvFYq3Cy9Z0GlILs8rgaxa5ZDzO7KuBC
32
+ dsyYAggknKiHbicxPfRnaCYyDHehPeYm+E8EdnnwIFR6+Od/J0sG38dhphwjWOmg
33
+ CJcz6NPzOeAYzCXjKtRF3Bn6yjtwR0Ew4eXjZJfk/y8/UVu6ORTSNOaylDYxemPe
34
+ kkzcQy0Hv2Pmzdcswo2dJRL+NJ8SD4EXlO3VUXzY1GmR+H5m1Vt65XbpeXT/dcyi
35
+ f4kUndjKYbnXAYWJJKFYMEb5FUZq/BQPJk3hqn7FiOr/rZ+BgqoGiNdUvqFXV4pm
36
+ dJTSHDdF6Lr/TdVPMMJdRDg6
37
+ -----END CERTIFICATE-----
38
+ date: 2020-01-02 00:00:00.000000000 Z
39
+ dependencies:
40
+ - !ruby/object:Gem::Dependency
41
+ name: polyrex-headings
42
+ requirement: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: 0.2.0
47
+ - - "~>"
48
+ - !ruby/object:Gem::Version
49
+ version: '0.2'
50
+ type: :runtime
51
+ prerelease: false
52
+ version_requirements: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: 0.2.0
57
+ - - "~>"
58
+ - !ruby/object:Gem::Version
59
+ version: '0.2'
60
+ description:
61
+ email: james@jamesrobertson.eu
62
+ executables: []
63
+ extensions: []
64
+ extra_rdoc_files: []
65
+ files:
66
+ - lib/myasteriskconf.rb
67
+ homepage: https://github.com/jrobertson/myasteriskconf
68
+ licenses:
69
+ - MIT
70
+ metadata: {}
71
+ post_install_message:
72
+ rdoc_options: []
73
+ require_paths:
74
+ - lib
75
+ required_ruby_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ required_rubygems_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ requirements: []
86
+ rubygems_version: 3.0.3
87
+ signing_key:
88
+ specification_version: 4
89
+ summary: Generates basic Asterisk configurations from a high level config in Markdown
90
+ format.
91
+ test_files: []
metadata.gz.sig ADDED
Binary file