This is the new home of the egghelp.org community forum.
All data has been migrated (including user logins/passwords) to a new phpBB version.


For more information, see this announcement post. Click the X in the top right-corner of this box to dismiss this message.

Tcl script with constant database check

Help for those learning Tcl or writing their own scripts.
Post Reply
s
seeb
Voice
Posts: 6
Joined: Thu Aug 14, 2008 5:09 pm

Tcl script with constant database check

Post by seeb »

hi all,
I just installed my first eggdrop bot. I never used tcl and it's some time ago that I programmed something in C.
I want to write a small script that checks my mysql database every minute and prints new infos to the channel. is that possible? does anything like this already exist? i found a tcl script which writes data into the DB, but how can I repeat such actions? Do I need a module or is a tcl script enough?
s
seeb
Voice
Posts: 6
Joined: Thu Aug 14, 2008 5:09 pm

Post by seeb »

Well, I found some parts and mixed them together. What do you think of this? How can I start it? How does my bot to know use this script? I hope I'll find out before wasting your time. And where can I get the libmysqltcl.so?


Code: Select all

# MySQL hostname
set db(host) "localhost"

# MySQL user
set db(id) "user"

# MySQL password
set db(pass) "pass"

# MySQL database
set db(dbase) "database"

# load the mysqltcl interface
load PATH/TO/libmysqltcl.so

bind time - "* * * * *" news_post

set db_handle [mysqlconnect -host $db(host) -user $db(id) -password $db(pass) -db $db(dbase)]

set news_version "1.0"
setudef flag newsengine

# --------------------------------------------------------------------
# news_post
#  jede Minute die aktuellen Nachrichten ausspucken
# --------------------------------------------------------------------
proc news_post { } {
  global db_handle news_noflags


    set sql "SELECT text FROM geschichte ORDER BY geschichte_id desc limit 1"
    #putloglev d * "NewsEngine: executing $sql"

    set result [mysqlquery $db_handle $sql]

    if {[set row [mysqlnext $result]] != ""} {
      set text [lindex $row 0]
      puthelp "PRIVMSG #elygor : $text"
    }
    else {
      puthelp "PRIVMSG $channel :Es ist nichts passiert!"
    }
    mysqlendquery $result
}
[/quote][/code]
Last edited by seeb on Thu Aug 14, 2008 5:50 pm, edited 1 time in total.
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

Tcl has no native support for mysql. There is, however, a package (mysqltcl) available for mysql connectivity. This would be loaded as a tcl package (or tcl module, not eggdrop module), and provides a complete API to the tcl environment. There is thus no need to develop your project as a module just for the mysql-support.

Most, if not all, that takes place within eggdrop is event-driven, so you'll most likely want some hook to trigger your check on a minutely basis. The time binding should do the trick (see doc/tcl-commands.doc for syntax and description). Roughly however, every minute it checks the current time against a given mask. If it matches, the associated code is evaluated.

I can't think of any specific script that does what you ask for at the moment, although they probably do exist. Since you are new to tcl, I'd suggest starting with a few simple projects just to get accustomed to tcl and it's variable- and namespaces, aswell as eggdrop's extensions.
NML_375
s
seeb
Voice
Posts: 6
Joined: Thu Aug 14, 2008 5:09 pm

Post by seeb »

thx for your reply, I guess we wrote simultaniously
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

Yup.
As for your posted code, I cannot see any direct flaws in it.
One remark however:

Code: Select all

load PATH/TO/libmysqltcl.so
This is not the preferred way of loading mysqltcl, instead, assuming it has been properly installed, use something like this:

Code: Select all

package require mysqltcl
seeb wrote:And where can I get the libmysqltcl.so?
Link is in my first post, but I'll repeat it here aswell:
http://www.xdobry.de/mysqltcl/
NML_375
s
seeb
Voice
Posts: 6
Joined: Thu Aug 14, 2008 5:09 pm

Post by seeb »

Well, I installed the lib and I changed the tcl script? How can I start it?
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

add a source command to your config-file, and restart your eggdrop:

Code: Select all

source scripts/yourscript.tcl
NML_375
s
seeb
Voice
Posts: 6
Joined: Thu Aug 14, 2008 5:09 pm

Post by seeb »

i did so, but nothing happens. i also added
bind pub "-|-" !news news_post
but when i say !news in channel or private chat nothing happens, too. how can i debug whats going wrong?
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

Best way is usually to connect to the dcc-chat partyline, as you can setup your console-settings to include tcl errors and other interesting information.

There is however, one flaw in the script that I overlooked. The proc (function) named news_post has an empty argument list, yet the time binding will try to call it with 5 parameters (minute, hour, day, month, and year). Thus there will be a mismatch... The same will be true for your pub-binding, which will call the function with 5 parameters (nick, host, handle, channel, text).

Code: Select all

#change this:
proc news_post { } {

#into this:
proc news_post {minute hour day month year} {
Although you will get away with it in this script, it is generally a bad idea to use the same function with multiple bindings of different kind, as different kinds of bindings provide different information as parameters, and in many cases, not the same number of parameters.
NML_375
s
seeb
Voice
Posts: 6
Joined: Thu Aug 14, 2008 5:09 pm

Post by seeb »

yeah it works thx a lot :)

here the script fpr those who want to try it, too

Code: Select all


 MySQL hostname
set db(host) "localhost"

# MySQL user
set db(id) "user"

# MySQL password
set db(pass) "pass"

# MySQL database
set db(dbase) "dbase"

# load the mysqltcl interface
package require mysqltcl

bind time - "* * * * *" news_post

set db_handle [mysqlconnect -host $db(host) -user $db(id) -password $db(pass) -db $db(dbase)]

set news_version "1.0"
setudef flag newsengine

# --------------------------------------------------------------------
# news_post
#  jede Minute die aktuellen Nachrichten ausspucken
# --------------------------------------------------------------------
proc news_post {minute hour day month year} {
  global db_handle news_noflags


    set sql "SELECT text FROM geschichte ORDER BY geschichte_id desc limit 1"
    #putloglev d * "NewsEngine: executing $sql"

    set result [mysqlquery $db_handle $sql]

    if {[set row [mysqlnext $result]] != ""} {
      set text [lindex $row 0]
      puthelp "PRIVMSG #elygor : $text"
    }
    else {
      puthelp "PRIVMSG $channel :Es ist nichts passiert!"
    }
    mysqlendquery $result
}
Post Reply