/* PageICQ.hrx by Sigbjørn Skjæret
** $VER: PageICQ.hrx v1.0 (01.08.98)
**
** ICQ pager...
*/
options results
signal on syntax
signal on ioerr
signal on break_c

parse arg nick userhost channel public uin txt

p = ' ' || address() || ' ' || show('P',,)
henryport = pos(' HENRY.',p)

if henryport > 0 then henryport = word(substr(p,henryport+1),1)
else
do
 say 'No HENRY port found!'
 exit 10
end

address(henryport)

if(public) then receiver=channel
else receiver=nick

if(upper(uin)='HELP') then
do
 ver=word(sourceline(2),4)
 NOTICE receiver d2c(2)'PageICQ 'ver' by Sigbjørn Skjæret'd2c(2)
 NOTICE receiver d2c(2)'Usage:'d2c(2)' "PageICQ ['d2c(31)'<UIN> <message to send> 'd2c(31)'|'d2c(31)' Info <uin number> (<specific info (Name|EMail|Nick|Homepage)>) 'd2c(31)'|'d2c(31)' Search <nick>,<first name>,<last name>,<email> 'd2c(31)'|'d2c(31)' Help'd2c(31)']"'
 exit
end

if(uin='' | txt='') then
do
 NOTICE receiver 'No UIN and/or text entered...'
 exit
end

select
 when datatype(uin)='NUM' then call QueryICQ('PAGE 'uin' 'txt)
 when upper(uin)='INFO' then call QueryICQ('INFO 'word(txt,1)' 'word(txt,2))
 when upper(uin)='SEARCH' then call QueryICQ('SEARCH 'txt)
 otherwise
 do
  NOTICE receiver 'Unknown request, please try again or check "PageICQ Help" for info...'
 end
end
exit



/*** PROCEDURES ***/
QueryICQ: procedure expose henryport receiver nick userhost channel
 parse arg query data
 data=strip(data)

 cr=d2c(13)

 if ~open(sock,'TCP:wwp.mirabilis.com/80') then call Failed('Sorry, the ICQ-server is down, unable to send request!')

 select
  when query='PAGE' then
  do
   reql='from='URLEncode(nick)'&fromemail='URLEncode(userhost)'&subject='URLEncode(channel' IRC-msg')'&body='URLEncode(subword(data,2))'&to='word(data,1)
   call writeln(sock,'POST /scripts/WWPMsg.dll HTTP/1.0')
   call writeln(sock,'Content-Type: application/x-www-form-urlencoded')
   call writeln(sock,'Content-Length: 'length(reql))
   call writeln(sock,'')
   call writeln(sock,reql)

   do while ~eof(sock)
    buf=readln(sock)
    if(buf='		<b>Your message has been processed</b>.<br>'cr) then
    do
     address(henryport)
     NOTICE receiver 'Your message was successfully sent to ICQ#'word(data,1)
     exit
    end
   end
  end
  when query='INFO' then
  do
   parse var data uin spec .
   first='';last='';email='';handle='';home='';found=0
   bf='<b>';bl='</b>'

   call writeln(sock,'GET /'uin' HTTP/1.0')
   call writeln(sock,'')

   do while ~eof(sock)
    buf=readln(sock)
    if(pos('name="contactingme"',buf)>0) then
    do while ~eof(sock)
     buf=readln(sock)
     sf=pos(bf,buf);sl=pos(bl,buf);found=1

     if(sf>0 & sl>0 & sf<sl) then
     do
      start=sf+length(bf);stop=sl-start
      string=substr(buf,start,stop)

      if(pos('First Name:',buf)>0) then first=string
      if(pos('Last Name:',buf)>0) then last=string
      if(pos('E-Mail:',buf)>0) then email=string
      if(pos('Handle/Nickname:',buf)>0) then handle=string
      if(pos('Homepage:',buf)>0) then home=string
     end

     if(pos('</table>',buf)>0) then break
    end
   end

   select
    when upper(spec)='NAME' then info='ICQ#'uin' (Name): 'first' 'last
    when upper(spec)='EMAIL' then info='ICQ#'uin' (EMail): 'email
    when upper(spec)='NICK' then info='ICQ#'uin' (Nick): 'handle
    when upper(spec)='HOMEPAGE' then info='ICQ#'uin' (Homepage): 'home
    otherwise info='ICQ#'uin': 'first' 'last' ('handle'), 'email', 'home
   end

   if(found) then
   do
    address(henryport)
    NOTICE receiver info
   end
   else
   do
    address(henryport)
    NOTICE receiver 'The ICQ#'uin' was not found!'
   end

   exit
  end
  when query='SEARCH' then
  do
   parse var data handle ',' first ',' last ',' email .
   bf='">';bl='</';users=''

   reql='first='URLEncode(first)'&last='URLEncode(last)'&email='URLEncode(email)'&handle='URLEncode(handle)'&to=%25U'
   call writeln(sock,'POST /scripts/SearchWWP.dll HTTP/1.0')
   call writeln(sock,'Content-Type: application/x-www-form-urlencoded')
   call writeln(sock,'Content-Length: 'length(reql))
   call writeln(sock,'')
   call writeln(sock,reql)

   do while ~eof(sock)
    buf=readln(sock)
    info='';num=0

    if(buf='<!-- User Info -->		'cr) then
    do while ~eof(sock)
     buf=readln(sock)
     sf=pos(bf,buf);sl=pos(bl,buf)

     if(sf>0 & sl>0 & sf<sl) then
     do
      num=num+1
      start=sf+length(bf);stop=sl-start
      string=substr(buf,start,stop)
      select
       when num=1 then info='('string')'
       when num=2 then info=string' 'info
       when num=3 then info=info' <'string'>'
       when num=4 then info=substr(string,1,pos('@',string)-1)' 'info
       otherwise nop
      end
     end

     if(buf='<!-- -->'cr) then break
    end

    if(info~='') then
    do
     if(users~='') then
     do
      address(henryport)
      NOTICE receiver users' 'd2c(2)'|'d2c(2)' 'info
      users=''
     end
     else users=info
    end

    if(buf='<!-- Not Found -->'cr) then
    do
     address(henryport)
     NOTICE receiver 'No user(s) were found!'
     exit
    end
   end

   address(henryport)
   if(users~='') then NOTICE receiver users
   NOTICE receiver 'End of "'data'" search.'
   exit
  end
  otherwise nop
 end

 address(henryport)
 NOTICE receiver 'Something went wrong with your request...'
exit 10

URLEncode: procedure
 parse arg txt

 encstr=''
 txt=strip(txt)
 do i=1 to length(txt)
  c=substr(txt,i,1)

  if(c=' ') then encstr=encstr'+'
  else if(~datatype(c,'A')) then encstr=encstr'%'c2x(c)
  else encstr=encstr''c
 end
return encstr

Failed:
 parse arg errormsg
 address(henryport)
 NOTICE receiver errormsg
exit 10

Break_C:
exit

Syntax:
IOErr:
 error=errortext(rc);errmsg='PageICQ.hrx: Error in Line #'sigl' - "'error'", exiting...'
 address(henryport)
 LOGENTRY errmsg
 call Failed(errmsg)
exit
