source: branches/guitools-2.0/shared/smbtree.vrs@ 348

Last change on this file since 348 was 348, checked in by Herwig Bauernfeind, 16 years ago

GUI-Tools: EVFSGUI Version 2.0 beta6 level code

File size: 14.9 KB
Line 
1/* Routines to handle smbtree output */
2
3/*:VRX */
4_RefreshTree:
5 say time()' _RefreshTree() started'
6 FirstRun = 0
7
8 ok = SysFileDelete(samba.!msg)
9
10 if UserCred = 'USERCRED' | UserCred = '' | UserCred = '--user=%' then UserCred = '-N'
11 if ShowHidden = 'SHOWHIDDEN' | ShowHidden = '' then ShowHidden = 0
12
13 if BroadCast = 1 then BroadCast = '-b'; else BroadCast = ''
14
15 say ' detach 'samba.!smbtreeexe' 'BroadCast' -S 'debuglevel' 'UserCred' >'samba.!msg
16 address cmd 'detach 'samba.!smbtreeexe' 'BroadCast' -S 'debuglevel' 'UserCred' >'samba.!msg
17
18 if BroadCast = '-b' then BroadCast = 1; else BroadCast = 0
19 if UserCred = '-N' then UserCred = ''
20
21 ok = VRMethod("CN_smbtree", "RemoveRecord", "ALL")
22
23 RefreshMode = "TREE"
24 ok = VRSet("CN_smbtree","Enabled", 0)
25 ok = VRset("TM_RefreshTreeDisplay","Enabled",1)
26
27 say time()' _RefreshTree() done'
28return
29
30/*:VRX */
31_RefreshTreeDisplay:
32 say time()' _RefreshTreeDisplay() started'
33 stat = stream(samba.!msg,'c','open read')
34 if stat <> "READY:" then return
35
36 if UserCred = 'USERCRED' | UserCred = '' | UserCred = '--user=%' then do
37 UserCred = '-N'
38 end
39
40 if ShowHidden = 'SHOWHIDDEN' | ShowHidden = '' then ShowHidden = 0
41
42 ok = VRset("TM_RefreshTreeDisplay","Enabled",0)
43/* ok = VRSet("Main", "StatusText", NLVGetMessage(50)) */
44 ok = VRSet("CN_smbtree", 'Enabled', 0 )
45/* ok = VRSet("Main", 'Pointer', 'Wait' ) */
46
47 ok = VRSet( "CN_smbtree", "Painting", 0 )
48 ok = VRMethod("CN_smbtree", "RemoveRecord", "ALL")
49
50 drop smbtree.
51 drop smbtreeline.
52
53 sl = 0
54
55 do until lines(samba.!msg) = 0
56 sl = sl + 1
57 smbtreeline.sl = linein(samba.!msg)
58 end
59 smbtreeline.0 = sl
60 ok = stream(samba.!msg,'c','close')
61 ok = SysFileDelete(samba.!msg)
62
63 do sl = 1 to smbtreeline.0
64 Header = c2x(left(smbtreeline.sl,3))
65 select
66 when Header = "09095C" then do /* share - obsolete, we do that differently now see below! */
67 smbtreeline.sl = strip(smbtreeline.sl,,'09'x)
68 parse var smbtreeline.sl '\\'machine'\'share '09'x comment
69 machine = strip(machine)
70 share = left(strip(share),10)
71 comment = strip(comment)
72 text = translate(share' 'comment)
73 res = _GuessIcon(text)
74 if VRGet("CN_smbtree","View") = "IconTree" then do
75 parent = smbtree.!machine
76 smbtree.!share = VRMethod( "CN_smbtree", "AddRecord",parent,, share||'0D0A'x||comment, res)
77 ok = VRMethod( "CN_smbtree", "SetRecordAttr", smbtree.!share, "ReadOnly", 1)
78 if pos("$", share) > 0 then ok = VRMethod( "CN_smbtree", "SetRecordAttr", smbtree.!share, "Visible", ShowHidden)
79 end
80 end /* end of obsolete share code */
81 when Header = "095C5C" then do /* Machine */
82 smbtreeline.sl = strip(smbtreeline.sl,,'09'x)
83 parse var smbtreeline.sl '\\'machine '0909'x comment
84 machine = left(strip(machine),13)
85 comment = strip(comment)
86 if VRGet("CN_smbtree","View") = "IconTree" then parent = smbtree.!workgroup; else parent = ""
87 smbtree.!machine = VRMethod( "CN_smbtree", "AddRecord",parent,, machine||'0D0A'x||comment)
88 /* We make any machine as sleeping initially */
89 ok = VRMethod( "CN_smbtree", "SetRecordAttr", smbtree.!machine, "Icon","#61:PMWP.DLL")
90 ok = VRMethod( "CN_smbtree", "SetRecordAttr", smbtree.!machine, "ReadOnly", 1)
91 ok = VRMethod( "CN_smbtree", "SetFieldData", smbtree.!machine, NBFH, machine, CommentFH, Comment, WorkGroupFH, CurWG)
92 ok = VRSet( "CN_smbtree", "Painting", 1 )
93 ok = VRSet( "CN_smbtree", "Painting", 0 )
94 if VRGet("CN_smbtree","View") = "Detail" then do
95 address cmd samba.!nmblookupexe' 'machine' 'debuglevel' -N >'samba.!msg
96 ipstr = ""
97 ip = ""
98 do until lines(samba.!msg) = 0
99 nmblookupline = linein(samba.!msg)
100 if pos(strip(machine)'<',nmblookupline) > 0 then do
101 parse var nmblookupline ip .
102 if pos(strip(ip), ipstr) = 0 then do
103 ipstr = ipstr||ip','
104 end
105 end
106 end
107 ok = SysFileDelete(samba.!msg)
108 ipstr = strip(ipstr,,',')
109 ok = VRMethod( "CN_smbtree", "SetFieldData", smbtree.!machine, IPFH, IPStr)
110 ok = stream(samba.!msg,'c','close')
111 if ip <> "" then do
112 address cmd samba.!smbclientexe' -L "'strip(machine)'" -I "'ip'" -N 'debuglevel' 2>'samba.!msg' 1>NUL'
113 smbline = linein(samba.!msg)
114 ok = stream(samba.!msg,'c','close')
115 parse var smbline "Domain=["WorkGroup"] OS=["OS"] Server=["Server"]"Rest
116 ok = VRMethod( "CN_smbtree", "SetFieldData", smbtree.!machine, OSFH, OS)
117 /* WorkGroupFH, Workgroup */
118 address cmd samba.!nmblookupexe' -A 'machine' 'debuglevel' -N >'samba.!msg
119 Master = ""
120 do until lines(samba.!msg) = 0
121 nmblookupline = linein(samba.!msg)
122 if pos('<1b>', nmblookupline) > 0 then Master = Master||"LMB," /* + */
123 if pos('<1d>', nmblookupline) > 0 then Master = Master||"DMB," /* * */
124 if pos('MAC',nmblookupline) > 0 then do
125 parse var nmblookupline . '=' MAC
126 MAC = strip(MAC)
127 end
128 end
129 Master= strip(Master,,',')
130 ok = stream(samba.!msg,'c','close')
131 ok = SysFileDelete(samba.!msg)
132 ok = VRMethod( "CN_smbtree", "SetFieldData", smbtree.!machine, MBFH, Master,MacFH,MAC)
133 end
134 end
135 else do
136 call _RefreshShares
137 end
138 end
139 when smbtreeline.sl = "" then nop
140 otherwise do
141 say ' Workgroup: "'smbtreeline.sl'"'
142 if pos("RECEIVING",translate(smbtreeline.sl)) > 0 then do
143 Msg.Type = "W"
144 Msg.Text = smbtreeline.sl
145 call _ShowMsg
146 end
147 else do
148 if VRGet("CN_smbtree","View") = "IconTree" then do
149 smbtree.!workgroup = VRMethod( "CN_smbtree", "AddRecord",,, smbtreeline.sl,"#62:PMWP.DLL")
150 ok = VRMethod( "CN_smbtree", "SetFieldData", smbtree.!workgroup, WorkGroupFH, smbtreeline.sl)
151 ok = VRMethod( "CN_smbtree", "SetRecordAttr", smbtree.!workgroup, "Collapsed", 0)
152 ok = VRMethod( "CN_smbtree", "SetRecordAttr", smbtree.!workgroup, "ReadOnly", 1)
153 ok = VRMethod( "CN_smbtree", 'SetRecordAttr', smbtree.!workgroup, "UserData", "WORKGROUP|")
154 end
155 CurWG = smbtreeline.sl
156 end
157 end
158 end
159 end
160
161 ok = VRSet( "CN_smbtree", "Painting", 1 )
162
163/* ok = VRSet("Main", 'Pointer', '<default>' ) */
164 ok = VRSet("CN_smbtree","Enabled", 1)
165 ok = VRSet("TM_Throbber","Enabled", 0)
166 ok = VRSet("Pict_Throbber","Visible", 0)
167 say time()' _RefreshTreeDisplay() done'
168return
169
170/*:VRX _RefreshShares */
171_RefreshShares:
172 say time()' _RefreshShares() started'
173 RefreshID = RANDOM()
174 smbmachine = TempDir||"smbmachine."||machine
175
176 say ' detach 'samba.!smbclientexe' -L "'strip(machine)'" 'UserCred' 'debuglevel' 1>'smbmachine' 2>NUL'
177 address cmd 'detach 'samba.!smbclientexe' -L "'strip(machine)'" 'UserCred' 'debuglevel' 1>'smbmachine' 2>NUL'
178
179 RefreshMode = "SHARE"
180
181 ok = VRSet("CN_smbtree","Enabled", 0)
182 ok = VRset("TM_RefreshTreeDisplay","Enabled",1)
183 say time()' _RefreshShares() done'
184return
185
186/*:VRX _AddSharesDisplay
187*/
188_AddSharesDisplay: /* New get shares code - uses smbclient output and is much faster */
189 say time()' _AddSharesDisplay() started'
190
191 ok = SysFileTree(Tempdir||'smbmachine.*',smbmachine.,'FO')
192 say ' 'smbmachine.0' file(s) to process.'
193
194 if smbmachine.0 = 0 then do /* we are done, no more files around, cleanup, disable Timer and exit */
195 RefreshMode = ""
196 ok = VRSet("CN_smbtree","Enabled", 1)
197 ok = VRset("TM_RefreshTreeDisplay","Enabled",0)
198 ok = VRSet( "CN_smbtree", "Painting", 0 )
199 ok = VRSet( "CN_smbtree", "Painting", 1 )
200 say time()' _AddSharesDisplay() completed'
201 return /* exit here */
202 end
203
204 if UserCred = 'USERCRED' | UserCred = '' | UserCred = '--user=%' then UserCred = '-N'
205 if ShowHidden = 'SHOWHIDDEN' | ShowHidden = '' then ShowHidden = 0
206
207 do I = 1 to smbmachine.0
208 say ' Going for "'smbmachine.I'"'
209 stat = stream(smbmachine.I,'c','open read')
210 if stat = "READY:" then do /* we found a readable output file */
211 /* Machine = VRParseFilename(smbmachine.I,'E') */
212 Machine = substr(smbmachine.I,pos('.',smbmachine.I)+1)
213
214 smbtree.!machine = _GetMachinehandle(Machine)
215 say ' Machine (handle) = "'machine'" ('smbtree.!machine')'
216
217 if smbtree.!machine = "" then do /* invalid (old) file */
218 say time()' _AddSharesDisplay() exit with Invalid file found (no corresponding machine)'
219 ok = stream(smbmachine.I,'c','close')
220 ok = SysFileDelete(smbmachine.I)
221 iterate
222 end
223 line = linein(smbmachine.I)
224 say ' Answer "'line'"'
225 ok = VRMethod('CN_smbtree', 'SetRecordAttr', smbtree.!machine, 'UserData', "SERVER|"||strip(line))
226
227 if pos("FAIL", translate(line)) > 0 then do /* we see an error message - the term "FAIL" seems to be common to all */
228 say time()' _AddSharesDisplay() exit with "'line'"'
229 ok = stream(smbmachine.I,'c','close')
230 ok = SysFileDelete(smbmachine.I)
231 iterate
232 end
233
234 retries = 0
235 do while(left(line,1) <> '09'x)
236 line = linein(smbmachine.I)
237 retries = retries + 1
238 say ' Skip 'retries' "'line'"'
239 if retries >=10 then do /* No valid output - error */
240 say time()' _AddSharesDisplay() exit with invalid output error'
241 ok = stream(smbmachine.I,'c','close')
242 ok = SysFileDelete(smbmachine.I)
243 leave
244 end
245 end
246 if retries >=10 then iterate
247
248 /* Skip header */
249 line = linein(smbmachine.I)
250 line = linein(smbmachine.I)
251
252 if left(line,5) = "Error" then ok = VRMethod('CN_smbtree', 'SetRecordAttr', smbtree.!machine, 'UserData', "SERVER|"||strip(line))
253
254 do while(left(line,1) = '09'x)
255 parse var line '09'x share type comment
256 type = translate(strip(type))
257 comment = strip(comment)
258
259 select
260 when type = "DISK" then res = '#34:PMWP.DLL'
261 when type = "PRINTER" then res = '#65:PMWP.DLL'
262 when type = "IPC" then res = '#59:PMWP.DLL'
263 when type = "DEVICE" then res = '#84:PMWP.DLL' /* There might be better ones around */
264 otherwise res = ''
265 end
266
267 /* Now the machine receives the wakeup icon */
268 ok = VRMethod('CN_smbtree', 'SetRecordAttr', smbtree.!machine, 'Icon', "#35:PMWP.DLL")
269 parent = smbtree.!machine
270 smbtree.!share = VRMethod( "CN_smbtree", "AddRecord",parent,, share||'0D0A'x||comment, res)
271 ok = VRMethod( "CN_smbtree", "SetRecordAttr", smbtree.!share, "ReadOnly", 1, 'UserData', type"|")
272 if pos("$", share) > 0 then ok = VRMethod( "CN_smbtree", "SetRecordAttr", smbtree.!share, "Visible", ShowHidden)
273
274 /* get next share */
275 line = linein(smbmachine.I)
276 end
277
278 do until left(line,10) = '09'x||'Workgroup'
279 line = linein(smbmachine.I)
280 end
281 line = linein(smbmachine.I)
282 /* Reading workgroup and master - eventually both empty */
283 line = linein(smbmachine.I)
284 parse var line '09'x workgroup master
285 master = strip(master)
286
287 /* we use this to set the workgroup for manually added servers */
288 if workgroup <> "" then do
289 wgh = _GetMachinehandle(workgroup)
290 if wgh = "" then do /* This machine is in a new workgroup - add it as well */
291 wgh = VRMethod( "CN_smbtree", "AddRecord",,, workgroup,"#62:PMWP.DLL")
292 ok = VRMethod( "CN_smbtree", "SetFieldData", wgh, WorkGroupFH, workgroup)
293 ok = VRMethod( "CN_smbtree", "SetRecordAttr", wgh, "Collapsed", 0)
294 ok = VRMethod( "CN_smbtree", "SetRecordAttr", wgh, "ReadOnly", 1)
295 ok = VRMethod( "CN_smbtree", 'SetRecordAttr', wgh, "UserData", "WORKGROUP|")
296 end
297 if wgh <> "" then ok = VRMethod('CN_smbtree', 'SetRecordAttr', smbtree.!machine, 'Parent', wgh)
298 end
299
300 say time()' _AddSharesDisplay() success and cleanup'
301 ok = stream(smbmachine.I,'c','close')
302 ok = SysFileDelete(smbmachine.I)
303 if ok <> 0 then say ' Failure 'ok' deleting "'smbmachine.I'"!'
304 end
305 else say ' Got "'stat'" for "'smbmachine.I'"'
306 end
307 say time()' _AddSharesDisplay() loop end'
308return
309
310/*:VRX _GetMachinehandle
311*/
312
313_GetMachinehandle: procedure /* get recordhandle by machine name (also works for workgroups) */
314 Machine = translate(arg(1))
315
316 ok = VRMethod("CN_smbtree", "GetRecordList", "All", rh.)
317 match = 0
318
319 do I = 1 to rh.0
320 ResName = translate(VRMethod("CN_smbtree","GetRecordAttr",rh.I,"Caption"))
321
322 parse var ResName ResName '0D0A'x .
323 ResName = strip(ResName)
324
325 if Machine = ResName then do /* we got a matching name */
326 match = 1
327 leave
328 end
329 end
330 if match = 0 then rh.I = "" /* return an empty handle, if there was no match */
331return rh.I
Note: See TracBrowser for help on using the repository browser.