ScriptXNowPlaying

From 40tude Dialog Wiki

this is a script that will add the currently playing song in winamp to an x-header or at the end of the post/e-mail. enjoy :)


// X-Now-Playing script for winamp 2.x
// version: 0.45
// date: 2003-06-22
// author: mkrnic [[at]] public [dot] srce [dot] hr

// please note that the script is applied just before dialog sends your
// message. if you write a message while something is playing and send
// it when your speakers are silent, the song name won't be displayed.

// the script is not guaranteed to work at all and it might crash
// dialog if you reload it and try sendind any posts. i've had that
// happen a couple of times, it's a dialog scripting quirk; after
// recompiling, restart dialog completely. if you lose anything
// valuable because of this script, it's your problem, not mine.

// this script was tested on win2k and winxp machines with winamp
// 2.90 and 2.91. it might not work elsewhere (most notably win9x
// systems, i don't know of anyone who uses these anymore), though
// it should be just fine with any winamp 2.x.

// it's imperative that you turn off song title scrolling in taskbar
// inside winamp's preferences, otherwise you'll get garbage.

// don't mind the warnings when compiling; they don't affect the
// script in any way and are dependant on the true/false choices.

// future plans:
// - winamp 3 support. not that you should be using it at all because
//   it's a slow and bloated piece of software crap :(
// - stripping of any file extension, not just mp3
// - NOT adding the skin name and/or previous/next song because it's
//   impossible to get details on these from outside winamp, and i'm
//   _not_ going to write a special winamp plugin just for this ;)

// changes to the source code and/or using it for coding your own
// scripts are allowed as long as you notify me about it by e-mail
// and send me your code so i can see what you've created :)


// ----------------------------------------------------------------------


program OnBeforeSendingMessage;


// ----------------------------------------------------------------------


const

  // set this to true if you want x-headers and/or song names applied
  // to e-mails, too.
  cApplyToEmails = false;


  // set this to false if you wish not to add the x-header at all.
  cAddXHeader = true;

  // the name of the x-header.
  // DO *NOT* ADD COLONS OR SPACES IN THE HEADER NAME!
  // IF YOU DON'T KNOW WHAT YOU'RE DOING, LEAVE THIS AS IT IS!
  cXHeaderName = 'X-Now-Playing';


  // set this to true if you want the current song to be added to the
  // end of the post.
  cAddSongNameToEndOfPost = false;

  // set this to false if you don't want an empty line before the
  // song name at the end of the post.
  cAddEmptyLineToEndOfPost = true;

  // the text shown before the song name at the end of the post.
  cEndOfPostText = 'Now playing: ';


  // used when nothing is playing.
  cNothing = 'nothing';


  // set this to false if you want X-Now-Playing: song name
  // instead of X-Now-Playing: "song name".
  cIncludeQuotationMarks = true;

  // set this to false if you don't want your winamp version to be
  // shown in round brackets after the song name.
  cIncludeWinampVersion = true;

  // set this to false to have song_name.mp3.
  cStripMp3Extension = true;


  // set this to true if you want a messagebox asking to include
  // the currently playing song in your post.
  // yes, it's stupid, but i use it for debugging, and so could
  // you if you want to mess around with the source code :)
  // please note that the song name is always added to the end
  // of the message if you specified so; the messagebox has no
  // effect here - as i said, it's for debugging purposes only.
  cShowMessageBox = false;

  // the messagebox shows
  // Insert X-Now-Playing: song_name (Winamp version) ?
  // by default; you can do some localization here :))
  cMessageBoxPrompt = 'Insert';

  // the title of the message box.
  cMessageBoxTitle = 'Insert X-Header?';



// ----------------------------------------------------------------------


// there's nothing for you to change down below, go away :)


// ----------------------------------------------------------------------



const
  winampWindowTitle = 'Winamp v1.x';

  WM_USER = 1024;
  WM_WA_IPC = WM_USER;
  IPC_GETVERSION = 0;

  MB_YESNO = 4;
  IDYES = 6;
  IDNO = 7;


// ----------------------------------------------------------------------


function findWindow(c1, c2: 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 sendMessage(hWnd: cardinal; Msg: word; wParam: word; lParam: integer): integer; external 'SendMessageA@user32.dll stdcall';

function messageBox(hWnd: Cardinal; lpText, lpCaption: PChar; uType: longword): Integer; external 'MessageBoxA@user32.dll stdcall';


// ----------------------------------------------------------------------


function findWinampHWND : cardinal;
begin
  result := findWindow (winampWindowTitle, '');
end;


// ----------------------------------------------------------------------


function winampVersion : string;

var version : integer;
    major, minor1, minor2 : word;

begin
  version := sendMessage (findWinampHWND, WM_WA_IPC, 0, IPC_GETVERSION);

  major := version shr 12;
  minor1 := (version shl 4) shr 12;
  minor2 := (version shl 8) shr 8;

  result := 'Winamp ' + inttostr(major) + '.' + inttostr(minor1) + inttostr(minor2);
end;


// ----------------------------------------------------------------------


function winampSong : string;

var titleLen  : integer;
    tempInt   : integer;
    tempStr   : string;
    plNum     : integer;
    tempmp3   : string;
    winampHWND: cardinal;

begin
  // used in case winamp is not running or the window is not found
  tempStr := cNothing;

  winampHWND := findWinampHWND;

  // if winamp's window is found...
  if winampHWND <> 0 then
    begin
      // find the caption text of winamp's window
      titleLen := getWindowTextLength (winampHWND) + 2;
      setLength (tempStr, titleLen);
      getWindowText (winampHWND, pchar(tempStr), titleLen);
      setLength (tempStr, length(tempStr));

      // this comes after the song name
      tempInt := pos (' - Winamp', tempStr);

      // and the first space is after the playlist number
      plNum := pos (' ', tempStr) + 1;

      // if there's no ' - Winamp' in the title, then winamp is started
      // but isn't playing anything (that is, the title is in the form of
      // "Winamp 2.91"), so we'll leave the song name as "nothing"
      if tempInt <> 0 then
        begin
          // the window title is "x. song artist - song name - Winamp", where the x
          // is the playlist position, but we don't want that, so we'll remove both
          // unnecessary things
          tempStr := copy (tempStr, plNum, tempInt - plNum);

          // check if the string ends with '.mp3' and remove it if specified
          if cStripMp3Extension then
            begin
              tempmp3 := lowerCase (copy (tempStr, length(tempStr) - 3, 4));
              if tempmp3 = '.mp3' then tempStr := copy (tempStr, 1, length(tempStr) - 4);
            end;

          // "artist - song name" ?
          if cIncludeQuotationMarks then
            tempstr := '"' + tempstr + '"';

          // (")artist - song name(") (Winamp 2.91) ?
          if cIncludeWinampVersion then
            tempstr := tempstr + ' (' + winampVersion + ')';

        end // if tempInt <> 0
      else
        tempStr := cNothing;

    end; // if winampHWND <> 0

  result := tempStr;
end;


// ----------------------------------------------------------------------


function OnBeforeSendingMessage(var Message: TStringlist; Servername: string; IsEmail: boolean):boolean;

var tempSong : string;

begin

  // apply to usenet posts only or e-mails, too?
  if ((not isEmail) or (isEmail and cApplyToEmails)) then
    begin

      // get the song name
      tempSong := winampSong;

      // add x-header?
      if cAddXHeader then
      begin
        // yes, i know the stuff below could've been solved with just one
        // message.insert, but it's impossible to read such a logical evaluation ;)

        // a messagebox asking you if you wish to include the x-header in your post
        if cShowMessageBox then
          begin
            if messageBox(0, cMessageBoxPrompt + ' ' + CXHeaderName + ': ' + tempSong + ' ?', CMessageBoxTitle, MB_YESNO) = IDYES then
              // insert the x-header at the beginning of the message, dialog (or perhaps
              // the news server) will sort it out as necessary.
              message.insert (1, cXHeaderName + ': ' + tempSong);
          end
        else
          // if you don't want to show the message box, the header is always added
          message.insert (1, cXHeaderName + ': ' + tempSong);

      end; // if cAddXHeader

      // add song name to end of post?
      if cAddSongNameToEndOfPost then
        begin
          // add one empty line?
          if cAddEmptyLineToEndOfPost then message.add ('');

          // the song name
          message.add (cEndOfPostText + tempSong);
        end; // if cAddSongToEndOfPost

    end; // if ((not isEmail) or (isEmail and cApplyToEmails))


  // if this is set to false, sending messages will *always* fail
  // use this for debugging along with the messagebox.
  result := true;
end;


// ----------------------------------------------------------------------

begin
end.