1 /**
2  * Replay an IRC session from an IrcClient log file.
3  *
4  * License:
5  *   This Source Code Form is subject to the terms of
6  *   the Mozilla Public License, v. 2.0. If a copy of
7  *   the MPL was not distributed with this file, You
8  *   can obtain one at http://mozilla.org/MPL/2.0/.
9  *
10  * Authors:
11  *   Stéphan Kochen <stephan@kochen.nl>
12  *   Vladimir Panteleev <ae@cy.md>
13  *   Vincent Povirk <madewokherd@gmail.com>
14  *   Simon Arlott
15  */
16 
17 module ae.net.irc.clientreplay;
18 
19 import ae.net.asockets;
20 
21 /// `IConnection` implementation which replays an `IrcClient` log file.
22 class IrcClientLogSource : IConnection
23 {
24 	/// `IConnection` stubs.
25 	bool isConnected;
26 	@property ConnectionState state() { return isConnected ? ConnectionState.connected : ConnectionState.disconnected; } /// ditto
27 
28 	void send(scope Data[] data, int priority) {} /// ditto
29 	alias send = IConnection.send; /// ditto
30 
31 	void disconnect(string reason = defaultDisconnectReason, DisconnectType type = DisconnectType.requested) {} /// ditto
32 
33 	@property void handleConnect(ConnectHandler value) { connectHandler = value; } /// ditto
34 	private ConnectHandler connectHandler;
35 
36 	@property void handleReadData(ReadDataHandler value)
37 	{
38 		readDataHandler = value;
39 	} /// ditto
40 	private ReadDataHandler readDataHandler;
41 
42 	@property void handleDisconnect(DisconnectHandler value) {} /// ditto
43 	@property void handleBufferFlushed(BufferFlushedHandler value) {} /// ditto
44 
45 	void recv(Data data)
46 	{
47 		if (readDataHandler)
48 			readDataHandler(data);
49 	} /// ditto
50 
51 	/// Play this log file.
52 	void run(string fn)
53 	{
54 		import std.algorithm;
55 		import std.stdio;
56 
57 		isConnected = true;
58 		connectHandler();
59 
60 		foreach (line; File(fn).byLine(KeepTerminator.yes))
61 		{
62 			if (line[0] != '[')
63 				continue;
64 			line = line.findSplit("] ")[2];
65 			if (line[0] == '<')
66 				recv(Data(line[2..$]));
67 		}
68 	}
69 }