~ubuntu-app-review-contributors/ubuntu-app-reviews/btlogger

« back to all changes in this revision

Viewing changes to src/log.c

  • Committer: App Bot
  • Date: 2012-06-12 11:20:35 UTC
  • Revision ID: appbot@holba.ch-20120612112035-kcaei7jok3calfjt
initial import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// License: GPLv3
 
2
// Copyright 2010 John P. T. Moore
 
3
// jmoore@zedstar.org
 
4
 
 
5
#include <stdio.h>
 
6
#include <stdlib.h>
 
7
#include <string.h>
 
8
#include <unistd.h>
 
9
#include <sys/time.h>
 
10
#include <sqlite3.h>
 
11
#include <dbus/dbus-glib.h>
 
12
#include <glib.h>
 
13
#include <libsoup/soup.h>
 
14
#include "scan.h"
 
15
#include "log.h"
 
16
#include "marshal.h"
 
17
#include "tweet.h"
 
18
#include "config.h"
 
19
 
 
20
#ifdef DEBUG_MODE
 
21
#define dbg(fmtstr, args...) \
 
22
  (g_print(PROGNAME ":%s: " fmtstr "\n", __func__, ##args))
 
23
#else
 
24
#define dbg(dummy...)
 
25
#endif
 
26
 
 
27
 
 
28
 
 
29
sqlite3 *openLog(char *file)
 
30
{
 
31
  int rc;
 
32
  sqlite3 *db;
 
33
 
 
34
  rc = sqlite3_open(file, &db);
 
35
  if( rc ) {
 
36
    fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
 
37
    sqlite3_close(db);
 
38
    exit(1);
 
39
  }
 
40
 
 
41
  return db;
 
42
}
 
43
 
 
44
 
 
45
void closeLog(sqlite3 *db)
 
46
{
 
47
  sqlite3_close(db);
 
48
}
 
49
 
 
50
 
 
51
int logDevice(sqlite3 *db, RestProxy *twitter, char *mac, char *name)
 
52
{
 
53
  int rc; // result code
 
54
  int id; // primary key
 
55
  char *sql;
 
56
  sqlite3_stmt *stmt;
 
57
  struct timeval t_now;
 
58
  time_t seen;
 
59
  char *zErrMsg = 0;
 
60
 
 
61
  gettimeofday(&t_now, NULL);
 
62
  seen = t_now.tv_sec;
 
63
 
 
64
  sql = sqlite3_mprintf("select id from log where mac = '%s'", mac);
 
65
  rc = sqlite3_prepare_v2(db, sql, strlen(sql), &stmt, NULL);
 
66
  sqlite3_free(sql);
 
67
  if(rc!=SQLITE_OK) {
 
68
    fprintf(stderr, "SQL error: %s\n", zErrMsg);
 
69
    sqlite3_free(zErrMsg);
 
70
    return EXIT_FAILURE;
 
71
  }
 
72
  
 
73
  rc = sqlite3_step(stmt);
 
74
  switch(rc) {
 
75
    case SQLITE_ROW:
 
76
    case SQLITE_DONE: 
 
77
    case SQLITE_OK:
 
78
      break;
 
79
    default:
 
80
      fprintf(stderr, "SQL error: %s\n", zErrMsg);
 
81
      sqlite3_free(zErrMsg);
 
82
      return EXIT_FAILURE;
 
83
  }
 
84
     
 
85
  id = sqlite3_column_int(stmt, 0);
 
86
  sqlite3_finalize(stmt);
 
87
  // if no record exists
 
88
  if (id == 0) {     
 
89
    sql = sqlite3_mprintf("insert into log (mac, name, seen) values ('%s', \"%s\", %d)", mac, name, seen);
 
90
    // post to Twitter if proxy set
 
91
    if (twitter) {
 
92
      gchar *message;
 
93
 
 
94
      message = g_strdup_printf ("saw %s with id %s #%s", 
 
95
                                 (name) ? name : "someone who didn't set their device name", mac, PROGNAME);
 
96
      tweet(twitter, message);
 
97
      g_free(message);
 
98
    }
 
99
  }
 
100
  else {
 
101
    sql = sqlite3_mprintf("update log set name = \"%s\", seen = %d where id = %d", name, seen, id);
 
102
  }
 
103
  dbg("%s", sql);
 
104
  rc = sqlite3_exec(db, sql, 0, 0, &zErrMsg);
 
105
  sqlite3_free(sql);
 
106
  if( rc!=SQLITE_OK ) {
 
107
    fprintf(stderr, "SQL error: %s\n", zErrMsg);
 
108
    sqlite3_free(zErrMsg);
 
109
    return EXIT_FAILURE;
 
110
  }
 
111
 
 
112
  return EXIT_SUCCESS;
 
113
 
 
114
}