file operations with "switch" method

Решение вопросов при работе с TCL скриптами.

Модератор: Модераторы

file operations with "switch" method

Сообщение heartbroken » 29 мар 2013 20:41

hi :)

i've been trying to make a code for someone for awhile .i dont know him actualy but he asking me for help to make this kinda file operation code.so i said ok but i can confess now i mess'd up ...

i wrote a code for him and three months later he turns and said "not working well and it has errors".
well honestly i didnt tried that one on my bot.i wrote and gave him .. so i try to re-build this code with using "switch" ..this is my first time i wrote a code with that so its confusing me now.

ok now i'm gonna try to explain what was his request and what he want to do with it. :

its basicly kinda "notification or a notice system" on a channel, channel ops can add/del or list these messages with channel triggers like :
!addnot <number-line/or a name> some-text-here...
!delnot <number-of-line/or-a-name> ..
!listnot

ok these triggers for channel ops.
but important part is ( all issues about this code begins for me now..i couldnt make it this part of code)
if an user joins in eggdrop's private and he/she triggers like : trig <number-line/or-a-name> ... or
trigger <number-line/or-a-name> .. then our eggdrop should post these specific line/s what added by channel ops before into that user'S PM.
so this post method for user/s should work on bot's pm/users pm.

so what i did.

i thought probably "switch" method is a good idea for this kinda code.and first time in my life i've been try that.
i thought i can handle all of that add/del/list file operations into "main proc" and i can creat a "child proc" for that egg's pm to user's PM post job. and then if i can call "main proc" into "child proc" within like "return $main_proc" so it can work into "default" part of the "switch" ... the idea was like that. i hope i can explain correctly enuff.

but in practise i couldn't do it.it wont work like what i wish..i changed couple of time i play on codes ,and tried change possitions or something but nope so i'm hopeless therefor i am here now again,for to asking your help....
here is the code what i have now:

i try to translate Turkish to English for you..

p.s: i still didnt tried this code on my bot.so i dont know what exactly has errors.i didnt,b-cus i already knew that isnt finished yet...and not working.i can see that.

thanks..best regards
У вас нет необходимых прав для просмотра вложений в этом сообщении.
Life iS Just a dReaM oN tHE wAy to DeaTh
Аватара пользователя
heartbroken
 
Сообщения: 18
Зарегистрирован: 07 апр 2012 09:25
Благодарил (а): 9 раз.
Поблагодарили: 0 раз.

Re: file operations with "switch" method

Сообщение tvrsh » 30 мар 2013 13:51

Adding lines:
[13:47:54] <@tvrsh> !addnot 1 one
[13:47:55] -NOTICE- <bionic> Added 1: one
[13:48:01] <@tvrsh> !addnot 2 two
[13:48:01] -NOTICE- <bionic> Added 2: two
[13:48:05] <@tvrsh> !addnot 3 three
[13:48:06] -NOTICE- <bionic> Added 3: three

Msg to bot:
[13:48:21] <tvrsh> 1
[13:48:21] <bionic> one
[13:48:22] <tvrsh> 2
[13:48:22] <bionic> two
[13:48:23] <tvrsh> 3
[13:48:23] <bionic> three

List:
[13:50:04] <@tvrsh> !listnot
[13:50:05] -NOTICE- <bionic>
[13:50:05] -NOTICE- <bionic> 1 one
[13:50:05] -NOTICE- <bionic> 2 two
[13:50:05] -NOTICE- <bionic> 3 three
[13:50:05] -NOTICE- <bionic> ended

Delete:
[13:50:38] <@tvrsh> !delnot 4
[13:50:38] -NOTICE- <bionic> No such notice: 4.
[13:52:30] <@tvrsh> !delnot 2
[13:52:31] -NOTICE- <bionic> 2 found 1 time, one line deleted
[13:52:38] <@tvrsh> !listnot
[13:52:38] -NOTICE- <bionic>
[13:52:38] -NOTICE- <bionic> 1 one
[13:52:38] -NOTICE- <bionic> 3 three
[13:52:38] -NOTICE- <bionic> ended


TCL: [ Скачать ] [ Скрыть ]
setudef flag notification

bind pubm -|- "*" egg:chan
bind msgm -|- "*" egg:pvt

proc egg:chan {nick uhost hand chan text} {
if {![channel get $chan notification] || ![isop $nick $chan]} {
    return 0
}
set notcmd [lindex [split $text] 0]
set notno [lindex [split $text] 1]
set notmsg [join [lrange [split $text] 2 end]]
set notfile "notification.txt"
    switch -- $notcmd {
        "!addnot" {
            if {[llength $notmsg] == "0"} {
                putserv "NOTICE $nick :Use !addnot <number-line/or a name> some-text-here."
                return 0
            }
            set abc [open "$notfile" a+]
            puts $abc "$notno $notmsg"
            close $abc
            putserv "NOTICE $nick :Added \002$notno:\002 $notmsg"
            return 0
        }
        "!delnot" {
            if {![file exists "$notfile"]} {
                putserv "NOTICE $nick :\002$notfile\002 empty."
                return 0
            }
            if {[llength "$notno"] == "0"} {
                putserv "NOTICE $nick :Use !delnot <number-line/or a name>."
                return 0
            }
            set abc [open "$notfile"]
            set lines [split [read -nonewline $abc] "\n"]
            close $abc
            if {[llength $lines] == "0"} {
                putserv "NOTICE $nick :\002$notfile\002 empty."
                return 0
            }
            set lines2 ""
            set say 0
            set linenr 0
            foreach line $lines {
                if {[lindex $line 0] == $notno} {
                  set lines2 [lreplace $lines $linenr $linenr]
                  incr say
                }
                incr linenr
            }
            if {$say == 0} {
                putserv "NOTICE $nick :No such notice: \002$notno\002."
                return 0
            }
            set abc [open "$notfile" w]
            puts $abc [join $lines2 "\n"]
            close $abc
            if {$say == 1} {
                set s ""
            } else {
                set s "s"
            }
            putserv "NOTICE $nick :\002$notno\002 found $say time$s, one line deleted"
        }
        "!listnot" {
              if {![file exists "$notfile"]} {
                putserv "NOTICE $nick :\002$notfile\002 empty"
                return 0
            }
            set notno 0
            set abc [open "$notfile"]
            set lines [split [read -nonewline $abc] "\n"]
            close $abc
            if {[llength $lines] == "0"} {
                putserv "notice $nick :\002$notfile\002 empty"
                return 0
            }
            foreach line $lines {
                putserv "NOTICE $nick :\002[lindex $line 0]\002 [lrange $line 1 end]"
            }
            putserv "NOTICE $nick :ended"
        }
        return 0
    }
}

proc egg:pvt {nick uhost hand text} {

    set notfile "notification.txt"

    if {![file exists "$notfile"]} {
        return 0
    }
    set notno 0
    set abc [open "$notfile"]
    set lines [split [read -nonewline $abc] "\n"]
    close $abc
    if {[llength $lines] == "0"} {
        return 0
    }
    foreach line $lines {
        if {[lindex $line 0] == $text} {
            putserv "PRIVMSG $nick :[lrange $line 1 end]"
        }
    }
    }

putlog "ok..."
Have fun.
-
Получить помощь можно на каналах #egghelp в сети IrcNet.ru и #eggdrop в сети RusNet(Ключ канала eggdrop).
Перед созданием новой темы внимательно читайте Правила оформления топиков.

За это сообщение автора tvrsh поблагодарил:
heartbroken (30 мар 2013 18:59)
Аватара пользователя
tvrsh
 
Сообщения: 1230
Зарегистрирован: 19 авг 2008 16:55
Откуда: Russian Federation, Podolsk
Благодарил (а): 6 раз.
Поблагодарили: 130 раз.
Версия бота: Eggdrop 1.6.20+suzi

Re: file operations with "switch" method

Сообщение heartbroken » 30 мар 2013 16:05

i dunno how i can thank you..i made lots of mistakes in my code i can see . now.

thank you soo much.tvrsh.i appreciated realy.
Life iS Just a dReaM oN tHE wAy to DeaTh
Аватара пользователя
heartbroken
 
Сообщения: 18
Зарегистрирован: 07 апр 2012 09:25
Благодарил (а): 9 раз.
Поблагодарили: 0 раз.


Вернуться в TCL скрипты

Кто сейчас на конференции

Сейчас этот форум просматривают: нет зарегистрированных пользователей и гости: 7

cron