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>.
9 Released under the terms of the GNU GPL, see file COPYING for more details.
11 Last modification: Sun Dec 17 2000
12 Modified by Jan Derfinak
20 #include <pgsql/libpq-fe.h>
21 #include <sys/types.h>
29 #include "gsm-common.h"
30 #include "gsm-ringtones.h"
31 #include "gsm-filetypes.h"
33 static PGconn *connIn = NULL;
34 static PGconn *connOut = NULL;
46 gint DB_ConnectInbox (const gchar * const conninfo)
48 connIn = PQconnectdb (conninfo);
50 if (PQstatus (connIn) == CONNECTION_BAD)
52 g_print ("Connection to database '%s' failed.\n", conninfo);
53 g_print ("%s", PQerrorMessage(connIn));
61 gint DB_ConnectOutbox (const gchar * const conninfo)
63 connOut = PQconnectdb (conninfo);
65 if (PQstatus (connOut) == CONNECTION_BAD)
67 g_print ("Connection to database '%s' failed.\n", conninfo);
68 g_print ("%s", PQerrorMessage(connOut));
76 gint DB_InsertSMS (const GSM_SMSMessage * const data)
81 buf = g_string_sized_new (128);
82 g_string_sprintf (buf, "INSERT INTO inbox VALUES ('%s',
83 '%02d-%02d-%02d %02d:%02d:%02d+01', 'now', '%s', 'f')",
84 data->Sender, data->Time.Year + 2000, data->Time.Month,
85 data->Time.Day, data->Time.Hour, data->Time.Minute,
86 data->Time.Second, data->MessageText);
87 res = PQexec(connIn, buf->str);
88 g_string_free(buf, TRUE);
89 if (!res || PQresultStatus(res) != PGRES_COMMAND_OK)
100 #define DBL_FieldString "id, number, text, hexencode, type, fromfile, netcode, udhpresent, longuseudh, eightbit, smsc"
116 #define DBL_false "f"
118 static int DBL_safestrcasecmp(const char *s1,const char *s2)
121 return((!!s1)-(!!s2));
122 return(strcasecmp(s1,s2));
128 PGresult *res1, *res2;
131 buf = g_string_sized_new (128);
134 g_string_sprintf (buf, "BEGIN");
135 res1 = PQexec(connOut, buf->str);
139 g_string_sprintf (buf, "SELECT " DBL_FieldString " FROM outbox \
140 WHERE processed='f' FOR UPDATE");
142 res1 = PQexec(connOut, buf->str);
143 if (!res1 || PQresultStatus (res1) != PGRES_TUPLES_OK)
145 g_print ("%s\n", PQcmdStatus (res1));
147 g_print ("%d: SELECT FROM command failed\n", __LINE__);
148 res1 = PQexec (connOut, "ROLLBACK TRANSACTION");
150 g_string_free (buf, TRUE);
154 for (i = 0; i < PQntuples (res1); i++)
157 char *type = PQgetvalue (res1, i, DBLF_type);
158 bool fromfile = !DBL_safestrcasecmp(DBL_true, PQgetvalue (res1, i, DBLF_fromfile));
159 char *smsc = PQgetvalue (res1, i, DBLF_smsc);
160 char *smsbuf = PQgetvalue (res1, i, DBLF_text);
161 size_t smsbuflen = strlen(smsbuf);
165 sms.MessageCenter.No = 1;
167 sms.MessageCenter.No = 0;
168 if (strlen(smsc)+1 > sizeof(sms.MessageCenter.Number))
169 continue; /* error */
170 strcpy(sms.MessageCenter.Number, smsc);
175 sms.Compression = false;
176 sms.EightBit = !DBL_safestrcasecmp(DBL_true, PQgetvalue (res1, i, DBLF_eightbit));
178 sms.UDHPresent = !DBL_safestrcasecmp(DBL_true, PQgetvalue (res1, i, DBLF_udhpresent));
180 if (!DBL_safestrcasecmp(DBL_true, PQgetvalue (res1, i, DBLF_hexencode))) {
181 char *d_end = (char *)SMS_BlockFromHex(smsbuf/*d*/, smsbuf/*s*/, smsbuflen);
185 smsbuflen = d_end - smsbuf;
189 if (!DBL_safestrcasecmp("ringtone" ,type)) {
190 SMS_SetupUDH(&sms, GSM_RingtoneUDH);
192 GSM_Ringtone ringtone;
193 static char ringbuf[GSM_MAX_RINGTONE_PACKAGE_LENGTH];
194 int ringbuflen=GSM_MAX_RINGTONE_PACKAGE_LENGTH;
196 if (GSM_ReadRingtoneFile(smsbuf, &ringtone))
197 continue; /* error */
198 GSM_PackRingtone(&ringtone, ringbuf, &ringbuflen);
200 smsbuflen=ringbuflen;
203 else if (!DBL_safestrcasecmp("oplogo" ,type)) {
204 SMS_SetupUDH(&sms, GSM_OpLogo);
207 else if (!DBL_safestrcasecmp("calleridlogo",type)) {
208 SMS_SetupUDH(&sms, GSM_CallerIDLogo);
213 static char textbuf[GSM_MAX_CONCATENATED_SMS_LENGTH];
217 if (-1==(fd=open(smsbuf,O_RDONLY)))
218 continue; /* error */
219 if (-1==(textbuflen=read(fd,textbuf,sizeof(textbuf))))
220 continue; /* error */
223 smsbuflen=textbuflen;
225 if (sms.UDHPresent) {
226 u8 len=1+(*(u8 *)smsbuf);
228 if (len>smsbuflen || len>sizeof(sms.UDH))
229 g_print ("%d: UDH too long: UDHlen=%d, smsbuflen=%d, sizeof(sms.UDH)=%d !!!\n", __LINE__,
230 len, smsbuflen,sizeof(sms.UDH));
232 memcpy(sms.UDH,smsbuf,len);
233 memmove(smsbuf,smsbuf+len,smsbuflen-len);
241 static u8 bin[sizeof(bitmap.bitmap) +64/*headers safety*/];
243 char *usernetcode=PQgetvalue (res1, i, DBLF_netcode);
245 if (GE_NONE!=GSM_ReadBitmapFile(smsbuf, &bitmap))
246 continue; /* error */
249 if (!DBL_safestrcasecmp("oplogo" ,type)) {
251 if (strlen(usernetcode)+1 > sizeof(bitmap.netcode))
252 continue; /* error */
253 strcpy(bitmap.netcode, usernetcode);
255 *d++ = ((bitmap.netcode[1] & 0x0f) << 4) | (bitmap.netcode[0] & 0xf);
256 *d++ = 0xf0 | (bitmap.netcode[2] & 0x0f);
257 *d++ = ((bitmap.netcode[5] & 0x0f) << 4) | (bitmap.netcode[4] & 0xf);
259 /* Set the logo size */
260 *d++ = 0x00; /* RFU by Nokia */
262 *d++ = bitmap.height;
263 *d++ = 0x01; /* depth=number of grayscales */
265 memcpy(d,bitmap.bitmap,bitmap.size);
272 strncpy (sms.Destination, PQgetvalue (res1, i, DBLF_number), GSM_MAX_DESTINATION_LENGTH + 1);
273 sms.Destination[GSM_MAX_DESTINATION_LENGTH] = '\0';
276 g_print ("To: %s\n", sms.Destination);
279 if (GE_SMSSENDOK == WriteSMS_deconcatenated (&sms, smsbuf, smsbuflen,
280 !DBL_safestrcasecmp(DBL_true, PQgetvalue (res1, i, DBLF_longuseudh))))
282 g_string_sprintf (buf, "UPDATE outbox SET processed='t' WHERE id='%s'",
283 PQgetvalue (res1, i, DBLF_id));
284 res2 = PQexec(connOut, buf->str);
285 if (!res2 || PQresultStatus (res2) != PGRES_COMMAND_OK)
287 g_print ("%s\n", PQcmdStatus (res2));
288 g_print ("%d: UPDATE command failed\n", __LINE__);
297 g_string_sprintf (buf, "COMMIT");
298 res1 = PQexec(connOut, buf->str);
302 g_string_free(buf, TRUE);
305 if (!access("/tmp/smsd-exit",F_OK)) {
306 g_print("/tmp/smsd-exit exists, terminating\n");
307 if (unlink("/tmp/smsd-exit"))
308 g_print("/tmp/smsd-exit unlink failed: %s\n",strerror(errno));