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