1
0
mirror of https://github.com/moparisthebest/Simba synced 2024-11-27 11:32:18 -05:00
Simba/Extensions/paster.sex
2010-09-26 17:16:56 -04:00

231 lines
6.6 KiB
Plaintext

program Paster;
var
Paster_Menu, Private_MenuItem, Paster_MenuItem, Divider_MenuItem, AltHost_Menu: TMenuItem;
AltHost_Menus: array[1..5] of TMenuItem;
AltHost_MenuItems: array[1..5] of array[1..2] of TMenuItem;
type
TJSON = record
name: string;
value_type: string;
value_string: string;
value_bool: boolean;
end;
TJSONArray = array of TJSON;
function EncodeString(Data: string): string;
var
Pattern, Replacement: TStringArray;
I: integer;
begin
Pattern := ['\', #8, #9, #10, #11, #12, #13, '"', {#39,} '/'];
Replacement := ['\\', '\b', '\t', '\n', '\v', '\f', '\r', '\"', {'\'#39,} '\/'];
Result := Data;
if (Length(Pattern) = Length(Replacement)) then
for I := Low(Pattern) to High(Pattern) do
Result := Replace(Result, Pattern[I], Replacement[I], [rfIgnoreCase, rfReplaceAll]);
end;
function CreateJSON(Name: string; Value: variant): TJSON;
begin
Result.name := Name;
case VarType(Value) of //Made it a case incase we add more =)
varString: begin
Result.value_type := 'string';
Result.value_string := EncodeString(Value);
exit;
end;
varBoolean: begin
Result.value_type := 'boolean';
Result.value_bool := Value;
end;
varNull: begin
Result.value_type := 'null';
end;
end;
end;
function DecodeJSON(JSON: string): TJSONArray;
var
StringArray: TStringArray;
AStringArray: array of TStringArray;
I: integer;
begin
JSON := Between('{', '}', JSON);
StringArray := Explode(', ', JSON);
SetArrayLength(AStringArray, Length(StringArray));
for I := 0 to High(StringArray) do
AStringArray[I] := Explode('": ', StringArray[I]);
SetArrayLength(Result, Length(AStringArray));
for I := 0 to High(AStringArray) do
Result[I] := CreateJSON(Between('"', '"', AStringArray[I][0] + '"'), Between('"', '"', AStringArray[I][1]));
end;
function EncodeJSON(JSON: TJSONArray): string;
var
I: integer;
begin
Result := '{';
for I := 0 to High(JSON) do
begin
Result := Result + '"' + JSON[I].name + '": ';
case JSON[I].value_type of
'string': Result := Result + '"' + JSON[I].value_string + '"';
'boolean': Result := Result + Lowercase(BoolToStr(JSON[I].value_bool));
end;
if (I < High(JSON)) then
Result := Result + ', ';
end;
Result := Result + '}';
end;
function GetName: string;
begin;
Result := 'Paster';
end;
function GetVersion: string;
begin;
Result := '0.1a';
end;
function PasteIt(out Data: string; HOST: string): boolean;
var
JSONArray: TJSONArray;
begin
if (HOST = '') then
HOST := 'paste.sheeva.villavu.com';
if (MessageDlg(GetName + ' ' + GetVersion + ' Extension', 'Upload this script to ' + HOST + '?', mtConfirmation, [mbYes, mbNo], 0) = mrYes) then
begin
Data := EncodeJSON([CreateJSON('language', 'delphi'), CreateJSON('code', ScriptText), CreateJSON('private', (Lowercase(Settings.getKeyValueDef('Private', 'true')) = 'true'))])
Data := GetPageEx('http://' + HOST + '/json/?method=pastes.newPaste', Data, 'application/json');
JSONArray := DecodeJSON(Data); //Should be 0 = id and 1 = error
if (JSONArray[0].value_string = '') then
begin
Data := '[Paster]Error: ' + JSONArray[1].value_string
Result := False;
end else
begin
Data := 'http://' + HOST + '/show/' + JSONArray[0].value_string + '/';
Result := True;
end;
end;
end;
procedure Paste(Host: string);
var
Data: string;
begin
if PasteIt(Data, Host) then
begin
WriteLn('Opening pasted script at "' + Data + '"!');
OpenWebPage(Data);
end else
WriteLn(Data);
end;
procedure UpdateHost(I: integer);
var
Data: string;
begin
if InputQuery(GetName + ' ' + GetVersion + ' Extension', 'Please input the LodgeIt Host! (Ex: paste.pocoo.org)', Data) then
begin
AltHost_Menus[I].Caption := Data;
AltHost_MenuItems[I][1].Enabled := True;
Settings.setKeyValue('Host' + IntToStr(I), Data);
end;
end;
procedure OnClick(Sender: TObject);
var
I: integer;
begin;
case Sender of
Paster_MenuItem: Paste('');
AltHost_MenuItems[1][1]: Paste(Settings.getKeyValue('Host1'));
AltHost_MenuItems[2][1]: Paste(Settings.getKeyValue('Host2'));
AltHost_MenuItems[3][1]: Paste(Settings.getKeyValue('Host3'));
AltHost_MenuItems[4][1]: Paste(Settings.getKeyValue('Host4'));
AltHost_MenuItems[5][1]: Paste(Settings.getKeyValue('Host5'));
AltHost_MenuItems[1][2]: UpdateHost(1);
AltHost_MenuItems[2][2]: UpdateHost(2);
AltHost_MenuItems[3][2]: UpdateHost(3);
AltHost_MenuItems[4][2]: UpdateHost(4);
AltHost_MenuItems[5][2]: UpdateHost(5);
Private_MenuItem: begin
Private_MenuItem.Checked := (not (Private_MenuItem.Checked));
Settings.SetKeyValue('Private', Lowercase(BoolToStr(Private_MenuItem.Checked)));
WriteLn('Private = ' + Lowercase(BoolToStr(Private_MenuItem.Checked)));
end;
end;
end;
procedure Attach;
begin;
WriteLn(GetName + ' ' + GetVersion + ' Plugin Loaded!');
Paster_Menu.Visible := True;
end;
Procedure Detach;
begin
Paster_Menu.Visible := False;
end;
procedure Init;
var
I, K: integer;
begin;
Paster_Menu := TMenuItem.Create(Simba_MainMenu);
Paster_Menu.Caption := GetName;
Simba_MainMenu.Items.Add(Paster_Menu);
Paster_MenuItem := TMenuItem.Create(Paster_Menu);
with Paster_MenuItem do
begin
Caption := 'Paste It!';
OnClick := @OnClick;
end;
Paster_Menu.Add(Paster_MenuItem);
Private_MenuItem := TMenuItem.Create(Paster_Menu);
with Private_MenuItem do
begin
Caption := 'Private';
OnClick := @OnClick;
Checked := (Lowercase(Settings.getKeyValueDef('Private', 'true')) = 'true');
end;
Paster_Menu.Add(Private_MenuItem);
Divider_MenuItem := TMenuItem.Create(Paster_Menu);
Divider_MenuItem.Caption := '-';
Paster_Menu.Add(Divider_MenuItem);
AltHost_Menu := TMenuItem.Create(Paster_Menu);
AltHost_Menu.Caption := 'Alternate Hosts';
Paster_Menu.Add(AltHost_Menu);
for I := 1 to 5 do
begin
AltHost_Menus[I] := TMenuItem.Create(AltHost_Menu);
AltHost_Menus[I].Caption := Settings.getKeyValueDef('Host' + IntToStr(I), 'Host ' + IntToStr(I));
AltHost_Menu.Add(AltHost_Menus[I]);
for K := 1 to 2 do
begin
AltHost_MenuItems[I][K] := TMenuItem.Create(AltHost_Menus[I]);
case K of
1: AltHost_MenuItems[I][1].Caption := 'Paste It!';
2: AltHost_MenuItems[I][2].Caption := 'Update Host';
end;
AltHost_MenuItems[I][K].OnClick := @OnClick;
if ((K = 1) and (AltHost_Menus[I].Caption = 'Host ' + IntToStr(I))) then
AltHost_MenuItems[I][1].Enabled := False;
AltHost_Menus[I].Add(AltHost_MenuItems[I][K]);
end;
end;
end;
begin
end.