#!/usr/bin/env python2.4
# -*- coding: utf-8 -*-
#
# Copyright (c) 2006 Torbjörn Svensson <azoff@se.linux.org>.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA


import cookielib
import urllib
import urllib2
import sys
import time

# Några stanardvärden, rör ej
Domain		= "sms.ballou.se"
LoginURL	= "http://%s/index.asp?p=login" % Domain
SendURL		= "http://%s/index.asp"		% Domain
SendURL		= "http://www.azoff.se/sms.php"

Domain		= "www.azoff.se"
LoginURL	= "http://%s/sms.php" % Domain
SendURL		= "http://%s/sms.php" % Domain


# Ange USER/PASSWORD här
Login		= {
		"p"			: "login",
		"loginName"		: "user",
		"loginPassword" 	: "passwd",
	}
	
# ----------------------------------------------------------
# 
# RÖR INGET HÄR UNDER!!!!!!!
#
# ----------------------------------------------------------

# Generar application/x-www-form-urlencoded
def make_url(message):
	"""Return urlencoded string"""
	return urllib.urlencode(message)


# Skriv ut lite information
def Debug(message):
	"""Just print a Debug message, uncomment return to do nothing"""
	#return
	print message


# Verifikation på antalet parametrar
if len(sys.argv) < 3:
	Debug("För få parametrar, kräver minst 3.")
	Debug("Syntax: %s <mobilnummer> <medelande>" % sys.argv[0])
	sys.exit(1)
	
# Fälten till sänd-sidan. RÖR EJ!
Message		= {
		"p"			: "qsend",
		"sendSMS"		: "true",
		"sender"		: "0",
		"receivers"		: sys.argv[1],
		"new_receiver"		: sys.argv[1],
		"send_when"		: "1",
		"message_text"		: ("Eclassical: %s" % " ".join(sys.argv[2:]))[:160],
	}


# Skapa kak-hanteraren
Debug("Skapar CookieJar")
cj = cookielib.CookieJar()


# Skapa klienten
Debug("Skapar Open-objekt")
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))


# Skapa förfrågningarna
Debug("Skapar Request-objekt\n")
LoginRequest	= urllib2.Request(LoginURL, make_url(Login))
SMSRequest	= urllib2.Request(SendURL, make_url(Message))


# Hämta första förfrågning (logga in)
Debug("Hämtar %s\n" % LoginURL)
opener.open(LoginRequest)


# Kontrollera att vi är inne (cookie)
if not Domain in cj._cookies.keys():
	Debug("Inte inloggad? Saknar cookie!!\n")
	sys.exit(1)
Debug("Inloggad, forsätter.\n")


# Hämta andra förfrågan (skicka meddelandet)
Debug("Skickar meddelande..")
if opener.open(SMSRequest).read().find("Meddelandet har skickats") == -1:
	Debug("Troligtvis skickades inte meddelandet\n")
else:
	Debug("Meddelande skickat\n")


# Rensa kak-listan (kanske finns kvar i minnet ändå?)
Debug("Rensar CookieJar")
cj.clear()


# Avsluta
sys.exit(0)

