The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Net::UCP - Perl extension for EMI - UCP Protocol.

SYNOPSIS

  use Net::UCP ':all';

$emi = Net::UCP->new(SMSC_HOST=>'smsc.somedomain.tld', SMSC_PORT=>3024, SENDER_TEXT=>'My Self 123', SRC_HOST=>'my.host.tld', SRC_PORT=>'1666');

DESCRIPTION

This module implements a Client Interface to the EMI - UCP Interface specification, This Protocol can be used to comunicate with an SMSC (Short Message Service Centre)

Usually the Network connection is based on TCP/IP or X.25.

The EMI/UCP specification can be found online. :)

This Class can be used to send an SMS message to an SMSC. (Text messages and Binary Messages)

You will of course be required to have a valid login at the SMSC to use their services. (Unless there is an SMSC which provides their services for free. Please, let me know about any such service provider. :-)

A Net::UCP object must be created with the new() constructor. Once this has been done, all commands are accessed via method calls on the object.

EXAMPLE

    C<use Net::UCP ':all';>
    
    C<($recipient,$text,$sender)=@ARGV;>
    
    C<my($acknowledge,$error_number,$error_text);>

    C<$emi = Net::UCP-E<gt>new(SMSC_HOST=E<gt>'smsc.somedomain.tld',
                               SMSC_PORT=E<gt>3024,
                               SENDER_TEXT=E<gt>'MyApp',
                               SRC_HOST=E<gt>'10.10.10.21', #optional see below
                               SRC_PORT=E<gt>'1666',        #optional see below
                               WARN=E<gt>1) || die("Failed to create SMSC object");>
    
    C<$emi-E<gt>open_link() || die("Failed to connect to SMSC")>
    
    C<($acknowledge,$error_number,$error_text) = $emi-E<gt>login(
                                                                 SMSC_ID=E<gt>'your_account_id',
                                                                 SMSC_PW=E<gt>'your password',
                                                                 SHORT_CODE=E<gt>'your Auth Code'
                                                                 );>
    
    C<die("Login to SMSC failed. Error nbr: $error_number, Error txt: $error_text\n") unless($acknowledge);>
    
    C<($acknowledge,$error_number,$error_text) = $emi-E<gt>send_sms(
                                                                    RECIPIENT=E<gt>$recipient,
                                                                    MESSAGE_TEXT=E<gt>$text,
                                                                    SENDER_TEXT=E<gt>$sender,
                                                                    UDH=E<gt>$udh,
                                                                    MESSAGE_BINARY=<gt>$binary_message
                                                                    );>
    
    C<die("Sending SMS failed. Error nbr: $error_number, Error txt: $error_text\n") unless($acknowledge);>
    
    C<$emi-E<gt>close_link();>

CONSTRUCTOR

new( SMSC_HOST=>'smsc.somedomain.tld', SMSC_PORT=>3024, SENDER_TEXT=>'My App', TIMEOUT=>10, WARN=>1, SRC_HOST=>'my.host.tld', SRC_PORT=>'Source Port')

The parameters may be given in arbitrary order.

SMSC_HOST=> Mandatory. The hostname or ip-address of the SMSC.

SMSC_PORT=> Optional. The TCP/IP port number of your SMSC. If omitted, port number 3024 will be used by default.

SMSC_HOST=> Optional. Your ip appdress.

SRC_PORT=> Optional. The TCP/IP source port number. You need to set it if you want to use auth. method based on AC.

SENDER_TEXT=> Optional. The text that will appear in the receivers mobile phone, identifying you as a sender. If omitted, the text 'Net::UCP' will be used by default. You will probably want to provide a more meaningful text than that.

TIMEOUT=> Optional. A timeout, given in seconds, to wait for an acknowledgement of an SMS message transmission. The value must be numeric, positive and within the range of 0 (zero) to 60. Failing this, or if the parameter is omitted, the default timeout of 15 seconds will be applied. The value of this parameter will be used in all calls to the send_sms() method. If the SMSC does not respond with an ACK or a NACK during this period, the send_sms() method will return a NACK to the caller. If the value of 0 (zero) is given, no timeout will occur but the send_sms() method will wait indefinitively for a response. Note that the value given to the constructor can temporarily be overruled in the call to the send_sms() method. As a final note, please remember that not all systems have implemented the alarm() call, which is used to create a timeout. On such systems, this module will still do a blocking call when reading data from the SMSC.

WARN=> Optional. If this parameter is given and if it evaluates to true, then any warnings and errors will be written to STDERR. If omitted, or if the parameter evaluates to false, then nothing is written to STDERR. It is strongly recommended to turn on warnings during the development of an application using the Net::UCP module. When development is finished, the developer may chose to not require warnings but to handle all error situations completely in the main application by checking the return values from Net::UCP.

The constructor returns undef if mandatory information is missing or invalid parameter values are detected. In this case, the object is discarded (out of scope) by the Perl interpreter and you cannot call any methods on the object handle.

Any errors detected will be printed on STDERR if the WARN=> parameter evaluates to true.

Test the return value from the constructor!

METHODS

open_link()

Open the communication link to the SMSC. In reality, this opens up a socket to the SMSC. Be aware that this is not an authenticated login but that the login() method must also be called before any SMS messagescan be sent to the SMSC if you will use an Authentication based on Login and Password.

open_link() is useful since the main application can verify that it's at all possible to communicate with the SMSC. (Think: getting through a firewall.)

This method takes no parameters since it will use the data given in the constructor parameters.

Any errors detected will be printed on STDERR if the WARN=> parameter in the constructor evaluates to true.

open_link() returns true on success and undef in case something went wrong.

login(SMSC_ID=>'my_account_id', SMSC_PW=>'MySecretPassword', SHORT_CODE=>'My Auth. Code')

You are able to use authentication based on Operation 60 of EMI Protocol. Authenticates against the SMSC with the given SMSC-id and password.

or

Directly trhough Operation 51. Authenticates against the SMSC with the given SHORT_CODE (AC parameter).

If the open_link() method has not explicitly been called by the main application, the login() method will do it before trying to authenticate with the SMSC.

The parameters may be given in arbitrary order.

SHORT_CODE=> Mandatory. A valid Authentication Code Mandatory for Auth based on OP 51.

You will use login method only to set Authentication Code.

or

SMSC_ID=> Mandatory. A string which should be a valid account ID at the SMSC.

SMSC_PW=> Mandatory. A valid password at the SMSC.

Any errors detected will be printed on STDERR if the WARN=> parameter in the constructor evaluates to true.

Return values:

In void context, login() will always return undef. login() will return a true value if you will use it only to set Authentication Code.

In scalar context, login() will return true for success, false for transmission failure and undef for application related errors. Application related errors may be for instance that a mandatory parameter is missing. All such errors will be printed on STDERR if the WARN=> parameter in the constructor evaluates to true.

    In array context, login() will return three values: C<($acknowledge, $error_number, $error_text);>
where C<$acknowledge> holds the same value as when the method is called in scalar context
(i.e. I<true>, I<false> or I<undef>),
C<$error_number> contains a numerical error code from the SMSC and
C<$error_text> contains a (relatively) explanatory text about the error.

Be aware that both $error_number and $error_text are provided in a response from the SMSC, which means that the data quality of these entities depends on how well the SMSC has implemented the protocol.

If $acknowledge is undef, then $error_number will be set to 0 (zero) and $error_text will contain a zero length string.

It is strongly recommended to call login() in an array context, since this provides for an improved error handling in the main application.

send_sms( RECIPIENT=>'391232345678', MESSAGE_TEXT=>'A Message', SENDER_TEXT=>'Marco', UDH=>'050415811581', MESSAGE_BYNARY=>'024A3A7125CD7DD1A1A5CD7DB1BDD994040045225D04985585D85D84106906985D84984A85585D85D84104D04104D85D0690410A24824C49A6289B09D093126986A800', TIMEOUT=>5 )

Submits the SMS message to the SMSC (Operation 51) and waits for an SMSC acknowledge.

The parameters may be given in arbitrary order.

RECIPIENT=> Mandatory.

This is the phone number of the recipient in international format with leading a '+' or '00'.

MESSAGE_TEXT=> Optional. A text message to be transmitted.

It is accepted to transfer an empty message, so if this parameter is missing, a zero length string will be sent.

MESSAGE_BINARY=> Optional. A binary message to be transmitted.

UDH=> Optional. User Data Header (you need to set UDH to use MESSAGE_BINARY).

First UDH Octet (length) will be internally calculated for this reason you need to omitted it.

SENDER_TEXT=> Optional. The text that will appear in the receivers mobile phone, identifying you as a sender.

This text will temporarily replace the text given to the constructor. If omitted, the text already given to the constructor will be used.

TIMEOUT=> Optional.

A timeout, given in seconds, to wait for an acknowledgement of this SMS message transmission. The value must be numeric, positive and within the range of 0 (zero) to 60. Failing this, or if the parameter is omitted, the timeout established in the new() constructor will be applied. If the SMSC does not respond with an ACK or a NACK during this period, the send_sms() method will return a NACK to the caller. If the value of 0 (zero) is given, no timeout will occur but the send_sms() method will wait indefinitively for a response. On a system that has not implemented the alarm() call, which is used to create a timeout, this module will still do a blocking call when reading data from the SMSC.

Any errors detected will be printed on STDERR if the WARN=> parameter in the constructor evaluates to true.

Return values:

In void context, send_sms() will always return undef.

In scalar context, send_sms() will return true for success, false for transmission failure and undef for application related errors. Application related errors may be for instance that a mandatory parameter is missing. All such errors will be printed on STDERR if the WARN=> parameter in the constructor evaluates to true.

In array context, send_sms() will return the three values: ($acknowledge, $error_number, $error_text); where $acknowledge holds the same value as when the method is called in scalar context (i.e. true, false or undef), $error_number contains a numerical error code from the SMSC and $error_text contains a (relatively) explanatory text about the error.

Be aware that both $error_number and $error_text are provided in a response from the SMSC, which means that the data quality of these entities depends on how well the SMSC has implemented the protocol.

If $acknowledge is undef, then $error_number will be set to 0 (zero) and $error_text will contain a zero length string.

It is strongly recommended to call send_sms() in array context, since this provides for an improved error handling in the main application.

Note! The fact that the message was successfully transmitted to the SMSC does not guarantee immediate delivery to the recipient or in fact any delivery at all.

About the timeout. Not all Perl systems are able to provide the timeout. The timeout is internally implemented with the alarm() call. If your system has implemented alarm(), then any timeout value provided will be honored. If not, you may provide any value you wish for the timeout, it will still be ignored and reading the ACK from the SMSC will block until everything is read. If the SMSC fails to send you the full response your application will freeze. If your system does implement alarm() but you do not provide any timeout value, then the default timeout of 15 seconds will be applied.

You may test this on your own system by executing the following on a command line:

perl -e "alarm(0)"

If the response is that alarm() is not implemented, then you're out of luck. You can still use the module, but in case the SMSC doesn't respond as expected your application will wait indefinitively.

Still, the author uses this module in a production environment without alarm(), and thereby without any timeout whatsoever, and has still not had any timeout related problem.

logout()
close_link()

logout() is an alias for close_link(). Whichever method name is used, the very same code will be executed.

What goes up, must also come down. If the main application will continue working on other tasks once the SMS message was sent, it is possible to explicitly close the communications link to the SMSC with this method.

If the Net::UCP object handle (returned by the new() method) goes out of scope in the main application, the link will be implicitly closed and in this case it is not necessary to explicitly close the link.

In reality, this method closes the socket established with the SMSC and does some additional house-keeping. Once the link is closed, a new call to either open_link() or to login() will try to re-establish the communications link (socket) with the SMSC.

returns nothing (void)

EXPORT

None by default.

TODO

    1 - A good Manual ;)
    2 - Test IT!

SEE ALSO

IO::Socket,

AUTHOR

Marco Romano, <nemux@cpan.org>

Based on Gustav Schaffter and Jochen Schneider. Net::EMI::*

COPYRIGHT AND LICENSE

Copyright (C) 2004 by Marco Romano

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.4 or, at your option, any later version of Perl 5 you may have available.