summary refs log tree commit diff
path: root/nixos/modules/services/misc/gammu-smsd.nix
blob: d4bb58d81dde3a17137885322480c2cc91cb742c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
{ pkgs, lib, config, ... }:

with lib;
let
  cfg = config.services.gammu-smsd;

  configFile = pkgs.writeText "gammu-smsd.conf" ''
    [gammu]
    Device = ${cfg.device.path}
    Connection = ${cfg.device.connection}
    SynchronizeTime = ${if cfg.device.synchronizeTime then "yes" else "no"}
    LogFormat = ${cfg.log.format}
    ${if (cfg.device.pin != null) then "PIN = ${cfg.device.pin}" else ""}
    ${cfg.extraConfig.gammu}


    [smsd]
    LogFile = ${cfg.log.file}
    Service = ${cfg.backend.service}

    ${optionalString (cfg.backend.service == "files") ''
      InboxPath = ${cfg.backend.files.inboxPath}
      OutboxPath = ${cfg.backend.files.outboxPath}
      SentSMSPath = ${cfg.backend.files.sentSMSPath}
      ErrorSMSPath = ${cfg.backend.files.errorSMSPath}
    ''}

    ${optionalString (cfg.backend.service == "sql" && cfg.backend.sql.driver == "sqlite") ''
      Driver = ${cfg.backend.sql.driver}
      DBDir = ${cfg.backend.sql.database}
    ''}

    ${optionalString (cfg.backend.service == "sql" && cfg.backend.sql.driver == "native_pgsql") (
      with cfg.backend; ''
        Driver = ${sql.driver}
        ${if (sql.database!= null) then "Database = ${sql.database}" else ""}
        ${if (sql.host != null) then "Host = ${sql.host}" else ""}
        ${if (sql.user != null) then "User = ${sql.user}" else ""}
        ${if (sql.password != null) then "Password = ${sql.password}" else ""}
      '')}

    ${cfg.extraConfig.smsd}
  '';

  initDBDir = "share/doc/gammu/examples/sql";

  gammuPackage = with cfg.backend; (pkgs.gammu.override {
    dbiSupport = (service == "sql" && sql.driver == "sqlite");
    postgresSupport = (service == "sql" && sql.driver == "native_pgsql");
  });

in {
  options = {
    services.gammu-smsd = {

      enable = mkEnableOption "gammu-smsd daemon";

      user = mkOption {
        type = types.str;
        default = "smsd";
        description = "User that has access to the device";
      };

      device = {
        path = mkOption {
          type = types.path;
          description = "Device node or address of the phone";
          example = "/dev/ttyUSB2";
        };

        group = mkOption {
          type = types.str;
          default = "root";
          description = "Owner group of the device";
          example = "dialout";
        };

        connection = mkOption {
          type = types.str;
          default = "at";
          description = "Protocol which will be used to talk to the phone";
        };

        synchronizeTime = mkOption {
          type = types.bool;
          default = true;
          description = "Whether to set time from computer to the phone during starting connection";
        };

        pin = mkOption {
          type = types.nullOr types.str;
          default = null;
          description = "PIN code for the simcard";
        };
      };


      log = {
        file = mkOption {
          type = types.str;
          default = "syslog";
          description = "Path to file where information about communication will be stored";
        };

        format = mkOption {
          type = types.enum [ "nothing" "text" "textall" "textalldate" "errors" "errorsdate" "binary" ];
          default = "errors";
          description = "Determines what will be logged to the LogFile";
        };
      };


      extraConfig = {
        gammu = mkOption {
          type = types.lines;
          default = "";
          description = "Extra config lines to be added into [gammu] section";
        };


        smsd = mkOption {
          type = types.lines;
          default = "";
          description = "Extra config lines to be added into [smsd] section";
        };
      };


      backend = {
        service = mkOption {
          type = types.enum [ "null" "files" "sql" ];
          default = "null";
          description = "Service to use to store sms data.";
        };

        files = {
          inboxPath = mkOption {
            type = types.path;
            default = "/var/spool/sms/inbox/";
            description = "Where the received SMSes are stored";
          };

          outboxPath = mkOption {
            type = types.path;
            default = "/var/spool/sms/outbox/";
            description = "Where SMSes to be sent should be placed";
          };

          sentSMSPath = mkOption {
            type = types.path;
            default = "/var/spool/sms/sent/";
            description = "Where the transmitted SMSes are placed";
          };

          errorSMSPath = mkOption {
            type = types.path;
            default = "/var/spool/sms/error/";
            description = "Where SMSes with error in transmission is placed";
          };
        };

        sql = {
          driver = mkOption {
            type = types.enum [ "native_mysql" "native_pgsql" "odbc" "dbi" ];
            description = "DB driver to use";
          };

          sqlDialect = mkOption {
            type = types.nullOr types.str;
            default = null;
            description = "SQL dialect to use (odbc driver only)";
          };

          database = mkOption {
            type = types.nullOr types.str;
            default = null;
            description = "Database name to store sms data";
          };

          host = mkOption {
            type = types.str;
            default = "localhost";
            description = "Database server address";
          };

          user = mkOption {
            type = types.nullOr types.str;
            default = null;
            description = "User name used for connection to the database";
          };

          password = mkOption {
            type = types.nullOr types.str;
            default = null;
            description = "User password used for connetion to the database";
          };
        };
      };
    };
  };

  config = mkIf cfg.enable {
    users.users.${cfg.user} = {
      description = "gammu-smsd user";
      isSystemUser = true;
      group = cfg.device.group;
    };

    environment.systemPackages = with cfg.backend; [ gammuPackage ]
    ++ optionals (service == "sql" && sql.driver == "sqlite")  [ pkgs.sqlite ];

    systemd.services.gammu-smsd = {
      description = "gammu-smsd daemon";

      wantedBy = [ "multi-user.target" ];

      wants = with cfg.backend; [ ]
      ++ optionals (service == "sql" && sql.driver == "native_pgsql") [ "postgresql.service" ];

      preStart = with cfg.backend;

        optionalString (service == "files") (with files; ''
          mkdir -m 755 -p ${inboxPath} ${outboxPath} ${sentSMSPath} ${errorSMSPath}
          chown ${cfg.user} -R ${inboxPath}
          chown ${cfg.user} -R ${outboxPath}
          chown ${cfg.user} -R ${sentSMSPath}
          chown ${cfg.user} -R ${errorSMSPath}
        '')
      + optionalString (service == "sql" && sql.driver == "sqlite") ''
         cat "${gammuPackage}/${initDBDir}/sqlite.sql" \
         | ${pkgs.sqlite.bin}/bin/sqlite3 ${sql.database}
        ''
      + (let execPsql = extraArgs: concatStringsSep " " [
          (optionalString (sql.password != null) "PGPASSWORD=${sql.password}")
          "${config.services.postgresql.package}/bin/psql"
          (optionalString (sql.host != null) "-h ${sql.host}")
          (optionalString (sql.user != null) "-U ${sql.user}")
          "$extraArgs"
          "${sql.database}"
        ]; in optionalString (service == "sql" && sql.driver == "native_pgsql") ''
         echo '\i '"${gammuPackage}/${initDBDir}/pgsql.sql" | ${execPsql ""}
       '');

      serviceConfig = {
        User = "${cfg.user}";
        Group = "${cfg.device.group}";
        PermissionsStartOnly = true;
        ExecStart = "${gammuPackage}/bin/gammu-smsd -c ${configFile}";
      };

    };
  };
}