Google Answers Logo
View Question
 
Q: PHP or Perl script to send instant message via Apache ( Answered 5 out of 5 stars,   4 Comments )
Question  
Subject: PHP or Perl script to send instant message via Apache
Category: Computers > Programming
Asked by: donphiltrodt-ga
List Price: $21.00
Posted: 14 May 2003 02:27 PDT
Expires: 13 Jun 2003 02:27 PDT
Question ID: 203507
--) I have Apache for Windows running on my personal system.

--) I wish to use my browser to send instant messages via AIM (AOL
Instant Messenger) via a Perl or PHP script running my Apache server.

--) If an AIM script is not available, I'll settle for Yahoo,ICQ or
MSN Messenger.

Request for Question Clarification by sycophant-ga on 16 May 2003 19:50 PDT
Hi,

What you are looking for should be possible, and there are scripts
that claim to do it. However the problem is, AOL, who own both ICQ and
AIM, change their protocols fairly often to keep people out of their
system. The OSCAR protocol that they both use is closed source. Some
people have reverse enginered it (Trillian etc) but generally the
details of their protocols are not publicly available.

There is a Net::AIM module for Perl, but I have not found any scripts
using it to do what you want.

Would you be happy with a script that acts as a front end for the
official ICQ Pager?

Regards,
Sycophant-ga

Clarification of Question by donphiltrodt-ga on 17 May 2003 02:43 PDT
>There is a Net::AIM module for Perl, but I have 
>not found any scripts using it to do what you want.

Yeah.  Me neither.  The lack of cgi scripts using Net::AIM is
surprising to me too (hence the GA question).

>Would you be happy with a script that acts as
> a front end for the official ICQ Pager?

Probably.  It depends on what it does.

If the script accepts input via HTTP (either POST or GET) and then
instantly causes an IM, then I'll accept it.

If however there's a delay of, say, greater than 10 seconds (like, for
example, a sketchy or over-busy email-to-IM gateway), then I'd prefer
to lower the question price ($11) before you posted the answer, seeing
that the "instantness" I requested isn't there.

Context: The IM script will actually triggered by an ASP script on my
webserver.  I'll use the script you find to enable me to be instantly
notified when certain events occur on my website.  I'm currently using
email for this task, but the architecture of POP3 isn't well suited
for instant notification.

Request for Question Clarification by sycophant-ga on 17 May 2003 04:56 PDT
Hi,

Unfortunately my Perl is more than a little rusty, so I can't manage
any Net::AIM scripts myself.

There is a Jabber class for PHP, which in theory could be used to do
what you want through a Jabber gateway server, but that seems a little
too troublesome.

Overall, I can't find anything that does what you want natively.

In theory it should be as simple as sending an email to
UIN@pager.icq.com - but I can't even make that work at the moment.

There is also this (http://www.icq.com/panels/webpanel/), which is
very simple, you can essentially have any webpage submit a form with
the right variables to the ICQ server. That seems to work.

I'll keep looking for better options, but I am not sure what I might
find. I'd be happy to create a little code, using either of those
methods, let me know if that would suit. Or any other thoughts or info
you have.


Regards,
Sycophant-ga

Clarification of Question by donphiltrodt-ga on 18 May 2003 16:18 PDT
>There is also this (http://www.icq.com/panels/webpanel/), 
>which is very simple, you can essentially have any 
>webpage submit a form with the right variables to 
> the ICQ server. That seems to work.

...but not well.  Every message is cluttered with IP address and
Subject, and my experimentation suggests that those fields can not be
omitted from the HTTP request (either GET or POST).  Receiving a
constant flow of such cluttered messages wouldn't meet my needs.  Thx
for the clarification.

And to errol...

I've hacked with that script a bit and it doesn't seem to work: It
does nothing but say...

>>Fatal error: Maximum execution time of 30 seconds
>>exceeded in d:\apache\htdocs\msn.php on line 62

...despite a) changing the msn server to the correct IP address
(207.46.107.13) and b) fiddling with the php little.

So... if it works, I can't tell.
Answer  
Subject: Re: PHP or Perl script to send instant message via Apache
Answered By: errol-ga on 27 May 2003 07:23 PDT
Rated:5 out of 5 stars
 
Hello again!

You will be pleased to know that I have a working script in my
possession, coded in Perl and using AIM.

I'm assuming that you either have Net::AIM installed or know how to.

Here is the code:
-----------------------

#!/usr/bin/perl

use lib '.';
use Net::AIM;

$aim = new Net::AIM;
$conn = $aim->newconn (Screenname   => 'AIMScreenName',
                        Password     => 'Password');

foreach my $i (0..14) { # Empirical 15s delay
    $aim->do_one_loop || last;
    sleep 1;
}

$aim->send_im ('SecondAIMScreenName', 'EnterMessage');
sleep 2;

-----------------------

If you do not have the latest Net:AIM module, you can download it from
here:
http://cpan.org/authors/id/A/AR/ARYEH/

Kind regards,
errol-ga.

Request for Answer Clarification by donphiltrodt-ga on 27 May 2003 13:52 PDT
Nearly excellent.

Would you mind altering the script to accept 4 strings by either POST or GET?...

1) SenderName (SN)
2) SenderPassword (SP)
3) Recipient (R)
4) Message (M)

Request for Answer Clarification by donphiltrodt-ga on 27 May 2003 13:54 PDT
(Oops.  Bumped the "Post Request" button too soon.)

It seems (to me) that my requested hack is very trivial.  But if you
would prefer me to raise the price, I'd be glad to, since I know
you've spent some considerable time.

Clarification of Answer by errol-ga on 27 May 2003 20:44 PDT
Hello again!

No problem at all, it is very easy to pass form values to a script -
you just do this:

Your HTML page
===============

<html>
<head>
<title>Compose Message</title>
</head>
<body>


<form method="post" action="sendmessage.pl">

<!-- You could also use action="sendmessage.cgi", depending on the
server setup -->
<!-- OR GET like this:
<!-- <form method="get" action="sendmessage.pl">

Sender Name:<br>
<input type="text" name="sendername"><br>
Sender Password:<br>
<input type="text" name="password"><br>
Recipient:<br>
<input type="text" name="recipient"><br>
Message:<br>
<textarea name="message"></textarea><br>
<input type="reset" value="Start Over">
<input type="submit" value="Send Message">
</form>

</body>
</html>


The modified script - "sendmessage.pl"
====================

#!/usr/bin/perl 
 
use lib '.'; 
use Net::AIM; 
 
$aim = new Net::AIM; 
$conn = $aim->newconn (Screenname   => '$sendername', 
                        Password     => '$password'); 
 
foreach my $i (0..14) { # Empirical 15s delay 
    $aim->do_one_loop || last; 
    sleep 1; 
} 
 
$aim->send_im ('$recipient', '$message'); 
sleep 2; 

As you can see, all you need to do is use variables such as
$sendername in the script and it will automatically capture the form
values and use them.
To use the GET method, you would do something like this:

http://www.yoursite.com/sendmessage.pl?sendername=myscreenname&password=thepassword&recipient=therecipient&message=Hello

Obviously, the POST method is better because it doesn't show the
password variable in the URL.

If this doesn't work for any reason, let me know and I'll write the
processing script in PHP with a .bat file which runs on the local
filesystem to execute the Perl script.


Kind regards,
errol-ga.

Request for Answer Clarification by donphiltrodt-ga on 02 Jun 2003 17:56 PDT
Wow.  You're definitely very close.  I have one more request (for a
higher price if nec):...

The script works only if the message is hard coded (like your first
version).  But if you use the second version (with string
interpolation), Apache throws this error...

Can't use an undefined value as a symbol reference at
D:/perl/site/lib/Net/AIM/Connection.pm line 781.

...So to what price should I raise the question so you feel
comfortable fixing this error?

Clarification of Answer by errol-ga on 03 Jun 2003 06:21 PDT
Hi there!

I have spent the (very frustrating!) morning working out the form
parsing code and I now have the final, working version ready.

Here it is, remember to replace the path to Perl with yours:



#!E:/Perl/bin/perl.exe
  
print "Content-type: text/html\n\n";

read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);
foreach $pair (@pairs) {
    ($name, $value) = split(/=/, $pair);
    $value =~ tr/+/ /;
    $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
    $FORM{$name} = $value;
}

print "Sent message from <b>$FORM{'sendername'}</b>, to
<b>$FORM{'recipient'}</b>!";

use lib '.';  
use Net::AIM;  

$aim = new Net::AIM;  
$conn = $aim->newconn (Screenname   => $FORM{'sendername'},  
                        Password     => $FORM{'password'});  

foreach my $i (0..4) { # Empirical 15s delay  
    $aim->do_one_loop || last;  
    sleep 1;  
}  


$aim->send_im ( $FORM{'recipient'},$FORM{'message'});  
sleep 1; 

print "</body></html>";


I've tested the code thoroughly by changing the various form fields
and it works like a dream, I shortened the sleep time for the code too
so it is faster than before.

errol-ga.

Request for Answer Clarification by donphiltrodt-ga on 03 Jun 2003 12:34 PDT
Wow.  Thanks for all your work.  I will compensate you via a tip.

This is really getting close.  This version, unfortunately, doesn't
work properly when using GET instead of POST.  If you need further
details, lemme know.

(I'll be available until 5:30 pm PDT, then I won't return until Friday
AM.)

Clarification of Answer by errol-ga on 03 Jun 2003 14:12 PDT
Hi!

No problem at all, here we have the finished script which will accept
either POST or the GET methods, I also cleaned up the code a little
bit:

===============

#!E:/Perl/bin/perl.exe

  if ($ENV{'REQUEST_METHOD'} eq 'GET')
  {
    @pairs = split(/&/, $ENV{'QUERY_STRING'});
  } 
  elsif ($ENV{'REQUEST_METHOD'} eq 'POST') 
  {
    read (STDIN, $buffer,  $ENV{'CONTENT_LENGTH'});
    @pairs = split(/&/, $buffer);
    if ($ENV{'QUERY_STRING'}) 
    {
      @getpairs = split(/&/,  $ENV{'QUERY_STRING'});
      push(@pairs,@getpairs);
    }
  }
else 
{
  print "Content-type: text/html\n\n";
  print "Use the POST or GET methods."; }
 foreach $pair (@pairs) { ($key, $value) = split (/=/, $pair);
$key =~ tr/+/ /; $key =~ tr/+/ /;
$key =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$value =~ tr/+/ /; $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",
hex($1))/eg;
$value =~ s///g; if ($formdata{$key}) { $formdata{$key} .= ", $value";
}
else { $formdata{$key} = $value; } } 1; 
print "Content-type: text/html\n\n";
print "Sent message from <b>$formdata{'sendername'}</b>, to
<b>$formdata{'recipient'}</b>!";
use lib '.';  
use Net::AIM;  
$aim = new Net::AIM;  
$conn = $aim->newconn (Screenname   => $formdata{'sendername'},  
                        Password     => $formdata{'password'});  
foreach my $i (0..4) {  
    $aim->do_one_loop || last;  
    sleep 1;  
}  
$aim->send_im ( $formdata{'recipient'},$formdata{'message'});  
sleep 1; 
print "</body></html>";


Kind regards,
errol-ga.

Clarification of Answer by errol-ga on 03 Jun 2003 15:07 PDT
Thank you for your generosity!
It was a good learning experience and very satisfying when the code
started to work. :)

errol-ga.
donphiltrodt-ga rated this answer:5 out of 5 stars and gave an additional tip of: $30.00
Thanks SO much for a) diving in and b) sticking to it.

Comments  
Subject: Re: PHP or Perl script to send instant message via Apache
From: errol-ga on 17 May 2003 06:35 PDT
 
Hi there, Donphiltrodt.


I have actually been looking for a similar script myself for a while
now but have had no luck at all, until this afternoon. :)
I also have experience of running Apache 2.x with PHP on a Windows
machine for occasional development.

After searching for a while, I found some MSN and PHP code [
http://www.msn.tweakzone.nl/scripts/bekijk/214 ] which appears to be
able to send messages using the MSN MIME format like this:

======
MSG 3 A 157
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
X-MMS-IM-Format: FN=Microsoft%20Sans%20Serif; EF=I; CO=000000; CS=0;
PF=22

Hello! How are you?
======

I didn't post this as an answer because the other researcher might
still be looking and I don't want to spoil their hard work.
Please let me know if the solution that I found is acceptable as an
answer.


Regards,
errol.
Subject: Re: PHP or Perl script to send instant message via Apache
From: donphiltrodt-ga on 18 May 2003 16:23 PDT
 
Well, after hacking a bit more, I at least got the script to connect
to server.  Now the script hangs at line 94.

PLEASE NOTE, though, that I'm providing this info purely for clarity
NOT as an attempt (or suggestion) to engage in GA hacking or
debugging.  I don't have time to futz with partially working
non-English scripts.
Subject: Re: PHP or Perl script to send instant message via Apache
From: errol-ga on 18 May 2003 18:48 PDT
 
Hello again.

I've tested the script available here:
http://www.evilwalrus.com/viewcode.php?codeEx=515 and it does show me
as being online (I had to create another Passport account for
testing).
The only thing missing in the script is the actual message sending,
which I don't think would be too difficult to include.
I'm going to test it out a bit more and get back to you as soon as
possible.

Regards,
errol.
Subject: Re: PHP or Perl script to send instant message via Apache
From: errol-ga on 19 May 2003 09:47 PDT
 
Bit of an update...

I have spent a long time trying to get this script/php class to work.
It will connect, I can change the status (away/online/busy etc) but I
can't make it connect properly to the switchboard server.
The main problem is dealing with the long hash codes that must be used
by the MSN client for authentication.

If you see here: [ http://www.hypothetic.org/docs/msn/index.php ],
there is a lot of work involved when attempting to actually send the
message and it does get complicated when the script needs to act on
responses from the MSN servers.

I'm sure it will work if a PHP expert picks this question up though.

Also, I had a quick look at the Net::AIM module which also connects
but the send message also fails.

===============
  use Net::AIM;

  $aim = new Net::AIM;
  $conn = $aim->newconn(Screenname   => 'AIMScreenname',
                        Password     => 'Password');
  $aim->start;

  $aim->send_im('AnotherScreenname', 'Message'); // Doesn't work??

===============

Regards,
errol.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy