This repository has been archived by the owner on Apr 26, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
146 lines (118 loc) · 4.51 KB
/
Program.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
using System;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading;
namespace LoggingMonkey {
public static class Program {
#if DEBUG
public static readonly string PrimaryPrefix = "http://logs.localhost/";
//public static readonly string PrimaryPrefix = "http://logs2.pandamojo.com" + (Platform.IsOnUnix?":8080":"") + "/";
#else
public static readonly string PrimaryPrefix = "http://logs.pandamojo.com" + (Platform.IsOnUnix?":8080":"") + "/";
#endif
public static readonly CultureInfo Culture = new CultureInfo("en-US",false);
public static readonly string AuthCookieName = "LoggingMonkeyAuth";
public static bool AutoAllow { get { return !Platform.IsOnUnix && File.Exists( Paths.AutoAllowTxt ); } }
static string GuessAndPrependProtocol( string url ) {
Match m = Regexps.UrlProtocol.Match(url);
if ( m.Success ) return url;
else if ( url.StartsWith("www.") ) return "http://"+url;
else if ( url.StartsWith("ftp.") ) return "ftp://"+url;
else return "http://"+url;
}
public static string HtmlizeUrls( string text, bool cats ) {
return Regexps.UrlPatterns.Replace( text, m => {
var url=GuessAndPrependProtocol(m.Value);
if ( cats && url.StartsWith("http://zao.se/~zao/cats/") ) return "<img src=\""+url+"\" alt=\"cats\">";
return "<a rel=\"nofollow\" class=\"link\" target=\"_blank\" href=\""+url+"\">"+m.Value+"</a>";
});
}
static void Main() {
Debug.LogExceptions( () => Work() );
}
static void Work() {
try {
Console.CancelKeyPress += (sender,args) => {
args.Cancel = true;
};
} catch ( NullReferenceException ) {
// generated by *nix when run in the background?
}
var procstart = DateTime.Now;
Debug.WriteLine( "=== Process start at {0} ===", procstart );
var logpattern = Paths.LogsDirectory+"{network}-{channel}-{year}-{month}-{day}.log";
#if DEBUG
var channels = new[] { "#sparta" };
//var whitelistChannels = new[] { "#sparta" };
var whitelistChannels = new string[0];
#else
var channels = new[] { "#gamedev", "#graphicschat", "#graphicsdev", "#anime", "#starcraft" };
var whitelistChannels = new[] { "#gamedev" };
#endif
var logs = new AllLogs() { { "irc.afternet.org", new NetworkLogs("irc.afternet.org",logpattern) } };
var afternet = logs["irc.afternet.org"];
foreach ( var ch in channels ) afternet.Channel(ch);
foreach ( var ch in whitelistChannels ) afternet.Channel(ch).RequireAuth = true;
afternet.Channel("#gamedev");
Debug.Write( "Beginning log server..." );
var server = new HttpLogServer();
Debug.WriteLine( "\rLog server started. " );
Debug.Write("LoggingMonkey comming online...");
var bot = new Network( "irc.afternet.org", channels, logs );
var bott = new Thread(bot.Work);
bott.Start();
Debug.WriteLine("\rLoggingMonkey online. ");
Debug.Write("Getting directory list...");
var files = Directory
.GetFiles(Paths.LogsDirectory, "*.log", SearchOption.TopDirectoryOnly )
.OrderBy( file => {
var m = Regexps.LogFilename.Match(file);
return new DateTime
( int.Parse(m.Groups["year"].Value)
, int.Parse(m.Groups["month"].Value)
, int.Parse(m.Groups["day"].Value)
);
})
.ToArray()
;
var bglogs = new AllLogs() { { "irc.afternet.org", new NetworkLogs("irc.afternet.org",logpattern) } };
var bgafternet = bglogs["irc.afternet.org"];
foreach ( var ch in channels ) bgafternet.Channel(ch);
Debug.Write("Starting GC...");
var before = GC.GetTotalMemory(false);
var after = GC.GetTotalMemory(true);
Debug.WriteLine("\rFinished GC. Before: {0} After: {1} Saved: {2}"
, Pretty.FormatMemory(before)
, Pretty.FormatMemory(after)
, Pretty.FormatMemory(before-after)
);
server.SetLogs(logs);
Debug.WriteLine("Logs now being served.");
for (;;) {
//Console.Write("> ");
string command;
try {
command = Console.ReadLine();
if ( command == null ) for (;;) {}
} catch ( NullReferenceException ) {
for (;;);
}
var split = command.Split(new[]{' '});
switch ( split[0] ) {
case "help":
Console.WriteLine("\t Command Description");
Console.WriteLine("\thelp displays this command list");
Console.WriteLine("\tquit Quits");
break;
case "quit":
return;
default:
Console.WriteLine( "No such command: {0}", split[0] );
break;
}
}
}
}
}