| 1 | # | ||
| 2 | # $Id: iesms.pm 350 2008-11-27 13:40:14Z mackers $ | ||
| 3 | |||
| 4 | package WWW::SMS::IE::iesms; | ||
| 5 | |||
| 6 | =head1 NAME | ||
| 7 | |||
| 8 | WWW::SMS::IE::iesms - A module to send SMS messages using .ie websites | ||
| 9 | |||
| 10 | =head1 SYNOPSIS | ||
| 11 | |||
| 12 | To use a subclass: | ||
| 13 | |||
| 14 | require WWW::SMS::IE::o2sms; | ||
| 15 | |||
| 16 | my $carrier = new WWW::SMS::IE::o2sms; | ||
| 17 | |||
| 18 | if ($carrier->login('o2_user', 'password')) | ||
| 19 | { | ||
| 20 | my $retval = $carrier->send('+353865551234', 'Hello World!'); | ||
| 21 | |||
| 22 | if (!$retval) | ||
| 23 | { | ||
| 24 | print $carrier->error() . "\n"; | ||
| 25 | } | ||
| 26 | } | ||
| 27 | |||
| 28 | To extend this class: | ||
| 29 | |||
| 30 | package WWW::SMS::IE::o2sms; | ||
| 31 | require WWW::SMS::IE::iesms; | ||
| 32 | @WWW::SMS::IE::o2sms::ISA = qw{WWW::SMS::IE::iesms}; | ||
| 33 | |||
| 34 | =head1 DESCRIPTION | ||
| 35 | |||
| 36 | L<WWW::SMS::IE::iesms> is a module to send SMS messages via the command line | ||
| 37 | using the websites of Irish mobile operators. This is done by simulating a web | ||
| 38 | browser's interaction with those websites. This module requires a valid web | ||
| 39 | account with O2 Ireland, Vodafone Ireland or Meteor Ireland. | ||
| 40 | |||
| 41 | The L<WWW::SMS::IE::iesms> class is abstract, i.e. it is only used as a base | ||
| 42 | class for L<WWW::SMS::IE::o2sms>, L<WWW::SMS::IE::vodasms>, | ||
| 43 | L<WWW::SMS::IE::meteorsms> and L<WWW::SMS::IE::threesms> and should never be | ||
| 44 | instantiated as itself. | ||
| 45 | |||
| 46 | The following methods are available: | ||
| 47 | |||
| 48 | =over 4 | ||
| 49 | |||
| 50 | =cut | ||
| 51 | |||
| 52 | use strict; | ||
| 53 | use warnings; | ||
| 54 | use vars qw( $VERSION ); | ||
| 55 | $VERSION = sprintf("0.%02d", q$Revision: 350 $ =~ /(\d+)/); | ||
| 56 | |||
| 57 | #use TestGen4Web::Runner 0.04; | ||
| 58 | use File::stat; | ||
| 59 | use Storable; | ||
| 60 | use File::Basename; | ||
| 61 | use File::Temp; | ||
| 62 | use POSIX qw(ceil); | ||
| 63 | use Data::Dumper; | ||
| 64 | |||
| 65 | use constant LOGIN_LIFETIME => 60 * 30; | ||
| 66 | use constant TG4W_VERIFY_TITLES => 0; | ||
| 67 | use constant TG4W_QUIET => 0; | ||
| 68 | $Storable::forgive_me = 1; | ||
| 69 | |||
| 70 | my %iesms_user_agent_strings = ( | ||
| 71 | "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)" => 1.0, | ||
| 72 | "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)" => 0.4, | ||
| 73 | "Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 4.0)" => 0.2, | ||
| 74 | "Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0)" => 0.7, | ||
| 75 | "Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.1)" => 0.4, | ||
| 76 | "Mozilla/4.0 (compatible; MSIE 5.21; Mac_PowerPC)" => 0.2, | ||
| 77 | "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; Q321120)" => 0.6, | ||
| 78 | "Mozilla/4.0 (compatible; MSIE 6.0; Windows 98)" => 0.4, | ||
| 79 | "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.5) Gecko/20041107 Firefox/1.0" => 0.9, | ||
| 80 | "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.0.1) Gecko/20020823 Netscape/7.0" => 0.7, | ||
| 81 | "Mozilla/5.0 (X11; U; SunOS sun4u; en-US; rv:1.1) Gecko/20020827" => 0.1, | ||
| 82 | "Mozilla/5.0 (X11; U; NetBSD i386; rv:1.7.3) Gecko/20041104 Firefox/0.10.1" => 0.1, | ||
| 83 | "Opera/6.05 (Windows 2000; U) [en]" => 0.4, | ||
| 84 | "Opera/7.0 (Windows 2000; U)" => 0.6, | ||
| 85 | "Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en) AppleWebKit/125.5.5 (KHTML, like Gecko) Safari/125.11" => 0.7, | ||
| 86 | ); | ||
| 87 | |||
| 88 | =item $carrier = new WWW::SMS::IE::iesms | ||
| 89 | |||
| 90 | This is the object constructor. It should only be called internally by this | ||
| 91 | library. External code should construct L<WWW::SMS::IE::o2sms>, | ||
| 92 | L<WWW::SMS::IE::vodasms>, L<WWW::SMS::IE::meteorsms> and | ||
| 93 | L<WWW::SMS::IE::threesms> (and L<WWW::SMS::IE::aftsms>) objects. | ||
| 94 | |||
| 95 | =cut | ||
| 96 | |||
| 97 | sub new | ||
| 98 | { | ||
| 99 | my $class = shift; | ||
| 100 | my $self = {}; | ||
| 101 | |||
| 102 | bless ($self, $class); | ||
| 103 | |||
| 104 | $self->debug(0); | ||
| 105 | $self->login_lifetime(LOGIN_LIFETIME); | ||
| 106 | |||
| 107 | $self->_init(); | ||
| 108 | |||
| 109 | return $self; | ||
| 110 | } | ||
| 111 | |||
| 112 | sub _init_tg4w_runner | ||
| 113 | { | ||
| 114 | my $self = shift; | ||
| 115 | |||
| 116 | return 1 if (defined($self->{tg4w_runner})); | ||
| 117 | |||
| 118 | require TestGen4Web::Runner; | ||
| 119 | |||
| 120 | $self->{tg4w_runner} = new TestGen4Web::Runner(debug=>($self->debug())); | ||
| 121 | |||
| 122 | if ($self->{tg4w_runner}->VERSION < 0.11) | ||
| 123 | { | ||
| 124 | $self->_log_error("TestGen4Web::Runner version 0.11 or higher required"); | ||
| 125 | |||
| 126 | return 0; | ||
| 127 | } | ||
| 128 | |||
| 129 | $self->{tg4w_runner}->user_agent()->agent($self->_choose_agent_string()); | ||
| 130 | $self->{tg4w_runner}->verify_titles(TG4W_VERIFY_TITLES); | ||
| 131 | $self->{tg4w_runner}->quiet(TG4W_QUIET); | ||
| 132 | $self->{tg4w_runner}->load($self->_action_file()); | ||
| 133 | $self->{tg4w_runner}->debug($self->{debug}); | ||
| 134 | |||
| 135 | if ($self->cookie_file() =~ m#/#) | ||
| 136 | { | ||
| 137 | $self->{tg4w_runner}->cookie_jar_file($self->cookie_file()); | ||
| 138 | } | ||
| 139 | else | ||
| 140 | { | ||
| 141 | $self->_log_debug("won't write '" . $self->cookie_file() . "' to working directory"); | ||
| 142 | } | ||
| 143 | |||
| 144 | return 1; | ||
| 145 | } | ||
| 146 | |||
| 147 | =item $carrier->login($username, $password) | ||
| 148 | |||
| 149 | Logs in to the mobile operator's website. If provided, use the username and password specified | ||
| 150 | in C<$username> and C<$password>, otherwise use the username and password provided with the | ||
| 151 | C<username()> and C<password()> methods. | ||
| 152 | |||
| 153 | Returns true on success, false on failure. The last error message is returned by the C<error()> | ||
| 154 | method. | ||
| 155 | |||
| 156 | =cut | ||
| 157 | |||
| 158 | sub login | ||
| 159 | { | ||
| 160 | my ($self, $username, $password) = @_; | ||
| 161 | |||
| 162 | # if ($self->is_logged_in()) | ||
| 163 | # { | ||
| 164 | # $self->_log_debug("last login still active, reusing"); | ||
| 165 | # | ||
| 166 | # return 1; | ||
| 167 | # } | ||
| 168 | |||
| 169 | $self->_init_tg4w_runner() if (!$self->{tg4w_runner}); | ||
| 170 | |||
| 171 | $self->username($username) if (defined($username)); | ||
| 172 | $self->password($password) if (defined($password)); | ||
| 173 | |||
| 174 | if (defined($self->{tg4w_runner}->cookie_jar())) | ||
| 175 | { | ||
| 176 | $self->_clear_cookie_jar(); | ||
| 177 | } | ||
| 178 | |||
| 179 | $self->_log_debug("about to log in to '" . $self->domain_name() . "' with username '" . $self->username() . "' and password '" . $self->password() . "'"); | ||
| 180 | $self->_log_debug("user agent set to '" . $self->{tg4w_runner}->user_agent()->agent() . "'"); | ||
| 181 | |||
| 182 | my $retval = $self->{tg4w_runner}->run($self->_login_start_step(), $self->_login_end_step()); | ||
| 183 | |||
| 184 | # save the error | ||
| 185 | $self->error($self->{tg4w_runner}->error()) if (!$retval); | ||
| 186 | |||
| 187 | $self->{matches} = $self->{tg4w_runner}->matches(); | ||
| 188 | |||
| 189 | $self->_log_debug("login " . ($retval?"succeeded":"failed with error '" . $self->error() . "'")); | ||
| 190 | $self->_log_debug(scalar(@{$self->{matches}}) . " matches found in last assertion"); | ||
| 191 | |||
| 192 | if (!$retval) | ||
| 193 | { | ||
| 194 | $self->_clear_cookie_jar(); | ||
| 195 | } | ||
| 196 | |||
| 197 | $self->_save_action_state(); | ||
| 198 | |||
| 199 | return $retval; | ||
| 200 | } | ||
| 201 | |||
| 202 | =item $carrier->send($number, $message) | ||
| 203 | |||
| 204 | Sends the SMS message in C<$message> to the recipient in C<$number>. | ||
| 205 | |||
| 206 | Returns true on success, false on failure. The last error message is returned by the C<error()> | ||
| 207 | method. | ||
| 208 | |||
| 209 | This method will fail if there is no login window, the number is invalid, or if there is | ||
| 210 | a problem sending via the operator. | ||
| 211 | |||
| 212 | After sending, the number of free messages remaining in your account is returned by | ||
| 213 | C<remaining_messages()>. | ||
| 214 | |||
| 215 | =cut | ||
| 216 | |||
| 217 | sub send | ||
| 218 | { | ||
| 219 | my $self = shift; | ||
| 220 | |||
| 221 | return $self->_real_send(@_); | ||
| 222 | } | ||
| 223 | |||
| 224 | sub _real_send | ||
| 225 | { | ||
| 226 | my ($self, $raw_number, $message) = @_; | ||
| 227 | |||
| 228 | $self->_init_tg4w_runner() if (!$self->{tg4w_runner}); | ||
| 229 | |||
| 230 | # validate this number | ||
| 231 | my $number = $self->validate_number($raw_number); | ||
| 232 | |||
| 233 | my $charsleft = $self->max_length() - length($message); | ||
| 234 | |||
| 235 | if ($number == -1) | ||
| 236 | { | ||
| 237 | $self->error("Invalid number: $raw_number"); | ||
| 238 | return 0; | ||
| 239 | } | ||
| 240 | |||
| 241 | # format number how this operator likes it | ||
| 242 | $number = $self->_format_number($number); | ||
| 243 | |||
| 244 | # pad message with spaces if less than min_length | ||
| 245 | if (length($message) < $self->min_length()) | ||
| 246 | { | ||
| 247 | $message .= " " x ($self->min_length() - length($message)); | ||
| 248 | $self->_log_warning("padded message to " . $self->min_length() . " characters"); | ||
| 249 | } | ||
| 250 | |||
| 251 | # trim message to max length (multiple messages should be handled by the client) | ||
| 252 | if (length($message) > $self->max_length()) | ||
| 253 | { | ||
| 254 | $message = substr($message, 0, $self->max_length()); | ||
| 255 | $self->_log_warning("trimmed message to " . $self->max_length() . " characters"); | ||
| 256 | } | ||
| 257 | |||
| 258 | # try to load action state from file if not in memory | ||
| 259 | if (!defined($self->{tg4w_runner}->action_state())) | ||
| 260 | { | ||
| 261 | if (!$self->_load_action_state()) | ||
| 262 | { | ||
| 263 | $self->_log_debug("Failed to load action state file"); | ||
| 264 | |||
| 265 | # is this so bad? - action state files only needed for re-using logins... | ||
| 266 | #$self->error("Failed to load action state file"); | ||
| 267 | #return 0; | ||
| 268 | } | ||
| 269 | } | ||
| 270 | |||
| 271 | # save this state as this is where we always want to start sending from | ||
| 272 | my $action_state = $self->{tg4w_runner}->action_state(); | ||
| 273 | |||
| 274 | $self->{tg4w_runner}->set_replacement("recipient", $number); | ||
| 275 | $self->{tg4w_runner}->set_replacement("message", $message); | ||
| 276 | $self->{tg4w_runner}->set_replacement("delay", $self->delay($message)); | ||
| 277 | $self->{tg4w_runner}->set_replacement("charsleft", $charsleft); | ||
| 278 | $self->{tg4w_runner}->set_replacement("username", $self->{username}); | ||
| 279 | $self->{tg4w_runner}->set_replacement("password", $self->{password}); | ||
| 280 | |||
| 281 | $self->remaining_messages('?'); | ||
| 282 | |||
| 283 | # send the message | ||
| 284 | $self->_log_debug("about to send the message '" . $message . "' to the recipient '" . $number . "'"); | ||
| 285 | |||
| 286 | if ($self->{dummy_send}) | ||
| 287 | { | ||
| 288 | $self->_log_debug("performed dummy send"); | ||
| 289 | return 1; | ||
| 290 | } | ||
| 291 | |||
| 292 | my $retval = $self->{tg4w_runner}->run($self->_send_start_step(), $self->_send_end_step()); | ||
| 293 | |||
| 294 | # save the error | ||
| 295 | $self->error($self->{tg4w_runner}->error()) if (!$retval); | ||
| 296 | |||
| 297 | # handle the matches | ||
| 298 | $self->{matches} = $self->{tg4w_runner}->matches(); | ||
| 299 | |||
| 300 | if (defined($self->{matches}[$self->_remaining_messages_match()])) | ||
| 301 | { | ||
| 302 | $self->remaining_messages($self->{matches}[$self->_remaining_messages_match()]); | ||
| 303 | } | ||
| 304 | |||
| 305 | $self->_log_debug("send " . ($retval?"succeeded":"failed with error '" . $self->error() . "'")); | ||
| 306 | $self->_log_debug(scalar(@{$self->{matches}}) . " matches found in last assertion"); | ||
| 307 | |||
| 308 | # return the action state to what it was before | ||
| 309 | $self->{tg4w_runner}->action_state($action_state); | ||
| 310 | $self->_save_action_state(); | ||
| 311 | |||
| 312 | return $retval; | ||
| 313 | } | ||
| 314 | |||
| 315 | =item $carrier->validate_number($number) | ||
| 316 | |||
| 317 | Returns the telephone number in C<$number> converted to the international format, e.g. | ||
| 318 | C<+353865551234>. | ||
| 319 | |||
| 320 | Also verifies that the mobile provider can send to this number. | ||
| 321 | |||
| 322 | Returns -1 if the number is invalid, C<validate_number_error()> gets the error message. | ||
| 323 | |||
| 324 | =cut | ||
| 325 | |||
| 326 | sub validate_number | ||
| 327 | { | ||
| 328 | my ($self, $number) = @_; | ||
| 329 | |||
| 330 | # strip whitespace and hyphens from the number | ||
| 331 | $number =~ s/[\s\-]//g; | ||
| 332 | |||
| 333 | if ($number =~ /[^\d\+]/) | ||
| 334 | { | ||
| 335 | # has an invalid character | ||
| 336 | $self->validate_number_error("number has an invalid character"); | ||
| 337 | return -1; | ||
| 338 | } | ||
| 339 | elsif ($number =~ /^0(8[3567])(\d*)/) | ||
| 340 | { | ||
| 341 | # is an irish mobile number - check length | ||
| 342 | if (length($number) != 10) | ||
| 343 | { | ||
| 344 | $self->validate_number_error("number is the wrong length for an Irish mobile number"); | ||
| 345 | return -1; | ||
| 346 | } | ||
| 347 | |||
| 348 | # length ok - make international | ||
| 349 | $number = "+353$1$2"; | ||
| 350 | } | ||
| 351 | elsif ($number =~ /^\+(\d*)/) | ||
| 352 | { | ||
| 353 | # is a plus style international number -- is ok | ||
| 354 | } | ||
| 355 | elsif ($number =~ /^00(\d*)/) | ||
| 356 | { | ||
| 357 | # is an 00 international number -- make plus | ||
| 358 | $number = "+$1"; | ||
| 359 | } | ||
| 360 | elsif ($number =~ /959\d{2,5}/) | ||
| 361 | { | ||
| 362 | # is an aft 959xxxx number | ||
| 363 | } | ||
| 364 | else | ||
| 365 | { | ||
| 366 | # don't know what this is | ||
| 367 | $self->validate_number_error("number is badly formed"); | ||
| 368 | return -1; | ||
| 369 | } | ||
| 370 | |||
| 371 | if (!$self->_is_valid_number($number)) | ||
| 372 | { | ||
| 373 | return -1; | ||
| 374 | } | ||
| 375 | |||
| 376 | return $number; | ||
| 377 | } | ||
| 378 | |||
| 379 | =item $carrier->validate_number_error() | ||
| 380 | |||
| 381 | After a unsuccessful C<validate_number()>, returns the error message. | ||
| 382 | |||
| 383 | =cut | ||
| 384 | |||
| 385 | sub validate_number_error | ||
| 386 | { | ||
| 387 | defined($_[1]) ? $_[0]->{validate_number_error} = $_[1] : $_[0]->{validate_number_error}; | ||
| 388 | } | ||
| 389 | |||
| 390 | =item $carrier->is_logged_in() | ||
| 391 | |||
| 392 | Returns success if C<login()> has been called successfully in this or a previous instance and if the | ||
| 393 | login window has not expired. | ||
| 394 | |||
| 395 | =cut | ||
| 396 | |||
| 397 | sub is_logged_in() | ||
| 398 | { | ||
| 399 | my ($self) = @_; | ||
| 400 | |||
| 401 | if (-f $self->cookie_file()) | ||
| 402 | { | ||
| 403 | my $fstat = stat($self->cookie_file()); | ||
| 404 | |||
| 405 | if ($fstat && ($fstat->mtime + $self->login_lifetime() > time())) | ||
| 406 | { | ||
| 407 | $self->_log_debug("found a fresh cookie file at " . $self->cookie_file()); | ||
| 408 | |||
| 409 | # touch to keep it up to date | ||
| 410 | utime (time, time, $self->cookie_file()); | ||
| 411 | |||
| 412 | return 1; | ||
| 413 | } | ||
| 414 | else | ||
| 415 | { | ||
| 416 | $self->_log_debug("found an old cookie file at " . $self->cookie_file()); | ||
| 417 | |||
| 418 | return 0; | ||
| 419 | } | ||
| 420 | } | ||
| 421 | |||
| 422 | $self->_log_debug("no cookie file found at " . $self->cookie_file()); | ||
| 423 | |||
| 424 | return 0; | ||
| 425 | } | ||
| 426 | |||
| 427 | =item $carrier->delay($message) | ||
| 428 | |||
| 429 | Some carriers required a pause of X seconds before the message is sent. This is | ||
| 430 | performed automatically by the C<send()> method. | ||
| 431 | |||
| 432 | This method will return how long that delay will be. | ||
| 433 | |||
| 434 | =cut | ||
| 435 | |||
| 436 | sub delay | ||
| 437 | { | ||
| 438 | my ($self, $message) = @_; | ||
| 439 | |||
| 440 | my $delay = ceil(length($message) * $self->_simulated_delay_perchar()); | ||
| 441 | $delay = $self->_simulated_delay_max() if ($delay > $self->_simulated_delay_max()); | ||
| 442 | $delay = $self->_simulated_delay_min() if ($delay < $self->_simulated_delay_min()); | ||
| 443 | |||
| 444 | return $delay; | ||
| 445 | } | ||
| 446 | |||
| 447 | =item $carrier->login_lifetime() | ||
| 448 | |||
| 449 | Set/retrieve the maximum length of time (seconds), between requests that a session is alive for. | ||
| 450 | |||
| 451 | =cut | ||
| 452 | |||
| 453 | sub login_lifetime | ||
| 454 | { | ||
| 455 | defined($_[1]) ? $_[0]->{login_lifetime} = $_[1] : $_[0]->{login_lifetime}; | ||
| 456 | } | ||
| 457 | |||
| 458 | =item $carrier->max_length() | ||
| 459 | |||
| 460 | Return the supported maximum length of a single SMS message. | ||
| 461 | |||
| 462 | This method can be overwritten by subclasses extending this class. | ||
| 463 | |||
| 464 | =cut | ||
| 465 | |||
| 466 | sub max_length | ||
| 467 | { | ||
| 468 | return 160; | ||
| 469 | } | ||
| 470 | |||
| 471 | =item $carrier->min_length() | ||
| 472 | |||
| 473 | Return the supported minimum length of a single SMS message. | ||
| 474 | |||
| 475 | This method can be overwritten by subclasses extending this class. | ||
| 476 | |||
| 477 | =cut | ||
| 478 | |||
| 479 | sub min_length | ||
| 480 | { | ||
| 481 | return 0; | ||
| 482 | } | ||
| 483 | |||
| 484 | |||
| 485 | =item $carrier->remaining_messages() | ||
| 486 | |||
| 487 | After a C<send()>, this method returns the number of free messages remaining in your account. | ||
| 488 | |||
| 489 | =cut | ||
| 490 | |||
| 491 | sub remaining_messages | ||
| 492 | { | ||
| 493 | defined($_[1]) ? $_[0]->{remaining_messages} = $_[1] : $_[0]->{remaining_messages}; | ||
| 494 | } | ||
| 495 | |||
| 496 | =item $carrier->write_message_file($message) | ||
| 497 | |||
| 498 | Write the string <$message> to the message file as returned by C<message_file()>. | ||
| 499 | |||
| 500 | =cut | ||
| 501 | |||
| 502 | sub write_message_file | ||
| 503 | { | ||
| 504 | my $msgfile; | ||
| 505 | my $self = $_[0]; | ||
| 506 | my $message = $_[1]; | ||
| 507 | |||
| 508 | chomp($message); | ||
| 509 | |||
| 510 | if ($msgfile = $self->message_file()) | ||
| 511 | { | ||
| 512 | if ($msgfile !~ m#/#) | ||
| 513 | { | ||
| 514 | $self->_log_debug("won't write '$msgfile' to working directory"); | ||
| 515 | } | ||
| 516 | elsif (open (SMSMSG, "> $msgfile")) | ||
| 517 | { | ||
| 518 | print SMSMSG $message . "\n"; | ||
| 519 | close (SMSMSG); | ||
| 520 | } | ||
| 521 | else | ||
| 522 | { | ||
| 523 | $self->_log_warning("can't open message file '$msgfile' for writing: $@"); | ||
| 524 | } | ||
| 525 | } | ||
| 526 | |||
| 527 | return 1; | ||
| 528 | } | ||
| 529 | |||
| 530 | =item $carrier->write_history_file($message) | ||
| 531 | |||
| 532 | Append the string <$message> to the history file as returned by C<history_file()>. | ||
| 533 | |||
| 534 | =cut | ||
| 535 | |||
| 536 | sub write_history_file | ||
| 537 | { | ||
| 538 | my $histfile; | ||
| 539 | my $self = $_[0]; | ||
| 540 | my $message = $_[1]; | ||
| 541 | my $recipient = $_[2]; | ||
| 542 | |||
| 543 | chomp($message); | ||
| 544 | |||
| 545 | if ($histfile = $self->history_file()) | ||
| 546 | { | ||
| 547 | if ($histfile !~ m#/#) | ||
| 548 | { | ||
| 549 | $self->_log_debug("won't write '$histfile' to working directory"); | ||
| 550 | } | ||
| 551 | elsif (open (SMSMSG, ">> $histfile")) | ||
| 552 | { | ||
| 553 | print SMSMSG "-- to $recipient at " . localtime() . " --\n"; | ||
| 554 | print SMSMSG $message . "\n"; | ||
| 555 | print SMSMSG "\n"; | ||
| 556 | close (SMSMSG); | ||
| 557 | } | ||
| 558 | else | ||
| 559 | { | ||
| 560 | $self->_log_warning("can't open history file '$histfile' for appending $@"); | ||
| 561 | } | ||
| 562 | } | ||
| 563 | |||
| 564 | return 1; | ||
| 565 | } | ||
| 566 | |||
| 567 | =item $carrier->username() | ||
| 568 | |||
| 569 | =item $carrier->password() | ||
| 570 | |||
| 571 | These methods get and set the username and password parameters respectively | ||
| 572 | used to log in to your provider's website. | ||
| 573 | |||
| 574 | =cut | ||
| 575 | |||
| 576 | sub username | ||
| 577 | { | ||
| 578 | if (defined($_[1])) | ||
| 579 | { | ||
| 580 | $_[0]->{username} = $_[1]; | ||
| 581 | $_[0]->{tg4w_runner}->set_replacement("username", $_[1]); | ||
| 582 | } | ||
| 583 | else | ||
| 584 | { | ||
| 585 | return $_[0]->{username} || ""; | ||
| 586 | } | ||
| 587 | } | ||
| 588 | |||
| 589 | sub password | ||
| 590 | { | ||
| 591 | if (defined($_[1])) | ||
| 592 | { | ||
| 593 | $_[0]->{password} = $_[1]; | ||
| 594 | $_[0]->{tg4w_runner}->set_replacement("password", $_[1]); | ||
| 595 | } | ||
| 596 | else | ||
| 597 | { | ||
| 598 | return $_[0]->{password} || ""; | ||
| 599 | } | ||
| 600 | } | ||
| 601 | |||
| 602 | sub cookie_file | ||
| 603 | { | ||
| 604 | if (defined($_[1])) | ||
| 605 | { | ||
| 606 | my ($filename, $directories, $suffix) = fileparse($_[1]); | ||
| 607 | mkdir ($directories, 0777) unless (-d $directories); | ||
| 608 | |||
| 609 | $_[0]->{cookie_jar_file} = $_[1]; | ||
| 610 | |||
| 611 | $_[0]->{tg4w_runner}->cookie_jar_file($_[1]) if ($_[0]->{tg4w_runner}); | ||
| 612 | } | ||
| 613 | else | ||
| 614 | { | ||
| 615 | return $_[0]->_abs_cf($_[0]->{cookie_jar_file}); | ||
| 616 | } | ||
| 617 | } | ||
| 618 | |||
| 619 | sub action_state_file | ||
| 620 | { | ||
| 621 | my $self = $_[0]; | ||
| 622 | my $action_state_file = $_[1]; | ||
| 623 | |||
| 624 | if (defined($action_state_file)) | ||
| 625 | { | ||
| 626 | $self->{action_state_file} = $action_state_file; | ||
| 627 | } | ||
| 628 | else | ||
| 629 | { | ||
| 630 | return $self->_abs_cf($self->{action_state_file}); | ||
| 631 | } | ||
| 632 | } | ||
| 633 | |||
| 634 | sub message_file | ||
| 635 | { | ||
| 636 | defined($_[1]) ? $_[0]->{message_file} = $_[1] : $_[0]->_abs_cf($_[0]->{message_file}); | ||
| 637 | } | ||
| 638 | |||
| 639 | sub history_file | ||
| 640 | { | ||
| 641 | defined($_[1]) ? $_[0]->{history_file} = $_[1] : $_[0]->_abs_cf($_[0]->{history_file}); | ||
| 642 | } | ||
| 643 | |||
| 644 | =item $carrier->full_name() | ||
| 645 | |||
| 646 | =item $carrier->domain_name() | ||
| 647 | |||
| 648 | These methods get/set the descriptive name and domain name respectively of this mobile operator. | ||
| 649 | |||
| 650 | These methods should be overwritten by subclasses extending this class. | ||
| 651 | |||
| 652 | =cut | ||
| 653 | |||
| 654 | sub full_name | ||
| 655 | { | ||
| 656 | defined($_[1]) ? $_[0]->{full_name} = $_[1] : $_[0]->{full_name}; | ||
| 657 | } | ||
| 658 | |||
| 659 | sub domain_name | ||
| 660 | { | ||
| 661 | defined($_[1]) ? $_[0]->{domain_name} = $_[1] : $_[0]->{domain_name}; | ||
| 662 | } | ||
| 663 | |||
| 664 | =item $carrier->is_vodafone() | ||
| 665 | |||
| 666 | =item $carrier->is_o2() | ||
| 667 | |||
| 668 | =item $carrier->is_meteor() | ||
| 669 | |||
| 670 | =item $carrier->is_three() | ||
| 671 | |||
| 672 | =item $carrier->is_aft() | ||
| 673 | |||
| 674 | These methods are used to determine what subclass is extending this class. | ||
| 675 | |||
| 676 | =cut | ||
| 677 | |||
| 678 | sub is_vodafone | ||
| 679 | { | ||
| 680 | return 0; | ||
| 681 | } | ||
| 682 | |||
| 683 | sub is_o2 | ||
| 684 | { | ||
| 685 | return 0; | ||
| 686 | } | ||
| 687 | |||
| 688 | sub is_meteor | ||
| 689 | { | ||
| 690 | return 0; | ||
| 691 | } | ||
| 692 | |||
| 693 | sub is_three | ||
| 694 | { | ||
| 695 | return 0; | ||
| 696 | } | ||
| 697 | |||
| 698 | sub is_aft | ||
| 699 | { | ||
| 700 | return 0; | ||
| 701 | } | ||
| 702 | |||
| 703 | =item $carrier->user_agent() | ||
| 704 | |||
| 705 | Return the C<LWP::UserAgent> object used internally. | ||
| 706 | |||
| 707 | =cut | ||
| 708 | |||
| 709 | sub user_agent | ||
| 710 | { | ||
| 711 | my $self = shift; | ||
| 712 | |||
| 713 | if (!defined($self->{tg4w_runner})) | ||
| 714 | { | ||
| 715 | $self->_init_tg4w_runner(); | ||
| 716 | } | ||
| 717 | |||
| 718 | return $self->{tg4w_runner}->user_agent(); | ||
| 719 | } | ||
| 720 | |||
| 721 | =item $carrier->config_file() | ||
| 722 | |||
| 723 | =item $carrier->config_dir() | ||
| 724 | |||
| 725 | Set/retrieve the location of the config dir/file. | ||
| 726 | |||
| 727 | =cut | ||
| 728 | |||
| 729 | sub config_file | ||
| 730 | { | ||
| 731 | defined($_[1]) ? $_[0]->{config_file} = $_[1] : $_[0]->_abs_cf($_[0]->{config_file}); | ||
| 732 | } | ||
| 733 | |||
| 734 | |||
| 735 | sub config_dir | ||
| 736 | { | ||
| 737 | my ($self, $dir) = @_; | ||
| 738 | |||
| 739 | if (!defined($dir)) | ||
| 740 | { | ||
| 741 | return $self->{config_dir}; | ||
| 742 | } | ||
| 743 | |||
| 744 | $dir .= "/" if ($dir !~ m#/$#); | ||
| 745 | |||
| 746 | if (-d $dir) | ||
| 747 | { | ||
| 748 | $self->{config_dir} = $dir; | ||
| 749 | |||
| 750 | $self->_log_debug("using config dir '" . $dir . "'"); | ||
| 751 | |||
| 752 | return 1; | ||
| 753 | } | ||
| 754 | else | ||
| 755 | { | ||
| 756 | # create config dir, failing silently if can't | ||
| 757 | |||
| 758 | my $retval = mkdir($dir, 0700); | ||
| 759 | |||
| 760 | if ($retval) | ||
| 761 | { | ||
| 762 | $self->_log_debug("Created config directory '$dir'"); | ||
| 763 | $self->{config_dir} = $dir; | ||
| 764 | } | ||
| 765 | else | ||
| 766 | { | ||
| 767 | $self->_log_debug("Could not create config directory '$dir': $!"); | ||
| 768 | } | ||
| 769 | |||
| 770 | return $retval; | ||
| 771 | } | ||
| 772 | } | ||
| 773 | |||
| 774 | sub _abs_cf | ||
| 775 | { | ||
| 776 | my ($self, $filename) = @_; | ||
| 777 | |||
| 778 | if ($filename eq "") | ||
| 779 | { | ||
| 780 | return ""; | ||
| 781 | } | ||
| 782 | elsif ($filename =~ m#^/#) | ||
| 783 | { | ||
| 784 | return $filename; | ||
| 785 | } | ||
| 786 | elsif ($self->config_dir()) | ||
| 787 | { | ||
| 788 | return $self->config_dir() . $filename; | ||
| 789 | } | ||
| 790 | else | ||
| 791 | { | ||
| 792 | return $filename; | ||
| 793 | } | ||
| 794 | } | ||
| 795 | |||
| 796 | =item $carrier->cookie_file() | ||
| 797 | |||
| 798 | =item $carrier->action_state_file() | ||
| 799 | |||
| 800 | =item $carrier->message_file() | ||
| 801 | |||
| 802 | =item $carrier->history_file() | ||
| 803 | |||
| 804 | These methods get/set the location of the files used by this module | ||
| 805 | to store session data between invocations and to log messages. | ||
| 806 | |||
| 807 | =cut | ||
| 808 | |||
| 809 | =item $carrier->_is_valid_number($number) | ||
| 810 | |||
| 811 | Mobile operator-specific checks on the number during a C<validate_number()>. | ||
| 812 | |||
| 813 | This methods should be overwritten by subclasses extending this class. | ||
| 814 | |||
| 815 | =cut | ||
| 816 | |||
| 817 | sub _is_valid_number | ||
| 818 | { | ||
| 819 | return 1; | ||
| 820 | } | ||
| 821 | |||
| 822 | sub _format_number | ||
| 823 | { | ||
| 824 | return $_[1]; | ||
| 825 | } | ||
| 826 | |||
| 827 | =item $carrier->error() | ||
| 828 | |||
| 829 | Returns the last error after a login() or send(). | ||
| 830 | |||
| 831 | =cut | ||
| 832 | |||
| 833 | sub error | ||
| 834 | { | ||
| 835 | my ($self, $error) = @_; | ||
| 836 | |||
| 837 | if (defined($error)) | ||
| 838 | { | ||
| 839 | $self->{last_error} = $error; | ||
| 840 | } | ||
| 841 | elsif ($self->{last_error}) | ||
| 842 | { | ||
| 843 | return $self->{last_error}; | ||
| 844 | } | ||
| 845 | else | ||
| 846 | { | ||
| 847 | return 'Internal Error'; | ||
| 848 | } | ||
| 849 | } | ||
| 850 | |||
| 851 | =item $carrier->debug() | ||
| 852 | |||
| 853 | Set/retrieve the debug level for the module (0,1,2). | ||
| 854 | |||
| 855 | =cut | ||
| 856 | |||
| 857 | sub debug | ||
| 858 | { | ||
| 859 | if (defined($_[1])) | ||
| 860 | { | ||
| 861 | $_[0]->{debug} = $_[1]; | ||
| 862 | |||
| 863 | $_[0]->{tg4w_runner}->debug($_[1]) if ($_[0]->{tg4w_runner}); | ||
| 864 | } | ||
| 865 | else | ||
| 866 | { | ||
| 867 | return $_[0]->{debug}; | ||
| 868 | } | ||
| 869 | } | ||
| 870 | |||
| 871 | # private methods | ||
| 872 | |||
| 873 | sub dummy_send | ||
| 874 | { | ||
| 875 | defined($_[1]) ? $_[0]->{dummy_send} = $_[1] : $_[0]->{dummy_send}; | ||
| 876 | } | ||
| 877 | |||
| 878 | sub is_win32 | ||
| 879 | { | ||
| 880 | return ($^O eq 'MSWin32'); | ||
| 881 | } | ||
| 882 | |||
| 883 | sub _get_home_dir | ||
| 884 | { | ||
| 885 | if (defined($ENV{HOME})) | ||
| 886 | { | ||
| 887 | return $ENV{HOME}; | ||
| 888 | } | ||
| 889 | elsif (is_win32() && defined($ENV{HOMEPATH})) | ||
| 890 | { | ||
| 891 | return $ENV{HOMEPATH} . "/"; | ||
| 892 | } | ||
| 893 | else | ||
| 894 | { | ||
| 895 | return "."; | ||
| 896 | } | ||
| 897 | } | ||
| 898 | |||
| 899 | sub _choose_agent_string | ||
| 900 | { | ||
| 901 | my $self = shift; | ||
| 902 | |||
| 903 | my @keys = keys(%iesms_user_agent_strings); | ||
| 904 | my $string_count = scalar(@keys); | ||
| 905 | my $min_prob = rand(); | ||
| 906 | |||
| 907 | my $idx = int(rand($string_count-1)); | ||
| 908 | |||
| 909 | for (my $i=0; $i<$string_count; $i++) | ||
| 910 | { | ||
| 911 | my $key = $keys[($idx+$i)%$string_count]; | ||
| 912 | |||
| 913 | $self->_log_debug("checking user agent string '$key' (index ".($idx+$i)%$string_count.") with probability '$iesms_user_agent_strings{$key}' against random seed '$min_prob'...", 2); | ||
| 914 | |||
| 915 | if ($iesms_user_agent_strings{$key} >= $min_prob) | ||
| 916 | { | ||
| 917 | $self->_log_debug("selected user agent string: '$key'"); | ||
| 918 | return $key; | ||
| 919 | } | ||
| 920 | } | ||
| 921 | |||
| 922 | $self->_log_error("failed to randomise user agent"); | ||
| 923 | |||
| 924 | return $keys[0]; | ||
| 925 | } | ||
| 926 | |||
| 927 | sub _match | ||
| 928 | { | ||
| 929 | my ($self, $match) = @_; | ||
| 930 | |||
| 931 | return ($self->{matches}[$match]); | ||
| 932 | } | ||
| 933 | |||
| 934 | sub _login_start_step | ||
| 935 | { | ||
| 936 | defined($_[1]) ? $_[0]->{login_start_step} = $_[1] : $_[0]->{login_start_step}; | ||
| 937 | } | ||
| 938 | |||
| 939 | sub _login_end_step | ||
| 940 | { | ||
| 941 | defined($_[1]) ? $_[0]->{login_end_step} = $_[1] : $_[0]->{login_end_step}; | ||
| 942 | } | ||
| 943 | |||
| 944 | sub _send_start_step | ||
| 945 | { | ||
| 946 | defined($_[1]) ? $_[0]->{send_start_step} = $_[1] : $_[0]->{send_start_step}; | ||
| 947 | } | ||
| 948 | |||
| 949 | sub _send_end_step | ||
| 950 | { | ||
| 951 | defined($_[1]) ? $_[0]->{send_end_step} = $_[1] : $_[0]->{send_end_step}; | ||
| 952 | } | ||
| 953 | |||
| 954 | sub _simulated_delay_max | ||
| 955 | { | ||
| 956 | defined($_[1]) ? $_[0]->{simulated_delay_max} = $_[1] : $_[0]->{simulated_delay_max}; | ||
| 957 | } | ||
| 958 | |||
| 959 | sub _simulated_delay_min | ||
| 960 | { | ||
| 961 | defined($_[1]) ? $_[0]->{simulated_delay_min} = $_[1] : $_[0]->{simulated_delay_min}; | ||
| 962 | } | ||
| 963 | |||
| 964 | sub _simulated_delay_perchar | ||
| 965 | { | ||
| 966 | defined($_[1]) ? $_[0]->{simulated_delay_perchar} = $_[1] : $_[0]->{simulated_delay_perchar}; | ||
| 967 | } | ||
| 968 | |||
| 969 | sub _remaining_messages_match | ||
| 970 | { | ||
| 971 | defined($_[1]) ? $_[0]->{remaining_messages_match} = $_[1] : $_[0]->{remaining_messages_match}; | ||
| 972 | } | ||
| 973 | |||
| 974 | sub _action_file | ||
| 975 | { | ||
| 976 | if (!defined($_[1])) | ||
| 977 | { | ||
| 978 | return $_[0]->{action_file}; | ||
| 979 | } | ||
| 980 | |||
| 981 | if ($_[1] =~ m#/#) | ||
| 982 | { | ||
| 983 | # provided path | ||
| 984 | $_[0]->{action_file} = $_[1]; | ||
| 985 | |||
| 986 | if (-f $_[0]->{action_file}) | ||
| 987 | { | ||
| 988 | return 1; | ||
| 989 | } | ||
| 990 | } | ||
| 991 | else | ||
| 992 | { | ||
| 993 | # search in include path | ||
| 994 | my @files = map {"$_/WWW/SMS/IE/" . $_[1]} @INC; | ||
| 995 | |||
| 996 | foreach my $file (@files) | ||
| 997 | { | ||
| 998 | if (-f $file) | ||
| 999 | { | ||
| 1000 | $_[0]->{action_file} = $file; | ||
| 1001 | return 1; | ||
| 1002 | } | ||
| 1003 | } | ||
| 1004 | } | ||
| 1005 | |||
| 1006 | $_[0]->_log_error("Can't find action file '" . $_[1] . "'"); | ||
| 1007 | |||
| 1008 | return 0; | ||
| 1009 | } | ||
| 1010 | |||
| 1011 | |||
| 1012 | sub _save_action_state | ||
| 1013 | { | ||
| 1014 | my $action_state_file; | ||
| 1015 | my $self = $_[0]; | ||
| 1016 | |||
| 1017 | if ($action_state_file = $self->action_state_file()) | ||
| 1018 | { | ||
| 1019 | if ($action_state_file !~ m#/#) | ||
| 1020 | { | ||
| 1021 | $self->_log_debug("won't write '$action_state_file' to working directory"); | ||
| 1022 | return 0; | ||
| 1023 | } | ||
| 1024 | elsif ($self->{tg4w_runner}->action_state()) | ||
| 1025 | { | ||
| 1026 | return store($self->{tg4w_runner}->action_state(), $action_state_file); | ||
| 1027 | } | ||
| 1028 | else | ||
| 1029 | { | ||
| 1030 | return 0; | ||
| 1031 | } | ||
| 1032 | } | ||
| 1033 | } | ||
| 1034 | |||
| 1035 | sub _load_action_state | ||
| 1036 | { | ||
| 1037 | if (-f $_[0]->action_state_file()) | ||
| 1038 | { | ||
| 1039 | $_[0]->{tg4w_runner}->action_state(retrieve($_[0]->action_state_file())); | ||
| 1040 | return 1; | ||
| 1041 | } | ||
| 1042 | else | ||
| 1043 | { | ||
| 1044 | return 0; | ||
| 1045 | } | ||
| 1046 | } | ||
| 1047 | |||
| 1048 | sub _clear_cookie_jar | ||
| 1049 | { | ||
| 1050 | my $self = shift; | ||
| 1051 | |||
| 1052 | $self->{tg4w_runner}->cookie_jar()->clear(); | ||
| 1053 | |||
| 1054 | if ($self->{tg4w_runner}->cookie_jar_file() && -f $self->{tg4w_runner}->cookie_jar_file()) | ||
| 1055 | { | ||
| 1056 | unlink($self->{tg4w_runner}->cookie_jar_file()); | ||
| 1057 | } | ||
| 1058 | |||
| 1059 | $self->_log_debug("emptied cookie jar"); | ||
| 1060 | } | ||
| 1061 | |||
| 1062 | sub _log_debug | ||
| 1063 | { | ||
| 1064 | if (!defined($_[2])) | ||
| 1065 | { | ||
| 1066 | $_[2] = 1; | ||
| 1067 | } | ||
| 1068 | |||
| 1069 | if ($_[0]->debug() >= $_[2]) | ||
| 1070 | { | ||
| 1071 | print "iesms: $_[1]\n"; | ||
| 1072 | } | ||
| 1073 | } | ||
| 1074 | |||
| 1075 | sub _log_error | ||
| 1076 | { | ||
| 1077 | print STDERR "[ ERROR: $_[1] ]\n"; | ||
| 1078 | } | ||
| 1079 | |||
| 1080 | sub _log_warning | ||
| 1081 | { | ||
| 1082 | print STDERR "[ warning: $_[1] ]\n"; | ||
| 1083 | } | ||
| 1084 | |||
| 1085 | =back | ||
| 1086 | |||
| 1087 | =head1 DISCLAIMER | ||
| 1088 | |||
| 1089 | The author accepts no responsibility nor liability for your use of this | ||
| 1090 | software. Please read the terms and conditions of the website of your mobile | ||
| 1091 | provider before using the program. | ||
| 1092 | |||
| 1093 | =head1 SEE ALSO | ||
| 1094 | |||
| 1095 | L<WWW::SMS::IE::o2sms>, | ||
| 1096 | L<WWW::SMS::IE::vodasms>, | ||
| 1097 | L<WWW::SMS::IE::meteorsms>, | ||
| 1098 | L<WWW::SMS::IE::threesms>, | ||
| 1099 | L<WWW::SMS::IE::aftsms> | ||
| 1100 | |||
| 1101 | L<http://www.mackers.com/projects/o2sms/> | ||
| 1102 | |||
| 1103 | =head1 AUTHOR | ||
| 1104 | |||
| 1105 | David McNamara (me.at.mackers.dot.com) | ||
| 1106 | |||
| 1107 | =head1 COPYRIGHT | ||
| 1108 | |||
| 1109 | Copyright 2000-2006 David McNamara | ||
| 1110 | |||
| 1111 | This program is free software; you can redistribute it and/or modify it under | ||
| 1112 | the same terms as Perl itself. | ||
| 1113 | |||
| 1114 | =cut | ||
| 1115 | |||
| 1116 | 1; | ||
| 1117 |