File: ruby_obexftp.rb

package info (click to toggle)
obexftp 0.24.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 792 kB
  • sloc: ansic: 7,735; ruby: 229; sh: 89; perl: 43; makefile: 39; python: 16
file content (235 lines) | stat: -rwxr-xr-x 5,663 bytes parent folder | download | duplicates (5)
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#!/usr/bin/env ruby

# == Synopsis
#
# Transfer files from/to Mobile Equipment.
#
# == Usage
#
#    ruby_obexftp.rb <transport> <operations...>
#      transport: [ -i | -b <dev> [-B <chan>] | -U <intf> | -t <dev> | -N <host> ]
#      operations: [-c <dir> ...] [-C <dir> ] [-l [<dir>]]
#    [-g <file> ...] [-p <files> ...] [-k <files> ...]
#
#    -i, --irda                  connect using IrDA transport (default)
#    -b, --bluetooth [<device>]  use or search a bluetooth device
#    [ -B, --channel <number> ]  use this bluetooth channel when connecting
#    -u, --usb [<intf>]          connect to a usb interface or list interfaces
#    -t, --tty <device>          connect to this tty using a custom transport
#    -n, --network <host>        connect to this host
#    
#    -F, --ftp                   use FTP (default)
#    -P, --push                  use OPUSH
#    -S, --sync                  use IRMC
#    -c, --chdir <DIR>           chdir
#    -C, --mkdir <DIR>           mkdir and chdir
#    -l, --list [<FOLDER>]       list current/given folder
#    -o, --output <PATH>         specify the target file name
#                                get and put always specify the remote name.
#    -g, --get <SOURCE>          fetch files
#    -G, --getdelete <SOURCE>    fetch and delete (move) files
#    -p, --put <SOURCE>          send files
#    -k, --delete <SOURCE>       delete files
#
#    -X, --capability            retrieve capability object
#    -v, --verbose               verbose messages
#    -V, --version               print version info
#    -h, --help, --usage         this help text
#
# fmtstring::
#    A +strftime+ format string controlling the
#    display of the date and time. If omitted,
#    use <em>"%Y% M% d %H:%m"</em>
#
# == Author
# Christian W. Zuckschwerdt <zany@triq.net>
#
# == Copyright
# Copyright (c) 2006 Christian W. Zuckschwerdt <zany@triq.net>
# Licensed under the same terms as Ruby.

require 'optparse'
require 'rdoc/usage'
require 'obexftp'


@cli = nil
@transport = Obexftp::BLUETOOTH
@service = Obexftp::FTP
@device = nil
@channel = -1


# connect with given uuid. re-connect every time
def cli_connect
  if @cli.nil?
    # Open
    @cli = Obexftp::Client.new(@transport)
    raise "Error opening obexftp-client" if @cli.nil?
  end

  @cli.callback Proc.new { |num, msg|
    puts "Callback no.#{num} '#{msg}'" if msg.length < 30
  }
  
#  for (retry = 0; retry < 3; retry++)
    # Connect
    @cli.connect(@device, @channel) and return @cli
#    fprintf(stderr, "Still trying to connect\n")
#  end

  @cli.close
  @cli = nil
end

def cli_disconnect
  return if @cli.nil?
  @cli.disconnect
  # @cli.close
  @cli = nil
end


# preset mode of operation depending on our name

# preset from environment

OptionParser.new do |opts|
  verbose = false
  target_path = nil
  output_file = nil
  
  opts.on('-i', '--irda') do
    @transport = Obexftp::IRDA
    @device = nil
    @channel = 0
  end
  
  opts.on('-b', '--bluetooth [ADDR]') do |arg|
    @transport = Obexftp::BLUETOOTH
    @device = arg

    if arg.nil?
      intfs = Obexftp.discover(@transport)
      intfs.each { |i| puts i }
      @device = intfs.first
    end

    @channel = Obexftp.browsebt(@device, @service)

    puts "Got channel #{@channel}"
  end
    
  opts.on('-B', '--channel N', Integer) do |arg|
    @channel = arg
  end

  opts.on('-u', '--usb [N]', Integer) do |arg|
    # "If USB doesn't work setup permissions in udev or run as superuser."
    @transport = Obexftp::USB
    @device = nil
    @channel = arg
    intfs = Obexftp.discover(@transport)
  end
    
  opts.on('-t', '--tty DEVICE') do |arg|
    @transport, @device, @channel = Obexftp::CABLE, arg, -1

    # if (strstr(arg, "ir") != NULL)
    #	fprintf(stderr, "Do you really want to use IrDA via ttys?\n");
  end

  opts.on('-n', '--network HOST') do |arg|
    @transport = Obexftp::NETWORK
    @device = arg # dotted quad, hopefully
    @channel = 0
  end

      
  opts.on('-F', '--ftp') do
    cli_disconnect
    @service = Obexftp::FTP
  end
  opts.on('-P', '--push') do
    cli_disconnect
    @service = Obexftp::PUSH
  end
  opts.on('-S', '--sync') do
    cli_disconnect
    @service = Obexftp::SYNC
  end

    
  opts.on('-l', '--list [PATH]') do |arg|
    puts cli_connect.list(arg)
  end

  opts.on('-c', '--chdir [PATH]') do |arg|
    cli_connect.chpath(arg)
  end

  opts.on('-C', '--mkdir PATH') do |arg|
    cli_connect.mkpath(arg)
  end


  opts.on('-o', '--output PATH') do |arg|
    output_file = arg
  end

  opts.on('-g', '--get PATH') do |arg|
    output_file ||= File.basename(arg)
    
    result = cli_connect.get(arg)
    raise "No such file" if result.nil?
    open(output_file, 'w') do |file|
      file.print(result)
    end
    output_file = nil
  end

  opts.on('-G', '--getdelete PATH') do |arg|
    output_file ||= File.basename(arg)

    cli = cli_connect
    result = cli.get(arg)
    raise "No such file" if result.nil?
    open(output_file, 'w') do |file|
      file.print(result)
      cli.del(arg)
    end
    output_file = nil
  end

  opts.on('-p', '--put PATH') do |arg|
    output_file ||= File.basename(arg)

    cli_connect.put_file(arg, output_file)
    output_file = nil
  end

  opts.on('-k', '--delete PATH') do |arg|
    cli_connect.delete(arg)
  end

  opts.on('-X', '--capabilities [PATH]') do |arg|
    cli_connect.get_capability(arg)
  end

  opts.on('-v', '--[no-]verbose', 'Run verbosely') do |arg|
    verbose = arg
  end
      
  opts.on('-V', '--version') do
    puts "ObexFTP #{VERSION}"
  end

  opts.on('-h', '--help') do
    RDoc::usage
  end

  opts.parse!(ARGV) # rescue RDoc::usage('usage')
end

cli_disconnect