Author Topic: Request: URL script  (Read 16241 times)

James Revan

  • Newbie
  • *
  • Posts: 32
Request: URL script
« on: February 11, 2008, 09:37:06 AM »
I've found a small module for XCHAT written in python, and i was wondering if anyone could rewrite that for IceChat. What the module does is it makes even very long url's redirected through the tinyurl.com and have the outcome printed as a message for the channel. My english is not too good, so it may not be too clear what i wanted to say, so here is an example:

Without:

<James_Revan> http://www.mapquest.com/maps/map.adp?ovi=1&mqmap.x=300&mqmap.y=75&mapdata=%252bKZmeiIh6N%252bIgpXRP3bylMaN0O4z8OOUkZWYe7NRH6ldDN96YFTIUmSH3Q6OzE5XVqcuc5zb%252fY5wy1MZwTnT2pu%252bNMjOjsHjvNlygTRMzqazPStrN%252f1YzA0oWEWLwkHdhVHeG9sG6cMrfXNJKHY6fML4o6Nb0SeQm75ET9jAjKelrmqBCNta%252bsKC9n8jslz%252fo188N4g3BvAJYuzx8J8r%252f1fPFWkPYg%252bT9Su5KoQ9YpNSj%252bmo0h0aEK%252bofj3f6vCP

With:

(the comand is: /tinyurl)

/tinyurl http://www.mapquest.com/maps/map.adp?ovi=1&mqmap.x=300&mqmap.y=75&mapdata=%252bKZmeiIh6N%252bIgpXRP3bylMaN0O4z8OOUkZWYe7NRH6ldDN96YFTIUmSH3Q6OzE5XVqcuc5zb%252fY5wy1MZwTnT2pu%252bNMjOjsHjvNlygTRMzqazPStrN%252f1YzA0oWEWLwkHdhVHeG9sG6cMrfXNJKHY6fML4o6Nb0SeQm75ET9jAjKelrmqBCNta%252bsKC9n8jslz%252fo188N4g3BvAJYuzx8J8r%252f1fPFWkPYg%252bT9Su5KoQ9YpNSj%252bmo0h0aEK%252bofj3f6vCP

outcome:

<James_Revan> http://tinyurl.com/6

This would help me a lot, so if this can be done...



This is the script:

__module_name__ = "pyTinyUrl"
__module_version__ = "0.1"
__module_description__ = "Make tinyurl from specified url and send it to current channel"

def onTinyUrl(word, word_eol, userdata):
   if len(word) < 2:
      print "you must specify a url eg, /tinyurl http://www.google.com/"
   else:
      # fetch tinyurl result
      sock = urllib2.urlopen("http://tinyurl.com/create.php?url=" + word[1])
      htmlSource = sock.read()
      sock.close()

      # parse out link
      match = re.search('<a href="(.*?)" target="_blank">Open in new window</a>', htmlSource)
      result = match.group(1)

      # display
      xchat.command("MSG " + xchat.get_info("channel") + " " + result)
   return xchat.EAT_ALL

xchat.hook_command("TINYURL", onTinyUrl, help="/TINYURL <url>")

Snerf

  • Administrator
  • Hero Member
  • *****
  • Posts: 1968
    • IceChat IRC Client
Re: Request: URL script
« Reply #1 on: February 11, 2008, 12:59:23 PM »
Open the IceChat Editor, and go to the Scripts Tab, and paste this in.
Save the Script

To run it, it will just be slightly different, as this is a VBS Function call.

/! tinyurl http://theverylong.url.name


Code: [Select]
Sub TinyUrl(SomeUrl)
strURL = "http://tinyurl.com/create.php?url=" & SomeUrl
Set objHTTP = CreateObject("MSXML2.XMLHTTP")
objHTTP.Open "GET", strURL, FALSE
objHTTP.Send
Response = objHTTP.ResponseText
findtarget= Instr(Response,"target=""_blank"">Open in new window")
Response = left(Response,findtarget - 2)
theUrl = mid(Response, instrrev(Response," ") + 7)
theUrl = left(theUrl,Len(theUrl)-1)
SendCommand "/say " & theUrl
set objHTTP = nothing
End Sub
The IceChat God

James Revan

  • Newbie
  • *
  • Posts: 32
Re: Request: URL script
« Reply #2 on: February 11, 2008, 10:07:54 PM »
Thanks Snerf, this is exactly what i wanted and works perfectly.

James Revan

  • Newbie
  • *
  • Posts: 32
Request: Data Base Script
« Reply #3 on: February 13, 2008, 08:10:49 AM »
I didn't want to open a new topic, so i put it here.

I could use another script, this time a data base script. What the script should do is gather definitions for different words. Examples are the best way to show what i want, so here is another one. I have an eggdrop bot with this kind of scripts, but on some networks it is forbitten to log in more then one user from same IP, and getting a shell account is not an option at this time.

<James_Revan> !db add WORD: DEFINITION (command only availble to me)
<James_Revan> Added to data base

<James_Revan> !db delword WORD (command only availble to me)
<James_Revan> WORD deleted from data base

<James_Revan> !db repair WORD DEFINITION (command only availble to me)
<James_Revan> Definition for WORD was changed

<James_Revan> !whatis WORD (command availble to everyone on the channel)
<James_Revan> WORD: DEFINITION


Would be also nice if the script wrote this to a seperate file, and worked only for a specified channel
« Last Edit: February 13, 2008, 08:12:35 AM by James Revan »

Morio

  • Global Moderator
  • Sr. Member
  • *****
  • Posts: 256
Re: Request: URL script
« Reply #4 on: February 13, 2008, 01:23:11 PM »
I think it might be easier to make a separate sub for adding the words something like

Code: [Select]
Sub Definition(action,word,definition)
If Definition = "add" then WriteInifile "definitions.ini","definitions",word,definition
If Definition = "remove" then WriteInifile "definitions.ini","definitions",word,""
If Definition = "repair" then WriteInifile "definitions.ini","definitions",word,definition
End Sub

then you could make an alias like /define:/! Define $1 | $2 | $3-

so to add a word you type /define add word definition
the 3rd word and the words after that will be the definition
to remove a word you type /define remove word

then for other users to access your definitions on a channel you need this:

Code: [Select]
Sub ONTEXT(Message,Channel,Nickname,Host,ServerNumber)
If Channel = "#specificchannel" then
  If Left(Message,8) = "!whatis " then
    If Len(Message) > 8 then
      word = Mid(Message,9)
      definition = GetInifile("definitions.ini","definitions",word,"")
      If Len(definition) > 0 then
        SendCommand "/msg " & Channel & " " & word & ": " & definition,ServerNumber
      Else
        SendCommand "/msg " & Channel & " word: " & word & ", not found in database",ServerNumber
      End If
    End If
  End If
End If
End Sub

this code won't trigger for yourself though

If you have any problems with the script, just ask. It's pretty late, and I didn't have time to test the script.
"I don't know what World War 3 will be fought with, but I know World War 4 with be fought with sticks and stones." - Albert Einstein

James Revan

  • Newbie
  • *
  • Posts: 32
Re: Request: URL script
« Reply #5 on: February 13, 2008, 02:42:07 PM »
Well.. I'm a complete newbie when it comes to scripting, just know where is what. So if you could step by step tell me what to do, would be nice...

Snerf

  • Administrator
  • Hero Member
  • *****
  • Posts: 1968
    • IceChat IRC Client
Re: Request: URL script
« Reply #6 on: February 13, 2008, 03:01:38 PM »
Should have put this into a new topic, but basically copy and paste what Morio posted into a new script file, and save it. Just change the name of the channel in on the ONTEXT part of it.
The IceChat God

James Revan

  • Newbie
  • *
  • Posts: 32
Re: Request: URL script
« Reply #7 on: February 13, 2008, 03:31:09 PM »
I get few errors with this script.

When i save it in IC 5

-- Script Engine Error:1041:Name redefined: Line #1 -- Sub Definition(action,word,definition)

and when i save it as a IC 7 no errors till i try to add a word

-- Script Engine Error:1025:Expected end of statement: Line #1 -- Define add WORD DEFINITION

I probably did something wrong, but this is what i get

Snerf

  • Administrator
  • Hero Member
  • *****
  • Posts: 1968
    • IceChat IRC Client
Re: Request: URL script
« Reply #8 on: February 13, 2008, 05:28:47 PM »
You have to save it as an IceChat 5 Script, as it is VBS.

The flaw with Morio's script is, he is using a Sub Name (definition) as a variable name as well.

Change "Sub Definition(action,word,definition)" to "Sub Define(action,word,definition)"
The IceChat God

James Revan

  • Newbie
  • *
  • Posts: 32
Re: Request: URL script
« Reply #9 on: February 14, 2008, 01:51:59 AM »
Hmm, i got it working, but there is still something wrong. I add a definition for a word "word" just to try the script out, but when i use the "!whatis" comand i get 3 messages
<+James_Revan> word: word, not found in database
<+James_Revan> word: word, not found in database
<+James_Revan> word: word, not found in database

Morio

  • Global Moderator
  • Sr. Member
  • *****
  • Posts: 256
Re: Request: URL script
« Reply #10 on: February 14, 2008, 03:43:42 AM »
It seems to have been too late when I wrote the script :D

Of course the first part should be:
Code: [Select]
Sub Define(action,word,definition)
If action = "add" then WriteInifile "definitions.ini","definitions",word,definition
If action = "remove" then WriteInifile "definitions.ini","definitions",word,""
If action = "repair" then WriteInifile "definitions.ini","definitions",word,definition

Also, are you sure you haven't loaded the script 3 times? That's the only explanation for getting the reply 3 times (at least I can't think of any else). I trired and with the fixes posted, it should work as it's supposed.
"I don't know what World War 3 will be fought with, but I know World War 4 with be fought with sticks and stones." - Albert Einstein

James Revan

  • Newbie
  • *
  • Posts: 32
Re: Request: URL script
« Reply #11 on: February 14, 2008, 09:30:57 AM »
Don't know why it gave me the 3 lines, but after unload/load it gives only one. And when i've pasted the code you just posted it works great. Could you make a simple flood protection for this script? Let's say 4 answers a minute or smth like that

Morio

  • Global Moderator
  • Sr. Member
  • *****
  • Posts: 256
Re: Request: URL script
« Reply #12 on: February 14, 2008, 06:12:36 PM »
Icechat has a built-in flood protection, just activate that
"I don't know what World War 3 will be fought with, but I know World War 4 with be fought with sticks and stones." - Albert Einstein

James Revan

  • Newbie
  • *
  • Posts: 32
Re: Request: URL script
« Reply #13 on: February 14, 2008, 08:20:34 PM »
Yes i know about the build-in flood protection, but i don't want to stop myself from typing. Just don't want for the script be abused, so i was thinking about a flood protection just for the script.

Snerf

  • Administrator
  • Hero Member
  • *****
  • Posts: 1968
    • IceChat IRC Client
Re: Request: URL script
« Reply #14 on: February 14, 2008, 11:40:18 PM »
Adding flood protection would be a lot of hassle.
The IceChat God