-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathModule1.cs
1007 lines (945 loc) · 45 KB
/
Module1.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
//
// GOOMBAServer
//
// Created by: FreeBSoD on GitHub
// Aka GoombaProgrammer
// ----
//
//
// Thanks for viewing!
//
#define BUILDTYPE_WINDOWS
using System;
using System.IO;
using System.Text;
using System.Net;
using System.Threading.Tasks;
using System.Collections.Generic;
using MySql.Data.MySqlClient;
using System.Xml;
using System.Collections.Specialized;
using System.CodeDom.Compiler;
using System.CodeDom;
using System.Linq;
namespace GOOMBAServer
{
public static class ExtensionClass
{
// Extension method to append the element
public static T[] Append<T>(this T[] array, T item)
{
List<T> list = new List<T>(array);
list.Add(item);
return list.ToArray();
}
}
class Module1
{
// Vars
public static XmlDocument doc = new XmlDocument();
public static string version = "0.2";
public static HttpListener listener;
public static string url = "";
public static int requestCount = 0;
public static string pageData =
"<h1 style=\"font-family: verdana;\">GOOMBAServer Error</h1><h3 style=\"font-family: verdana;\">HTTP 500 Server Error <small>(GoombaErr #2)</small></h3><br /><br /><p style=\"font-family: verdana;\">GOOMBAServer v0.2</p>";
public static MySqlConnection connection;
public static string server;
public static string database;
public static string uid;
public static List<string> vs1 = new List<string>();
public static string password;
public static string[] StringSplit(string StringToSplit, string Delimitator)
{
return StringToSplit.Split(new[] { Delimitator }, StringSplitOptions.None);
}
//Initialize values
public static void InitializeSQL(string host, string db, string user, string pass)
{
server = host;
database = db;
uid = user;
password = pass;
string connectionString;
connectionString = "SERVER=" + server + ";" + "DATABASE=" +
database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";";
connection = new MySqlConnection(connectionString);
}
public static void ErrorHandler(string error)
{
pageData = error;
#if BUILDTYPE_WINDOWS
File.AppendAllText(Environment.CurrentDirectory + @"\www\goomba_errors", error);
#else
File.AppendAllText(Environment.CurrentDirectory + @"/www/goomba_errors", error);
#endif
}
//open connection to database
private static bool OpenConnection()
{
try
{
connection.Open();
return true;
}
catch (MySqlException ex)
{
switch (ex.Number)
{
case 0:
Console.WriteLine("SQL #0 Says: Cannot connect to server.");
ErrorHandler("SQL #0 Says: Cannot connect to server.");
break;
case 1045:
Console.WriteLine("SQL #1045 Says: Invalid user name and/or password.");
ErrorHandler("SQL #1045 Says: Incorrect username/password");
break;
}
Console.WriteLine(ex.Message);
ErrorHandler(ex.Message);
return false;
}
}
//Close connection
public static bool CloseConnection()
{
try
{
connection.Close();
return true;
}
catch
{
return false;
}
}
//Query
public static string[] Query(string cmad, bool useSafeStr)
{
Console.WriteLine("Query Started.");
string query = cmad;
if (query.ToUpper().StartsWith("SELECT"))
{
Console.WriteLine("ooOOH, a select query!");
//Open connection
if (OpenConnection() == true)
{
string[] forSelect = query.Split(' ');
Console.WriteLine("Open");
//create mysql command
MySqlCommand cmd = new MySqlCommand();
//Assign the query using CommandText
cmd.CommandText = query;
//Assign the connection using Connection
cmd.Connection = connection;
//Execute query
var ret = cmd.ExecuteReader();
string[] vs = { };
var i = -1;
vs1.Clear();
if (forSelect[1] != "*")
{
while (ret.Read())
{
i++;
vs1.Add(ret[forSelect[1]].ToString());
}
}
else
{
while (ret.Read())
{
for (var f = 0; f < ret.FieldCount; f++)
{
i++;
vs1.Add(ret[ret.GetName(f)].ToString());
}
}
}
ret.Close();
Console.WriteLine("Done");
connection.Close();
return vs;
}
}
else
{
//Open connection
if (OpenConnection() == true)
{
Console.WriteLine("Open");
//create mysql command
MySqlCommand cmd = new MySqlCommand();
//Assign the query using CommandText
cmd.CommandText = query;
//Assign the connection using Connection
cmd.Connection = connection;
//Execute query
cmd.ExecuteNonQuery();
Console.WriteLine("Done");
string[] strtoret = { };
connection.Close();
return strtoret;
}
string[] strtorets = { };
return strtorets;
}
string[] strtoretss = { };
return strtoretss;
}
public static void print(object text)
{
Console.WriteLine(text);
}
public static string Escape(string input)
{
using (var writer = new StringWriter())
{
using (var provider = CodeDomProvider.CreateProvider("CSharp"))
{
provider.GenerateCodeFromExpression(new CodePrimitiveExpression(input), writer, null);
return writer.ToString();
}
}
}
public static bool IsNumeric(string value)
{
return value.All(char.IsNumber);
}
public static string SafeINSERT(string INTO, NameValueCollection VALUES)
{
print(VALUES.Count);
string queryString = "INSERT INTO " + INTO + " (";
for (int i = 0; i < VALUES.Count; i++)
{
if(i + 1 == VALUES.Count)
{
queryString += VALUES.Keys[i];
}
else
{
queryString += VALUES.Keys[i] + ",";
}
}
queryString += ") VALUES (";
for (int i = 0; i < VALUES.Count; i++)
{
queryString += "@a" + i + (i + 1 == VALUES.Count ? (")") : (","));
}
Console.WriteLine(queryString);
//create mysql command
MySqlCommand cmd = new MySqlCommand();
//Assign the query using CommandText
cmd.CommandText = queryString;
for (int i = 0; i < VALUES.Count; i++)
{
if(!IsNumeric(VALUES[i]))
{
print("oops");
cmd.Parameters.Add("@a" + Convert.ToString(i), MySqlDbType.String);
cmd.Parameters["@a" + Convert.ToString(i)].Value = VALUES[i];
continue;
}
cmd.Parameters.Add("@a" + Convert.ToString(i), MySqlDbType.Int32);
cmd.Parameters["@a" + Convert.ToString(i)].Value = VALUES[i];
print("INT");
}
//Assign the connection using Connection
cmd.Connection = connection;
//Execute query
cmd.ExecuteNonQuery();
Console.WriteLine("Done");
queryString += ");";
return queryString;
}
public static string[] QuerySAFE(string cmad, bool useSafeStr)
{
Console.WriteLine("Query Started.");
string query = cmad;
if (query.ToUpper().StartsWith("SELECT"))
{
Console.WriteLine("ooOOH, a select query!");
//Open connection
if (OpenConnection() == true)
{
string[] forSelect = query.Split(' ');
Console.WriteLine("Open");
//create mysql command
MySqlCommand cmd = new MySqlCommand();
//Assign the query using CommandText
cmd.CommandText = query;
//Assign the connection using Connection
cmd.Connection = connection;
//Execute query
var ret = cmd.ExecuteReader();
string[] vs = { };
var i = -1;
vs1.Clear();
if (forSelect[1] != "*")
{
while (ret.Read())
{
i++;
vs1.Add(ret[forSelect[1]].ToString().Replace("<", "").Replace("<", "").Replace("%3c", "").Replace(">", "").Replace(">", "").Replace("%3e", ""));
}
}
else
{
while (ret.Read())
{
for (var f = 0; f < ret.FieldCount; f++)
{
i++;
vs1.Add(ret[ret.GetName(f)].ToString().Replace("<", "").Replace("<", "").Replace("%3c", "").Replace(">", "").Replace(">", "").Replace("%3e", ""));
}
}
}
ret.Close();
Console.WriteLine("Done");
connection.Close();
return vs;
}
}
else if(query.ToUpper().StartsWith("INSERT"))
{
Console.WriteLine("Insert");
//Open connection
if (OpenConnection() == true)
{
Console.WriteLine("Open");
NameValueCollection coolValueCollection0 = new NameValueCollection();
NameValueCollection coolValueCollection1 = new NameValueCollection();
var donextpart0 = false;
var donextpart = false;
var q1 = query.Split(' ');
Console.WriteLine("Done with " + q1.Length);
var i = -69;
for (i = 0; i < q1.Length; i++)
{
if (q1[i].StartsWith("(") && i < 4)
{
donextpart0 = true;
donextpart = false;
continue;
}
if (donextpart0) // DO IT JUST, DO IT!!!!
{
i--;
coolValueCollection0.Add(q1[i].Replace("(", "").Replace(")", ""), null);
i++;
continue;
}
}
var y = i - 1;
donextpart = true;
for (y = i - 2; y < q1.Length - 1; y++)
{
print("ran");
if(donextpart) // DO IT, JUST, DO IT!!!
{
print(q1.Length - i);
coolValueCollection1.Add(coolValueCollection0.Keys[q1.Length - i], q1[y + 1].Replace("(", "").Replace(")", "").Replace("'", "").Replace("`", "").Replace(";", ""));
}
}
print(coolValueCollection1.Count);
SafeINSERT(query.Split(' ')[2], coolValueCollection1);
string[] strtoret = { };
connection.Close();
return strtoret;
}
string[] strtorets = { };
return strtorets;
}
string[] strtoretss = { };
return strtoretss;
}
public static void runGoombaCode(HttpListenerRequest req, string file = "", bool isFunc = false)
{
List<List<string>> arrays = new List<List<string>>();
List<string> arrays2 = new List<string>();
List<string> strVars = new List<string>();
List<string> strVars2 = new List<string>();
List<string> functions = new List<string>();
List<int> functionLengths = new List<int>();
List<string> functionCodes = new List<string>();
var isIndex = false;
if (req.Url.AbsolutePath == "" || req.Url.AbsolutePath == "/")
{
isIndex = true;
}
var i = 0;
var goombaMode = false;
if (!isFunc)
{
pageData = "";
}
StreamReader streamReader = new StreamReader(file);
var toread = streamReader.ReadToEnd();
streamReader.Close();
if (isIndex == true)
{
#if BUILDTYPE_WINDOWS
streamReader = new StreamReader(Environment.CurrentDirectory + @"\www\" + "index.goomba");
#else
streamReader = new StreamReader(Environment.CurrentDirectory + @"/www/" + "index.goomba");
#endif
toread = streamReader.ReadToEnd();
streamReader.Close();
}
using (var reader = new StringReader(toread))
{
for (string line = reader.ReadLine(); line != null; line = reader.ReadLine())
{
var commandline = line.Split(' ');
i += 1;
if (goombaMode)
{
if (line == "->STOPITGOOMBA<-")
{
if (goombaMode == true)
{
goombaMode = false;
}
else
{
pageData = "Goomba says: Unexpected STOPITGOOMBA at line " + i + ". Code will not continue.";
ErrorHandler("\nGoomba says: Unexpected STOPITGOOMBA at line " + i + ". Code will not continue.");
return;
}
}
else if (line == "->GOOMBATIME<-")
{
if (goombaMode == false)
{
goombaMode = true;
}
else
{
pageData = "Goomba says: Unexpected GOOMBATIME at line " + i + ". Code will not continue.";
ErrorHandler("Goomba says: Unexpected GOOMBATIME at line " + i + ". Code will not continue.");
return;
}
}
// First command ever added to GOOMBA!
else if (commandline[0] == "SPELLWORD:")
{
pageData += commandline[1] + " ";
}
// Comments but think??? what???
else if (line.StartsWith("THINK"))
{
}
// Array things.
else if (line.StartsWith("ARRAYADD:"))
{
if (commandline.Length != 2)
{
ErrorHandler("\nGoomba says: Expecting 1 argument after ARRAYADD at line " + i + ". Code will not continue.");
return;
}
arrays.Add(vs1);
arrays2.Add(commandline[1]);
}
else if (line.StartsWith("SPELLARRAY:"))
{
if (commandline.Length != 3)
{
ErrorHandler("\nGoomba says: Expecting 2 arguments after SPELLARRAY at line " + i + ". Code will not continue.");
return;
}
if (arrays2.Contains(commandline[1]))
{
pageData += arrays[arrays2.IndexOf(commandline[1])][Convert.ToInt32(commandline[2])];
}
}
// Function support
else if (line.StartsWith("FUNCTION:"))
{
if (!isFunc)
{
if (commandline.Length < 3)
{
ErrorHandler("\nGoomba says: Expecting 2 or more arguments after FUNCTION at line " + i + ". Code will not continue.");
pageData = "Goomba says: Expecting 2 or more arguments after FUNCTION at line " + i + ". Code will not continue.";
return;
}
functions.Add(commandline[1]);
var txt = "";
for (int j = 2, loopTo = commandline.Length - 1; j <= loopTo; j++)
txt += commandline[j] + " ";
var c = StringSplit(txt, ";;");
functionLengths.Add(c.Length);
functionCodes.AddRange(c);
}
else
{
ErrorHandler("\nGoomba says: A function in a function at function code " + i + ".");
}
}
// Function support Pt.2 (RUNFUN runs a function, while running with fun!)
else if (line.StartsWith("RUNFUN:"))
{
if (!isFunc)
{
if (commandline.Length != 2)
{
ErrorHandler("\nGoomba says: Expecting 1 or more argument after RUNFUN at line " + i + ". Code will not continue.");
pageData = "Goomba says: Expecting 1 argument after RUNFUN at line " + i + ". Code will not continue.";
return;
}
if (functions.Contains(commandline[1]))
{
int ok = functions.FindIndex(commandline[1].StartsWith);
int len = functionLengths[ok];
string codes = "";
foreach (string s in functionCodes)
{
if (functionCodes.FindIndex(s.StartsWith) < len)
{
if (functionCodes.FindIndex(s.StartsWith) != len - 1) { codes += s + "\n"; } else { codes += s; }
}
}
int rn = new Random().Next(1, 999999999);
#if BUILDTYPE_WINDOWS
File.WriteAllText(Environment.CurrentDirectory + @"\www\" + rn + ".goomba", "->GOOMBATIME<-\n" + codes + "->STOPITGOOMBA<-");
runGoombaCode(req, Environment.CurrentDirectory + @"\www\" + rn + ".goomba", true);
#else
File.WriteAllText(Environment.CurrentDirectory + @"/www/" + rn + ".goomba", "->GOOMBATIME<-\n" + codes + "->STOPITGOOMBA<-");
runGoombaCode(req, Environment.CurrentDirectory + @"/www/" + rn + ".goomba", true);
#endif
}
else
{
ErrorHandler("Goomba says: RUNFUN could not find function " + commandline[1] + " at line " + i + ". Code will not continue.");
pageData = "Goomba says: RUNFUN could not find function " + commandline[1] + " at line " + i + ". Code will not continue.";
return;
}
}
else
{
ErrorHandler("\nGoomba says: A function in a function at function code " + i + ".");
}
}
else if (commandline[0] == "STR:")
{
var txt = "";
for (int j = 2, loopTo = commandline.Length - 1; j <= loopTo; j++)
txt += commandline[j] + " ";
strVars.Add(commandline[1]);
strVars2.Add(txt);
}
// Create variable
else if (commandline[0] == "CREATEHTMLVAR:")
{
if (commandline.Length != 3)
{
ErrorHandler("\nGoomba says: Expecting 2 arguments after CREATEHTMLVAR at line " + i + ". Code will not continue.");
pageData = "Goomba says: Expecting 2 arguments after CREATEHTMLVAR at line " + i + ". Code will not continue.";
return;
}
else
{
var rng = new Random().Next(65535);
if (rng == 69)
{
rng = 69420;
}
#if BUILDTYPE_WINDOWS
string theName = Environment.CurrentDirectory + @"\www\" + "AksTEMPMethod" + rng + ".xml";
#else
string theName = Environment.CurrentDirectory + @"/www/" + "AksTEMPMethod" + rng + ".xml";
#endif
File.WriteAllText(theName, pageData);
try
{
doc.Load(theName);
XmlElement elem = doc.GetElementById(commandline[2]);
if (elem != null)
{
strVars.Add(commandline[1]);
strVars2.Add(elem.InnerText);
File.Delete(theName);
}
else
{
ErrorHandler("\nGoomba says: Expecting valid ID at line " + i + ". Code will not continue.");
pageData = "Goomba says: Expecting valid ID at line " + i + ". Code will not continue.";
return;
}
}
catch
{
ErrorHandler("\nGoomba says: Unexpected error at line " + i + ". Code will not continue.");
pageData = "Goomba says: Unexpected error at line " + i + ". Code will not continue.";
File.Delete(theName);
return;
}
}
}
// Get variable and print it
else if (commandline[0] == "SPELLVAR:")
{
if (strVars.Contains(commandline[1]))
{
int ok = strVars.FindIndex(commandline[1].StartsWith);
pageData += strVars2[ok];
}
else
{
ErrorHandler("\nGoomba says: Undefined variable " + commandline[1] + " at line " + i + ". Code will not continue.");
pageData = "Goomba says: Undefined variable " + commandline[1] + " at line " + i + ". Code will not continue.";
return;
}
}
// Write a file.
else if (commandline[0] == "WRITEPAPER:")
{
if (commandline.Length == 1)
{
ErrorHandler("\nGoomba says: Expecting 2 arguments after WRITEPAPER at line " + i + ". Code will not continue.");
pageData = "Goomba says: Expecting 2 arguments after WRITEPAPER at line " + i + ". Code will not continue.";
return;
}
if (commandline.Length == 2)
{
ErrorHandler("\nGoomba says: Expecting 2 arguments after WRITEPAPER at line " + i + ". Code will not continue.");
pageData = "Goomba says: Expecting 2 arguments after WRITEPAPER at line " + i + ". Code will not continue.";
return;
}
if (commandline.Length > 1000)
{
ErrorHandler("Goomba says: WRITEPAPER does not accept " + commandline.Length + " arguments at line " + i + ". Code will not continue.");
pageData = "Goomba says: WRITEPAPER does not accept " + commandline.Length + " arguments at line " + i + ". Code will not continue.";
return;
}
var txt = "";
for (int j = 2, loopTo = commandline.Length - 1; j <= loopTo; j++)
txt += commandline[j] + " ";
#if BUILDTYPE_WINDOWS
File.WriteAllText((Environment.CurrentDirectory + @"\www\" + commandline[1]).Replace("..", ""), txt.Replace(@"\n", "\n"));
#else
File.WriteAllText((Environment.CurrentDirectory + @"/www/" + commandline[1]).Replace("..", ""), txt.Replace(@"\n", "\n"));
#endif
}
// SPELLSENTENCE is the same as SPELLWORD, but it allows spaces.
else if (commandline[0] == "SPELLSENTENCE:")
{
if (commandline.Length == 1)
{
ErrorHandler("\nGoomba says: Expecting 1 argument after SPELLSENTENCE at line " + i + ". Code will not continue.");
pageData = "Goomba says: Expecting 1 argument after SPELLSENTENCE at line " + i + ". Code will not continue.";
return;
}
if (commandline.Length > 1000)
{
ErrorHandler("\nGoomba says: SPELLSENTENCE does not accept " + commandline.Length + " arguments at line " + i + ".Code will not continue.");
pageData = "Goomba says: SPELLSENTENCE does not accept " + commandline.Length + " arguments at line " + i + ". Code will not continue.";
return;
}
var txt = "";
for (int j = 1, loopTo = commandline.Length - 1; j <= loopTo; j++)
txt += commandline[j] + " ";
pageData += txt.Replace(@"\n", "<br />");
}
else if (commandline[0] == "APPENDTOPAPER:")
{
if (commandline.Length == 1)
{
ErrorHandler("\nGoomba says: Expecting 2 arguments after APPENDTOPAPER at line " + i + ". Code will not continue.");
pageData = "Goomba says: Expecting 2 arguments after APPENDTOPAPER at line " + i + ". Code will not continue.";
return;
}
if (commandline.Length == 2)
{
ErrorHandler("\nGoomba says: Expecting 2 arguments after APPENDTOPAPER at line " + i + ". Code will not continue.");
pageData = "Goomba says: Expecting 2 arguments after APPENDTOPAPER at line " + i + ". Code will not continue.";
return;
}
if (commandline.Length > 1000)
{
ErrorHandler("\nGoomba says: APPENDTOPAPER does not accept " + commandline.Length + " arguments at line " + i + ". Code will not continue.");
pageData = "Goomba says: APPENDTOPAPER does not accept " + commandline.Length + " arguments at line " + i + ". Code will not continue.";
return;
}
var txt = "";
for (int j = 2, loopTo = commandline.Length - 1; j <= loopTo; j++)
txt += commandline[j] + " ";
#if BUILDTYPE_WINDOWS
File.AppendAllText((Environment.CurrentDirectory + @"\www\" + commandline[1]).Replace("..", ""), txt.Replace(@"\n", "\n"));
#else
File.AppendAllText((Environment.CurrentDirectory + @"/www/" + commandline[1]).Replace("..", ""), txt.Replace(@"\n", "\n"));
#endif
}
else if (commandline[0] == "READPAPER:")
{
if (commandline.Length < 2)
{
ErrorHandler("\nGoomba says: Expecting 1 or more arguments after READPAPER, not " + commandline.Length + " at line " + i + ". Code will not continue.");
pageData = "Goomba says: Expecting 1 or more arguments after READPAPER, not " + commandline.Length + " at line " + i + ". Code will not continue.";
return;
}
if (commandline[1] == "USEADVANCEDTEXT:")
{
StreamReader streamReader2 = new StreamReader(Environment.CurrentDirectory + @"\www\" + commandline[2]);
var ok = streamReader2.ReadToEnd();
pageData += ok.Replace(@"\n", "<br />");
return;
}
try
{
#if BUILDTYPE_WINDOWS
StreamReader streamReader1 = new StreamReader(Environment.CurrentDirectory + @"\www\" + commandline[1]);
#else
StreamReader streamReader1 = new StreamReader(Environment.CurrentDirectory + @"/www/" + commandline[1]);
#endif
var txt = streamReader1.ReadToEnd();
pageData += txt;
}
catch
{
ErrorHandler("Goomba says: Undefined error");
}
}
else if (commandline[0] == "CONNECT:")
{
if (commandline.Length != 5)
{
ErrorHandler("\nGoomba says: Expecting 4 arguments after CONNECT, not " + commandline.Length + " at line " + i + ". Code will not continue.");
pageData = "Goomba says: Expecting 4 arguments after CONNECT, not " + commandline.Length + " at line " + i + ". Code will not continue.";
return;
}
InitializeSQL(commandline[1], commandline[2], commandline[3], commandline[4]);
}
else if (commandline[0] == "REMOVE:")
{
if (commandline.Length != 2)
{
ErrorHandler("\nGoomba says: REMOVE accepts 1 argument at line " + i + ". Code will not continue.");
pageData = "Goomba says: REMOVE accepts 1 argument at line " + i + ". Code will not continue.";
}
pageData = pageData.Replace(commandline[1], "");
}
// Query a MySQL query.
else if (commandline[0] == "QUERY:")
{
var useSafeStr = false;
if (commandline[1] == "USESAFESTR:")
{
useSafeStr = true;
}
if (useSafeStr)
{
if (line.Substring(19, 1) == "\"")
{
if (line.LastIndexOf('"') > 18)
{
QuerySAFE(line.Substring(line.IndexOf('"') + 1, line.LastIndexOf('"') - 1 - line.IndexOf('"')), useSafeStr);
}
else
{
ErrorHandler("\nGoomba says: Expecting \" after QUERY, at line " + i + ". Code will not continue.");
pageData = "Goomba says: Expecting \" after QUERY, at line " + i + ". Code will not continue.";
}
}
}
else if (!useSafeStr)
{
if (line.Substring(7, 1) == "\"")
{
if (line.LastIndexOf('"') > 7)
{
Query(line.Substring(line.IndexOf('"') + 1, line.LastIndexOf('"') - 1 - line.IndexOf('"')), useSafeStr);
}
else
{
ErrorHandler("\nGoomba says: Expecting \" after QUERY, at line " + i + ". Code will not continue.");
pageData = "Goomba says: Expecting \" after QUERY, at line " + i + ". Code will not continue.";
}
}
}
else
{
ErrorHandler("\nGoomba says: Expecting \" right after QUERY, at line " + i + ". Code will not continue.");
pageData = "Goomba says: Expecting \" right after QUERY, at line " + i + ". Code will not continue.";
}
}
else
{
ErrorHandler("\nUndefined GOOMBA_THING " + commandline[0] + " at line " + i + ". Code will not continue.");
pageData = "Undefined GOOMBA_THING " + commandline[0] + " at line " + i + ". Code will not continue.";
return;
}
}
else
{
if (line == "->GOOMBATIME<-")
{
// Is GOOMBA running?
if (goombaMode == false)
{
// No!
goombaMode = true;
}
else
{
// yep...
pageData = "Goomba says: Unexpected GOOMBATIME at line " + i + ". Code will not continue.";
return;
}
}
else if (line == "->STOPITGOOMBA<-")
{
if (goombaMode == true)
{
goombaMode = false;
}
else
{
pageData = "Goomba says: Unexpected STOPITGOOMBA at line " + i + ". Code will not continue.";
return;
}
}
else
{
pageData += "\n" + line;
goombaMode = false;
}
}
}
goombaMode = false;
}
}
public static async Task HandleIncomingConnections()
{
bool runServer = true;
// While a user hasn't visited the `shutdown` url, keep on handling requests
while (runServer)
{
// Will wait here until we hear from a connection
HttpListenerContext ctx = await listener.GetContextAsync();
// Peel out the requests and response objects
HttpListenerRequest req = ctx.Request;
HttpListenerResponse resp = ctx.Response;
// Print out some info about the request
Console.WriteLine("Request #: {0}", ++requestCount);
Console.WriteLine(req.Url.ToString());
Console.WriteLine(req.HttpMethod);
Console.WriteLine(req.UserHostName);
Console.WriteLine(req.UserAgent);
Console.WriteLine();
// Find all files to check
#if BUILDTYPE_WINDOWS
List<string> allFiles = GetFilesFromPath(Environment.CurrentDirectory + @"\www");
#else
List<string> allFiles = GetFilesFromPath(Environment.CurrentDirectory + @"/www");
#endif
foreach (string file in allFiles)
{
#if BUILDTYPE_WINDOWS
string thething = "/" + file.Replace(Environment.CurrentDirectory + @"\www\", "").Replace("\\", "/");
#else
string thething = "/" + file.Replace(Environment.CurrentDirectory + @"/www/", "").Replace("\\", "/");
#endif
if (req.Url.AbsolutePath == thething || req.Url.AbsolutePath == "" || req.Url.AbsolutePath == "/")
{
if (req.Url.AbsolutePath.EndsWith(".goomba"))
{
runGoombaCode(req, file);
}
else
{
var isIndex = false;
if (req.Url.AbsolutePath == "" || req.Url.AbsolutePath == "/")
{
isIndex = true;
}
if (isIndex)
{
runGoombaCode(req, file);
}
else
{
StreamReader sr = new StreamReader(file);
pageData = sr.ReadToEnd();
sr.Close();
}
}
goto foundfile;
}
else
{
pageData = "<h1 style=\"font-family: verdana;\">GOOMBAServer Error</h1><h3 style=\"font-family: verdana;\">HTTP 404 Not Found <small>(GoombaErr #1)</small></h3><br /><br /><p style=\"font-family: verdana;\">GOOMBAServer v0.2</p>";
}
}
// File is found
foundfile:
// Write the response info
byte[] data = Encoding.UTF8.GetBytes(pageData);
resp.ContentType = "text/html";
resp.ContentEncoding = Encoding.UTF8;
resp.ContentLength64 = data.LongLength;
// Write out to the response stream (asynchronously), then close it
await resp.OutputStream.WriteAsync(data, 0, data.Length);
resp.Close();
}
}
static List<string> GetFilesFromPath(string path)
{
List<string> files = new List<string>();
try
{
files.AddRange(Directory.GetFiles(path));
foreach (string dir in Directory.GetDirectories(path))
{
files.AddRange(GetFilesFromPath(dir));
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
return files;
}
public static void Main(string[] args)
{
#if BUILDTYPE_WINDOWS
if (!Directory.Exists(Environment.CurrentDirectory + "\\www\\"))
{
Directory.CreateDirectory(Environment.CurrentDirectory + "\\www\\");
}
#else
if (!Directory.Exists(Environment.CurrentDirectory + "/www"))
{
Directory.CreateDirectory(Environment.CurrentDirectory + "/www");
}
#endif
#if BUILDTYPE_WINDOWS
if (!File.Exists(Environment.CurrentDirectory + "\\GOOMBA.cfg"))
{
File.WriteAllText(Environment.CurrentDirectory + "\\GOOMBA.cfg", "http://localhost:8000/");
}
#else
if (!File.Exists(Environment.CurrentDirectory + "/GOOMBA.cfg"))
{
File.WriteAllText(Environment.CurrentDirectory + "/GOOMBA.cfg", "http://localhost:8000/");
}
#endif
url = File.ReadAllText(Environment.CurrentDirectory + "/GOOMBA.cfg");
#if BUILDTYPE_WINDOWS
url = File.ReadAllText(Environment.CurrentDirectory + "\\GOOMBA.cfg");
#endif
// Create a Http server and start listening for incoming connections
listener = new HttpListener();
listener.Prefixes.Add(url);
listener.Start();
Console.WriteLine("Listening for connections on {0}", url);
Console.WriteLine("Make sure you have index.goomba in your /www folder.");
// Handle requests
Task listenTask = HandleIncomingConnections();