view ruoka.tcl @ 424:825cac46b1cb

Cosmetic / stray trailing whitespace cleanup.
author Matti Hamalainen <ccr@tnsp.org>
date Sun, 08 Jan 2017 03:55:55 +0200
parents 44593f1fb916
children 85fe3bc36307
line wrap: on
line source

##########################################################################
#
# RuokaLista v1.1 by Matti 'ccr' Hamalainen <ccr@tnsp.org>
# (C) Copyright 2004-2010 Tecnic Software productions (TNSP)
#
# Requires hae_ruoka.tcl to be run as a cronjob, for example
# 15 * * * *     /absolute/path/to/hae_ruoka.tcl
#
# This script is freely distributable under GNU GPL (version 2) license.
#
##########################################################################

# Vakioravintola, jos muuta ei annettu/asetettu
set ruoka_restaurant "OAMK"

# Datatiedosto, oltava sama kuin hae_ruoka.tcl:n vastaava asetus
set ruoka_datafile "/home/niinuska/bot/data.ruoka"

# Kellonaika, jolloin default-päivä vaihtuu seuraavaksi vuorokaudeksi
# -1 = Ei vaihdeta
# esim. 15 = klo 15 jälkeen näytetään vakiona seuraavan päivän ruokalista
set ruoka_threshold 15

# Yleiset asetukset
set ruoka_logmsg 1
set ruoka_preferredmsg "PRIVMSG"


##########################################################################
# No need to look below this line
##########################################################################
set ruoka_name "RuokaLista"
set ruoka_version "1.1"

### Binding initializations
bind pub - !ruoka ruoka_get_pub
bind msg - !ruoka ruoka_get_msg
bind pub - !ruokaset ruoka_set_pub

### Initialization messages
set ruoka_message "$ruoka_name v$ruoka_version by ccr/TNSP"
putlog "$ruoka_message"

set ruoka_days {Sunnuntai Maanantai Tiistai Keskiviikko Torstai Perjantai Lauantai}

#-------------------------------------------------------------------------
proc ruoka_log {arg} {
  global ruoka_logmsg ruoka_name
  if {$ruoka_logmsg != 0} {
    putlog "$ruoka_name: $arg"
  }
}


proc ruoka_msg {apublic anick achan amsg} {
  global ruoka_preferredmsg
  if {$apublic == 1} {
    putserv "$ruoka_preferredmsg $achan :$amsg"
  } else {
    putserv "$ruoka_preferredmsg $anick :$amsg"
  }
}


proc ruoka_get_restaurants {urestaurants} {
  global ruoka_datafile
  upvar $urestaurants restaurants

  if {![catch {set ufile [open "$ruoka_datafile" r 0600]} uerrmsg]} {
    while {![eof $ufile]} {
      gets $ufile uline
      set urec [split $uline "½"]
      if {[llength $urec] == 4} {
        set restaurants([lindex $urec 0]) 1
      }
    }
  }
}


proc ruoka_match_day {umatch} {
  global ruoka_days
  if {$umatch == "" || $umatch == "{}"} { return "" }
  set udate ""
  foreach i $ruoka_days {
    if {[string match "$umatch*" [string tolower $i]]} { set udate $i }
  }
  return $udate
}


proc ruoka_match_restaurant {urestaurants umatch} {
  upvar $urestaurants restaurants
  if {$umatch == "" || $umatch == "{}"} { return "" }
  foreach {nimi arvo} [array get restaurants] {
    if {[string match "$umatch*" [string tolower $nimi]]} {
      return $nimi
    }
  }
  return ""
}


#-------------------------------------------------------------------------
proc ruoka_get {unick uhand uchan uargs upublic} {
  global ruoka_restaurant ruoka_datafile ruoka_days ruoka_threshold

  # Get list of restaurants
  ruoka_get_restaurants restaurants

  # Check and handle arguments
  set ulist [split [string tolower $uargs] " "]
  set rarg1 [lindex $ulist 0]
  set rarg2 [lindex $ulist 1]

  if {$rarg1 == "?" || $rarg1 == "help"} {
    set tmp [join [array names restaurants] ", "]
    ruoka_msg $upublic $unick $uchan "Käyttö: <!>ruoka \[ravintola\] \[päivä\]"
    ruoka_msg $upublic $unick $uchan "Huom! Jos päivää ei anneta, tiedot haetaan tältä päivältä."
    ruoka_msg $upublic $unick $uchan "Käyttö: <!>ruokaset <ravintola>"
    ruoka_msg $upublic $unick $uchan "Jossa ravintola on yksi seuraavista: $tmp"
    return 0
  }

  # Try to parse arguments, if any
  set urestaurant ""
  set udate ""
  if {$rarg1 != ""} {
    set udate [ruoka_match_day $rarg1]
    if {$udate == ""} {
      set udate [ruoka_match_day $rarg2]
      set urestaurant [ruoka_match_restaurant restaurants $rarg1]
    } else {
      set urestaurant [ruoka_match_restaurant restaurants $rarg2]
    }
  }

  # Default date to today
  if {$udate == ""} {
    set sstamp [unixtime]

    # Check if threshold setting is active and act accordingly
    if {$ruoka_threshold > 0 && [clock format $sstamp -format "%H"] > $ruoka_threshold} {
      set sstamp [expr $sstamp + 60*60*(24 - $ruoka_threshold)+30]
    }

    set udate [lindex $ruoka_days [clock format $sstamp -format "%w"]]
  }

  # If no restaurant given, try to get one from user settings
  if {$urestaurant == ""} {
    set urestaurant [getuser $uhand XTRA "restaurant"]
    # If still no known/set restaurant, use global default
    if {$urestaurant == ""} {
      set urestaurant $ruoka_restaurant
    }
  }

  # Read datafile
  if {![catch {set ufile [open "$ruoka_datafile" r 0600]} uerrmsg]} {
    set found 0
    while {![eof $ufile]} {
      gets $ufile uline
      set urec [split $uline "½"]
      if {[llength $urec] == 4 && [lindex $urec 1] == $udate && [lindex $urec 0] == $urestaurant} {
        ruoka_msg $upublic $unick $uchan "\002[lindex $urec 0]\002, [lindex $urec 1] \002[lindex $urec 2]\002: [lindex $urec 3]"
        set found 1
        break
      }
    }
    close $ufile
    if {$found == 0} {
      ruoka_msg $upublic $unick $uchan "Ei tietoja ($udate, $urestaurant)"
    }
  } else {
    ruoka_log "Could not open datafile: $uerrmsg"
  }
}



#-------------------------------------------------------------------------
proc ruoka_get_pub {unick uhost uhand uchan uargs} {
  ruoka_get $unick $uhand $uchan $uargs 1
  return 0
}

#-------------------------------------------------------------------------
proc ruoka_get_msg {unick uhost uhand uargs} {
  ruoka_get $unick $uhand "PRIV" $uargs 0
  return 0
}

#-------------------------------------------------------------------------
proc ruoka_set_pub {unick uhost uhand uchan uargs} {
  array unset restaurants
  ruoka_get_restaurants restaurants
  set rav ""
  if {$uargs != "{}" && $uargs != ""} {
    set rav [ruoka_match_restaurant restaurants $uargs]
  }
  if {$rav != ""} {
    ruoka_msg 1 $unick $uchan "Vakioravintolaksi asetettu: $rav"
    setuser $uhand XTRA "restaurant" "$rav"
  } else {
    set tmp [join [array names restaurants] ", "]
    ruoka_msg 1 $unick $uchan "Ravintolan oltava yksi seuraavista: $tmp"
  }
  return 0
}

# end of script