Thread Problem mit WWW::Mechanize: oder was anderem ;) (18 answers)
Opened by FlorianL at 2007-06-30 21:35

FlorianL
 2007-07-02 15:26
#37587 #37587
User since
2007-05-18
142 Artikel
BenutzerIn
[default_avatar]
Ist mir ja schon fast peinlich das zu posten, hab hier soooo viel rumgefrickelt...

aber ich glaube das läuft soweit, kanns wohl gerad nicht testen weil das board down ist, aber is ja latte, her mit euren vorschlägen, was hätte ich besser machen können? Wo seht ihr ganz klar schlampigen code? (goto! ;) )

Code (perl): (dl )
1
2
3
4
5
6
7
8
9
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#!/usr/bin/perl
#
# ibProBattle Bot
#  (c) Mindfuck 2007
#
#  This Bot automates the Invision Power Board -Board Game ibP-Battle
#
#  Features & HowTo: rtfm!
#
#######################
use strict;
use warnings;
use WWW::Mechanize;
use HTML::TokeParser;
use HTTP::Cookies;
use Term::ANSIColor;
############################## CONFIG #########################################

my $guild = "0";            # Use guild mode? # NOT Implemented...
my $DEBUG = "1";            # Show debug messages?
my $username = "Mindfuck";        # ipB Login
my $password = "Gehaim";        # Your Password
my $url = "usb.unitedsb.de";        # ipB Url
my $protection = '1';            # Use suicide Protection?
my $deathtimer = '5';            # Check timer when youre dead...
my @friends = (10202,7222);        # Friend Id's
my $healthwatcher = '1';        # fork() and check if we need Hp?
my $delay = '151';            # Delay between Attacks
my $max_attacks = '5';            # How many Attacks should we do?
my $watchdelay = '60';            # Time between Health-Checks in seconds
my $watchlog = '0';            # Show Healthwatcher messages?
my $spread = '15';            # Randomize our timers :)

################# NO NEED TO EDIT BELOW THIS LINE #############################
my $agent = WWW::Mechanize->new();
$agent->cookie_jar(HTTP::Cookies->new);
my $count = '1';
our $stop = '0';
my $version = "0.2b";

sub gettime {
    chomp(my @time = localtime);
    my $time2 = "$time[2]:$time[1]:$time[0] ";
    return $time2;
}

sub randomizer {
        my $seconds = shift;
    my $rand_number = int(rand($spread));
    my $twisted = ($seconds + $rand_number);
    return $twisted;
}

sub timer {
        my $minutes = shift;
    my $seconds = ($minutes * 60);
    my $magic = randomizer($seconds);
    print gettime(); print("Sleeping for $minutes minutes\n");
    sleep($magic);
}

sub id2nick {
    my $id = shift;
    $agent->get("http://$url/index.php?showuser=$id");
    my $nick = $agent->content =~ m/<title>(.+) - Profil ansehen<\/title>/;
    my $dood = $1;
    return $dood;
}

sub logmein {
    CONNECT:
    if ($DEBUG == '1') { print("DEBUG: Accessing $url\n");}
        $agent->get("http://$url/index.php?act=Login&CODE=00");
        $agent->success or (sleep(600) and (goto CONNECT));
        $agent->form_name('LOGIN') or goto SLEEP;
        $agent->field('UserName',$username);
        $agent->field('PassWord',$password);
    if ($DEBUG == '1') { print("DEBUG: Logging in...\n");}
        $agent->submit();
    print("Send User\/Pass to $url\n");
}

sub attack {
    $agent->get("http://$url/index.php?act=atkshop&do=target");
        my @targetsite = $agent->content;
    if ($DEBUG == '1') { print("DEBUG: Parsing Victims...\n");}
    my @targets;
    foreach (@targetsite) {
        push @targets, $_ =~ m/act=attack&m=(\d+)/gs;
    }
    if ($DEBUG == '1') { print ("DEBUG: Found Target: @targets\n");}
    if ($DEBUG == '1') { print("DEBUG: Attacking...\n");}
        foreach (@targets) {
               
 my $target = $_;
               
 $agent->get("http://$url/index.php?act=attack&m=$target");
        if ($DEBUG == '1') { print("DEBUG: Choosing best Weapon...\n");}
               
 my @weaponlist = $agent->content =~ m/.+&w=(\d)/;
               
 my @weapons = sort(@weaponlist);
               
 if ((defined $weapons[0]) and ($stop == '0') and ($target != @friends)) {
            if ($DEBUG == '1') { print("DEBUG: Attack \#$count\n");}
               
         $agent->get("http://$url/index.php?act=attack&m=$target&w=$weapons[0]");
               
         print gettime(); print("\#$count $username Attacked Target id "); print id2nick($target); print(" with Weapon $weapons[0]\n");
               
         $count++;
            healcheck();
               
 } else {
               
         print gettime(); print("Cant attack "); print id2nick($_); print(" \(Id: $_\), moving on to next Victim\n")
               
 }
               
 if ($count == $max_attacks) {
               
         print gettime(); print("Attacked $max_attacks Targets, stopping...\n");
            healcheck();
               
         goto SLEEP;
               
 }
        }
    if ($DEBUG == '1') { print("DEBUG: Attack finished\n");}
}

sub healcheck {
    if ($DEBUG == '1') { print gettime(); print("DEBUG: Checking if we need a Potion\n");}
    $agent->get("http://$url/index.php?act=atkshop&do=target");
    my @hpmaxhp = $agent->content =~ m/.+<strong>Hp: <\/strong> (\d+)\/(\d+)/;
    my $hp = $hpmaxhp[0]; my $maxhp = $hpmaxhp[1];
    my $prozente = $hp * 100 / $maxhp;
    if ($prozente == '0') {
        if ($DEBUG == '1') { print("DEBUG: Im Dead!\n");}
        my $deathsleep = ($deathtimer * 60);
        print gettime(); print("!!!YOU ARE DEAD!!!\n");
        print gettime(); print("Sleeping for $deathtimer minutes\n");
        sleep($deathsleep);
        goto START;
    } elsif ($prozente < '25') {
        if ($DEBUG == '1') { print("DEBUG: Health below 25%, trying to use middle potion.\n");}
        heal("25");
    } elsif ($prozente < '101') {
        if ($DEBUG == '1') { print("DEBUG: Health below 55%, trying to use small potion.\n");}
        heal("50");
    }
}

sub heal {
    my $pro = shift;
    if ($pro == '50') {
        print("Got 50 procent\n");
        $agent->get("http://$url/index.php?act=atkshop");
        my @shopsite = $agent->content;
        my $money1 = $shopsite[0];
        my ($money) = $money1 =~ m/Euro:<\/strong> (\d+(?:,\d+)?)/;
        $money = ~ m/.+Euro:<\/strong> (\d+)<br>/;
        my $s_potions = $shopsite[0];
        $s_potions =~ m/.+>War Pills \((\d+)\)</;
        print("Money: $money \| Small Potions: $s_potions\n");
        if ($s_potions > 1) {
            print gettime(); print("Using small Potion\n");
            $agent->get("http://$url/index.php?act=atkshop&do=use&item=1");
        } elsif (($s_potions == '0') and ($money > '125')) {
            print gettime(); print("Buying small Potion\n");
            $agent->get("http://$url/index.php?act=atkshop&do=buy&item=1");
            print gettime(); print("Using small Potion\n");
            $agent->get("http://$url/index.php?act=atkshop&do=use&item=1");
        } elsif (($s_potions == '0') and ($money < '125')) {
            print gettime(); print("No Money, No Potions, sleeping...\n");
            goto SLEEP;
        }
        } elsif ($pro == '25') {
            print("Got 25 procent\n");
            $agent->get("http://$url/index.php?act=atkshop");
            my @shopsite = $agent->content;
            my $money1 = $shopsite[0];
            my ($money) = $money1 =~ m/Euro:<\/strong> (\d+(?:,\d+)?)/;
            my $s_potions = $shopsite[0];
            $s_potions =~ m/.+>War Pills \((\d+)\)</;
            print("Money: $money \| Small Potions: $s_potions\n");
            if ($s_potions > '1') {
            print gettime(); print("Using small Potion\n");
            $agent->get("http://$url/index.php?act=atkshop&do=use&item=1");
        } elsif (($s_potions == '0') and ($money > '125')) {
            print gettime(); print("Buying small Potion\n");
            $agent->get("http://$url/index.php?act=atkshop&do=buy&item=1");
            print gettime(); print("Using small Potion\n");
            $agent->get("http://$url/index.php?act=atkshop&do=use&item=1");
        } elsif (($s_potions == '0') and ($money < '125')) {
            print gettime(); print("No Money, No Potions, sleeping...\n");
            goto SLEEP;
        }
    }
}

sub healthwatch {
    logmein();
    WATCHER:
    if ($watchlog == '1') { print gettime(); print("Checking if we need a Potion\n"); }
    healcheck();
    sleep($watchdelay);
    goto WATCHER;
}

main {
#    system("clear");
    if ($DEBUG == '1') { print gettime(); print("DEBUGGING STARTED\n\n");}
    print color("white"), "Mindfucks ipB Battle Bot version $version\n", color("reset");
    if ($healthwatcher == '1') {
        if(! defined(my $pid= fork())){ die "Error on Fork\n";}
        elsif($pid==0){
            healthwatch();
        }
        else{
            START:
            logmein();
            healcheck();
            attack();
            if ($DEBUG == '1') { print gettime(); print("DEBUGGING ENDED\n");}
            SLEEP:
            timer($delay);
            goto START;
        }
    } else {
        START:
        logmein();
        healcheck();
        attack();
        if ($DEBUG == '1') { print("DEBUGGING ENDED\n");}
        SLEEP:
        timer($delay);
        goto START;
    }
}

View full thread Problem mit WWW::Mechanize: oder was anderem ;)