Updated paste.sex

Added superobject unit
Added GetJSONValue function to extensions
This commit is contained in:
Dgby714 2010-09-29 06:07:55 -04:00
parent 66c94e1fe7
commit 073bb3ce27
4 changed files with 7552 additions and 97 deletions

View File

@ -1,18 +1,11 @@
program Paster;
//{$DEFINE DEV}
{$IFDEF EXTENSION}
var
Paster_Menu, Private_MenuItem, Paster_MenuItem, Divider_MenuItem, AltHost_Menu: TMenuItem;
Paster_Menu, GetPaste_MenuItem, Private_MenuItem, Paster_MenuItem, AltHost_Menu: TMenuItem;
Divider_MenuItems: array[1..2] of 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;
AltHost_MenuItems: array[1..5] of array[1..4] of TMenuItem;
function EncodeString(Data: string): string;
var
@ -27,57 +20,17 @@ begin
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;
function DecodeString(Data: string): string;
var
StringArray: TStringArray;
AStringArray: array of TStringArray;
Pattern, Replacement: 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 + '}';
Pattern := ['\\', '\b', '\t', '\n', '\v', '\f', '\r', '\"', {'\'#39,} '\/'];
Replacement := ['\', #8, #9, #10, #11, #12, #13, '"', {#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 GetName: string;
@ -87,29 +40,87 @@ end;
function GetVersion: string;
begin;
Result := '0.1a';
Result := '0.2a';
end;
function JSONRequest(var Data: string; const HOST, Method: string): boolean;
{$IFDEF DEV}var
I: integer;{$ENDIF}
begin
{$IFDEF DEV}
WriteLn('(HOST, Method) := ('#39 + HOST + #39', '#39 + Method + #39');');
WriteLn('Data := '#39 + Data + #39';');
{$ENDIF}
Data := GetPageEx('http://' + HOST + '/json/?method=' + Method, Data, 'application/json');
{$IFDEF DEV}WriteLn('Data := '#39 + Data + #39';');{$ENDIF}
if (Data = '') then
begin
Data := '{"data": null, "error": "Server unresponsive!"}';
Result := False;
Exit;
end;
if (Method = 'pastes.getPaste') then
begin
//Little hack for getPaste too work....
Data := Replace(Data, '{"data": {', '{', []);
Data := Replace(Data, '}, "error": ', ', "error": ', []);
end;
Result := True;
end;
function GetPaste(HOST: string): boolean;
var
Data: string;
begin
if (HOST = '') then
HOST := 'paste.sheeva.villavu.com';
if (InputQuery(GetName + ' ' + GetVersion + ' Extension', 'Which ID would you like too grab?', Data)) then
begin
if IntToStr(StrToInt(Data)) = Data then
Data := '{"paste_id": ' + Data + '}'
else
Data := '{"paste_id": "' + Data + '"}';
if (not (JSONRequest(Data, HOST, 'pastes.getPaste'))) then
begin
WriteLn('[Paster]Error: ' + GetJSONValue(Data, 'error'));
Result := False;
Exit;
end;
if ((GetJSONValue(Data, 'code') = 'null') and (GetJSONValue(Data, 'error') = 'null')) then
begin
WriteLn('[Paster]Error: Invalid Paste ID!');
Result := False;
Exit;
end;
Data := GetJSONValue(Data, 'code');
WriteLn('[Paster]Info: Sorry guys I can''t open a script from extensions yet...');
WriteLn(Data);
Result := True;
end;
end;
function PasteIt(out Data: string; HOST: string): boolean;
var
JSONArray: TJSONArray;
is_private: boolean;
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
is_private := (Lowercase(Settings.getKeyValueDef('Private', 'true')) = 'true');
Data := '{"language": "delphi", "code": "' + EncodeString(ScriptText) + '", "private": ' + Lowercase(BoolToStr(is_private)) + '}';
JSONRequest(Data, HOST, 'pastes.newPaste');
if (GetJSONValue(Data, 'error') = 'null') then
begin
Data := '[Paster]Error: ' + JSONArray[1].value_string
Result := False;
end else
begin
Data := 'http://' + HOST + '/show/' + JSONArray[0].value_string + '/';
Data := 'http://' + HOST + '/show/' + GetJSONValue(Data, 'data') + '/';
Result := True;
end;
end else
Data := '[Paster]Error: ' + GetJSONValue(Data, 'error');
end;
end;
@ -131,34 +142,45 @@ var
begin
if InputQuery(GetName + ' ' + GetVersion + ' Extension', 'Please input the LodgeIt Host! (Ex: paste.pocoo.org)', Data) then
begin
{$IFDEF DEV}WriteLn('Data := '#39 + Data + #39';');{$ENDIF}
if (Data = '') then
begin
WriteLn('[Paster] Error: The server cannot be blank!');
Exit;
end;
AltHost_Menus[I].Caption := Data;
AltHost_MenuItems[I][1].Enabled := True;
AltHost_MenuItems[I][2].Enabled := True;
Settings.setKeyValue('Host' + IntToStr(I), Data);
end;
end;
procedure OnClick(Sender: TObject);
var
I: integer;
I, K: 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);
GetPaste_MenuItem: GetPaste('');
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)));
Settings.setKeyValue('Private', Lowercase(BoolToStr(Private_MenuItem.Checked)));
{$IFDEF DEV}WriteLn('Private = ' + Lowercase(BoolToStr(Private_MenuItem.Checked)));{$ENDIF}
end;
end;
for I := 1 to 5 do
for K := 1 to 4 do
if (Sender = AltHost_MenuItems[I][K]) then
begin
{$IFDEF DEV}WriteLn('Sender = ' + IntToStr(I) + ', ' + IntToStr(K));{$ENDIF}
if (K = 4) then
UpdateHost(I);
if (K = 2) then
GetPaste(Settings.getKeyValue('Host' + IntToStr(I)));
if (K = 1) then
Paste(Settings.getKeyValue('Host' + IntToStr(I)));
Break;
end;
end;
procedure Attach;
@ -167,12 +189,30 @@ begin;
Paster_Menu.Visible := True;
end;
Procedure Detach;
procedure Detach;
begin
Paster_Menu.Visible := False;
end;
procedure Init;
procedure Free;
var
I, K: integer;
begin
for I := 5 downto 1 do
begin
for K := 4 downto 1 do
AltHost_MenuItems[I][K].Free;
AltHost_Menus[I].Free;
end;
AltHost_Menu.Free;
Private_MenuItem.Free;
for I := 2 downto 1 do
Divider_MenuItems[I].Free;
Paster_MenuItem.Free;
Paster_Menu.Free;
end;
procedure init;
var
I, K: integer;
begin;
@ -187,7 +227,24 @@ begin;
OnClick := @OnClick;
end;
Paster_Menu.Add(Paster_MenuItem);
GetPaste_MenuItem := TMenuItem.Create(Paster_Menu);
with GetPaste_MenuItem do
begin
Caption := 'Get Paste!';
OnClick := @OnClick;
{$IFNDEF DEV}Visible := False;{$ENDIF}
end;
Paster_Menu.Add(GetPaste_MenuItem);
for I := 1 to 2 do
begin
Divider_MenuItems[I] := TMenuItem.Create(Paster_Menu);
Divider_MenuItems[I].Caption := '-';
end;
Paster_Menu.Add(Divider_MenuItems[1]);
Private_MenuItem := TMenuItem.Create(Paster_Menu);
with Private_MenuItem do
begin
@ -196,10 +253,8 @@ begin;
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);
Paster_Menu.Add(Divider_MenuItems[2]);
AltHost_Menu := TMenuItem.Create(Paster_Menu);
AltHost_Menu.Caption := 'Alternate Hosts';
@ -211,20 +266,30 @@ begin;
AltHost_Menus[I].Caption := Settings.getKeyValueDef('Host' + IntToStr(I), 'Host ' + IntToStr(I));
AltHost_Menu.Add(AltHost_Menus[I]);
for K := 1 to 2 do
for K := 1 to 4 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';
2: AltHost_MenuItems[I][2].Caption := 'Get Paste!';
3: AltHost_MenuItems[I][3].Caption := '-';
4: AltHost_MenuItems[I][4].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;
if ((not ((K = 3) or (K = 4))) and (AltHost_Menus[I].Caption = 'Host ' + IntToStr(I))) then
AltHost_MenuItems[I][K].Enabled := False;
{$IFNDEF DEV}if (K = 2) then
AltHost_MenuItems[I][K].Visible := False;{$ENDIF}
AltHost_Menus[I].Add(AltHost_MenuItems[I][K]);
end;
end;
end;
{$ENDIF}
begin
{$IFNDEF EXTENSION}
WriteLn('This is a Extension for Simba. If you are in Simba click View->Extensions and enable paster.sex!');
{$ENDIF}
end.

View File

@ -67,6 +67,7 @@ uses
SynRegExpr,
lclintf,
httpsend,
superobject,
SimbaUnit,updateform, mmisc, mmlpsthread; // for GetTickCount and others.//Writeln
{$ifdef Linux}
@ -185,6 +186,7 @@ begin
AddFunction(@ext_ScriptText,'function ScriptText: string;');
AddRegisteredPTRVariable('Settings','TMMLSettingsSandbox');
AddFunction(@ext_GetPageEx,'function GetPageEx(const URL, PostData, MimeType: string): string;');
AddFunction(@ext_GetJSONValue,'function GetJSONValue(const Data, Value: string): string;');
AddRegisteredVariable('Simba','TForm');
AddRegisteredVariable('Simba_MainMenu','TMainMenu');
AddRegisteredVariable('Client','TClient');

View File

@ -153,3 +153,11 @@ begin
HTTPSend.Free;
end;
end;
function ext_GetJSONValue(const Data, Value: string): string;
var
SuperObject: ISuperObject;
begin
SuperObject := SO(Data);
Result := SuperObject.AsObject.S[Value];
end;

7380
Units/Misc/superobject.pas Normal file

File diff suppressed because it is too large Load Diff