🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: news.software.readers
Article #127898

Re: [dialog] X-Now-Playing script v0.45

#127898
From: Jernej Simončič
Date: Sun, 06 Jul 2003 21:24
149 lines
8004 bytes
On Sun, 22 Jun 2003 13:54:57 +0200, matija wrote:

> here you go, this one is slightly better and - hopefully - bug free :)

As I don't use WinAmp,but foobar2000 <http://foobar2000.hydrogenaudio.org/>,
I made my own version of X-Now-Playing script, which uses foobar's titlebar.
For everybody interested, here it is:

======8<--------------------------------------------------------------------
program OnBeforeSendingMessage;

(* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *)
(* Script: Add X-Now-Playing based on infromation from foobar2000 <http://foobar2000.hydrogenaudio.org/>         *)
(* Version: 0.1                                                                                                  *)
(* Author: Jernej Simoncic <jernej simoncic at guest arnes si>                                                   *)
(*                                                                                                               *)
(* Usage: The script directly inserts the text from your foobar2000 titlebar; use it's excellent formatting      *)
(* formatting functions to fit the text to your needs; the script won't add anything if the titlebar starts with *)
(* foobar2000 - as this normally means that the player is stopped                                                *)
(* You can set exactly what is added to the message below                                                        *)
(*                                                                                                               *)
(* Tested with foobar2000 0.667 on Win2k.                                                                        *)
(*                                                                                                               *)
(* ToDo:                                                                                                         *)
(*  - 8-bit/unicode character processing                                                                         *)
(*                                                                                                               *)
(* Use this script at your own risk.                                                                             *)
(* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *)

const
  EMail = 1; News = 2; XHeader = 1; EndOfMessage = 2;

  (* * * * * * * * * * * * * * * * * * * * * * * * * * SETTINGS * * * * * * * * * * * * * * * * * * * * * * * * * *)
  UseWith = EMail + News;  //EMail: e-mail messages; News: newsgroup posts
  AddTo = XHeader + EndOfMessage; //XHeader, EndOfMessage 
  XHeaderText = 'X-Now-Playing: %song%'; //the x-header text to add
  EndMessageText = '%n%Now playing: %song%'; //text to add to end of message
  //In both XHeaderText and EndMessageText, %song% will be replaced by the song title; if %song% doesn't appear
  //in the text, song title is added at the end
  //You can also use %n% in the EndMessageText to represent a line break
  (* * * * * * * * * * * * * * * * * * * * * * * * * END OF SETTINGS  * * * * * * * * * * * * * * * * * * * * * * *)
  
  fb2kWindowClass = 'FOOBAR2000_CLASS'; //don't change unless you know what you're doing

function FindWindow(lpClassName, lpWindowName: PChar): Cardinal; external 'FindWindowA@user32.dll stdcall';
function GetWindowTextLength(hWnd: Cardinal): Integer; external 'GetWindowTextLengthA@user32.dll stdcall';
function GetWindowText(hWnd: cardinal; lpString: PChar; nMaxCount: Integer): Integer; external 'GetWindowTextA@user32.dll stdcall';
function MessageBox(hWnd: Cardinal; lpText, lpCaption: PChar; uType: longword): Integer; external 'MessageBoxA@user32.dll stdcall';

procedure MsgBox(text: String);
begin
  MessageBox(null,text,'',0);
end;

function GetFB2kTitle: string;
var handle: Cardinal;
    tlen: Integer;
    title: String;
begin

  title := '';
  handle := FindWindow(fb2kWindowClass, null);;

  if (handle <> 0) then
  begin
    tlen := getWindowTextLength (handle)+1;
    SetLength(title, tlen);
    GetWindowText(handle, pchar(title), tlen);
    SetLength(title, Length(title)-1);
  end;
  
  Result := title;
end;

(*drop-in replacement for the missing StringReplace; acts as if [rfReplaceAll] was specified*)
function StringReplace(text,old,new: String): String;
begin

  while (Pos(old,text) > 0) do
    text := Copy(text,1,Pos(old,text)-1) + new + Copy(text,Pos(old,text)+Length(old),Length(text));
    
  Result := text;
end;

function OnBeforeSendingMessage(var Message: TStringlist; Servername: string; IsEmail: boolean):boolean;    
var song, text: String;
    i: Integer;
begin

  if (((not isEmail) and ((UseWith and News) = News)) or (isEmail and ((UseWith and EMail) = EMail))) then
  begin
    song := GetFB2kTitle();
    
    if (song = '') then
      exit;
    if (Copy(song,1,10) = 'foobar2000') then
      exit;

    if ((AddTo and XHeader) = XHeader) then
    begin
      if (Pos('%song%',XHeaderText) > 0) then
        text := StringReplace(XHeaderText,'%song%',song{,[rfReplaceAll]})
      else
        text := XHeaderText + song;
        
      //clean 8-bit characters from header
      for i := 127 to 255 do
      	if (Pos(Chr(i),text) > 0) then
      	  text := StringReplace(text,Chr(i),'_');  
      
      //sanity checks
      if (Copy(text,1,2) <> 'X-') then
      	text := 'X-' + text;
      if (Pos(': ', text) > 2) and (Pos(': ',text) < (Length(text)-3)) then
        for i := 0 to Message.Count - 1 do
          if (Message.Strings[i] = '') then //insert as the last x-header
          begin
            Message.Insert(i, text);
            break;
          end;
    end;
  
    if ((AddTo and EndOfMessage) = EndOfMessage) then
    begin
      if (Pos('%song%', EndMessageText) > 0) then
        text := StringReplace(EndMessageText,'%song%',song{,[rfReplaceAll]})
      else
      	text := EndMessageText + song;
      
      text := StringReplace(text,'%n%',#13#10{,[rfReplaceAll]});
      	
      Message.Add(text);
    end;
    
  end;

  Result := True;
end;

begin
end.
======8<--------------------------------------------------------------------

-- 
Jernej Simon�i�, <jernej simoncic at guest arnes si>
http://www2.arnes.si/~sopjsimo/

Now playing: Yoko Kanno, Ilaria Graziano - Ghost In The Shell ~ Stand Alone Complex OST [16] - ????�? [Monochrome]
Technical Details
Message-ID: <80htwmoh087n$.dlg@jernej.isg.si>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!illumos!news.pugleaf.net!.TX!archive.pugleaf.net!archive.newsdeef.eu!mbox2nntp-news.software.readers.mbox.zip!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!logbridge.uoregon.edu!fu-berlin.de!uni-berlin.de!tm.213.143.81.18.dc.telemach.NET!not-for-mail
References: <myfhectiqs8$.dlg@shpongle.604>

Show Full Headers
Complete Article Headers
Message-ID: <80htwmoh087n$.dlg@jernej.isg.si>
Subject: Re: [dialog] X-Now-Playing script v0.45
Date: Sun, 6 Jul 2003 23:24:26 +0200
From: Jernej Simončič <echo@arnes.si>
References: <myfhectiqs8$.dlg@shpongle.604>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!illumos!news.pugleaf.net!.TX!archive.pugleaf.net!archive.newsdeef.eu!mbox2nntp-news.software.readers.mbox.zip!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!logbridge.uoregon.edu!fu-berlin.de!uni-berlin.de!tm.213.143.81.18.dc.telemach.NET!not-for-mail
Newsgroups: news.software.readers
Organization: E-mailaholics International
Lines: 148
NNTP-Posting-Host: tm.213.143.81.18.dc.telemach.net (213.143.81.18)
Mime-Version: 1.0
Content-Type: text/plain; charset="iso-8859-2"
Content-Transfer-Encoding: 8bit
X-Trace: fu-berlin.de 1057527129 2922092 213.143.81.18 (16 [164251])
X-Face: "C"$"pch<BRjs*}C`7SY%L_!%,c:9`'2_oB$Z$}sq.Vh<Y/8#L#_Z~1&&^pF]TjLN8yb}Ui %Ce%9}L{tX[Z3#>JZ+mZ]>tG>$Lx#rA*Q~V53T`-Ch0dT5S!XE9l(*XG{#*tQ1nh}Y\X]u'1"\kvH3 Bn\r*^bE89Jm9PJOBMTt7%F+jytYh>`4n"h?x[^4Hg~B?0MMxNE7%\s20h7'l@pQ[:8wdV^U_"ng6w qOnOn1#xF'+d\`S=s2KB*#Xg?c-/0A7+I.{fFbzWt
X-Ultimate-Answer: 42
User-Agent: 40tude_Dialog/2.0.5.53 (f1bbb369.131.157)
X-Now-Playing: Yoko Kanno, Ilaria Graziano - Ghost In The Shell ~ Stand Alone Complex OST [16] - ????_? [Monochrome]