#-SMS Gateway over SMTP e-mail protocol: #-(c)ode by Brodie Ponas #- #- User Socket, and Select Packages: use IO::Socket; use IO::Select; use Socket; use Net::SMTP; #-first arg is ip address #-2nd arg is port: foreach $arg (@ARGV) { $_ = $arg; #-Address: if (m/\-addr\:(.+)/i) {$address = $1;} #-Port: if (m/\-port\:(.+)/i) {$port = $1;} } #-Setup sockets: $ConnectionSocket = new IO::Socket::INET(Listen => 1, LocalHost => $address ,LocalPort => $port); $SelectHandle = new IO::Select( $ConnectionSocket ); print "iLs SMS SMTP Gateway Listening...\r\n"; print "LocalAddr: $address\r\n"; print "LocalPort: $port\r\n"; $isRecvData = 0; while(@Connections = $SelectHandle->can_read) { foreach $SocketHandle (@Connections) { #-Incomming Connection: if($SocketHandle == $ConnectionSocket) { # Create a new socket $NewClient = $ConnectionSocket->accept; $SelectHandle->add($NewClient); print "Client Connect ", $NewClient->peerhost, ":", $NewClient->peerport, "\r\n"; #-Tell mail client we are ready: print $NewClient "220\r\n"; } #-Established Connections: else { # Process socket data: #-We are recv'n the actual SMTP DATA field now, we don't want to send anythign back to the client: if ($isRecvData == 1) { $Data = <$SocketHandle>; $_ = $Data; #-End of Data: reset flag: send OK: if (m/\./i) { print $SocketHandle "250\r\n"; $isRecvData = 0; } print "$Data\r\n"; next; } #-Recv Stream: $Data = <$SocketHandle>; $_ = $Data; if (m/QUIT/i) { print $SocketHandle "221\r\n"; $SelectHandle->remove($SocketHandle); $SocketHandle->close; } #-Start of DATA: elsif(m/DATA/i) { print $SocketHandle "250\r\n"; $isRecvData = 1; } #-Any other command we just give them an OK we just care about the data messaage and sender address for now: else {print $SocketHandle "250\r\n";} print "$Data"; } $Data = ""; } }