mpw 2.0.3 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/mpw/sync.rb DELETED
@@ -1,138 +0,0 @@
1
- #!/usr/bin/ruby
2
- # author: nishiki
3
- # mail: nishiki@yaegashi.fr
4
- # info: a simple script who manage your passwords
5
-
6
- require 'rubygems'
7
- require 'i18n'
8
- require 'yaml'
9
- require 'tempfile'
10
- require 'mpw/mpw'
11
- require 'mpw/item'
12
-
13
- module MPW
14
- class Sync
15
-
16
- attr_accessor :error_msg
17
-
18
- # Constructor
19
- # raise an exception if there is a bad parameter
20
- def initialize(config, local, password=nil)
21
- @error_msg = nil
22
- @config = config
23
- @local = local
24
- @password = password
25
-
26
- raise I18n.t('error.class') if not @local.instance_of?(MPW)
27
- end
28
-
29
- # Get the data on remote host
30
- # @rtrn: true if get the date, else false
31
- def get_remote
32
- case @config.sync_type
33
- when 'mpw'
34
- require 'mpw/sync/mpw'
35
- @sync = SyncMPW.new(@config.sync_host, @config.sync_user, @config.sync_pwd, @config.sync_path, @config.sync_port)
36
- when 'sftp', 'scp', 'ssh'
37
- require 'mpw/sync/ssh'
38
- @sync = SyncSSH.new(@config.sync_host, @config.sync_user, @config.sync_pwd, @config.sync_path, @config.sync_port)
39
- when 'ftp'
40
- require 'mpw/sync/ftp'
41
- @sync = SyncFTP.new(@config.sync_host, @config.sync_user, @config.sync_pwd, @config.sync_path, @config.sync_port)
42
- else
43
- @error_msg = I18n.t('error.unknown_type')
44
- return false
45
- end
46
-
47
- if not @sync.connect
48
- @error_msg = @sync.error_msg
49
- return false
50
- end
51
-
52
-
53
- file_tmp = Tempfile.new('mpw-')
54
- raise @sync.error_msg if not @sync.get(file_tmp.path)
55
-
56
- @remote = MPW.new(file_tmp.path, @config.key)
57
- raise @remote.error_msg if not @remote.decrypt(@password)
58
-
59
- file_tmp.close(true)
60
- return true
61
- rescue Exception => e
62
- @error_msg = "#{I18n.t('error.sync.download')} #{e}"
63
- file_tmp.close(true)
64
- return false
65
- end
66
-
67
- # Sync remote data and local data
68
- # raise an exception if there is a problem
69
- def sync
70
-
71
- if not @remote.to_s.empty?
72
- @local.list.each do |item|
73
- update = false
74
- @remote.list.each do |r|
75
-
76
- # Update item
77
- if item.id == r.id
78
- if item.last_edit < r.last_edit
79
- raise item.error_msg if not item.update(name: r.name,
80
- group: r.group,
81
- host: r.host,
82
- protocol: r.protocol,
83
- user: r.user,
84
- password: r.password,
85
- port: r.port,
86
- comment: r.comment
87
- )
88
- end
89
-
90
- r.delete
91
- update = true
92
-
93
- break
94
- end
95
- end
96
-
97
- # Remove an old item
98
- if not update and item.last_sync.to_i < @config.last_sync and item.last_edit < @config.last_sync
99
- item.delete
100
- end
101
- end
102
- end
103
-
104
- # Add item
105
- @remote.list.each do |r|
106
- if r.last_edit > @config.last_sync
107
- item = Item.new(id: r.id,
108
- name: r.name,
109
- group: r.group,
110
- host: r.host,
111
- protocol: r.protocol,
112
- user: r.user,
113
- password: r.password,
114
- port: r.port,
115
- comment: r.comment,
116
- created: r.created,
117
- last_edit: r.last_edit
118
- )
119
- raise @local.error_msg if not @local.add(item)
120
- end
121
- end
122
-
123
- @local.list.each do |item|
124
- item.set_last_sync
125
- end
126
-
127
- raise @mpw.error_msg if not @local.encrypt
128
- raise @sync.error_msg if not @sync.update(@config.file_gpg)
129
-
130
- @config.set_last_sync
131
-
132
- return true
133
- rescue Exception => e
134
- @error_msg = "#{I18n.t('error.sync.unknown')} #{e}"
135
- return false
136
- end
137
- end
138
- end
data/lib/mpw/ui/clissh.rb DELETED
@@ -1,42 +0,0 @@
1
- #!/usr/bin/ruby
2
- # author: nishiki
3
- # mail: nishiki@yaegashi.fr
4
- # info: a simple script who manage your passwords
5
-
6
- require 'mpw/ui/cli'
7
-
8
- class CliSSH < Cli
9
-
10
- attr_accessor :server, :port, :login
11
-
12
- # Connect to SSH
13
- # args: search -> string to search
14
- def ssh(search)
15
- result = @mpw.list(search: search, protocol: 'ssh')
16
-
17
- if result.length > 0
18
- result.each do |item|
19
- server = @server.nil? ? item.host : @server
20
- port = @port.nil? ? item.port : @port
21
- login = @login.nil? ? item.user : @login
22
-
23
- passwd = item.password
24
-
25
- if port.nil? and port.empty?
26
- port = 22
27
- end
28
-
29
- puts "#{I18n.t('ssh.display.connect')} ssh #{login}@#{server} -p #{port}"
30
- if passwd.empty?
31
- system("ssh #{login}@#{server} -p #{port}")
32
- else
33
- system("sshpass -p '#{passwd}' ssh #{login}@#{server} -p #{port}")
34
- end
35
- end
36
-
37
- else
38
- puts I18n.t('ssh.display.nothing')
39
- end
40
- end
41
- end
42
-