1
0
mirror of https://github.com/moparisthebest/Simba synced 2024-12-23 07:48:50 -05:00

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; program Paster;
//{$DEFINE DEV}
{$IFDEF EXTENSION}
var 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_Menus: array[1..5] of TMenuItem;
AltHost_MenuItems: array[1..5] of array[1..2] of TMenuItem; AltHost_MenuItems: array[1..5] of array[1..4] 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; function EncodeString(Data: string): string;
var var
@ -27,57 +20,17 @@ begin
Result := Replace(Result, Pattern[I], Replacement[I], [rfIgnoreCase, rfReplaceAll]); Result := Replace(Result, Pattern[I], Replacement[I], [rfIgnoreCase, rfReplaceAll]);
end; end;
function CreateJSON(Name: string; Value: variant): TJSON; function DecodeString(Data: string): string;
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 var
StringArray: TStringArray; Pattern, Replacement: TStringArray;
AStringArray: array of TStringArray;
I: integer; I: integer;
begin begin
JSON := Between('{', '}', JSON); Pattern := ['\\', '\b', '\t', '\n', '\v', '\f', '\r', '\"', {'\'#39,} '\/'];
StringArray := Explode(', ', JSON); Replacement := ['\', #8, #9, #10, #11, #12, #13, '"', {#39,} '/'];
SetArrayLength(AStringArray, Length(StringArray)); Result := Data;
for I := 0 to High(StringArray) do if (Length(Pattern) = Length(Replacement)) then
AStringArray[I] := Explode('": ', StringArray[I]); for I := Low(Pattern) to High(Pattern) do
SetArrayLength(Result, Length(AStringArray)); Result := Replace(Result, Pattern[I], Replacement[I], [rfIgnoreCase, rfReplaceAll]);
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; end;
function GetName: string; function GetName: string;
@ -87,29 +40,87 @@ end;
function GetVersion: string; function GetVersion: string;
begin; 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; end;
function PasteIt(out Data: string; HOST: string): boolean; function PasteIt(out Data: string; HOST: string): boolean;
var var
JSONArray: TJSONArray; is_private: boolean;
begin begin
if (HOST = '') then if (HOST = '') then
HOST := 'paste.sheeva.villavu.com'; HOST := 'paste.sheeva.villavu.com';
if (MessageDlg(GetName + ' ' + GetVersion + ' Extension', 'Upload this script to ' + HOST + '?', mtConfirmation, [mbYes, mbNo], 0) = mrYes) then if (MessageDlg(GetName + ' ' + GetVersion + ' Extension', 'Upload this script to ' + HOST + '?', mtConfirmation, [mbYes, mbNo], 0) = mrYes) then
begin begin
Data := EncodeJSON([CreateJSON('language', 'delphi'), CreateJSON('code', ScriptText), CreateJSON('private', (Lowercase(Settings.getKeyValueDef('Private', 'true')) = 'true'))]) is_private := (Lowercase(Settings.getKeyValueDef('Private', 'true')) = 'true');
Data := GetPageEx('http://' + HOST + '/json/?method=pastes.newPaste', Data, 'application/json'); Data := '{"language": "delphi", "code": "' + EncodeString(ScriptText) + '", "private": ' + Lowercase(BoolToStr(is_private)) + '}';
JSONArray := DecodeJSON(Data); //Should be 0 = id and 1 = error JSONRequest(Data, HOST, 'pastes.newPaste');
if (JSONArray[0].value_string = '') then if (GetJSONValue(Data, 'error') = 'null') then
begin begin
Data := '[Paster]Error: ' + JSONArray[1].value_string Data := 'http://' + HOST + '/show/' + GetJSONValue(Data, 'data') + '/';
Result := False;
end else
begin
Data := 'http://' + HOST + '/show/' + JSONArray[0].value_string + '/';
Result := True; Result := True;
end; end else
Data := '[Paster]Error: ' + GetJSONValue(Data, 'error');
end; end;
end; end;
@ -131,34 +142,45 @@ var
begin begin
if InputQuery(GetName + ' ' + GetVersion + ' Extension', 'Please input the LodgeIt Host! (Ex: paste.pocoo.org)', Data) then if InputQuery(GetName + ' ' + GetVersion + ' Extension', 'Please input the LodgeIt Host! (Ex: paste.pocoo.org)', Data) then
begin 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_Menus[I].Caption := Data;
AltHost_MenuItems[I][1].Enabled := True; AltHost_MenuItems[I][1].Enabled := True;
AltHost_MenuItems[I][2].Enabled := True;
Settings.setKeyValue('Host' + IntToStr(I), Data); Settings.setKeyValue('Host' + IntToStr(I), Data);
end; end;
end; end;
procedure OnClick(Sender: TObject); procedure OnClick(Sender: TObject);
var var
I: integer; I, K: integer;
begin; begin;
case Sender of case Sender of
Paster_MenuItem: Paste(''); Paster_MenuItem: Paste('');
AltHost_MenuItems[1][1]: Paste(Settings.getKeyValue('Host1')); GetPaste_MenuItem: GetPaste('');
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: begin
Private_MenuItem.Checked := (not (Private_MenuItem.Checked)); Private_MenuItem.Checked := (not (Private_MenuItem.Checked));
Settings.SetKeyValue('Private', Lowercase(BoolToStr(Private_MenuItem.Checked))); Settings.setKeyValue('Private', Lowercase(BoolToStr(Private_MenuItem.Checked)));
WriteLn('Private = ' + Lowercase(BoolToStr(Private_MenuItem.Checked))); {$IFDEF DEV}WriteLn('Private = ' + Lowercase(BoolToStr(Private_MenuItem.Checked)));{$ENDIF}
end; end;
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; end;
procedure Attach; procedure Attach;
@ -167,12 +189,30 @@ begin;
Paster_Menu.Visible := True; Paster_Menu.Visible := True;
end; end;
Procedure Detach; procedure Detach;
begin begin
Paster_Menu.Visible := False; Paster_Menu.Visible := False;
end; 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 var
I, K: integer; I, K: integer;
begin; begin;
@ -188,6 +228,23 @@ begin;
end; end;
Paster_Menu.Add(Paster_MenuItem); 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); Private_MenuItem := TMenuItem.Create(Paster_Menu);
with Private_MenuItem do with Private_MenuItem do
begin begin
@ -197,9 +254,7 @@ begin;
end; end;
Paster_Menu.Add(Private_MenuItem); Paster_Menu.Add(Private_MenuItem);
Divider_MenuItem := TMenuItem.Create(Paster_Menu); Paster_Menu.Add(Divider_MenuItems[2]);
Divider_MenuItem.Caption := '-';
Paster_Menu.Add(Divider_MenuItem);
AltHost_Menu := TMenuItem.Create(Paster_Menu); AltHost_Menu := TMenuItem.Create(Paster_Menu);
AltHost_Menu.Caption := 'Alternate Hosts'; AltHost_Menu.Caption := 'Alternate Hosts';
@ -211,20 +266,30 @@ begin;
AltHost_Menus[I].Caption := Settings.getKeyValueDef('Host' + IntToStr(I), 'Host ' + IntToStr(I)); AltHost_Menus[I].Caption := Settings.getKeyValueDef('Host' + IntToStr(I), 'Host ' + IntToStr(I));
AltHost_Menu.Add(AltHost_Menus[I]); AltHost_Menu.Add(AltHost_Menus[I]);
for K := 1 to 2 do for K := 1 to 4 do
begin begin
AltHost_MenuItems[I][K] := TMenuItem.Create(AltHost_Menus[I]); AltHost_MenuItems[I][K] := TMenuItem.Create(AltHost_Menus[I]);
case K of case K of
1: AltHost_MenuItems[I][1].Caption := 'Paste It!'; 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; end;
AltHost_MenuItems[I][K].OnClick := @OnClick; AltHost_MenuItems[I][K].OnClick := @OnClick;
if ((K = 1) and (AltHost_Menus[I].Caption = 'Host ' + IntToStr(I))) then if ((not ((K = 3) or (K = 4))) and (AltHost_Menus[I].Caption = 'Host ' + IntToStr(I))) then
AltHost_MenuItems[I][1].Enabled := False; 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]); AltHost_Menus[I].Add(AltHost_MenuItems[I][K]);
end; end;
end; end;
end; end;
{$ENDIF}
begin begin
{$IFNDEF EXTENSION}
WriteLn('This is a Extension for Simba. If you are in Simba click View->Extensions and enable paster.sex!');
{$ENDIF}
end. end.

View File

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

View File

@ -153,3 +153,11 @@ begin
HTTPSend.Free; HTTPSend.Free;
end; end;
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