:pserver:cvs@pserver.samba.org:/cvsroot - gnokii - Tue Dec 4 22:45 CET 2001
[gnokii.git] / smsd / db.c
1 /*
2
3   S M S D
4
5   A Linux/Unix GUI for Nokia mobile phones.
6   Copyright (C) 1999 Pavel Janík ml., Hugh Blemings
7   & Ján Derfiòák <ja@mail.upjs.sk>.
8
9   Released under the terms of the GNU GPL, see file COPYING for more details.
10
11   Last modification: Sun Dec 17 2000
12   Modified by Jan Derfinak
13
14 */
15
16 #include <string.h>
17 #include <glib.h>
18 #include <libpq-fe.h>
19 #include "db.h"
20 #include "smsd.h"
21 #include "gsm-common.h"
22
23 static PGconn *connIn = NULL;
24 static PGconn *connOut = NULL;
25
26 void DB_Bye (void)
27 {
28   if (connIn)
29     PQfinish (connIn);
30   
31   if (connOut)
32     PQfinish (connOut);
33 }
34
35
36 gint DB_ConnectInbox (const gchar * const conninfo)
37 {
38   connIn = PQconnectdb (conninfo);
39   
40   if (PQstatus (connIn) == CONNECTION_BAD)
41   {
42      g_print ("Connection to database '%s' failed.\n", conninfo);
43      g_print ("%s", PQerrorMessage(connIn));
44      return (1);
45   }
46
47   return (0);
48 }
49
50
51 gint DB_ConnectOutbox (const gchar * const conninfo)
52 {
53   connOut = PQconnectdb (conninfo);
54   
55   if (PQstatus (connOut) == CONNECTION_BAD)
56   {
57      g_print ("Connection to database '%s' failed.\n", conninfo);
58      g_print ("%s", PQerrorMessage(connOut));
59      return (1);
60   }
61
62   return (0);
63 }
64
65
66 gint DB_InsertSMS (const GSM_SMSMessage * const data)
67 {
68   GString *buf;
69   PGresult *res;
70     
71   buf = g_string_sized_new (128);
72   g_string_sprintf (buf, "INSERT INTO inbox VALUES ('%s',
73                     '%02d-%02d-%02d %02d:%02d:%02d+01', 'now', '%s', 'f')",
74                     data->RemoteNumber.number, data->Time.Year + 2000, data->Time.Month,
75                     data->Time.Day, data->Time.Hour, data->Time.Minute,
76                     data->Time.Second, data->MessageText);
77   res = PQexec(connIn, buf->str);
78   g_string_free(buf, TRUE);
79   if (!res || PQresultStatus(res) != PGRES_COMMAND_OK)
80   {
81     PQclear (res);
82     return (1);
83   }
84   
85   PQclear (res);
86     
87   return (0);
88 }
89
90
91 void DB_Look (void)
92 {
93   GString *buf;
94   PGresult *res1, *res2;
95   register int i;
96
97   buf = g_string_sized_new (128);
98
99   g_string_sprintf (buf, "BEGIN");
100
101   res1 = PQexec(connOut, buf->str);
102   PQclear (res1);
103
104   g_string_sprintf (buf, "SELECT id, number, text FROM outbox \
105                           WHERE processed='f' FOR UPDATE");
106
107   res1 = PQexec(connOut, buf->str);
108   if (!res1 || PQresultStatus (res1) != PGRES_TUPLES_OK)
109   {
110     g_print ("%s\n", PQcmdStatus (res1));
111     PQclear (res1);
112     g_print ("%d: SELECT FROM command failed\n", __LINE__);
113     res1 = PQexec (connOut, "ROLLBACK TRANSACTION");
114     PQclear (res1);
115     g_string_free (buf, TRUE);
116     return;
117   }
118
119   for (i = 0; i < PQntuples (res1); i++)
120   {
121     GSM_SMSMessage sms;
122     
123     sms.MessageCenter.No = 1;
124     sms.Type = SMS_Submit;
125     sms.DCS.Type = SMS_GeneralDataCoding;
126     sms.DCS.u.General.Compressed = false;
127     sms.DCS.u.General.Alphabet = SMS_DefaultAlphabet;
128     sms.DCS.u.General.Class = 0;
129     sms.Validity.VPF = SMS_RelativeFormat;
130     sms.Validity.u.Relative = 4320; /* 4320 minutes == 72 hours */
131     sms.UDH_No = 0;
132     sms.Report = false;
133
134     strncpy (sms.RemoteNumber.number, PQgetvalue (res1, i, 1), GSM_MAX_DESTINATION_LENGTH + 1);
135     sms.RemoteNumber.number[GSM_MAX_DESTINATION_LENGTH] = '\0';
136     if (sms.RemoteNumber.number[0] == '+') sms.RemoteNumber.type = SMS_International;
137     else sms.RemoteNumber.type = SMS_Unknown;
138     
139     strncpy (sms.MessageText, PQgetvalue (res1, i, 2), GSM_MAX_SMS_LENGTH + 1);
140     sms.MessageText[GSM_MAX_SMS_LENGTH] = '\0';
141
142 #ifdef XDEBUG
143     g_print ("%s, %s\n", sms.Destination, sms.MessageText);
144 #endif
145     
146     if (WriteSMS (&sms) != 0)
147     {
148       g_string_sprintf (buf, "UPDATE outbox SET processed='t' WHERE id='%s'",
149                         PQgetvalue (res1, i, 0));
150       res2 = PQexec(connOut, buf->str);
151       if (!res2 || PQresultStatus (res2) != PGRES_COMMAND_OK)
152       {
153         g_print ("%s\n", PQcmdStatus (res2));
154         g_print ("%d: UPDATE command failed\n", __LINE__);
155       }
156       PQclear (res2);
157     }
158   }
159
160   PQclear (res1);
161
162   g_string_sprintf (buf, "COMMIT");
163   res1 = PQexec(connOut, buf->str);
164
165   g_string_free(buf, TRUE);
166   PQclear (res1);
167 }