This repository was archived by the owner on Mar 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathirccommands.pas
111 lines (86 loc) · 2.44 KB
/
irccommands.pas
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
unit IRCCommands;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils;
type
{ TIRCCommand }
TAlias = array[0..1] of string;
TIRCCommand = class
private
function FormatCommand(const Raw: string): string;
function GetCommandFromInput(const RawInput: string): string;
function RemoveSlash(const Value: string): string;
function ReplaceAlias(const Command: string): string;
function FixChannelName(const Command: string): string;
public
function GetRawCommand(const RawInput: string): string;
end;
implementation
const
CommandAlias: array[0..0] of TAlias = (
('J', 'JOIN')
);
{ TIRCCommand }
function TIRCCommand.FormatCommand(const Raw: string): string;
var
UserCommand, NewCommand: string;
begin
UserCommand := GetCommandFromInput(Raw);
NewCommand := ReplaceAlias(UpperCase(UserCommand));
Result := StringReplace(Raw, UserCommand, NewCommand, []);
if NewCommand = '/JOIN' then
Result := FixChannelName(Result);
Result := RemoveSlash(Result);
end;
function TIRCCommand.GetCommandFromInput(const RawInput: string): string;
var
CommandEnd: Integer;
begin
CommandEnd := Pos(' ', RawInput);
if CommandEnd = 0 then
CommandEnd := MaxInt;
Result := Copy(RawInput, 1, CommandEnd -1)
end;
function TIRCCommand.RemoveSlash(const Value: string): string;
begin
Result := StringReplace(Value, '/', '', []);
end;
function TIRCCommand.ReplaceAlias(const Command: string): string;
var
I: Integer;
function AddSlash(const Value: string): string;
begin
Result := '/' + Value;
end;
function Replace: string;
begin
Result := StringReplace(Command, AddSlash(CommandAlias[I][0]), AddSlash(CommandAlias[I][1]), []);
end;
begin
Result := Command;
for I := Low(CommandAlias) to High(CommandAlias) do
begin
if Command <> AddSlash(CommandAlias[I][0]) then
Continue;
Exit(Replace);
end;
end;
function TIRCCommand.FixChannelName(const Command: string): string;
var
Channel: string;
begin
Channel := Trim(Copy(Command, Pos(' ', Command), MaxInt));
if (Channel = '') then
Exit;
Result := Command;
if Channel[1] <> '#' then
Result := StringReplace(Result, Channel, '#' + Channel, [])
end;
function TIRCCommand.GetRawCommand(const RawInput: string): string;
begin
if Pos('/', RawInput) <> 1 then
Exit('');
Result := FormatCommand(RawInput);
end;
end.