From 6728f88c7a692d679813495588213df5b08fdd59 Mon Sep 17 00:00:00 2001
From: neingeist <neingeist@bl0rg.net>
Date: Sat, 11 Jun 2005 18:49:31 +0000
Subject: [PATCH] Get contacts from Evolution

---
 smstool.py | 80 +++++++++++++++++++++++++++++++++++++++++++++---------
 1 file changed, 67 insertions(+), 13 deletions(-)

diff --git a/smstool.py b/smstool.py
index e1a16b2..5fc3994 100644
--- a/smstool.py
+++ b/smstool.py
@@ -1,10 +1,42 @@
 #!/usr/bin/python
 
 from wxPython.wx import *
-from os import system
-from re import sub
+import bsddb
+import os
+import re
+
+# Constants
 
 SMSLEN = 160
+DB_PATH = "~/.evolution/addressbook/local/system/addressbook.db"
+
+# EvolutionsCells gets cell phone numbers out of the Evolution database
+
+class EvolutionCells:
+  def __init__(self):
+    contacts = {}
+    self.contacts = contacts
+
+    db = bsddb.hashopen(os.path.expanduser(DB_PATH),"r")
+    self.db = db
+
+    for k in db.keys():
+      name = ""
+      cell = ""
+      for e in db[k].split('\r\n'):
+        if re.search("^FN:", e):
+          name = re.sub("^FN:", "", e)
+        if re.search("^TEL;TYPE=CELL:", e):
+          cell = re.sub("^TEL;TYPE=CELL:", "", e)
+      if cell != "":
+        contacts[name] = cell
+
+    db.close
+
+  def getContacts(self):
+    return self.contacts
+
+# SimserFrame is the application
 
 class SimserFrame(wxFrame):
   def __init__(self, parent, id, title):
@@ -18,7 +50,7 @@ class SimserFrame(wxFrame):
 
     wxStaticText(panel, -1, "Text:",
            wxDLG_PNT(panel, wxPoint(4, 4)),  wxDefaultSize)
-    wxStaticText(panel, -1, "Nummer:",
+    wxStaticText(panel, -1, "Contact:",
            wxDLG_PNT(panel, wxPoint(4, 20)), wxDefaultSize)
 
     self.textCtrl = wxTextCtrl(panel, 20, "",
@@ -27,11 +59,7 @@ class SimserFrame(wxFrame):
     self.textCtrl.SetMaxLength(SMSLEN);
     self.textCtrl.SetFocus()
 
-    self.nrCtrl = wxTextCtrl(panel, -1, "",
-                  wxDLG_PNT(panel, wxPoint(30, 20)),
-                  wxDLG_SZE(panel, wxSize(60, -1)))
-  
-    self.sendCtrl = wxButton(panel, 10, "Senden",
+    self.sendCtrl = wxButton(panel, 10, "Send",
                   wxDLG_PNT(panel, wxPoint(100, 20)),
                   wxDLG_SZE(panel, wxSize(30, -1)))
     EVT_BUTTON(self, 10, self.OnSendClick)
@@ -40,23 +68,49 @@ class SimserFrame(wxFrame):
            wxDLG_PNT(panel, wxPoint(150, 20)), wxDefaultSize)
 
     EVT_TEXT(self, 20, self.OnChange)
+      
+    # Get contacts
+    cells = EvolutionCells()
+    self.contacts = cells.getContacts()
+    
+    # Fill contact list and the combobox
+
+    contactlist = self.contacts.keys()
+    contactlist.sort()
+    self.contactCtrl = wxComboBox(panel, -1, contactlist[0],
+                  wxDLG_PNT(panel, wxPoint(30, 20)),
+                  wxDLG_SZE(panel, wxSize(60, -1)), 
+                  contactlist)
 
   def OnCloseWindow(self, event):
     self.Destroy()
 
   def OnSendClick(self, event):
     text = self.textCtrl.GetValue()
-    nr   = self.nrCtrl.GetValue()
+    contact = self.contactCtrl.GetValue()
+
+    # Figure out if we have a number or a name
+    if re.search('^[+0-9]+$', contact):
+      nr = contact
+    else: 
+      if self.contacts.has_key(contact):
+        nr = self.contacts[contact]
+      else:
+        self.foodialog = wxMessageDialog(self.panel, 
+                         "Contact '" + contact + "' is unknown!",
+                         "Erros", wxOK, wxPoint(100, 100))
+        self.foodialog.Show(true)
+        return
 
     # Quote text
-    text = sub("'", "''", text)
-    nr = sub("'", "''", nr)
+    text = re.sub("'", "''", text)
+    nr = re.sub("'", "''", nr)
 
     # Send SMS and report back
-    rc = system("echo '" + text + "' | gsmsendsms '" + nr + "'")
+    rc = os.system("echo '" + text + "' | gsmsendsms '" + nr + "'")
     if(rc == 0):
       self.foodialog = wxMessageDialog(self.panel, 
-                       "Message sent",
+                       "Message sent to " + nr,
                        "Success", wxOK, wxPoint(100, 100))
     else:
       self.foodialog = wxMessageDialog(self.panel,