Guest User

Untitled

a guest
Mar 2nd, 2018
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.10 KB | None | 0 0
  1. /*
  2.  
  3.   $Id$
  4.  
  5.   S M S D
  6.  
  7.   A Linux/Unix tool for the mobile phones.
  8.  
  9.   This file is part of gnokii.
  10.  
  11.   Gnokii is free software; you can redistribute it and/or modify
  12.   it under the terms of the GNU General Public License as published by
  13.   the Free Software Foundation; either version 2 of the License, or
  14.   (at your option) any later version.
  15.  
  16.   Gnokii is distributed in the hope that it will be useful,
  17.   but WITHOUT ANY WARRANTY; without even the implied warranty of
  18.   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19.   GNU General Public License for more details.
  20.  
  21.   You should have received a copy of the GNU General Public License
  22.   along with gnokii; if not, write to the Free Software
  23.   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  24.  
  25.   Copyright (C) 1999 Pavel Janík ml., Hugh Blemings
  26.   Copyright (C) 1999-2005 Jan Derfinak
  27.  
  28.   This file is a module to smsd for PostgreSQL db server.
  29.  
  30. */
  31.  
  32. #include "config.h"
  33. #include <string.h>
  34. #include <stdlib.h>
  35. #include <glib.h>
  36. #include <libpq-fe.h>
  37. #include "smsd.h"
  38. #include "gnokii.h"
  39. #include "compat.h"
  40. #include "utils.h"
  41.  
  42. static PGconn *connIn = NULL;
  43. static PGconn *connOut = NULL;
  44. static gchar *schema = NULL;        /* database schema */
  45.  
  46. GNOKII_API void DB_Bye (void)
  47. {
  48.   if (connIn)
  49.     PQfinish (connIn);
  50.  
  51.   if (connOut)
  52.     PQfinish (connOut);
  53. }
  54.  
  55.  
  56. GNOKII_API gint DB_ConnectInbox (DBConfig connect)
  57. {
  58.   connIn = PQsetdbLogin (connect.host[0] != '\0' ? connect.host : NULL,
  59.                          NULL,
  60.                          NULL,
  61.                          NULL,
  62.                          connect.db,
  63.                          connect.user[0] != '\0' ? connect.user : NULL,
  64.                          connect.password[0] != '\0' ? connect.password : NULL);
  65.  
  66.   if (PQstatus (connIn) == CONNECTION_BAD)
  67.   {
  68.     g_print (_("Connection to database '%s' on host '%s' failed.\n"),
  69.              connect.db, connect.host);
  70.     g_print (_("Error: %s\n"), PQerrorMessage (connIn));
  71.     return (1);
  72.   }
  73.  
  74.   if (schema == NULL)
  75.     schema = g_strdup (connect.schema);
  76.  
  77.   return (0);
  78. }
  79.  
  80.  
  81. GNOKII_API gint DB_ConnectOutbox (DBConfig connect)
  82. {
  83.   connOut = PQsetdbLogin (connect.host[0] != '\0' ? connect.host : NULL,
  84.                           NULL,
  85.                           NULL,
  86.                           NULL,
  87.                           connect.db,
  88.                           connect.user[0] != '\0' ? connect.user : NULL,
  89.                           connect.password[0] != '\0' ? connect.password : NULL);
  90.  
  91.   if (PQstatus (connOut) == CONNECTION_BAD)
  92.   {
  93.     g_print (_("Connection to database '%s' on host '%s' failed.\n"),
  94.              connect.db, connect.host);
  95.     g_print (_("Error: %s\n"), PQerrorMessage (connOut));
  96.     return (1);
  97.   }
  98.  
  99.   if (schema == NULL)
  100.     schema = g_strdup (connect.schema);
  101.  
  102.   return (0);
  103. }
  104.  
  105.  
  106. GNOKII_API gint DB_InsertSMS (const gn_sms * const data, const gchar * const phone)
  107. {
  108.   GString *buf, *phnStr;
  109.   gchar *text;
  110.   PGresult *res;
  111.  
  112.   if (phone[0] == '\0')
  113.     phnStr = g_string_new ("");
  114.   else
  115.   {
  116.     phnStr = g_string_sized_new (32);
  117.     g_string_printf (phnStr, "'%s',", phone);
  118.   }
  119.  
  120.   text = strEscape ((gchar *) data->user_data[0].u.text);
  121.  
  122.   buf = g_string_sized_new (256);
  123.   g_string_printf (buf, "INSERT INTO %s.inbox (\"number\", \"smsdate\", \"insertdate\",\
  124.                         \"text\", %s \"processed\") VALUES ('%s', \
  125.                         '%02d-%02d-%02d %02d:%02d:%02d+01', 'now', '%s', %s 'f')",
  126.                    schema,
  127.                    phone[0] != '\0' ? "\"phone\"," : "", data->remote.number,
  128.                    data->smsc_time.year, data->smsc_time.month,
  129.                    data->smsc_time.day, data->smsc_time.hour,
  130.                    data->smsc_time.minute, data->smsc_time.second, text, phnStr->str);
  131.   g_free (text);
  132.   g_string_free (phnStr, TRUE);
  133.  
  134.   res = PQexec (connIn, buf->str);
  135.   g_string_free (buf, TRUE);
  136.   if (!res || PQresultStatus(res) != PGRES_COMMAND_OK)
  137.   {
  138.     g_print (_("%d: INSERT INTO %s.inbox failed.\n"), __LINE__, schema);
  139.     g_print (_("Error: %s\n"), PQerrorMessage (connIn));
  140.     PQclear (res);
  141.     return (1);
  142.   }
  143.  
  144.   PQclear (res);
  145.    
  146.   return (0);
  147. }
  148.  
  149.  
  150. GNOKII_API void DB_Look (const gchar * const phone)
  151. {
  152.   GString *buf, *phnStr;
  153.   PGresult *res1, *res2;
  154.   register int i;
  155.   gint numError, error;
  156.  
  157.   if (phone[0] == '\0')
  158.     phnStr = g_string_new ("");
  159.   else
  160.   {
  161.     phnStr = g_string_sized_new (32);
  162.     g_string_printf (phnStr, "AND phone = '%s'", phone);
  163.   }
  164.  
  165.   buf = g_string_sized_new (128);
  166.  
  167.   res1 = PQexec (connOut, "BEGIN");
  168.   PQclear (res1);
  169.  
  170.   g_string_printf (buf, "SELECT id, number, text, dreport FROM %s.outbox \
  171.                         WHERE processed='f' AND localtime(0) >= not_before \
  172.                         AND localtime(0) <= not_after %s FOR UPDATE",
  173.                    schema, phnStr->str);
  174.   g_string_free (phnStr, TRUE);
  175.  
  176.   res1 = PQexec (connOut, buf->str);
  177.   if (!res1 || PQresultStatus (res1) != PGRES_TUPLES_OK)
  178.   {
  179.     g_print (_("%d: SELECT FROM %s.outbox command failed.\n"), __LINE__, schema);
  180.     g_print (_("Error: %s\n"), PQerrorMessage (connOut));
  181.     PQclear (res1);
  182.     res1 = PQexec (connOut, "ROLLBACK TRANSACTION");
  183.     PQclear (res1);
  184.     g_string_free (buf, TRUE);
  185.     return;
  186.   }
  187.  
  188.   for (i = 0; i < PQntuples (res1); i++)
  189.   {
  190.     gn_sms sms;
  191.    
  192.     gn_sms_default_submit (&sms);
  193.     memset (&sms.remote.number, 0, sizeof (sms.remote.number));
  194.     sms.delivery_report = atoi (PQgetvalue (res1, i, 3));
  195.  
  196.     strncpy (sms.remote.number, PQgetvalue (res1, i, 1), sizeof (sms.remote.number) - 1);
  197.     sms.remote.number[sizeof(sms.remote.number) - 1] = '\0';
  198.     if (sms.remote.number[0] == '+')
  199.       sms.remote.type = GN_GSM_NUMBER_International;
  200.     else
  201.       sms.remote.type = GN_GSM_NUMBER_Unknown;
  202.    
  203.     strncpy ((gchar *) sms.user_data[0].u.text, PQgetvalue (res1, i, 2), 10 * GN_SMS_MAX_LENGTH + 1);
  204.     sms.user_data[0].u.text[10 * GN_SMS_MAX_LENGTH] = '\0';
  205.     sms.user_data[0].length = strlen ((gchar *) sms.user_data[0].u.text);
  206.     sms.user_data[0].type = GN_SMS_DATA_Text;
  207.     sms.user_data[1].type = GN_SMS_DATA_None;
  208.     if (!gn_char_def_alphabet (sms.user_data[0].u.text))
  209.        sms.dcs.u.general.alphabet = GN_SMS_DCS_UCS2;
  210.  
  211.     gn_log_xdebug ("Sending SMS: %s, %s\n", sms.remote.number, sms.user_data[0].u.text);
  212.    
  213.     numError = 0;
  214.     do
  215.     {
  216.       error = WriteSMS (&sms);
  217.       sleep (1);
  218.     }
  219.     while ((error == GN_ERR_TIMEOUT || error == GN_ERR_FAILED) && numError++ < 3);
  220.  
  221.     g_string_printf (buf, "UPDATE %s.outbox SET processed='t', error='%d', \
  222.                           processed_date='now' WHERE id='%s'",
  223.                      schema, error, PQgetvalue (res1, i, 0));
  224.  
  225.     res2 = PQexec (connOut, buf->str);
  226.     if (!res2 || PQresultStatus (res2) != PGRES_COMMAND_OK)
  227.     {
  228.       g_print (_("%d: UPDATE command failed.\n"), __LINE__);  
  229.       g_print (_("Error: %s\n"), PQerrorMessage (connOut));
  230.     }
  231.  
  232.     PQclear (res2);
  233.   }
  234.  
  235.   PQclear (res1);
  236.  
  237.   res1 = PQexec(connOut, "COMMIT");
  238.  
  239.   g_string_free(buf, TRUE);
  240.   PQclear (res1);
  241. }
Add Comment
Please, Sign In to add comment