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.

Code for botnet to run same script (Rev 1.4)

Support & discussion of released scripts, and announcements of new releases.
Post Reply
User avatar
incith
Master
Posts: 275
Joined: Sat Apr 23, 2005 2:16 am
Location: Canada

Code for botnet to run same script (Rev 1.4)

Post by incith »

Hard to describe what I mean in the topic. Basically, 2+ bots can all load the same script, and activate it, and only 1 of them will service the request. If that bot drops, the next one in line will start servicing the requests. I chose not to bind bot_disc by default as it will work better this way I think. Consider if a bot just gets .unlinked, only one will keep serving requests if bot_disc isn't called, until the other bot is truly gone (leaves the channel). If bot_disc is enabled, as soon as a bot leaves the partyline, a new bot will start serving requests, but if the old bot isn't actually timing out or disconnecting (just unlinked), then 2 bots will start serving requests. Hope that makes sense. Maybe a check in bot_disc, loop through all the channels the bot is on, and check for the disconnecting bot in them, if the bot is disconnecting + not found, then remove them?


Example of what this code does (visitant is the hub, leaftant and another_leaf are leafs):
(visitant, another_leaf, and leaftant are all in the same channel)

(10:32:18) <@incith> !layout http://incith.com
(10:32:18) <@visitant> URL: http://incith.com/ \\ TITLE: incith.com => main menu
(10:32:24) +|+ visitant has quit [Client Quit]

(10:32:33) <@incith> !layout http://incith.com
(10:32:35) <@another_leaf> URL: http://incith.com/ \\ TITLE: incith.com => main menu
(10:33:01) +|+ another_leaf has quit [Client Quit]

(10:33:03) <@incith> !layout http://incith.com
(10:33:05) <@leaftant> URL: http://incith.com/ \\ TITLE: incith.com => main menu
1.4: Apparently if botnet-nick does not exist in your conf file, it is not set until after everything is sourced, so I've accounted for this in the startup portion of the botnet code now. Heh, it also seems I forgot to paste a line in here in startup too.
1.3: removed the 'unbind' else area. Oops.
1.2: Found some bugs in 1.1, cleaned it up. Putlogs being sent when they shouldn't be and bots being added to the static array unnecessarily. Whether you choose to bind disc or not I leave up to the authors.

In your scripts startup, add the below. This initializes an array to remember certain things going on. I use [clock seconds] in order to decide which bot is 'first'.

Code: Select all

namespace eval incith::layout {
  variable botnet 1
  variable debug 0
  variable version {r79}

  global botnet-nick nick
  if {${botnet-nick} == ""} { set botnet-nick ${nick} }
  array set static {}
  if {${incith::layout::botnet} >= 1} {
    set static(botnet,${botnet-nick},time) [clock seconds]
  } else {
    set static(botnet,${botnet-nick},time) "noswarm"
  }
  if {![info exists static(botnet,bots)]} {
    set static(botnet,bots) ${botnet-nick}
  }
}
You will need to bind bot, link, and optionally disc. bind bot works by receiving a message starting with its 'bind', in this case "incith:layout". So any [putallbots "incith:layout *"] messages will be picked up by this.

Code: Select all

# bind the botnet binds
if {${incith::layout::botnet} >= 1} {
  bind bot - incith:layout incith::layout::bot_msg
  bind link - * incith::layout::bot_link
  # it really depends what works better for you, checking if
  # the bot is onchan or just making sure they are linked.
  # bind disc - * incith::layout::bot_disc
}
And add the actual procedures that we are binding to:

Code: Select all

namespace eval incith::layout {
  proc bot_msg {from cmd text} {
    upvar #0 incith::layout::static static
    if {${incith::layout::debug} >= 1} {
      putlog "${incith::layout::version} (botmsg): <${from}> ${cmd} ${text}"
    }

    # receiving a bots load time
    if {[string match "time ?*" $text]} {
      regexp -- {time (.*)} $text - time
      set static(botnet,${from},time) $time
      # make sure this bot is in our bot list
      if {![string match "*${from}*" $static(botnet,bots)]} {
        putlog "${incith::layout::version} (botnet): ${from} has joined the incith:layout swarm."
        append static(botnet,bots) ";${from}"
        regsub -all -- {;;} $static(botnet,bots) {;} static(botnet,bots)
        set static(botnet,bots) [string trimright $static(botnet,bots) {;}]
      }
    }
  }

  proc bot_link {bot hub} {
    global botnet-nick
    upvar #0 incith::layout::static static
    # send our time to the bots
    putallbots "incith:layout time $static(botnet,${botnet-nick},time)"
  }

  proc bot_disc {bot} {
    upvar #0 incith::layout::static static
    if {[string match "*${bot}*" $static(botnet,bots)]} {
      if {$static(botnet,${bot},time) != "noswarm"} {
        putlog "${incith::layout::version} (botnet): ${bot} has left the incith:layout swarm."
      }
    }
    # remove this bot from our bot list.  If they were merely unlinked
    # then this code will make it all fail.  They can be unlinked and still
    # serve seperately since the time will be stored, and they'd have to
    # leave the channel additionally before another bot takes over.
    regsub -all -- $bot $static(botnet,bots) {} static(botnet,bots)
    regsub -all -- {;;} $static(botnet,bots) {;} static(botnet,bots)
    set static(botnet,bots) [string trimright $static(botnet,bots) {;}]
    # remove their time?  If they just lost link, they might still be [onchan]
    # unset static(botnet,${bot},time)
  }
}
And then in your message handler (your proc receiving input), add something like the below (NOTE: This should only be for public (bind pub) messages. If a specific bot receives a private message, they should serve it as normal, as it cannot exactly pass off private messages to another bot. And that would be silly anyway.

Code: Select all

namespace eval incith::layout {
  proc some_public_procedure {nick uhand hand chan input} {
    global botnet-nick
    upvar #0 incith::layout::static static
    set input(where) $chan

    # botnet
    if {${incith::layout::botnet} >= 1 && $input(where) != "private"} {
      foreach bot [split $static(botnet,bots) ";"] {
        # skip ourselves, bots not on the input channel, and bots not participating
        if {${bot} == ${botnet-nick} || ![onchan ${bot} $input(where)] || $static(botnet,${bot},time) == "noswarm"} {
          continue
        # bots that load first will serve first.  change < to > to reverse.
        } elseif {$static(botnet,${bot},time) < $static(botnet,${botnet-nick},time)} {
          if {${incith::layout::debug} >= 1} {
            putlog "${incith::layout::version} (botnet): $bot loaded before me."
          }
          return
        # should 2 bots have the same time, set a new random time (this did happen in testing)
        } elseif {$static(botnet,${bot},time) == $static(botnet,${botnet-nick},time)} {
          if {${incith::layout::debug} >= 1} {
            putlog "${incith::layout::version} (botnet): $bot had the same load time as me, fixing."
          }
          set static(botnet,${botnet-nick},time) [expr [clock seconds] + int(rand()*60)+1]
          putallbots "incith:layout time $static(botnet,${botnet-nick},time)"
          return
        }
      }
      # looks like we're serving, make sure we keep the botnet up to date
      putallbots "incith:layout time $static(botnet,${botnet-nick},time)"
    }
  }
}
And I add this at the end of the script, before the 'putlog "loaded"' type line. This will send the time out to the botnet on a .rehash etc, in case you modify a bots config to be removed from the swarm etc.

Code: Select all

namespace eval incith::layout {
  putallbots "incith:layout time $static(botnet,${botnet-nick},time)"
}
Last edited by incith on Wed Jan 21, 2009 1:57 am, edited 9 times in total.
User avatar
wac
Halfop
Posts: 80
Joined: Sun Dec 10, 2006 1:22 am
Location: in my cardboard box

Post by wac »

interesting looking script, just thought I should point out that using two similar looking names is confusing, you should try something like hub1, leaf1, leaf2 and so on, even if that's not what they actually are, heck even bot1, bot2, bot3 would work, just a simple glance at what you wrote for your example is kinda confusing... good work on the script though...
I see j00!
User avatar
incith
Master
Posts: 275
Joined: Sat Apr 23, 2005 2:16 am
Location: Canada

Post by incith »

Fixed to be more readable, thanks. The above hasn't really changed at all, it does really work quite well at allowing only 1 bot to respond to a trigger at a time. Kind of like giving yourself "backup" bots, so if one goes down, you can still have your !triggers working on another bot.
User avatar
wac
Halfop
Posts: 80
Joined: Sun Dec 10, 2006 1:22 am
Location: in my cardboard box

Post by wac »

np, I hadn't tried it yet, but it looks interesting, I actually divided up the script s I had running onto several bots because it was causing the bot to become unresponsive, so it wouldn't really work for me to have them all together again, but I guess if I had several more they could do backup for the bot that does it mainly, but that'd be a bit much just to keep the script alive... :P
I see j00!
User avatar
incith
Master
Posts: 275
Joined: Sat Apr 23, 2005 2:16 am
Location: Canada

Post by incith »

Strange that your bot would get overloaded, on the one hand the above code should help in the sense that, if it gets an incoming message, and it is not the 'active' bot, it will just return.

More appropriate uses of the above code would be for channel security based scripts, so if one bot dies, another one can still have a go.

To be honest this code just came from sitting there going, "why can't my bots realise that another bot is going to serve this request?"... I wanted to fix that.
User avatar
wac
Halfop
Posts: 80
Joined: Sun Dec 10, 2006 1:22 am
Location: in my cardboard box

Post by wac »

Yeah makes sense

probably overloaded from the more intensive scripts like tvrage and an rss feed with several feeds...
I see j00!
User avatar
incith
Master
Posts: 275
Joined: Sat Apr 23, 2005 2:16 am
Location: Canada

Post by incith »

(not arguing or anything, just proposing uses for the code).. It could still be adapted for those scripts, e.g., if <leaf_bot> is not first or etc, just return, don't fetch any data or etc, don't run the timers, just return again.. ah well. :)

I am including it in my future releases, for whatever purpose, my scripts are/will be botnet compatible, lol.
User avatar
wac
Halfop
Posts: 80
Joined: Sun Dec 10, 2006 1:22 am
Location: in my cardboard box

Post by wac »

hehe didn't take it as arguing :D but yeah it would work for the tvrage script (I assume), but for rss not sure since it announces every so often, whenever the feed updates, so I'm not sure how that would fit in :D
I see j00!
User avatar
incith
Master
Posts: 275
Joined: Sat Apr 23, 2005 2:16 am
Location: Canada

Post by incith »

It's all the same.. it would check it's priority on the botnet, and decide whether or not to announce or check any rss feeds at all.

I'll post a modified version of (demonds rss script? do you use a different one?) in a bit, just going out at the moment.

Demonds RSS News 2.2 + botnet code:
http://incith.com/eggdrop/rssnews2.2-botnet.tcl

RSS Synd 0.5b1 + botnet code:
http://incith.com/eggdrop/rss-synd-05b1-botnet.tcl
Last edited by incith on Sat Jan 17, 2009 8:29 pm, edited 1 time in total.
User avatar
wac
Halfop
Posts: 80
Joined: Sun Dec 10, 2006 1:22 am
Location: in my cardboard box

Post by wac »

I see j00!
User avatar
incith
Master
Posts: 275
Joined: Sat Apr 23, 2005 2:16 am
Location: Canada

Post by incith »

Ok, I updated/fixed the botnet code some (rev1.2), and added it to rss-synd 0.5b1 as an example, untested but it should work (it loads at least :P)

Demonds RSS News 2.2 + botnet code:
http://incith.com/eggdrop/rssnews2.2-botnet.tcl

RSS Synd 0.5b1 + botnet code:
http://incith.com/eggdrop/rss-synd-05b1-botnet.tcl
Post Reply