# -*- coding:utf8 -*-
from ircBase import *
import smtplib 
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText


CONST_CONTACT = 2
CONST_MESSAGE = 3


def getContactList():
	configFile = open('configs/smsConfig','r')
	contactList = configFile.readlines()
	contactList = [x.replace('\n', '') for x in contactList]

	return contactList

def getContacts(contactList):
	contacts = ''
	for index, item in enumerate(contactList):
		if index%2 == 0 and index>=2:
			contacts += "| " + contactList[index]
	return contacts[1:]
	

def sendMail(contactList, to, text):
	fromAddr = contactList[0]
	password = contactList[1]

	toAddr = contactList[contactList.index(to) + 1]	

	mailServer = smtplib.SMTP('smtp.gmail.com:587')
	mailServer.ehlo()
	mailServer.starttls()
	mailServer.ehlo()
	mailServer.login(fromAddr,password)
	mailServer.sendmail(fromAddr,toAddr,text)
	mailServer.close()

	
def getQuery(contactList, message, messagePart):
	stringMatch = "(text|txt)(" + getContacts(contactList) + ")? (.*)"
	expression = re.compile(stringMatch, re.IGNORECASE)
	match = expression.match(message)
	if match:
		try:
			return match.group(messagePart).strip()
		except:
			return None
	else:
		return None
	

def main(irc):
 	message = irc.lastMessage()
	contactList = getContactList()
	if message.botCommand == "contacts":
		ircMessage.newRoomMessage(irc, "Contacts in system:" + getContacts(contactList).replace("|",",")).send()		
	if message.body != None:
		text = getQuery(contactList,message.body,CONST_MESSAGE)
		if text:
			contact = getQuery(contactList,message.body,CONST_CONTACT)
			if contact:
				try:
					print message.sendingNick
					sendMail(contactList,contact,message.sendingNick + " says " + text)
					ircMessage.newRoomMessage(irc, "Text message sent to " + contact).send()
				except:
					return
			else:
				ircMessage.newRoomMessage(irc, "Does not match any known contact.").send()

@@ -211,16 +211,141 @@
 E = 3%0A%0A%0A
+def getContactIndex (contactList, contact):%0A%09for i, s in enumerate(contactList):%0A%09%09if contact in s:%0A%09%09%09return i%0A%09return -1%0A%0A%0A
 def getC
@@ -358,16 +358,16 @@
 List():%0A
-
 %09configF
@@ -797,16 +797,17 @@
 List%5B1%5D%0A
+%09
 %0A%09toAddr
@@ -821,16 +821,32 @@
 actList%5B
+getContactIndex(
 contactL
@@ -852,15 +852,9 @@
 List
-.index(
+,
 to) 
@@ -1186,9 +1186,8 @@
 + %22)
-?
  (.*
@@ -1695,16 +1695,16 @@
 ESSAGE)%0A
-
 %09%09if tex
@@ -1706,16 +1706,74 @@
 f text:%0A
+%09%09%09print getQuery(contactList,message.body,CONST_CONTACT)%0A
 %09%09%09conta

Fix bug in smsModule for users with aliases