# MNET (Message Network) - Networked Broadcasting of Messages over IRC for Communities
# Copyright (C) 2004 Paul-Dieter Klumpp
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

set configs_file "scripts/mnet_conf.tcl"

#your notes:
# - Requirements: 
#      -> eggdrop bot  version >= 1.6.17
#      -> using TCL  version >= 8.3
#      -> TCLLib  version >= 1.6
#      -> TCLXML version >= 2.6

#my notes:
# ?- aliasing of channels!
# ?- alternative configs! backupconf..
# ?- xml:dom!
# ?- better http get implementation!
# ?- korrekte Statistiken der User usw?
# ?- wordbanning?


# Now, hands away pls :) except you know what you are doing.
######
# we need a nice tcl version
package require Tcl 8.3
# it's an eggdrop script, right? :)
package require eggdrop 1.6
# we need the tclxml package.. at least 2.6 i think
package require xml 2.6
# tclxml needs uri of tcllib.. at least 1.1 i think
package require uri 1.1



############ XML CONFIGURATION PARSING FOLLOWS!! WTF! :) #########
global mnet_networks mnet_bots mnet_channels mnet_freqs mnet_bans \
  mnet_colors mnet_colors_optional \
  mnet_reached_users mnet_reached_chans \
  mnet_http_config mnet_local_config \
  mnet_histories \
  mnet_netconfig_timer \
  currentNetworkDesc currentNetworkSig \
  startNetworkInstance \
  currentBot currentOwner \
  startBotInstance \
  currentFreqName currentFreqPrefix currentFreqHelpmsg currentFreqDesc \
  startFrequencyInstance \
  botnet-nick

# list
set mnet_local_config ""
unset mnet_local_config


# list
set mnet_http_config ""
unset mnet_http_config

set mnet_cfg_reload_interval 0
# list
#set mnet_freqs_onoff "lala lala"
#unset mnet_freqs_onoff


proc mnet:clean_netconfig {} {
	global mnet_bots mnet_channels mnet_freqs mnet_bans mnet_colors mnet_networks
	
	# secure deletion of all config-arrays..
	if {[array exists mnet_bots]} {
		array unset mnet_bots
	}
	if {[array exists mnet_channels]} {
		array unset mnet_channels
	}
	if {[array exists mnet_freqs]} {
		array unset mnet_freqs
	}
	if {[array exists mnet_bans]} {
		array unset mnet_bans
	}
	#if {[array exists mnet_freqs_onoff]} {
	#	array unset mnet_freqs_onoff
	#}
	if {[array exists mnet_colors]} {
		array unset mnet_colors
	}
	if {[array exists mnet_colors_optional]} {
		array unset mnet_colors_optional
	}
	# list
	set mnet_networks ""
	unset mnet_networks

	# list
	set mnet_freqs "lala lala"
	unset mnet_freqs

	set mnet_bans "lala lala"
	unset mnet_bans

	# list FIXME (this kills the history upon every .mnet_reload ... :( .. not good)
	if {[array exists mnet_histories]} {
		array unset mnet_histories
	}
	set mnet_histories ""
	unset mnet_histories
}

set p [xml::parser \
  -elementstartcommand xmlElementStart \
  -elementendcommand xmlElementEnd \
  -defaultcommand xmlDefaultCmd \
  -characterdatacommand xmlCdata \
]
#  -errorcommand xmlHandleError



proc mnet:initvars {} {
	global currentNetworkDesc currentNetworkSig \
	  startNetworkInstance \
	  currentBot currentOwner \
	  startBotInstance \
	  startFrequencyInstance \
	  mnet_networks mnet_freqs mnet_bans mnet_networks_by_prefix \
	  botnet-nick
	  
	  
	  
	set currentNetwork ""
	set currentSig ""
	set startNetworkInstance ""
	set currentBot ""
	set currentOwner ""
	set startBotInstance ""
	set startFrequencyInstance ""
}
mnet:initvars
  


proc xmlElementStart {tagname attlist args} {
	global mnet_networks mnet_bots mnet_channels mnet_freqs mnet_bans \
	  mnet_colors mnet_colors_optional \
	  mnet_freqs_onoff \
	  currentNetworkDesc currentNetworkSig \
	  startNetworkInstance \
	  currentBot currentOwner \
	  startBotInstance \
	  currentFreqName currentFreqPrefix currentFreqHelpmsg currentFreqDesc \
	  startFrequencyInstance \
	  botnet-nick

	
	set proper_botnick [mnet:proper_botnick ${botnet-nick}]
	  
#	putlog "mnet: attr: '$attlist' arg: '$args'"

	# getting NETWORK NAME
	if {"$tagname" == "mnet"} {
		set currentNetworkDesc ""
		set currentNetworkSig ""
		foreach {key value} $attlist {
			if {"$key" == "sig"} {
				set currentNetworkSig $value
			}
			if {"$key" == "desc"} {
				set currentNetworkDesc $value
			}
			if {"$currentNetworkSig" != ""} {
				if {"$currentNetworkDesc" != ""} {
					putlog "yo: $currentNetworkSig $currentNetworkDesc"
					lappend mnet_networks "$currentNetworkSig" "$currentNetworkDesc"
					set startNetworkInstance 1
				}
			}

		}
	}

	# getting botinfos...
	if {"$startNetworkInstance" == "1"} {
		if {"$tagname" == "bot"} {
			set currentBot ""
			set currentOwner ""
			foreach {key value} $attlist {
				if {"$key" == "nick"} {
					set currentBot [mnet:proper_botnick $value]
				}
				if {"$key" == "owner"} {
					set currentOwner $value
				}
			}
			if {"$currentBot" != ""} {
				if {"$currentOwner" != ""} {
					# currentbot auf 9 zeichen runter.. lowercase
					# botnick ebenso
					
					lappend mnet_bots($currentNetworkSig) "$currentBot"
					putlog "mnet: cfg: reached botcfg for: $currentBot - and my name is: $proper_botnick"
					if {"$currentBot" == "$proper_botnick"} {
						putlog "mnet: Oh yes, I, ${botnet-nick}, am listed for the network $currentNetworkSig by using '$currentBot'!"
						set startBotInstance 1
					}
				}
			}
		} 
		# end of $tagname == "bot"

		if {"$startBotInstance" == "1"} {
			if {"$tagname" == "ch"} {
				foreach {key value} $attlist {
					if {"$key" == "name"} {
						putlog "test: ${value}"
						lappend mnet_channels($currentNetworkSig,$currentBot) [string tolower ${value}]
						putlog "mnet: cfg: my channels for $currentNetworkSig $mnet_channels($currentNetworkSig,$currentBot)"

					}
				}
			}
			# end of $tagname == "ch"
		}
		# end of a bot...


		if {"$tagname" == "frequency"} {
			set currentFreqName ""
			set currentFreqPrefix ""
			set currentFreqHelpmsg ""
			set currentFreqDesc ""
			set currentDelay "120"
			foreach {key value} $attlist {
				if {"$key" == "name"} {
					set currentFreqName $value
				}
				if {"$key" == "prefix"} {
					set currentFreqPrefix $value
				}

				if {"$key" == "helpmsg"} {
					set currentFreqHelpmsg $value
				}
				if {"$key" == "desc"} {
					set currentFreqDesc $value
				}
				if {"$key" == "delay"} {
					set currentDelay $value
				}
			}
			if {"$currentFreqName" != ""} {
				if {"$currentFreqPrefix" != ""} {
					if {"$currentFreqHelpmsg" != ""} {
						if {"$currentFreqDesc" != ""} {

							
							putlog "mnet: i, ${botnet-nick}, serve the frequency: $currentFreqName ($currentFreqPrefix) of $currentNetworkSig!"
							set startFrequencyInstance 1
							lappend mnet_freqs($currentNetworkSig) "$currentNetworkSig" "$currentFreqName" "$currentFreqPrefix" "$currentFreqHelpmsg" "$currentFreqDesc" "$currentDelay"
						

						}
					}
				}
			}
			# end of: alle attribs vorhanden fr eine valide frequenz

		}
		# end of $tagname == "frequency"

		# in element Frequency, there are bans as children.
		if {"$startFrequencyInstance" == "1"} {
			if {"$tagname" == "ban"} {
				set currentBanHostmask ""
				set currentBanReason ""
				foreach {key value} $attlist {
					if {"$key" == "hostmask"} {
						set currentBanHostmask $value
					}
					if {"$key" == "reason"} {
						set currentBanReason $value
					}
				}
				if {"$currentBanHostmask" != ""} {
					if {"$currentBanReason" != ""} {
						putlog "mnet: adding ban to frequency: $currentNetworkSig, $currentFreqPrefix, $currentBanHostmask, reason: $currentBanReason"
						lappend mnet_bans($currentNetworkSig,$currentFreqPrefix) "$currentBanHostmask" "$currentBanReason"
					}
				}
			}
		}


		if {"$tagname" == "color"} {
			set currentColor ""
			set currentColorOptional ""
			foreach {key value} $attlist {
				if {"$key" == "id"} {
					set currentColor $value
					putlog "mnet: gone into colors! $value"
				}
				if {"$key" == "optional"} {
					set currentColorOptional $value
					putlog "mnet: color optional? $value"
				}
			}
			
			if {"$currentColor" != ""} {
				set mnet_colors($currentNetworkSig) $currentColor
				putlog "mnet: the color-id for $currentNetworkSig is: $currentColor"
			} else {
				# make NO color if no color-id or tag has been given:
				set mnet_colors($currentNetworkSig) none
			}
			if {"$currentColorOptional" != ""} {
				set mnet_colors_optional($currentNetworkSig) $currentColorOptional
				putlog "mnet: the color is optional: $currentColorOptional"
			} else {
				set mnet_colors_optional($currentNetworkSig) 0
				putlog "mnet: the color is NOT optional and will be forced." 
			}

		}

		

	}
	
}

proc xmlElementEnd {tagname} {
	global mnet_networks mnet_bots mnet_channels \
	  currentNetworkDesc currentNetworkSig \
	  startNetworkInstance \
	  startBotInstance \
	  startFrequencyInstance \
	  currentBot currentOwner
	  
	if {"$tagname" == "mnet"} {
		set startNetworkInstance 0
		set currentNetworkDesc ""
		set currentNetworkSig ""
	}
	if {"$tagname" == "bot"} {
		set startBotInstance 0
	}
	if {"$tagname" == "frequency"} {
		set startFrequencyInstance 0
	}
}


proc xmlCdata {data} {
#	putlog "mnet: data found: $data"
	return 1
}

proc xmlHandleError {errorcode errormsg} {
	putlog "mnet: some error occured: errcode $errorcode: $errormsg"
}

proc xmlDefaultCmd {data} {
	putlog "mnet: i don't know this element: $data"
	return 1
}
##################################################################


## some internal variables for messaging and stuff :) not your business after all.
set mnet(version) "mnet 1.010conffile"

set mnet_max_history 8

set mnet(reached) "0"
set mnet(reached_channels) "0"
set mnet_reached_users(dummy) "0"
set mnet_reached_chans(dummy) "0"
##





proc mnet:get_data {n} {
	if {[string range $n 0 0] == "\{"} {
		set n "\\$n"
	}
	return $n
}

proc mnet:proper_botnick {botnick} {
	
	set temp [string tolower $botnick]
	# abfrage ob zu lang, dann fixen
#	putlog "mnet: $botnick lowercase: $temp"
	if {[string length $botnick] > 9} {
		set temp [string range $temp 0 8 ]
#		putlog "mnet: botnickname $botnick too long: capping to: $temp"
	}
	return $temp

}

proc mnet:proper_channelname {channelname} {
	# channel names ARE NOT and SHOULD NOT BE CASE SENSITIVE!
	set temp [string tolower $channelname]
#	putlog "mnet: $channelname lowercase: $temp"

	return $temp
}


proc mnet:put_local_netmessage {network name chan rec_nickname rec_channel rec_text} {
	global mnet_colors mnet_colors_optional mnet_colors_onoff
	
	# validating incoming vars..
	set chan [mnet:proper_channelname $chan]

	
	if {$mnet_colors($network) == "none"} {
	# if no color id was found...
		set color_string ""
	} else {
	# if there was a color id...
	
		if {$mnet_colors_optional($network) == 1} {
			# check for the channel if activated..
			if {$mnet_colors_onoff($name,$chan) == 1} {
				set color_string "\003$mnet_colors($network)"
				set bold_string "\002"
				set bold_string_end "\002"
			} else {
				set color_string ""
				set bold_string ""
				set bold_string_end ""
			}
		
		} else {
			# if not optional then force colors.. whatever :)
			set color_string "\003$mnet_colors($network)"
			set bold_string "\002"
			set bold_string_end "\002"
		}
		
	}

	putserv "PRIVMSG $chan :${bold_string}$name${bold_string_end} ${color_string}$rec_nickname - ${rec_channel} : $rec_text"
	putlog "mnet: + local message from $rec_nickname @ $rec_channel to $chan : $rec_text"

}


proc mnet:put_local_msg {chan text} {
	global mnet_colors mnet_freqs_onoff

	putserv "PRIVMSG $chan :$text" -next
	putlog "mnet: + normal message: ${chan} => '$text'"

}


proc get_httpfile {host file port} {
	global botnet-nick

	if {[catch {set sock [socket $host $port] } sockerror]} {
		putlog "mnet: error: $sockerror"
		return 0 
	} else {
		puts $sock "GET $file HTTP/1.0"
		puts $sock "User-Agent: Mozilla/5.0 (compatible; ${botnet-nick}; mnet.tcl)"
		puts $sock "Host: $host"
		puts $sock "Connection: close"
		puts $sock ""
		flush $sock
		set header_done 0
		set data ""
		# get some strings out of the http stream.. better write them in a var.. normally.
		while {[eof $sock] != 1} {
			set bl [gets $sock]
		#	putlog "test: $bl"
			if {[string match "" $bl]} {
			  set header_done 1
			}
			if {$header_done == 1} {
				set data "$data\n$bl"
			}
		}
		close $sock
		return $data
	}
}
proc get_localfile {file} {
	global botnet-nick

	set data ""
	if {[file isfile "$file"] == 1} {
	
		set fh [open "$file" "r"]
		while {![eof $fh]} {
			set line [gets $fh]
			append data $line
		}
		return $data
		
	} else {
		putlog "mnet: Local configfile $file not found. Ignoring."
	}
}


proc mnet:put_nick {nick msg} { 
	putserv "NOTICE $nick :$msg" 
}


proc mnet:dummy {} {
	return 0
}

#not used..
proc mnet:kill_delaytimer {prefix chan} {
	global mnet_delay
	set mnet_delay($prefix,$chan) 0
	putlog "Sending in $chan allowed again, since the timer expired"
}


proc mnet:put_bot {botnetnick data} {
	set testlink [islinked $botnetnick]
	if {$testlink == 1} {
		putlog "mnet: put_bot: + a message delivered to $botnetnick."
		putbot $botnetnick $data
	} else {
		putlog "mnet: put_bot: + a message couldn't be delivered. Bot $botnetnick is not linked"
	}
}

proc mnet:chanstat_users {chan users} {
	
	set ch_users [chanlist $chan]
	set ch_users_count [llength $ch_users]
	

	set users [expr $users + $ch_users_count]

	return $users
}

proc mnet:history_queue {bucket nickname channel rec_text} {
#mnet:history_queue $network,$name "$timestamp $rec_nickname $rec_channel $rec_text"
	global mnet_histories mnet_max_history
	
	#putlog "mnet: HISTORY $nickname $channel $rec_text"

	set timestamp [clock format [clock seconds] -format "%H:%M:%S"]
	#putlog "tstamp: $timestamp"

	# no clean:text needed .. already done sometime before.
	
	set input_history "$timestamp / $nickname - $channel : $rec_text"
	
#	putlog "tst: $timestamp nick: $nickname chan: $channel txt: $rec_text"
	
	set list_length 0
	if {[info exists mnet_histories($bucket)] == 1} {
		set list_length [llength $mnet_histories($bucket)]
		putlog "listlength of mnet_histories($bucket) before: $list_length"
	}
	if {$list_length >= $mnet_max_history} { 
		# $mnet_max_history 10 - 9
		# 5= 1 4
		#10= 1 9
		# wenn man von 10 auf 5 wechselt: 6 9
		set mnet_histories($bucket) [lrange $mnet_histories($bucket) [expr $list_length - $mnet_max_history + 1] [expr $list_length - 1]]
#		putlog "grosser oder gleich 10: str: $input"
	}
	lappend mnet_histories($bucket) $input_history
	
	#set list_length [llength $mnet_histories($bucket)]
	#putlog "listlength of mnet_histories($bucket) now: $list_length"

	# define your own logging
	if {[info procs mnet:history_logger] == "mnet:history_logger"} {
		mnet:history_logger $bucket $nickname $channel $rec_text
	} else {
		putlog "mnet: procedure mnet:history_logger hasn't been defined. Do it, if you want logging."
	}
	
}

## procs which are executed remotely follow:

proc mnet:receive_message {rec_botnick cmd rec_data} {
	global mnet mnet_channels mnet_freqs_onoff mnet_bots \
	  mnet_histories \
	  botnet-nick
	
	#putlog  "mnet: = BRACE 1"
	
	set tmp_network [lindex $rec_data 0]
	set tmp_name [lindex $rec_data 1]
	
	#putlog "tmp_net: '$tmp_network'"
	#putlog "tmp_nam: '$tmp_name'"
	
	set network [mnet:kill_spaces $tmp_network]
	#putlog  "mnet: = '$network'"
	
	set name  [mnet:kill_spaces $tmp_name]
	#putlog  "mnet: = '$name'"
	
	
	# filtering the braces {} .. the braces were needed to not evaluate things like: \n \t and stuff
	set rec_nickname [mnet:kill_spaces [mnet:clean_braces [lindex $rec_data 2]]]
	set rec_channel  [mnet:kill_spaces [mnet:clean_braces [lindex $rec_data 3]]]
	set rec_text [mnet:clean_txt [lrange $rec_data 4 end]]
#	putlog "EMPFANGEN: $rec_data"
#	putlog "TEXT: $rec_text"
	
	set allowed [mnet:allowed_netbot $network ${rec_botnick}]
	if {$allowed == 0} {
		return 0
	}
	# now the bot is declared as a known bot.
	
	
	set proper_botnick [mnet:proper_botnick ${botnet-nick}]
	
	putlog "mnet: + Relaying Message from: $rec_nickname @ $rec_botnick @ $rec_channel: $rec_text"
	
	set users 0
	set count_channels 0
			
	mnet:history_queue $network,$name $rec_nickname $rec_channel $rec_text

	foreach {chan} $mnet_channels($network,$proper_botnick) {
	

		if {[mnet:botonchannel $chan] == "1"} {

			# erster disable/enable check
			if {$mnet_freqs_onoff($name,$chan) == 1} {
		  
				incr count_channels
	
				set users [mnet:chanstat_users $chan $users]
				putlog "mnet: * Remote counting $chan users: $users"
				mnet:put_local_netmessage $network $name $chan $rec_nickname $rec_channel $rec_text
				
			} else {
				putlog "mnet: + $chan has disabled $name output"
			}

		} else {

			putlog "mnet: + Can't relay a message to '$chan' since I am not there."

		}
	}
	
	# ANSWER! SEND!
	mnet:put_bot $rec_botnick "mnet_answer $network $name $rec_channel $users $count_channels"
	return 0
}

proc mnet:receive_answer {rec_botnick cmd rec_data} {
	global mnet mnet_reached_users mnet_reached_chans

	set network [lindex $rec_data 0]
	set name    [lindex $rec_data 1]
        set chan    [lindex $rec_data 2]
	
        set rec_usercount [lindex $rec_data 3]
	set rec_chancount [lindex $rec_data 4]
#	set rec_text [lrange $rec_data 2 end]

	# get this bots results to our global variable:
	# FIXME: make simple userstats more accurate by sending nicknames back..
	# done: userstats should be counted in a correct variablename .. according to network and frequency
	#        if you don't pay attention to it, a conflict between two broadcasts counting users may appear.
	#     $mnet_reached_chans($name,$chan) <- $mnet_reached_users($name,$chan)
	set mnet_reached_users($name,$chan) [expr $mnet_reached_users($name,$chan) + $rec_usercount]
	set mnet_reached_chans($name,$chan) [expr $mnet_reached_chans($name,$chan) + $rec_chancount]
	
	putlog "mnet: * $rec_botnick told me he sent its message to $rec_usercount people in $rec_chancount channels for $network frequency $name!"
	#putlog "mnet: * Users reached since $name call: $mnet_reached_users($name,$chan) in channels: $mnet_reached_chans($name,$chan)"
	
}

proc mnet:clean_braces {text} {
	regsub "{" $text "" text
	regsub "}" $text "" text
	return $text
}
proc mnet:kill_spaces {text} {
	regsub -all "\\s+" $text "" text 
	return $text
}
proc mnet:clean_txt {text} {

#	putlog "filter_A: ${text}"
#	regsub -all "\\" $text "\\\\" text

	regsub "{" $text "" text
	regsub "}" $text "" text

	# fixes many whitespace between words down to one space between words
	regsub -all "\\s+" $text " " text 
	
	# filtering out all colorcodes (works well)
	regsub -all "\003\[0-9\]\{1,2\},\[0-9\]\{1,2\}" $text "" text
	regsub -all "\003\[0-9\]\{1,2\}" $text "" text
	regsub -all "\003" $text "" text
	
	# filtering out BOLD text
	regsub -all "\002" $text "" text
	
	# underline gets filtered too. (since +c on quakenet would suppress it ...)
	regsub -all "\037" $text "" text
	

	# replacing like !!!!!!!!!!!!! with !!!!! (5 letters)
	# s/(.?)\1{4,}/\1\1\1\1\1/g;
	# - max 5 same chars in a row
	regsub -all -nocase -expanded {(.)\1\1\1\1+} $text {\1\1\1\1\1} text
#	putlog "test: $text"
	

	set text [string trim $text]
#	putlog "filter_B: ${text}"
	return $text

}

proc mnet:botonchannel {chan} { 

	# safe "bot-knows-the-channel-and-is-in-there"-function
	
	if {[validchan $chan] == "1" && [botonchan $chan] == "1"} {
		return 1
	} else {
		return 0
	}
}
			

# allowcheck for public commands..
proc mnet:allowed_channel {network name prefix nick mask chan} {
	global mnet mnet_channels mnet_bots mnet_freqs mnet_networks_by_prefix mnet_colors \
	  botnick botnet-nick \
	  lastbind

	# validating incoming vars
	set chan [mnet:proper_channelname $chan]

	putlog "mnet: = a command for ($prefix) triggered by $nick ($mask) in $chan"

	# the command must be from a channel of "ownchannels"
	
	putlog "mnet: = trying if $chan is allowed in network: $network"
	set found 0
	set proper_botnick [mnet:proper_botnick ${botnet-nick}]
	foreach _trigger_chan $mnet_channels($network,$proper_botnick) {
		#putlog "mnet: = test: $_trigger_chan vs $chan"
		if {[string compare -nocase $_trigger_chan $chan] == 0} {
			putlog "mnet: = channel: $_trigger_chan is allowed in network: $network"
			set found 1
		} else {
#			putlog "mnet: = not in list: $chan"
		}
	}
	if {$found == 0} {
		putlog "mnet: = triggering $prefix of network $network in $chan is not allowed (in my point of view)"
		return 0
	}
	 
	set snd_nick [mnet:get_data $nick]

	if {![isop $nick $chan]} {
		putlog "mnet: = allowed in $chan, but $nick is no op"
		mnet:put_nick $nick "Sorry $nick, you need to be op."
		return 0
	}
	if {$nick == $botnick} {
		# no recursion :)
		return 0
	}
	
	
	# got through all checks .. so it's an OK channel
	return 1
}

# checks if the user is allowed, returns 1 if OK.
# checks against the xml config of the banned ones.
proc mnet:allowed_user {network name prefix nick mask chan} {
	global mnet mnet_channels mnet_bots mnet_bans mnet_networks_by_prefix mnet_colors \
	  botnick botnet-nick \
	  lastbind

	# validating incoming vars
	set chan [mnet:proper_channelname $chan]

	# does mnet_bans even exist? it's optional after all.
	if {[info exists mnet_bans($network,$prefix)] == 1} {
		putlog "mnet: = a command for ($prefix) triggered by $nick ($mask) in $chan"
	
		# the command must be from a channel of "ownchannels"
		
		putlog "mnet: = trying if $nick ($mask) banned from frequency: $network,$prefix"
		set found 0
		foreach {hostmask reason} $mnet_bans($network,$prefix) {
			if {[string match [string tolower $hostmask] [string tolower $mask]]} {
				putlog "mnet: = allowed_user: $nick ($mask) is not allowed in network ($network) by hostmask ($hostmask) for reason: $reason"
				set found 1
			} else {
	#			putlog "mnet: = not in list: $chan"
			}
		}
		if {$found == 1} {
			# tell the user his ban. :)
			mnet:put_nick $nick "mnet: Not allowed to send messages. Reason: $reason"
			return 0
		}
	}
	# got through all checks .. so user is not in banlist
	return 1
}



# allowcheck for bots sending netmessages
proc mnet:allowed_netbot {network netbot} {
	global mnet mnet_bots \
	  botnick botnet-nick \
	  lastbind

	putlog "mnet: = a netbot $netbot sending message... allowed?"

	# the command must be from a channel of "ownchannels"
	
	set found 0
	set proper_botnick [mnet:proper_botnick ${botnet-nick}]
	set proper_netbot [mnet:proper_botnick $netbot]
	
	foreach {_netbotnick} $mnet_bots($network) {
		if {$_netbotnick == $proper_netbot} {
			putlog "mnet: = netbot: $proper_netbot is allowed in network: $network"
			set found 1
		} else {
#			putlog "mnet: = netbot: ...."
		}
	}
	if {$found == 0} {
		putlog "mnet: = $proper_netbot of network $network sent me something - but it is not allowed in my network: $network"
		return 0
	}
	 
	if {$netbot == ${botnet-nick}} {
		# if myself (for some reason) then don't do anything... no recursion :)
		return 0
	}
	
	
	# got through all checks .. so it's an OK netbot for a given network
	return 1
}



proc mnet:reply_timeout {name chan} {
	global mnet mnet_reached_users mnet_reached_chans
	
	# clean $name and $chan
	set name [mnet:kill_spaces $name]
	set chan [mnet:kill_spaces $chan]
	
	# easify variables
	set users $mnet_reached_users($name,$chan)
	set chans $mnet_reached_chans($name,$chan)

	#putlog "mnet: HIER $name UND $chan"
	# inzwischen drfte auch die antwort gekommen sein.. also jetzt mnet_reached_* auswerten nach timeout..
	
	# make stats channel-dependent
	putlog "mnet: * After ALL: Count_Users: $mnet_reached_users($name,$chan) Count_Channels: $mnet_reached_chans($name,$chan)"
	
	# Give me some reply.
	mnet:put_local_msg ${chan} "mnet: Message sent to $users users in $chans channels."
}


### public reachable commands
proc mnet:history_frequency {nick mask hand chan text} {
# proc triggered by specified $prefix_history
	global mnet mnet_channels mnet_bots mnet_networks_by_prefix mnet_colors \
	  mnet_freqs mnet_freqs_onoff \
	  mnet_histories mnet_max_history \
	  botnick lastbind

	set com $lastbind

	# validating incoming vars
	set chan [mnet:proper_channelname $chan]
	
	# cutting .prefix_history down to .prefix
	set prefix [string range $com 0 [expr [string first _ $com] -1]]
	#putlog "mnet: the prefix is $prefix"

	set network [lindex $mnet_networks_by_prefix($prefix) 0]
	set name [lindex $mnet_networks_by_prefix($prefix) 1]
	set help [lindex $mnet_networks_by_prefix($prefix) 2]
	set desc [lindex $mnet_networks_by_prefix($prefix) 3]

	putlog "network: $network"
	set allowed [mnet:allowed_channel $network $name $prefix $nick $mask $chan]
	if {$allowed == 0} {
		return 0
	}
	# now generally allowed.
	putlog "mnet_history: $com triggered by $nick in $chan"
	if {[info exists mnet_histories($network,$name)] == 1} {
		set list_length [llength $mnet_histories($network,$name)]
		if {$list_length >= 1} {
			set msg "mnet: last $mnet_max_history messages of $network $name:"
			mnet:put_nick $nick $msg

		
			set x_count 0
			foreach {msg} $mnet_histories($network,$name) {
				incr x_count
				set backwards_x [expr $list_length - $x_count]
				set msg [lindex $mnet_histories($network,$name) $backwards_x]
				mnet:put_nick $nick "($x_count/$list_length) $msg"
		
			}
		} 
	} else {
		mnet:put_nick $nick "mnet: no accumulated history for $network $name"
	}
}

proc mnet:enable_frequency {nick mask hand chan text} {
# proc triggered by specified $prefix_enable
	global mnet mnet_channels mnet_bots mnet_networks_by_prefix mnet_colors \
	  mnet_freqs mnet_freqs_onoff \
	  botnick lastbind

	set com $lastbind
	
	# validating incoming vars
	set chan [mnet:proper_channelname $chan]
	

	# cutting .prefix_enable down to .prefix
	set prefix [string range $com 0 [expr [string first _ $com] -1]]
	#putlog "mnet: the prefix is $prefix"

	set network [lindex $mnet_networks_by_prefix($prefix) 0]
	set name [lindex $mnet_networks_by_prefix($prefix) 1]
	set help [lindex $mnet_networks_by_prefix($prefix) 2]
	set desc [lindex $mnet_networks_by_prefix($prefix) 3]

	#putlog "network: $network"
	set allowed [mnet:allowed_channel $network $name $prefix $nick $mask $chan]
	if {$allowed == 0} {
		return 0
	}
	# now generally allowed.
	
	
	putlog "mnet_freq_status: before $mnet_freqs_onoff($name,$chan)"
	set mnet_freqs_onoff($name,$chan) 1
	putlog "mnet_freq_status: now $mnet_freqs_onoff($name,$chan)"
	
	mnet:put_local_msg $chan "mnet: $name _output_ enabled for $chan"
	
}

proc mnet:disable_frequency {nick mask hand chan text} {
# proc triggered by specified $prefix_enable
	global mnet mnet_channels mnet_bots mnet_networks_by_prefix mnet_colors \
	  mnet_freqs mnet_freqs_onoff \
	  botnick lastbind

	set com $lastbind
	
	# validating incoming vars
	set chan [mnet:proper_channelname $chan]

	# cutting .prefix_enable down to .prefix
	set prefix [string range $com 0 [expr [string first _ $com] -1]]
	putlog "mnet: the prefix is $prefix"

	set network [lindex $mnet_networks_by_prefix($prefix) 0]
	set name [lindex $mnet_networks_by_prefix($prefix) 1]
	set help [lindex $mnet_networks_by_prefix($prefix) 2]
	set desc [lindex $mnet_networks_by_prefix($prefix) 3]

	#putlog "network: $network"
	set allowed [mnet:allowed_channel $network $name $prefix $nick $mask $chan]
	if {$allowed == 0} {
		return 0
	}
	# now generally allowed.

	
	putlog "mnet_freq_status: before $mnet_freqs_onoff($name,$chan)"
	set mnet_freqs_onoff($name,$chan) 0
	putlog "mnet_freq_status: now $mnet_freqs_onoff($name,$chan)"
	
	mnet:put_local_msg $chan "mnet: $name _output_ disabled for $chan"
	
}

proc mnet:toggle_frequency {nick mask hand chan text} {
# proc triggered by specified $prefix_toggle
	global mnet mnet_channels mnet_bots mnet_networks_by_prefix \
	  mnet_colors \
	  mnet_freqs mnet_freqs_onoff \
	  botnick lastbind
	
	set com $lastbind

	# validating incoming vars
	set chan [mnet:proper_channelname $chan]


	# cutting .prefix_enable down to .prefix
	set prefix [string range $com 0 [expr [string first _ $com] -1]]
	# putlog "mnet: the prefix is $prefix"

	set network [lindex $mnet_networks_by_prefix($prefix) 0]
	set name [lindex $mnet_networks_by_prefix($prefix) 1]
	set help [lindex $mnet_networks_by_prefix($prefix) 2]
	set desc [lindex $mnet_networks_by_prefix($prefix) 3]

	# putlog "network: $network"
	set allowed [mnet:allowed_channel $network $name $prefix $nick $mask $chan]
	if {$allowed == 0} {
		return 0
	}
	# now generally allowed.

	
	putlog "mnet_freq_status: toggle $mnet_freqs_onoff($name,$chan)"
	if {$mnet_freqs_onoff($name,$chan) == 1} {
		set mnet_freqs_onoff($name,$chan) 0
		mnet:put_local_msg $chan "mnet: toggling ${name} _output_, now disabled for ${chan}"
	} else {
		set mnet_freqs_onoff($name,$chan) 1
		mnet:put_local_msg $chan "mnet: toggling ${name} _output_, now enabled for ${chan}"
	}
	putlog "mnet_freq_status: toggle now $mnet_freqs_onoff($name,$chan)"
	
	# frequency toggled .. :)
	
}

#mnet:enable_color
proc mnet:enable_color {nick mask hand chan text} {
# proc triggered by specified $prefix_enable
	global mnet mnet_channels mnet_bots mnet_networks_by_prefix \
	  mnet_colors_optional mnet_colors_onoff \
	  botnick lastbind

	set com $lastbind
	
	# validating incoming vars
	set chan [mnet:proper_channelname $chan]

	# cutting .prefix_enable down to .prefix
	set prefix [string range $com 0 [expr [string first _ $com] -1]]
	#putlog "mnet: the prefix is $prefix"

	set network [lindex $mnet_networks_by_prefix($prefix) 0]
	set name [lindex $mnet_networks_by_prefix($prefix) 1]
	set help [lindex $mnet_networks_by_prefix($prefix) 2]
	set desc [lindex $mnet_networks_by_prefix($prefix) 3]

	#putlog "network: $network"
	set allowed [mnet:allowed_channel $network $name $prefix $nick $mask $chan]
	if {$allowed == 0} {
		return 0
	}
	# now generally allowed.
	if {$mnet_colors_optional($network) != 1} {
		return 0
	}

	putlog "mnet_color_status: before $mnet_colors_onoff($name,$chan)"
	set mnet_colors_onoff($name,$chan) 1
	putlog "mnet_color_status: now $mnet_colors_onoff($name,$chan)"
	
	mnet:put_local_msg $chan "mnet: $name _coloroutput_ enabled for $chan"
}

#mnet:disable_color
proc mnet:disable_color {nick mask hand chan text} {
# proc triggered by specified $prefix_enable
	global mnet mnet_channels mnet_bots mnet_networks_by_prefix \
	  mnet_colors_optional mnet_colors_onoff \
	  botnick lastbind

	set com $lastbind

	# validating incoming vars
	set chan [mnet:proper_channelname $chan]

	# cutting .prefix_enable down to .prefix
	set prefix [string range $com 0 [expr [string first _ $com] -1]]
	#putlog "mnet: the prefix is $prefix"

	set network [lindex $mnet_networks_by_prefix($prefix) 0]
	set name [lindex $mnet_networks_by_prefix($prefix) 1]
	set help [lindex $mnet_networks_by_prefix($prefix) 2]
	set desc [lindex $mnet_networks_by_prefix($prefix) 3]

	#putlog "network: $network"
	set allowed [mnet:allowed_channel $network $name $prefix $nick $mask $chan]
	if {$allowed == 0} {
		return 0
	}
	# now generally allowed.
	if {$mnet_colors_optional($network) != 1} {
		return 0
	}

	putlog "mnet_color_status: before $mnet_colors_onoff($name,$chan)"
	set mnet_colors_onoff($name,$chan) 0
	putlog "mnet_color_status: now $mnet_colors_onoff($name,$chan)"
	
	mnet:put_local_msg $chan "mnet: $name _coloroutput_ disabled for $chan"
}


#mnet:toggle_color
proc mnet:toggle_color {nick mask hand chan text} {
# proc triggered by specified $prefix_toggle
	global mnet mnet_channels mnet_bots mnet_networks_by_prefix \
	  mnet_colors_optional mnet_colors_onoff \
	  botnick lastbind

	set com $lastbind
	
	# validating incoming vars
	set chan [mnet:proper_channelname $chan]


	# cutting .prefix_enable down to .prefix
	set prefix [string range $com 0 [expr [string first _ $com] -1]]
	# putlog "mnet: the prefix is $prefix"

	set network [lindex $mnet_networks_by_prefix($prefix) 0]
	set name [lindex $mnet_networks_by_prefix($prefix) 1]
	set help [lindex $mnet_networks_by_prefix($prefix) 2]
	set desc [lindex $mnet_networks_by_prefix($prefix) 3]

	# putlog "network: $network"
	set allowed [mnet:allowed_channel $network $name $prefix $nick $mask $chan]
	if {$allowed == 0} {
		return 0
	}
	# now generally allowed.
	if {$mnet_colors_optional($network) != 1} {
		return 0
	}

	
	putlog "mnet_color_status: toggle $mnet_colors_onoff($name,$chan)"
	if {$mnet_colors_onoff($name,$chan) == 1} {
		set mnet_colors_onoff($name,$chan) 0
		mnet:put_local_msg $chan "mnet: toggling ${name} _coloroutput_, now disabled for ${chan}"
	} else {
		set mnet_colors_onoff($name,$chan) 1
		mnet:put_local_msg $chan "mnet: toggling ${name} _coloroutput_, now enabled for ${chan}"
	}
	putlog "mnet_color_status: toggle now $mnet_colors_onoff($name,$chan)"
	
	# color for a channel toggled .. :)
	
}


#mnet:toggle_color
proc mnet:status_frequency {nick mask hand chan text} {
# proc triggered by specified $prefix_toggle
	global mnet mnet_channels mnet_bots mnet_networks_by_prefix \
	  mnet_colors_optional mnet_colors_onoff \
	  mnet_freqs_onoff \
	  mnet_reached_users mnet_reached_chans \
	  botnick lastbind

	set com $lastbind
	
	# validating incoming vars
	set chan [mnet:proper_channelname $chan]

	# cutting .prefix_enable down to .prefix
	set prefix [string range $com 0 [expr [string first _ $com] -1]]
	# putlog "mnet: the prefix is $prefix"

	set network [lindex $mnet_networks_by_prefix($prefix) 0]
	set name [lindex $mnet_networks_by_prefix($prefix) 1]
	set help [lindex $mnet_networks_by_prefix($prefix) 2]
	set desc [lindex $mnet_networks_by_prefix($prefix) 3]

	# putlog "network: $network"
	set allowed [mnet:allowed_channel $network $name $prefix $nick $mask $chan]
	if {$allowed == 0} {
		return 0
	}
	# now generally allowed.
	
	## get status for this frequency.
	set status_message "$chan $name: "
	
	if {$mnet_freqs_onoff($name,$chan) == 1} {
			append status_message "frequency output is on. "
		
	} else {
			append status_message "frequency output is off. "
	}

	if {$mnet_colors_optional($network) == 1} {
		if {$mnet_colors_onoff($name,$chan) == 1} {
			append status_message "colors are turned on. "
		} else {
			append status_message "colors are turned off. "
		}
		
	} else {
		append status_message "message-colors are forced. "
	}
	
	if {[info exists mnet_reached_users($name,$chan)]} {
		if {[info exists mnet_reached_chans($name,$chan)]} {
			append status_message "Your last message reached about $mnet_reached_users($name,$chan) users and $mnet_reached_chans($name,$chan) channels. "
		}
	}

	
	mnet:put_local_msg $chan "$status_message"


}



# checks timers of channels ... if not found, then ok with "0" secs left.. if found then return $secsleft
proc mnet:check_spam_protection {prefix chan} {
	global mnet_delay
	
	# validating incoming vars
	set chan [mnet:proper_channelname $chan]

	# check for pending timers.. spam-protection
	set timerlist [utimers]
	set secondsLeft 0
	if {![info exists mnet_delay($prefix,$chan)]} {
		set mnet_delay($prefix,$chan) 0
	} else {
		foreach {timerinfo} $timerlist {
		
			set secsleft [lindex $timerinfo 0]
			set timer_id [lindex $timerinfo 2]
		
	#		putlog "sec: $secsleft tid: $timer_id"
	#		putlog "checking timer ($timer_id) against mdelay-id: $mnet_delay($prefix,$chan)"
			if {$timer_id == $mnet_delay($prefix,$chan)} {
				return $secsleft
			}
		}
	}
	return 0
}		


proc mnet:messaging_public {nick mask hand chan text} {
# proc triggered by specified $prefix and calling the following (internal) one
	global mnet mnet_channels mnet_bots mnet_networks_by_prefix mnet_colors \
	  mnet_freqs mnet_freqs_onoff mnet_delay \
	  mnet_histories \
	  mnet_reached_users mnet_reached_chans \
	  botnet-nick lastbind
	
	# validating incoming vars
	set chan [mnet:proper_channelname $chan]
	
	set prefix $lastbind
	set network [lindex $mnet_networks_by_prefix($prefix) 0]
	set name [lindex $mnet_networks_by_prefix($prefix) 1]
	set help [lindex $mnet_networks_by_prefix($prefix) 2]
	set desc [lindex $mnet_networks_by_prefix($prefix) 3]
	set delay [lindex $mnet_networks_by_prefix($prefix) 4]

	set allowed [mnet:allowed_channel $network $name $prefix $nick $mask $chan]
	if {$allowed == 0} {
		return 0
	}
	# now generally allowed.

	# is user banned from sending??
	set user_allowed [mnet:allowed_user $network $name $prefix $nick $mask $chan]
	if {$user_allowed == 0} {
		return 0
	}
	# user has not been matched against banlist. So he's allowed here.

	
	
	# cleaning give strings...
	set text [mnet:clean_txt $text]

	# check here if we have some text or not.. :)
	if {$text == "" || $text == "{}"} {
	
		# output some help:
		putlog  "mnet: = gave him some help"
		mnet:put_local_msg $chan $help

	} else {
		# ok, now send! (if timer allows it)
		
		### spamprotection
		# returns the seconds that are left for this channel .. or 0 if no seconds left.
		set secsleft [mnet:check_spam_protection $prefix $chan]
		if {$secsleft > 0} { 
			putlog "mnet: The timer of $prefix for $chan is still running. The guy has to wait ($secsleft/$delay) more seconds. I tell him."
			mnet:put_nick $nick "Sorry $nick, usage of $prefix only every $delay seconds. $secsleft seconds for $chan remaining."
			# now BREAK "messaging_public".
			# NO break.. for debug :)) arf
			return 0
		}
		# if we went through the timer check ... so it doesn't exist anymore then we set a new one:
		# set a timer-id to check for again, if it's allowed or not .. delay
		set mnet_delay($prefix,$chan) [utimer $delay mnet:dummy]
		### end spamprotection
		
		## HE IS SENDING NOW!
		
		set mnet(reached) "0"
		set mnet(reached_channels) "0"

		# reset stats .. because he is just freshly sending
		set mnet_reached_users($name,$chan) "0"
		set mnet_reached_chans($name,$chan) "0"

		mnet:history_queue $network,$name $nick $chan $text
		
		# send them first, we await a reply!

		set proper_botnick [mnet:proper_botnick ${botnet-nick}]
#		putlog "{botnet-nick} huar: $proper_botnick"

		# single sends TO ALL REMOTE BOTS with their remote CHANNELS...
		# let's see which mode for sending is used...
		foreach _botnick $mnet_bots($network) {
			if {$_botnick == $proper_botnick} {
		
			} else { # if not myself!

				putlog "mnet: send: + I am about to send a message to $_botnick for $network: $name: $text"
				# safe put_bot function..
				mnet:put_bot $_botnick "mnet_receive $network {$name } {$nick } {$chan } {$text }"
				
			}
		}

		
		# SEND TO ALL OWN CHANNELS...
		# send to all own channels, except to the channel the message originating from:
		foreach _ownchan $mnet_channels($network,$proper_botnick) {
			if {$_ownchan == $chan} {
			
			} else { # if not own channel!

				if {[mnet:botonchannel $_ownchan] == "1"} {
					
					putlog "mnet: = i am on $_ownchan"

					# second enable/disable check
					if {$mnet_freqs_onoff($name,$_ownchan) == 1} {
					
						# userstats local
#						set mnet(reached) [mnet:chanstat_users $_ownchan $mnet(reached)]
						set mnet_reached_users($name,$chan) [mnet:chanstat_users $_ownchan $mnet_reached_users($name,$chan)]
						
						putlog "mnet: * Localcounting up $_ownchan users: $mnet_reached_users($name,$chan)"
	
						# channel count local
						incr mnet_reached_chans($name,$chan)
	
						mnet:put_local_netmessage $network $name $_ownchan $nick $chan $text
					} else {
						putlog "mnet: + $_ownchan has disabled $name output"
					}

				} else {

					putlog "mnet: + Can't send a local message to '$_ownchan' since I am not there."

				}
			}
		}
	
		# wait here some more! (10 seconds with utimer)
		utimer 5 "mnet:reply_timeout {$name } {$chan }"

		# notice: better would be to go into the "reply" when all known bots already answered..
		# then, if a bot is missing, but it was a known bot, just use a timeout with about 5-10 seconds..
		# if the bot still didn't answer, then just go into "reply".
	
	}

}
### END public reachable commands



proc mnet:binds {} {
	global currentNetworkDesc currentNetworkSig \
	  startNetworkInstance \
	  currentBot currentOwner \
	  startBotInstance \
	  mnet_networks mnet_freqs mnet_networks_by_prefix mnet_channels \
	  mnet_freqs_onoff mnet_colors_onoff \
	  botnet-nick

	
	set proper_botnick [mnet:proper_botnick ${botnet-nick}]
	# binding of irc commands ... only works well if a config was loaded..
	foreach {network desc} $mnet_networks {
#		putlog "test: network is $network"
#		putlog "test: networkdesc is $desc"
		
		foreach {network name prefix help desc msgdelay} $mnet_freqs($network) {
		
			putlog "network: $network"
			putlog "name: $name"
			putlog "prefix: $prefix"
			putlog "help: $help"
			putlog "desc: $desc"
			putlog "msgdelay: $msgdelay"
			
			set mnet_networks_by_prefix($prefix) "{$network} {$name} {$help} {$desc} {$msgdelay}"
			
			# setting variables on init.
			foreach {channel} $mnet_channels($network,$proper_botnick) {
				# catch like php isset() - but catch returns 0 if OK!
				if {[catch {set mnet_freqs_onoff($name,$channel)}] == 1} {
					set mnet_freqs_onoff($name,$channel) 1
					putlog "mnet: initially enabling $name for channel: $channel"
				} else {
					putlog "mnet: $name for $channel is already on output: $mnet_freqs_onoff($name,$channel)"
				}
				
			
				# catch like php isset() - but catch returns 0 if OK!
				if {[catch {set mnet_colors_onoff($name,$channel)}] == 1} {
					set mnet_colors_onoff($name,$channel) 0
					putlog "mnet: initially disabling colors for $name for channel (if not forced): $channel"
				} else {
					putlog "mnet: coloroutput of $name for $channel is already on (if not forced): $mnet_colors_onoff($name,$channel)"
				}
			}
				
			
			bind pub - ${prefix} 		mnet:messaging_public
			# $network $name $prefix $help $desc
			bind pub - ${prefix}_enable	mnet:enable_frequency
			bind pub - ${prefix}_disable	mnet:disable_frequency
			bind pub - ${prefix}_toggle	mnet:toggle_frequency

			bind pub - ${prefix}_enable_colors	mnet:enable_color
			bind pub - ${prefix}_disable_colors	mnet:disable_color
			bind pub - ${prefix}_toggle_colors	mnet:toggle_color
			# with typos..
			bind pub - ${prefix}_enable_color	mnet:enable_color
			bind pub - ${prefix}_disable_color	mnet:disable_color
			bind pub - ${prefix}_toggle_color	mnet:toggle_color
			
			bind pub - ${prefix}_history	mnet:history_frequency

			bind pub - ${prefix}_status	mnet:status_frequency
			
			bind pub - .mnet_status		mnet:dummy
			# FIXME: make some more commands available to have control over the messages
			# - "_stats"?
		
		}
	
	
	}
}
bind bot - mnet_receive		mnet:receive_message	
bind bot - mnet_answer		mnet:receive_answer
bind dcc m mnet_reload		mnet:dcc_configload

proc mnet:conf_parsing {} {
	global mnet_config mnet_num_configs p
	
	for {set y 1} {$y <= $mnet_num_configs} {incr y} {
		set configfile $mnet_config($y)
#		putlog "HRM?: $configfile"
		$p parse "$configfile"
	}
	
}

proc mnet:dcc_configload {dummy1 dummy2 dummy3} {
	# wrapper command for mnet:netconfigload
	putlog "mnet: = reloading configs..."
	mnet:clean_netconfig
	mnet:netconfigload
}
proc mnet:netconfigload {} {
	global mnet_http_config mnet_local_config mnet_config mnet_num_configs

	

	# possible methods to load a config are: get_httpfile and get_localfile
	# get_httpfile 
	# get_localfile RELATIVE_PATH_AND_FILE
	set x 0
	if {[info exists mnet_http_config]} {
		foreach {host httpfile port} $mnet_http_config {
			# FIXME config validating.. is the file received correctly? or do
			# we only have a 404 error message there? ... bad thing still.
			incr x
			set mnet_config($x) [get_httpfile $host $httpfile $port]
		}
	}
	
	if {[info exists mnet_local_config]} {
		foreach {localfile} $mnet_local_config {
			# FIXME config validating.. could we open the file?
			# is the file a correct config?
			incr x
			set mnet_config($x) [get_localfile $localfile]
		}
	}
	# $x is now at its highest count.
	
	set mnet_num_configs $x
	
	if {$x > 0} {
		utimer 5 mnet:conf_parsing
		utimer 10 mnet:binds
	} else {
		putlog "mnet: no configurations loaded."
	}
}

# looping this one
proc mnet:interval_conf {} {
	global mnet_cfg_reload_interval mnet_configtimer_set mnet_netconfig_timer
	
#	putlog "mnet: interval conf! $mnet_configtimer_set huh?"
	
	if {$mnet_configtimer_set == 1} {
		putlog "mnet: starting intervalled configload ($mnet_cfg_reload_interval minutes)"
		mnet:clean_netconfig
		mnet:netconfigload
		set mnet_netconfig_timer [timer $mnet_cfg_reload_interval mnet:interval_conf]
	} else {
		putlog "mnet: was about to make a automatic configload again, but it has been disabled recently."
	}
}

############# CONFIG SECTION ##############
### the real config section is supposed to be here... 
if {[info exists configs_file]} {
	if {[file isfile "$configs_file"] == 1} {
		source $configs_file
		putlog "mnet: configs file '$configs_file' loaded."
	}
} 
############# CONFIG SHOULD NOW BE LOADED ONTO THE VARIABLES ##############


# pls hands away again: starting up just defined configs ...
putlog "mnet: = Messaging Network ($mnet(version)) loaded"
putlog "mnet: =  as master, reload current net-configs with .mnet_reload"
putlog "mnet: =  do .rehash and .mnet_reload if you defined a new net-config."
if {$mnet_cfg_reload_interval >= 90} {
	if {![info exists mnet_netconfig_timer]} {
		set mnet_configtimer_set 1
		mnet:interval_conf
		# this one loops
	}
} else {
	if {[info exists mnet_netconfig_timer]} {
		set mnet_configtimer_set 0
		killtimer $mnet_netconfig_timer
		unset mnet_netconfig_timer
	}
	set mnet_cfg_reload_interval 0
	mnet:clean_netconfig
	mnet:netconfigload
}
