10-24-2017, 07:52 +0200
AuthorPost
thommey  10-03-2006, 17:21   | [EGG] Parsing and/or changing mode-strings (as in chanset chanmode) Jabber
(Administrator)
Member since 03/2004
62 Posts
                                                                                                                              
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# converts a modestring into a modelist (eg. "+ack-bl key" => {{+a} {-b} {+c} {+k key} {-l}})
proc splitmodes {mstr} {
    set key ""; set limit ""; set pre +; set result [list]
    foreach {modes param1 param2} [split $mstr] break
    foreach chr [split $modes ""] {
        if {$chr == "+"} { set pre +; continue }
        if {$chr == "-"} { set pre -; continue };
        if {$chr == "k" && $pre == "+"} {
            lappend result "$pre$chr $param1"; set param1 $param2; continue
        } elseif {$chr == "l" && $pre == "+"} {
            lappend result "$pre$chr $param1"; set param1 $param2; continue
        }
        lappend result $pre$chr
    }
    return $result
}
# converts a modelist into a modestring (eg. {{+a} {-b} {+c} {+k key} {-l}} => "+ack-bl key")
proc joinmodes {mlist} {
    set plus [list]
    set minus [list]
    set param1 ""
    set param2 ""
    foreach modestr $mlist {
        foreach {md param} [split $modestr] break
        foreach {pre mode} [split $md ""] break
        set list plus
        if {$pre == "-"} { set list minus }
        if {$mode == "k" || $mode == "l"} { if {$param1 == ""} { set param1 $param } { set param2 $param} }
        lappend $list $mode
    }
    if {![llength $plus]} { set plusstr "" } { set plusstr "+[join $plus ""]" }
    if {![llength $minus]} { set minusstr "" } { set minusstr "-[join $minus ""]" }
    set paramstr ""
    if {$param1 != ""} { set paramstr " $param1" }
    if {$param2 != ""} { set paramstr " $param1 $param2" }
    return $plusstr$minusstr$paramstr
}
# changes a modestring depending on the second modestring
# (zB "+nkt-m key" "-k+m" => "+ntm-k")
proc changemodes {oldmodes newmodes} {
    set oldmodes [splitmodes $oldmodes]
    set newmodes [splitmodes $newmodes]
    foreach md $newmodes {
        foreach {md2 param} [split $md] break
        foreach {pre mode} [split $md2 ""] break
        if {$pre == "+"} { set remove "-" }
        if {$pre == "-"} { set remove "+" }
        # delete the old
        if {[set pos [lsearch -glob $oldmodes "$remove$mode*"]] != -1} {
            set oldmodes [lreplace $oldmodes $pos $pos]
        }
        # add the new
        if {$param == ""} { lappend oldmodes $pre$mode } { lappend oldmodes "$pre$mode $param" }
    }
    return [joinmodes $oldmodes]
}

Advanced options for this topic:

Ignore this topic (Do not list this topic in the "unread topics" search. You are currently not ignoring this topic.)
Hide this topic (Hidden topics are not displayed in the topics list. This topic is currently not hidden.)
Go to forum

Unclassified NewsBoard 1.5.3-d | © 2003-4 by Yves Goergen