SIP Witch 1.9.15
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
scripting.cpp
Go to the documentation of this file.
1 // Copyright (C) 2006-2014 David Sugar, Tycho Softworks.
2 // Copyright (C) 2015 Cherokees of Idaho.
3 //
4 // This program is free software; you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation; either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 
17 #include <sipwitch-config.h>
18 #include <sipwitch/sipwitch.h>
19 
20 namespace sipwitch {
21 
22 static const char *dirpath = NULL;
23 static char prior[65] = "down";
24 
25 class __LOCAL scripting : public modules::sipwitch
26 {
27 public:
28  scripting();
29 
30 private:
31  void start(service *cfg);
32  void reload(service *cfg);
33  void activating(MappedRegistry *rr);
34  void expiring(MappedRegistry *rr);
35 };
36 
37 static scripting scripting_plugin;
38 
40 modules::sipwitch()
41 {
42  shell::log(shell::INFO, "scripting plugin loaded");
43 }
44 
46 {
47  assert(cfg != NULL);
48 
49  if(dirpath == NULL)
50  start(cfg);
51 
52  if(!dirpath)
53  return;
54 
55  const char *state = cfg->getRoot()->getPointer();
56  if(!state)
57  state = "up";
58 
59  if(String::equal(state, prior))
60  return;
61 
62  control::libexec("%s/sipstate %s", dirpath, state);
63 
64  String::set(prior, sizeof(prior), state);
65 }
66 
68 {
69  assert(cfg != NULL);
70 
71  static char buf[256];
72  const char *home = control::env("HOME");
73 
74  if(fsys::is_dir(DEFAULT_CFGPATH "/sysconfig/sipwitch-scripts"))
75  dirpath = DEFAULT_CFGPATH "/sysconfig/sipwitch-scripts";
76  else if(fsys::is_dir(DEFAULT_LIBEXEC "/sipwitch"))
77  dirpath = DEFAULT_LIBEXEC "/sipwitch";
78  else if(home) {
79  snprintf(buf, sizeof(buf), "%s/.sipwitch-scripts", home);
80  if(fsys::is_dir(buf))
81  dirpath = buf;
82  }
83 
84  if(dirpath)
85  shell::log(shell::INFO, "scripting plugin path %s", dirpath);
86  else
87  shell::log(shell::ERR, "scripting plugin disabled; no script directory");
88 }
89 
91 {
92  char addr[128];
93  if(!dirpath)
94  return;
95 
96  Socket::query((struct sockaddr *)&rr->contact, addr, sizeof(addr));
97  control::libexec("%s/sipup %s %d %s:%d %d", dirpath, rr->userid, rr->ext,
98  addr, Socket::port((struct sockaddr *)&rr->contact),
99  (int)(rr->type - MappedRegistry::EXPIRED));
100 }
101 
103 {
104  if(!dirpath)
105  return;
106 
107  control::libexec("%s/sipdown %s %d", dirpath, rr->userid, rr->ext);
108 }
109 
110 } // end namespace
Representation of a mapped active user record.
Definition: mapped.h:95
sockaddr_internet contact
Definition: mapped.h:111
keynode * getRoot(void)
Definition: service.h:304
Top level include directory for GNU Telephony SIP Witch Server.
void reload(service *cfg)
Definition: scripting.cpp:45
static bool libexec(const char *fmt,...) __PRINTF(1
Execute an external shell command on behalf of the server.
Definition: control.cpp:261
void expiring(MappedRegistry *rr)
Notify plugins a user registration has been expired or released.
Definition: scripting.cpp:102
void activating(MappedRegistry *rr)
Notify plugins a user registration is being activated.
Definition: scripting.cpp:90
void start(service *cfg)
Definition: scripting.cpp:67
System configuration instance and service functions.
Definition: service.h:78
Common interfaces and clases for plugins.
Definition: modules.h:55
Common base class for sipwitch plugin services.
Definition: modules.h:66
static const char * env(const char *id)
Return the value of a server environment variable.
Definition: control.h:131
enum sipwitch::MappedRegistry::@5 type