Mattie

Untitled

Mar 5th, 2010
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.40 KB | None | 0 0
  1. ;;--Simple google interface.
  2. ;;--
  3. ;;--Performs calculation, time, and weather checks on text triggers.
  4. ;;--
  5. ;;--Bugs: Google sometimes doesn't provide bits of weather data, in which case the bot will just not retrieve ANY.
  6. ;;--
  7. ;;--Enjoy. ~ataraxia_
  8.  
  9.  
  10. ;;-- This section self explanatory.
  11. ;;-- Aliases you can call, channel popups.
  12.  
  13. alias no_wait {
  14.  
  15. hadd -m commands except. $+ # true
  16. msg # * Command flood protection disabled in this channel.
  17.  
  18. }
  19. alias cmd_wait {
  20.  
  21. if ($hget(commands,except. $+ #)) hdel commands except. $+ #
  22. msg # * Command flood protection enabled in this channel.
  23.  
  24. }
  25.  
  26. menu channel {
  27.  
  28. Google commands:
  29. .Disable flood protection:no_wait
  30. .Enable flood protection:cmd_wait
  31. .Disable in channel:hadd -m nocommands $chan true
  32. .Enable in channel:hdel nocommands $chan
  33.  
  34. }
  35.  
  36.  
  37.  
  38.  
  39. ;--Main text command.
  40. on *:TEXT:*:*:{
  41.  
  42. if ($hget(nocommands,$chan)) halt
  43.  
  44. if ($left($1,1) = !) {
  45.  
  46. ;--Get trigger.
  47. var %z $right($1,-1)
  48. if ($len(%z) > 1) halt
  49.  
  50. if (%z = l) {
  51.  
  52. if ($2) {
  53. writeini locations.ini people $nick $regsubex($2-,/([^\d\w])/ig,$chr(37) $+ $base($asc(\1),10,16,2))
  54. msg # * Stored location for $nick as: $2-
  55. halt
  56. }
  57.  
  58. else {
  59.  
  60. msg # * Currently stored location for $nick is: $regsubex($readini(locations.ini,n,people,$nick),/(\x25[0-9a-f]{2})/ig,$chr($base($remove(\A,$chr(37)),16,10)))
  61. halt
  62.  
  63. }
  64.  
  65. }
  66.  
  67. ;--Open appropriate socket name.
  68. if (%z isin twc) {
  69.  
  70. ;--Flood protections. (Basic - needs improvement.)
  71. if (!$hget(commands,except. $+ $chan)) {
  72. if ($hget(commands,$nick $+ . $+ $chan)) { .raw notice # $nick : $+ System usage disallowed. Try again in $hget(commands,$nick $+ . $+ $chan).unset seconds. | halt }
  73. hadd -mu30 commands $nick $+ . $+ $chan true
  74. }
  75.  
  76. if (!$2) && (!$readini(locations.ini,n,people,$nick)) {
  77.  
  78. raw notice # $nick : $+ Please input a valid location.
  79. halt
  80.  
  81. }
  82.  
  83. var %s g. $+ $replacex(%z,w,weather,t,time,c,calc) $+ . $+ $chan $+ . $+ $ticks
  84. sockopen %s google.com 80
  85.  
  86. ;--Replace any non-alphanumerics with HTTP-compliant hex strings. (%20 etc.)
  87. sockmark %s $readini(locations.ini,n,people,$nick)
  88. if ($2) sockmark %s $regsubex($2-,/([^\d\w])/ig,$chr(37) $+ $base($asc(\1),10,16,2))
  89.  
  90. .raw notice # $nick : $+ Fetching data for location: $regsubex($sock(%s).mark,/(\x25[0-9a-f]{2})/ig,$chr($base($remove(\1,$chr(37)),16,10))) $+ . This may take a moment..
  91. hadd -m commands %s $nick
  92.  
  93. }
  94.  
  95. }
  96. }
  97.  
  98.  
  99. on *:sockopen:g.*:{
  100.  
  101. if ($sockerr) {
  102. msg $gettok($sockname,3,46) * Sorry $hget(commands,$sockname) $+ , there has been a problem communicating with the server. Please try again in a moment.
  103. halt
  104. }
  105.  
  106. ;--Fetch correct page depending on sockname.
  107. if ($gettok($sockname,2,46) = weather) sockwrite -tn $sockname GET /search?hl=en&q=Weather%20in%20 $+ $sock($sockname).mark
  108.  
  109. if ($gettok($sockname,2,46) = time) sockwrite -tn $sockname GET /search?hl=en&q=Time%20in%20 $+ $sock($sockname).mark
  110.  
  111. if ($gettok($sockname,2,46) = calc) sockwrite -tn $sockname GET /search?hl=en&q= $+ $sock($sockname).mark
  112.  
  113. ;--Requisite headers. Connection keep-alive to stop Google from killing the socket before mIRC finishes reading.
  114. sockwrite -tn $sockname HOST: www.google.com
  115. sockwrite -tn $sockname CONNECTION: Keep-alive
  116. sockwrite -tn $sockname $str($crlf,2)
  117.  
  118. }
  119.  
  120.  
  121. on *:sockread:g.*:{
  122. ;--Sockread using a binvar - prevent the socket waiting forever for a CRLF by using -fn 4096
  123. sockread -fn 4096 &r
  124. while ($sockbr > 0) {
  125. var %r $bvar(&r, 1-).text
  126.  
  127. ;--Return results.
  128.  
  129. if ($window(@gout)) echo @gout %r
  130.  
  131. if ($gettok($sockname,2,46) = calc) {
  132.  
  133. if ($regex(%r,/<font size=\+1><b>(.+?)<.b>/ig)) {
  134. msg $gettok($sockname,3,46) * Calculation request by $hget(commands,$sockname) $+ : $remove($replace($utfencode($regsubex($regml(1),/(<\/?font.*?>)/ig,$null)),<sup>,^),</sup>)
  135. sockclose $sockname
  136. }
  137.  
  138. }
  139.  
  140. if ($gettok($sockname,2,46) = time) {
  141.  
  142. if ($regex(%r,/<b>Time</b>\sin\s<b>(.+?)</b>(.+?)\x3C/ig)) set -u5 %l. [ $+ [ $sockname ] ] $regml(1) $+ $remove($regml(2),</td>)
  143.  
  144. if ($regex(%r,/valign=.+?><b>(.+?)<.b>/ig)) {
  145. if (*Web* !iswm $regml(1)) {
  146. msg $gettok($sockname,3,46) * Time request by $hget(commands,$sockname) for: $utfencode(%l. [ $+ [ $sockname ] ] - $regsubex($regml(1),/(<\/?font.*?>)/ig,$null))
  147. sockclose $sockname
  148. }
  149. }
  150.  
  151. }
  152.  
  153. if ($gettok($sockname,2,46) = weather) {
  154.  
  155. if ($regex(%r,/>Weather</b>\sfor\s<b>(.+?)</ig)) set -u5 %l. [ $+ [ $sockname ] ] $regml(1)
  156.  
  157. if ($regex(%r,/font-size:140%.+>(.+?)</b>.+?Current.+?<b>(.+?)</b>.+?Wind:(.+?)<br>Humidity:(.+?)<.+/ig)) {
  158. msg $gettok($sockname,3,46) * Weather request by $hget(commands,$sockname) for: $utfencode(%l. [ $+ [ $sockname ] ] - Current Conditions: $regml(2) / Temperature: $regml(1) / Wind: $regml(3) / Humidity: $regml(4))
  159. sockclose $sockname
  160. }
  161.  
  162. }
  163.  
  164. ;--Continue loop reading.
  165.  
  166. if ($sock($sockname)) sockread -fn 4096 &r
  167.  
  168.  
  169. }
  170. }
  171.  
  172. ;--Only called if socket closes by itself, not if sockclose is triggered by information found.
  173. on *:sockclose:g.*:msg $gettok($sockname,3,46) * Sorry, $hget(commands,$sockname) $+ , no information was found for your $gettok($sockname,2,46) request.
Add Comment
Please, Sign In to add comment