Skip to content

Tut_weather_part_2

Sven Roelse edited this page Aug 17, 2019 · 6 revisions

Let's make a weather add-on - Part 2

mIRC versions < 7.57.528 make it difficult to work with a new script file in the remote editor when PnP scripts are loaded. Seeing 7.57 is the last stable version, create a new file with the following content and call it WEATHER.PPA. Place it in %APPDATA%\mIRC\addons and load the weather addon via menubar > PnP > Addons > More...

; #= ppweather -rs
; ########################################
; Peace and Protection
; PnP Weather (standard addon)
; ########################################

#.ppa.info off
[addon]
name=Weather
group=Tutorial
popup=Weather
author=acvxqs
version=1.00
url=
email=
id=ppweather
ppver=4.22
config=
help=
unload=
vars=
vars2=
windows=
dialogs=weather
timers=
sockets=
hashcid=
hashpnp=
[files]
1=weather.ppa
[notes]
1=This addon allows you to retrieve weather information from OpenWeatherMap.org using command: /weather {city},{country code} or respond in channel to a !weather {city},{country code} trigger.
2=Please note that city name and country code are divided by comma, use ISO 3166 country codes.
[menu]
1=
[nicklist]
1=
[query]
1=
[channel]
1=
#.ppa.info end

on *:LOAD:{
  ; PnP check
  if (!$_ispnp) {
    echo 4 -ati2 *** This addon requires Peace and Protection by pai to use.
    echo 4 -ati2 *** You can download Peace and Protection at http://www.kristshell.net/pnp/
    .timer -mio 1 0 .unload -rs " $+ $script $+ "
    halt
  }
  ; Invalid load method check
  if (!$istok($_cfgx(addons,ids),$readini($script,n,addon,id),32)) {
    .timer -mio 1 0 .unload -rs " $+ $script $+ "
    dispa Unloading ' $+ $script $+ ' $chr(40) $+ addon is not properly loaded; use /addon to load it $+ $chr(41)
    halt
  }
}
dialog weather {
  title "Weather Central"
  size -1 -1 202 214
  option dbu
  icon script\pnp.ico

  box "Respond to trigger on networks:", 1, 5 5 95 90
  combo 2, 10 15 50 50, size
  button "Add", 3, 65 15 27 11, disable
  button "Remove", 4, 65 30 27 11, disable
  radio "Whitelist channels", 5, 10 70 60 10, group disable
  radio "Blacklist channels", 6, 10 80 60 10, disable

  box "Specify channels:", 7, 103 5 95 90
  combo 8, 108 15 50 50, size disable
  button "Add", 9, 163 15 27 11, disable
  button "Remove", 10, 163 30 27 11, disable

  box "Choose which information to display:", 11, 5 100 193 90
  check "City", 12, 10 110 50 10, disable
  check "Country", 13, 10 120 50 10, disable
  check "Weather short", 14, 10 130 50 10, disable
  check "Weather long", 15, 10 140 50 10, disable
  check "Humidity", 16, 10 150 50 10, disable

  check "Temperature", 17, 65 110 50 10, disable
  check "Temp. min.", 18, 65 120 50 10, disable
  check "Temp. max.", 19, 65 130 50 10, disable
  check "Pressure", 20, 65 140 50 10, disable
  check "Visibility", 21, 65 150 50 10, disable

  check "Wind speed", 22, 120 110 50 10, disable
  check "Wind degree", 23, 120 120 50 10, disable
  check "Sunrise", 24, 120 130 50 10, disable
  check "Sunset", 25, 120 140 50 10, disable
  check "Clouds", 26, 120 150 50 10, disable

  radio "Kelvin", 27, 10 175 50 10, group disable
  radio "Metric", 28, 65 175 50 10, disable
  radio "Imperial", 29, 120 175 50 10, disable
  text "Unit format:", 30, 10 165 37 8

  button "OK", 100, 5 195 40 12, ok
  button "Cancel", 101, 65 195 37 12, cancel
  button "API Key", 102, 120 195 37 12
}

Now we can continue to work from within the remote script editor. Please note I've already taken the liberty to create a dialog window. Take a look at it by running: /_dialog -am weather weather

Storing, modifying and retrieving data - `set

For $cid independent configuration information, use the `set command. This uses hash table pnp.config to store keys and values therein. PnP will save this hash table on EXIT in file $mircdirCONFIG\<profile>\config.ini. And PnP will load this file in hash table pnp.config on START. To see what's going on, it's a good idea to write yourself a hash table viewer or grab HASH.PPA from this repository: https://github.com/acvxqs/PnPAddons422

Examples:

Storing

`set weather.apikey 11111111111111

Retrieving

if ($hget(pnp.config,weather.apikey)) var %appid &APPID= $+ $v1

Modifying

`set weather.apikey 22222222222222

Deleting

`set weather.apikey

Storing, modifying and retrieving data - Use a dedicated hash table

We'll also want to use a dedicated hash table, but how to implement this? You'd have to take care of creating it and loading the data on START, as well as saving the data on EXIT For inspiration and ideas, let's have a look at %APPDATA%\mIRC\addons\EXTRAS.PPA

We can make do with the following:

; This will inject startup commands in PnP's main startup routine
; on START will also fire when the addon is loaded, so weather.setup can initialize accordingly
on *:START:{ _startup.add _weather.start Initializing weather addon }
; This will save the pnp.weather hash table to $_cfg(weather.dat)
on *:EXIT:{ weather.save }
alias _weather.start { weather.setup }
; We might modify the below later down the track, so as to make it easier to select from different providers
alias -l weather.setup {
  if ($hget(pnp.weather) == $null) weather.load
  if (!$hget(pnp.config,weather.apiurl)) `set weather.apiurl $weather.owm(apiurl)
  if (!$hget(pnp.config,weather.pchar)) `set weather.pchar $weather.owm(pchar)
  if (!$hget(pnp.config,weather.qstring)) `set weather.qstring $weather.owm(qstring)
  if (!$hget(pnp.config,weather.ustring)) `set weather.ustring $weather.owm(ustring)
  if (!$hget(pnp.config,weather.astring)) `set weather.astring $weather.owm(astring)
  if (!$hget(pnp.config,weather.mstring)) `set weather.mstring $weather.owm(mstring)
  if (!$hget(pnp.config,weather.istring)) `set weather.istring $weather.owm(istring)
  if (!$hget(pnp.config,weather.kstring)) `set weather.kstring $weather.owm(kstring)
  if (!$hget(pnp.config,weather.trigger)) `set weather.trigger !weather
}
alias -l weather.owm {
  if ($istok(apiurl pchar qstring ustring astring mstring istring kstring,$1,32)) goto $1
  else return $null
  
  :apiurl | return http://api.openweathermap.org/data/2.5/weather
  :pchar | return ?
  :qstring | return q=
  :ustring | return &units=
  :astring | return &appid=
  :mstring | return metric
  :istring | return imperial
  :kstring | return kelvin
}
alias -l weather.load {
  if ($hget(pnp.weather)) hdel -w pnp.weather *
  else hmake pnp.weather 30
  var %file $_cfg(weather.dat)
  if ($isfile(%file)) hload pnp.weather " $+ %file $+ "
}
alias -l weather.save {
  var %file $_cfg(weather.dat)
  if ($hget(pnp.weather)) {
    hsave pnp.weather " $+ %file $+ "
    if ($1) hfree pnp.weather
  }
}
; remember the `set command that works on hash table pnp.config? We can use that alias as well (L 650 - aliases.mrc) and modify it to work on hash table pnp.weather and file $_cfg(weather.dat) ...
; `weather varname [value]
; varname should not include the %`
alias `weather {
  if ($hget(pnp.weather,$1) == $2-) return
  if ($2- == $null) {
    hdel pnp.weather $1
    write -ds [ $+ [ $1 ] ] config\ $+ $hget(pnp,user) $+ \weather.dat
    _broadcastp hdel pnp.weather $1
  }
  else {
    hadd pnp.weather $1 $2-
    write -s [ $+ [ $1 ] ] config\ $+ $hget(pnp,user) $+ \weather.dat $1-
    _broadcastp _`weather $1
  }
}
alias _`weather hadd pnp.config $1 $read(config\ $+ $hget(pnp,user) $+ \weather.dat,nts,$1)
Clone this wiki locally