/
SymSession.cs
executable file
·1291 lines (1191 loc) · 43.5 KB
/
SymSession.cs
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.IO;
using System.Net;
using System.Text;
using System.Threading;
using System.Net.Sockets;
using System.Collections.Generic;
namespace Symitar
{
public class SymSession
{
//========================================================================
public struct FMRunNfo
{
public int sequence;
public string title;
public FMRunNfo(int Sequence, string Title)
{
sequence = Sequence;
title = Title;
}
}
//------------------------------------------------------------------------
public enum FMType
{
Account = 0,
Inventory = 1,
Payee = 2,
GLAccount = 3,
ReceivedItem = 4,
Participant = 5,
Participation = 6,
Dealer = 7,
User = 8,
Collateral = 9,
Member =10,
MemberAddress =11,
NonAccountName =12,
FinancialReporting =13,
Wire =14
}
//========================================================================
private TcpClient socket;
private NetworkStream stream;
private Thread keepAlive;
private DateTime lastActive;
private bool isKeepAlive;
private Semaphore sockSem;
//------------------------------------------------------------------------
public string server;
public string sym;
public string error;
public bool isConnected;
public bool isLoggedIn;
public bool inUse;
public int defaultTimeout = 5000;
public int keepAliveInterval = 45000;
//========================================================================
public delegate void SessionDebugCallback(string str);
public SessionDebugCallback debug=null;
//========================================================================
public SymSession()
{
socket = null;
stream = null;
keepAlive = null;
isKeepAlive = false;
server = sym = error = "";
isConnected = isLoggedIn = inUse = false;
sockSem = new Semaphore(1,1);
}
//========================================================================
private void SockLock(int timeout)
{
if(sockSem.WaitOne(timeout)==false)
throw new Exception("Unable to Obtain Socket Lock");
inUse = true;
}
//------------------------------------------------------------------------
private void SockUnlock()
{
inUse=false;
sockSem.Release();
}
//------------------------------------------------------------------------
public bool LockTest()
{
try
{
SockLock(2500);
SockUnlock();
}
catch(Exception)
{
return false;
}
return true;
}
//========================================================================
private void Write(byte[] buff, int off, int size, int timeout)
{
SockLock(5000);
int oto = stream.WriteTimeout;
stream.WriteTimeout = timeout;
stream.Write(buff, off, size);
stream.WriteTimeout = oto;
SockUnlock();
lastActive = DateTime.Now;
if(debug != null)
{
debug("WRITE: ");
for(int i=0; i<size; i++) debug(buff[off+i].ToString("X2")+", ");
debug("\n");
}
}
//------------------------------------------------------------------------
private void Write(byte[] buff, int timeout) { Write(buff, 0, buff.Length, timeout); }
private void Write(string str , int timeout) { Write(EncodeString(str), timeout); }
private void Write(SymCommand cmd, int timeout) { Write(cmd.Packet(), timeout); }
private void Write(byte[] buff) { Write(buff, defaultTimeout); }
private void Write(string str) { Write(EncodeString(str), defaultTimeout); }
private void Write(SymCommand cmd) { Write(cmd.Packet(), defaultTimeout); }
//========================================================================
private void WakeUp() { try{Write(new SymCommand("WakeUp"), 1000);} catch(Exception){} }
//========================================================================
private byte[] Read(int size, int timeout)
{
SockLock(5000);
int oto = stream.ReadTimeout;
stream.ReadTimeout = timeout;
DateTime begin = DateTime.Now;
int read=0;
byte[] buff = new byte[size];
while(read < size)
{
if((DateTime.Now - begin).Milliseconds > timeout)
{
SockUnlock();
Disconnect();
throw new Exception("Socket Read Timeout");
}
read += stream.Read(buff,read,size-read);
}
stream.ReadTimeout = oto;
SockUnlock();
lastActive = DateTime.Now;
if(debug != null)
{
debug("READ: ");
for(int i=0; i<buff.Length; i++) debug(buff[i].ToString("X2")+", ");
debug("\n");
}
return buff;
}
//------------------------------------------------------------------------
private byte[] Read(int size) { return Read(size, defaultTimeout); }
//------------------------------------------------------------------------
private byte[] ReadUntil(byte[] match, int timeout)
{
SockLock(5000);
int oto = stream.ReadTimeout;
stream.ReadTimeout = timeout;
DateTime begin = DateTime.Now;
bool hit = false;
byte[] test = new byte[match.Length];
List<byte> buff = new List<byte>();
while(!hit)
{
if((DateTime.Now - begin).Milliseconds > timeout)
{
SockUnlock();
Disconnect();
throw new Exception("Socket Read Timeout");
}
for(int i=0; i<(test.Length-1); i++) test[i]=test[i+1];
stream.Read(test,test.Length-1,1);
buff.Add(test[test.Length-1]);
hit = true;
for(int c=0; c<test.Length; c++)
{
if(test[c] != match[c])
{
hit=false;
break;
}
}
}
stream.ReadTimeout = oto;
SockUnlock();
lastActive = DateTime.Now;
if(debug != null)
{
byte[] dbg = buff.ToArray();
debug("READ: ");
for(int i=0; i<dbg.Length; i++) debug(dbg[i].ToString("X2")+", ");
debug("\n");
}
return buff.ToArray();
}
//------------------------------------------------------------------------
private byte[] ReadUntil(string match, int timeout) { return ReadUntil(EncodeString(match), timeout); }
private string ReadUntilString(byte[] match, int timeout) { return DecodeString(ReadUntil(match, timeout)); }
private string ReadUntilString(string match, int timeout) { return DecodeString(ReadUntil(match, timeout)); }
private byte[] ReadUntil(byte[] match) { return ReadUntil(match, defaultTimeout); }
private byte[] ReadUntil(string match) { return ReadUntil(match, defaultTimeout); }
private string ReadUntilString(byte[] match) { return ReadUntilString(match, defaultTimeout); }
private string ReadUntilString(string match) { return ReadUntilString(match, defaultTimeout); }
//========================================================================
private SymCommand ReadCommand(int timeout)
{
ReadUntil(new byte[] {0x1B, 0xFE}, timeout);
string data = ReadUntilString(new byte[] {0xFC}, timeout);
SymCommand cmd = SymCommand.Parse(data.Substring(0, data.Length-1));
if((cmd.command == "MsgDlg") && (cmd.HasParam("Text")))
if(cmd.parameters["Text"].IndexOf("From PID") != -1)
cmd = ReadCommand(timeout);
return cmd;
}
//------------------------------------------------------------------------
private SymCommand ReadCommand(){ return ReadCommand(defaultTimeout); }
//========================================================================
private byte[] ReadUntilOr(byte[] matchA, byte[] matchB, int timeout)
{
SockLock(5000);
int oto = stream.ReadTimeout;
stream.ReadTimeout = timeout;
DateTime begin = DateTime.Now;
bool hit = false;
byte[] testA = new byte[matchA.Length];
byte[] testB = new byte[matchB.Length];
List<byte> buff = new List<byte>();
while(!hit)
{
if((DateTime.Now - begin).Milliseconds > timeout)
{
SockUnlock();
Disconnect();
throw new Exception("Socket Read Timeout");
}
for(int i=0; i<(testA.Length-1); i++) testA[i]=testA[i+1];
for(int j=0; j<(testB.Length-1); j++) testB[j]=testB[j+1];
stream.Read(testA,testA.Length-1,1);
testB[testB.Length-1] = testA[testA.Length-1];
buff.Add(testA[testA.Length-1]);
hit = true;
for(int c=0; c<testA.Length; c++)
{
if(testA[c] != matchA[c])
{
hit=false;
break;
}
}
if(hit == false)
{
hit=true;
for(int d=0; d<testB.Length; d++)
{
if(testB[d] != matchB[d])
{
hit=false;
break;
}
}
}
}
stream.ReadTimeout = oto;
SockUnlock();
lastActive = DateTime.Now;
if(debug != null)
{
byte[] dbg = buff.ToArray();
debug("READ: ");
for(int i=0; i<dbg.Length; i++) debug(dbg[i].ToString("X2")+", ");
debug("\n");
}
return buff.ToArray();
}
//------------------------------------------------------------------------
private byte[] ReadUntilOr(string matchA, string matchB, int timeout) { return ReadUntilOr(EncodeString(matchA),EncodeString(matchB),defaultTimeout); }
private string ReadUntilOrString(string matchA, string matchB, int timeout){ return DecodeString(ReadUntilOr(matchA, matchB, timeout)); }
//========================================================================
public string DecodeString(byte[] byt)
{
string ret="";
for(int i=0; i<byt.Length; i++)
ret += (char)byt[i];
return ret;
}
//------------------------------------------------------------------------
public byte[] EncodeString(string str)
{
byte[] ret = new byte[str.Length];
for(int i=0; i<str.Length; i++)
ret[i] = (byte)(str[i] & 0x00FF);
return ret;
}
//========================================================================
private void KeepAliveStart()
{
if(!isConnected) return;
if(keepAlive != null) KeepAliveStop();
isKeepAlive = true;
keepAlive = new Thread(KeepAlive);
try
{
keepAlive.Start();
}
catch(Exception ex)
{
isKeepAlive = false;
throw new Exception("Error Starting Keep-Alive Thread\n"+ex.Message);
}
}
//------------------------------------------------------------------------
private void KeepAliveStop()
{
if(keepAlive == null) return;
/***** this was seriously slowing down application exit, waiting 2s per connection ****
//attempt to let thread die naturally
isKeepAlive = false;
Thread.Sleep(2000);
*/
//now force thread to terminate
try
{
keepAlive.Abort();
}
catch(Exception)
{
//thread died on it's own
}
keepAlive = null;
}
//------------------------------------------------------------------------
private void KeepAlive()
{
int oto = stream.WriteTimeout;
byte[] wakeCmd = EncodeString((new SymCommand("WakeUp")).Packet());
try
{
while(isConnected && isKeepAlive)
{
if((DateTime.Now - lastActive).TotalMilliseconds >= keepAliveInterval)
{
try
{
SockLock(5000);
try
{
stream.WriteTimeout = 1000;
stream.Write(wakeCmd, 0, wakeCmd.Length);
stream.WriteTimeout = oto;
SockUnlock();
lastActive = DateTime.Now;
}
catch(Exception)
{
Disconnect();
}
}
catch(Exception)
{
//failed in SockLock, must be in use.
//no need to complain
}
}
Thread.Sleep(1000);
}
}
catch(ThreadAbortException)
{
//we were forcefully killed
}
}
//========================================================================
public bool Connect(string Server, int Port)
{
error = "";
server = Server;
try
{
SockLock(5000);
socket = new TcpClient();
socket.Connect(IPAddress.Parse(server), Port);
stream = socket.GetStream();
}
catch(Exception ex)
{
if(inUse) SockUnlock();
error = "Unable to Connect to Server\n"+ex.Message;
return false;
}
isConnected = true;
lastActive = DateTime.Now;
SockUnlock();
return true;
}
//========================================================================
public void Disconnect()
{
KeepAliveStop();
try { stream.Close(); } catch (Exception) { }
try { socket.Close(); } catch (Exception) { }
stream = null; socket = null;
isConnected = isLoggedIn = inUse = false;
}
//========================================================================
public bool AIXTest(string usr, string pwd)
{
error = "";
if(!isConnected)
{
error = "Not Connected to Host";
return false;
}
//Telnet Handshake
try
{
Write(new byte[] {0xFF, 0xFB, 0x18} , 1000);
Write(new byte[] {0xFF, 0xFA, 0x18, 0x00, 0x61, 0x69, 0x78, 0x74, 0x65, 0x72, 0x6D, 0xFF, 0xF0}, 1000);
Write(new byte[] {0xFF, 0xFD, 0x01} , 1000);
Write(new byte[] {0xFF, 0xFD, 0x03, 0xFF, 0xFC, 0x1F, 0xFF, 0xFC, 0x01} , 1000);
}
catch(Exception ex)
{
error = "Error During Telnet Handshake\n"+ex.Message;
return false;
}
//AIX Login
try
{
string stat;
Write(usr+'\r', 1000);
stat = ReadUntilOrString("Password:", "[c", 1000);
if(stat.IndexOf("[c") == -1)
{
if(stat.IndexOf("invalid login") != -1)
{
error = "Invalid AIX Login";
return false;
}
Write(pwd+'\r', 1000);
stat = ReadUntilString(":", 1000);
if(stat.IndexOf("invalid login") != -1)
{
error = "Invalid AIX Login";
return false;
}
stat = ReadUntilString("[c", 1000);
}
}
catch(Exception ex)
{
error = "Error During AIX Login\n"+ex.Message;
return false;
}
return true;
}
//========================================================================
public bool Login(string usr, string pwd, string symdir, string symid)
{
return Login(usr, pwd, symdir, symid, 0);
}
public bool Login(string usr, string pwd, string symdir, string symid, int stage)
{
error = "";
sym = symdir;
SymCommand cmd;
if(!isConnected)
{
error = "Not Connected to Host";
return false;
}
if(stage <1)
{
//Telnet Handshake
try
{
Write(new byte[] {0xFF, 0xFB, 0x18} , 1000);
Write(new byte[] {0xFF, 0xFA, 0x18, 0x00, 0x61, 0x69, 0x78, 0x74, 0x65, 0x72, 0x6D, 0xFF, 0xF0}, 1000);
Write(new byte[] {0xFF, 0xFD, 0x01} , 1000);
Write(new byte[] {0xFF, 0xFD, 0x03, 0xFF, 0xFC, 0x1F, 0xFF, 0xFC, 0x01} , 1000);
}
catch(Exception ex)
{
error = "Error During Telnet Handshake\n"+ex.Message;
return false;
}
}
if(stage <2)
{
//AIX Login
try
{
string stat;
Write(usr+'\r', 1000);
stat = ReadUntilOrString("Password:", "[c", 1000);
if(stat.IndexOf("[c") == -1)
{
if(stat.IndexOf("invalid login") != -1)
{
error = "Invalid AIX Login";
return false;
}
Write(pwd+'\r', 1000);
stat = ReadUntilString(":", 1000);
if(stat.IndexOf("invalid login") != -1)
{
error = "Invalid AIX Login";
return false;
}
stat = ReadUntilString("[c", 1000);
}
}
catch(Exception ex)
{
error = "Error During AIX Login\n"+ex.Message;
return false;
}
}
//Symitar Login
try
{
if(stage <2)
{
Write("WINDOWSLEVEL=3\n", 1000);
ReadUntil("$ ", 1000);
Write("sym "+sym+'\r', 1000);
cmd = ReadCommand(2000);
while(cmd.command != "Input")
{
if(cmd.command == "SymLogonError")
if(cmd.GetParam("Text").IndexOf("Too Many Invalid Password Attempts") > -1)
{
error = "Too Many Invalid Password Attempts";
return false;
}
cmd = ReadCommand(2000);
if((cmd.command == "Input") && (cmd.GetParam("HelpCode") == "10025"))
{
Write("$WinHostSync$\r");
cmd = ReadCommand(2000);
}
}
}
Write(symid+'\r', 1000);
cmd = ReadCommand(2000);
if(cmd.command == "SymLogonInvalidUser")
{
error = "Invalid Sym User";
Write("\r", 1000); ReadCommand(2000);
return false;
}
if(cmd.command == "SymLogonError")
if(cmd.GetParam("Text").IndexOf("Too Many Invalid Password Attempts") > -1)
{
error = "Too Many Invalid Password Attempts";
return false;
}
Write("\r", 1000); ReadCommand(2000);
Write("\r", 1000); ReadCommand(2000);
}
catch(Exception ex)
{
error = "Error During Sym Login\n"+ex.Message;
return false;
}
//Keep-Alive Thread
try
{
KeepAliveStart();
}
catch(Exception ex)
{
error = ex.Message;
return false;
}
isLoggedIn = true;
return true;
}
//========================================================================
public List<SymFile> FileList(string pattern, SymFile.Type type)
{
List<SymFile> files = new List<SymFile>();
SymCommand cmd = new SymCommand("File");
cmd.SetParam("Type" , SymFile.TypeDescriptor[(int)type]);
cmd.SetParam("Name" , pattern);
cmd.SetParam("Action", "List");
Write(cmd);
while(true)
{
cmd = ReadCommand(2000);
if(cmd.HasParam("Status"))
break;
if(cmd.HasParam("Name"))
files.Add(new SymFile(server, sym, cmd.GetParam("Name"), cmd.GetParam("Date"), cmd.GetParam("Time"), int.Parse(cmd.GetParam("Size")), type));
if(cmd.HasParam("Done"))
break;
}
return files;
}
//========================================================================
public bool FileExists(SymFile file)
{
return (FileList(file.name, file.type).Count > 0);
}
//------------------------------------------------------------------------
public bool FileExists(string filename, SymFile.Type type)
{
return (FileList(filename, type).Count > 0);
}
//========================================================================
public SymFile FileGet(string filename, SymFile.Type type)
{
List<SymFile> files = FileList(filename, type);
if(files.Count < 1)
throw new FileNotFoundException("File \""+filename+"\" Not Found");
return files[0];
}
//========================================================================
public void FileRename(string oldName, SymFile.Type type, string newName)
{
SymCommand cmd = new SymCommand("File");
cmd.SetParam("Action" , "Rename");
cmd.SetParam("Type" , SymFile.TypeString(type));
cmd.SetParam("Name" , oldName);
cmd.SetParam("NewName", newName);
Write(cmd);
cmd = ReadCommand(2000);
if(cmd.HasParam("Status"))
{
if(cmd.GetParam("Status").IndexOf("No such file or directory") != -1)
throw new FileNotFoundException("File \""+oldName+"\" Not Found");
else
throw new Exception("Filename Too Long");
}
else if(cmd.HasParam("Done"))
return;
else
throw new Exception("Unknown Renaming Error");
}
//------------------------------------------------------------------------
public void FileRename(SymFile file, string newName) { FileRename(file.name, file.type, newName); }
//========================================================================
public void FileDelete(string name, SymFile.Type type)
{
SymCommand cmd = new SymCommand("File");
cmd.SetParam("Action", "Delete");
cmd.SetParam("Type" , SymFile.TypeString(type));
cmd.SetParam("Name" , name);
Write(cmd);
cmd = ReadCommand(2000);
if(cmd.HasParam("Status"))
{
if(cmd.GetParam("Status").IndexOf("No such file or directory") != -1)
throw new FileNotFoundException("File \""+name+"\" Not Found");
else
throw new Exception("Filename Too Long");
}
else if(cmd.HasParam("Done"))
return;
else
throw new Exception("Unknown Deletion Error");
}
//------------------------------------------------------------------------
public void FileDelete(SymFile file) { FileDelete(file.name, file.type); }
//========================================================================
public string FileRead(string name, SymFile.Type type)
{
StringBuilder content = new StringBuilder();
SymCommand cmd = new SymCommand("File");
cmd.SetParam("Action", "Retrieve");
cmd.SetParam("Type" , SymFile.TypeString(type));
cmd.SetParam("Name" , name);
Write(cmd);
while(true)
{
cmd = ReadCommand();
if(cmd.HasParam("Status"))
{
if(cmd.GetParam("Status").IndexOf("No such file or directory") != -1)
throw new FileNotFoundException("File \""+name+"\" Not Found");
else if(cmd.GetParam("Status").IndexOf("Cannot view a blank report") != -1)
return "";
else
throw new Exception("Filename Too Long");
}
string chunk = cmd.GetFileData();
if((chunk.Length > 0) || (type == SymFile.Type.REPORT))
{
content.Append(chunk);
if(type==SymFile.Type.REPORT)
content.Append('\n');
}
if(cmd.HasParam("Done"))
break;
}
return content.ToString();
}
//------------------------------------------------------------------------
public string FileRead(SymFile file) { return FileRead(file.name, file.type); }
//========================================================================
public void FileWrite(string name, SymFile.Type type, string content)
{
int chunkMax = 1024;
SymCommand cmd = new SymCommand("File");
cmd.SetParam("Action", "Store");
cmd.SetParam("Type" , SymFile.TypeString(type));
cmd.SetParam("Name" , name);
WakeUp();
Write(cmd);
cmd = ReadCommand();
int wtf_is_this = 0;
while(cmd.data.IndexOf("BadCharList") == -1)
{
cmd = ReadCommand();
wtf_is_this++;
if(wtf_is_this > 5)
throw new Exception("Null Pointer");
}
if(cmd.data.IndexOf("MaxBuff") > -1)
chunkMax = int.Parse(cmd.GetParam("MaxBuff"));
if(content.Length > (999*chunkMax))
throw new Exception("File Too Large");
if(cmd.GetParam("Status").IndexOf("Filename is too long") != -1)
throw new Exception("Filename Too Long");
string[] badChars = cmd.GetParam("BadCharList").Split(new char[] { ',' });
for(int i=0; i<badChars.Length; i++)
content = content.Replace(((char)int.Parse(badChars[i]))+"", "");
int sent=0, block=0; string blockStr; byte[] resp;
while(sent < content.Length)
{
int chunkSize = (content.Length - sent);
if(chunkSize > chunkMax)
chunkSize = chunkMax;
string chunk = content.Substring(sent, chunkSize);
string chunkStr = chunkSize.ToString("D5");
blockStr = block.ToString("D3");
resp = new byte[]{0x4E,0x4E,0x4E,0x4E,0x4E,0x4E,0x4E,0x4E,0x4E,0x4E,0x4E};
while(resp[7] == 0x4E)
{
Write("PROT"+blockStr+"DATA"+chunkStr);
Write(chunk);
resp = Read(16);
}
block++;
sent += chunkSize;
}
blockStr = block.ToString("D3");
Write("PROT"+blockStr+"EOF\u0020\u0020\u0020\u0020\u0020\u0020");
resp = Read(16);
cmd = ReadCommand();
WakeUp();
}
//------------------------------------------------------------------------
public void FileWrite(SymFile file, string content) { FileWrite(file.name, file.type, content); }
//========================================================================
public RepErr FileCheck(SymFile file)
{
if(file.type != SymFile.Type.REPGEN)
throw new Exception("Cannot Check a "+file.TypeString()+" File");
Write("mm3\u001B"); ReadCommand();
Write("7\r"); ReadCommand(); ReadCommand();
Write(file.name+'\r');
SymCommand cmd = ReadCommand();
if(cmd.HasParam("Warning") || cmd.HasParam("Error"))
{
ReadCommand();
throw new Exception("File \""+file.name+"\" Not Found");
}
if(cmd.GetParam("Action")=="NoError")
{
ReadCommand();
return RepErr.None();
}
int errRow=0, errCol=0;
string errFile="", errText="";
if(cmd.GetParam("Action")=="Init")
{
errFile = cmd.GetParam("FileName");
cmd = ReadCommand();
while(cmd.GetParam("Action")!="DisplayEdit")
{
if(cmd.GetParam("Action")=="FileInfo")
{
errRow = int.Parse(cmd.GetParam("Line").Replace(",", ""));
errCol = int.Parse(cmd.GetParam("Col" ).Replace(",", ""));
}
else if(cmd.GetParam("Action")=="ErrText")
errText += cmd.GetParam("Line")+" ";
cmd = ReadCommand();
}
ReadCommand();
return new RepErr(file, errFile, errText, errRow, errCol);
}
throw new Exception("Unknown Checking Error");
}
//========================================================================
public RepErr FileInstall(SymFile file)
{
if(file.type != SymFile.Type.REPGEN)
throw new Exception("Cannot Install a "+file.TypeString()+" File");
Write("mm3\u001B"); ReadCommand();
Write("8\r"); ReadCommand(); ReadCommand();
Write(file.name+'\r');
SymCommand cmd = ReadCommand();
if(cmd.HasParam("Warning") || cmd.HasParam("Error"))
{
ReadCommand();
throw new Exception("File \""+file.name+"\" Not Found");
}
if(cmd.command=="SpecfileData")
{
ReadCommand();
Write("1\r");
ReadCommand(); ReadCommand();
return RepErr.None(int.Parse(cmd.GetParam("Size").Replace(",","")));
}
int errRow = 0, errCol = 0;
string errFile = "", errText = "";
if (cmd.GetParam("Action") == "Init")
{
errFile = cmd.GetParam("FileName");
cmd = ReadCommand();
while (cmd.GetParam("Action") != "DisplayEdit")
{
if (cmd.GetParam("Action") == "FileInfo")
{
errRow = int.Parse(cmd.GetParam("Line").Replace(",", ""));
errCol = int.Parse(cmd.GetParam("Col").Replace(",", ""));
}
else if (cmd.GetParam("Action") == "ErrText")
errText += cmd.GetParam("Line") + " ";
cmd = ReadCommand();
}
ReadCommand();
return new RepErr(file, errFile, errText, errRow, errCol);
}
throw new Exception("Unknown Install Error");
}
//========================================================================
// Report Running Stuff
//========================================================================
public delegate void FileRun_Status(int code, string description);
public delegate string FileRun_Prompt(string prompt);
//------------------------------------------------------------------------
public bool IsFileRunning(int sequence)
{
SymCommand cmd;
bool running = false;
cmd = new SymCommand("Misc");
cmd.SetParam("InfoType", "BatchQueues");
Write(cmd);
cmd = ReadCommand();
while(!cmd.HasParam("Done"))
{
if((cmd.GetParam("Action")=="QueueEntry") && (int.Parse(cmd.GetParam("Seq")) == sequence))
running = true;
cmd = ReadCommand();
}
return running;
}
//------------------------------------------------------------------------
private List<int> GetPrintSequences(string which)
{
List<int> seqs = new List<int>();
SymCommand cmd;
cmd = new SymCommand("File");
cmd.SetParam("Action" , "List");
cmd.SetParam("MaxCount", "50");
cmd.SetParam("Query" , "LAST 20 \"+"+which+"+\"");
cmd.SetParam("Type" , "Report");
Write(cmd);
cmd = ReadCommand();
while(!cmd.HasParam("Done"))
{
if(cmd.HasParam("Sequence"))
seqs.Add(int.Parse(cmd.GetParam("Sequence")));
cmd = ReadCommand();
}
seqs.Sort();
seqs.Reverse();
return seqs;
}
//------------------------------------------------------------------------
public int GetReportSequence(string repName, int time)
{
List<int> seqs = GetPrintSequences("REPWRITER");
foreach(int i in seqs)
{
SymFile file = new SymFile(server, sym, i.ToString(), DateTime.Now, 0, SymFile.Type.REPORT);
string contents = FileRead(file);
int beganIndex = contents.IndexOf("Processing begun on");
if(beganIndex != -1)
{
contents = contents.Substring(beganIndex+41);
string timeStr = contents.Substring(0, 8);
int currTime = int.Parse(timeStr.Substring(timeStr.LastIndexOf(':')+1));
currTime += 60 * int.Parse(timeStr.Substring(timeStr.IndexOf(':')+1, 2));
currTime += 3600 * int.Parse(timeStr.Substring(0, timeStr.IndexOf(':')));
contents = contents.Substring(contents.IndexOf("(newline when done):") + 21);
string name = contents.Substring(0, contents.IndexOf('\n'));
if(name == repName)
if((time+1==currTime) || (time==currTime) || (time-1==currTime))
return i;
}
}
return -1;
}
//------------------------------------------------------------------------
public RepRunErr FileRun(SymFile file, FileRun_Status callStatus, FileRun_Prompt callPrompt, int queue)
{
if (file.type != SymFile.Type.REPGEN)
throw new Exception("Cannot Run a " + file.TypeString() + " File");
SymCommand cmd;
callStatus(0,"Initializing...");
Write("mm0\u001B");
cmd = ReadCommand();
while(cmd.command != "Input")
cmd = ReadCommand();
callStatus(1,"Writing Commands...");
Write("1\r");
cmd = ReadCommand();
while(cmd.command != "Input")
cmd = ReadCommand();
callStatus(2,"Writing Commands...");
Write("11\r");
cmd = ReadCommand();
while(cmd.command != "Input")
cmd = ReadCommand();
callStatus(3,"Writing Commands...");
Write(file.name + "\r");