Built motion from commit 95b01fa.|0.0.70
[motion.git] / server / models / telephone.js
1 'use strict';
2
3 var crypto = require('crypto');
4 var md5 = require('md5');
5 var _ = require('lodash');
6
7 module.exports = function(sequelize, DataTypes) {
8   var Telephone = sequelize.define('Telephone', {
9     name: {
10       type: DataTypes.STRING,
11       unique: true,
12       validate: {
13         notEmpty: true,
14         is: /^[A-Za-z0-9\.\_]+$/i
15       },
16       set: function(name) {
17         this.setDataValue('name', name);
18         this.setDataValue('defaultuser', name);
19       }
20     },
21     password: {
22       type: DataTypes.STRING,
23       allowNull: false,
24       validate: {
25         notEmpty: true
26       },
27       set: function(password) {
28         this.salt = this.makeSalt();
29         this.setDataValue('password', this.encryptPassword(password));
30         this.setDataValue('md5secret', this.md5Password(this.name + ':asterisk:' + password));
31       }
32     },
33     internal: {
34       type: DataTypes.INTEGER(11),
35       unique: true,
36       set: function(internal) {
37         this.setDataValue('internal', internal);
38         this.setDataValue('accountcode', internal);
39       }
40     },
41     ipaddr: {
42       type: DataTypes.STRING,
43       allowNull: true,
44     },
45     port: {
46       type: DataTypes.INTEGER(5),
47       allowNull: true,
48     },
49     regseconds: {
50       type: DataTypes.INTEGER(11),
51       allowNull: true,
52     },
53     defaultuser: {
54       type: DataTypes.STRING,
55       allowNull: true,
56     },
57     fullcontact: {
58       type: DataTypes.STRING,
59       allowNull: true,
60     },
61     regserver: {
62       type: DataTypes.STRING,
63       allowNull: true,
64     },
65     useragent: {
66       type: DataTypes.STRING,
67       allowNull: true,
68     },
69     lastms: {
70       type: DataTypes.INTEGER(11),
71       allowNull: true,
72     },
73     host: {
74       type: DataTypes.STRING,
75       allowNull: true,
76       defaultValue: 'dynamic'
77     },
78     type: {
79       type: DataTypes.ENUM('friend', 'user', 'peer'),
80       allowNull: true,
81       defaultValue: 'friend'
82     },
83     context: {
84       type: DataTypes.STRING,
85       allowNull: true,
86       defaultValue: 'from-sip'
87     },
88     permit: {
89       type: DataTypes.STRING,
90       allowNull: true,
91     },
92     deny: {
93       type: DataTypes.STRING,
94       allowNull: true,
95     },
96     secret: {
97       type: DataTypes.STRING,
98       allowNull: true,
99     },
100     md5secret: {
101       type: DataTypes.STRING,
102       allowNull: true
103     },
104     remotesecret: {
105       type: DataTypes.STRING,
106       allowNull: true,
107     },
108     transport: {
109       type: DataTypes.STRING,
110       allowNull: true,
111       defaultValue: 'udp'
112     },
113     dtmfmode: {
114       type: DataTypes.ENUM('rfc2833', 'info', 'shortinfo', 'inband',
115         'auto'),
116       allowNull: true,
117       defaultValue: 'rfc2833'
118     },
119     directmedia: {
120       type: DataTypes.ENUM('yes', 'no', 'nonat', 'update'),
121       allowNull: true,
122       defaultValue: 'no'
123     },
124     nat: {
125       type: DataTypes.STRING,
126       allowNull: true,
127       defaultValue: 'force_rport,comedia'
128     },
129     callgroup: {
130       type: DataTypes.STRING,
131       allowNull: true,
132     },
133     pickupgroup: {
134       type: DataTypes.STRING,
135       allowNull: true,
136     },
137     language: {
138       type: DataTypes.STRING,
139       allowNull: true,
140       defaultValue: 'en'
141     },
142     disallow: {
143       type: DataTypes.STRING,
144       allowNull: true,
145       defaultValue: 'all'
146     },
147     allow: {
148       type: DataTypes.STRING,
149       allowNull: true,
150       defaultValue: 'alaw;ulaw;gsm'
151     },
152     insecure: {
153       type: DataTypes.STRING,
154       allowNull: true,
155       defaultValue: 'port,invite'
156     },
157     trustrpid: {
158       type: DataTypes.ENUM('yes', 'no'),
159       allowNull: true,
160       defaultValue: 'no'
161     },
162     progressinband: {
163       type: DataTypes.ENUM('yes', 'no', 'never'),
164       allowNull: true,
165     },
166     promiscredir: {
167       type: DataTypes.ENUM('yes', 'no'),
168       allowNull: true,
169     },
170     useclientcode: {
171       type: DataTypes.ENUM('yes', 'no'),
172       allowNull: true,
173     },
174     accountcode: {
175       type: DataTypes.INTEGER(11),
176       allowNull: true,
177     },
178     setvar: {
179       type: DataTypes.STRING,
180       allowNull: true,
181     },
182     callerid: {
183       type: DataTypes.STRING,
184       allowNull: true,
185       defaultValue: '"" <>'
186     },
187     amaflags: {
188       type: DataTypes.STRING,
189       allowNull: true,
190     },
191     callcounter: {
192       type: DataTypes.ENUM('yes', 'no'),
193       allowNull: true,
194       defaultValue: 'yes'
195     },
196     busylevel: {
197       type: DataTypes.INTEGER(11),
198       allowNull: true,
199     },
200     allowoverlap: {
201       type: DataTypes.ENUM('yes', 'no'),
202       allowNull: true,
203     },
204     allowsubscribe: {
205       type: DataTypes.ENUM('yes', 'no'),
206       allowNull: true,
207     },
208     videosupport: {
209       type: DataTypes.ENUM('yes', 'no'),
210       allowNull: true,
211     },
212     maxcallbitrate: {
213       type: DataTypes.INTEGER(11),
214       allowNull: true,
215     },
216     rfc2833compensate: {
217       type: DataTypes.ENUM('yes', 'no'),
218       allowNull: true,
219     },
220     mailbox: {
221       type: DataTypes.STRING,
222       allowNull: true,
223     },
224     "session-timers": {
225       type: DataTypes.ENUM('accept', 'refuse', 'originate'),
226       allowNull: true,
227     },
228     "session-expires": {
229       type: DataTypes.INTEGER(11),
230       allowNull: true,
231     },
232     "session-minse": {
233       type: DataTypes.INTEGER(11),
234       allowNull: true,
235     },
236     "session-refresher": {
237       type: DataTypes.ENUM('uac', 'uas'),
238       allowNull: true,
239     },
240     t38pt_usertpsource: {
241       type: DataTypes.STRING,
242       allowNull: true,
243     },
244     regexten: {
245       type: DataTypes.STRING,
246       allowNull: true,
247     },
248     fromdomain: {
249       type: DataTypes.STRING,
250       allowNull: true,
251     },
252     fromuser: {
253       type: DataTypes.STRING,
254       allowNull: true,
255     },
256     qualify: {
257       type: DataTypes.ENUM('yes', 'no'),
258       allowNull: true,
259       defaultValue: 'yes'
260     },
261     defaultip: {
262       type: DataTypes.STRING,
263       allowNull: true,
264     },
265     rtptimeout: {
266       type: DataTypes.INTEGER(11),
267       allowNull: true,
268     },
269     rtpholdtimeout: {
270       type: DataTypes.INTEGER(11),
271       allowNull: true,
272     },
273     sendrpid: {
274       type: DataTypes.ENUM('yes', 'no'),
275       allowNull: true,
276       defaultValue: 'no'
277     },
278     outboundproxy: {
279       type: DataTypes.STRING,
280       allowNull: true,
281     },
282     callbackextension: {
283       type: DataTypes.STRING,
284       allowNull: true,
285     },
286     timert1: {
287       type: DataTypes.INTEGER(11),
288       allowNull: true,
289     },
290     timerb: {
291       type: DataTypes.INTEGER(11),
292       allowNull: true,
293     },
294     qualifyfreq: {
295       type: DataTypes.INTEGER(11),
296       allowNull: true,
297     },
298     constantssrc: {
299       type: DataTypes.ENUM('yes', 'no'),
300       allowNull: true,
301     },
302     contactpermit: {
303       type: DataTypes.STRING,
304       allowNull: true,
305     },
306     contactdeny: {
307       type: DataTypes.STRING,
308       allowNull: true,
309     },
310     usereqphone: {
311       type: DataTypes.ENUM('yes', 'no'),
312       allowNull: true,
313       defaultValue: 'no'
314     },
315     textsupport: {
316       type: DataTypes.ENUM('yes', 'no'),
317       allowNull: true,
318     },
319     faxdetect: {
320       type: DataTypes.ENUM('yes', 'no'),
321       allowNull: true,
322     },
323     buggymwi: {
324       type: DataTypes.ENUM('yes', 'no'),
325       allowNull: true,
326     },
327     auth: {
328       type: DataTypes.STRING,
329       allowNull: true,
330     },
331     fullname: {
332       type: DataTypes.STRING,
333       allowNull: true,
334     },
335     trunkname: {
336       type: DataTypes.STRING,
337       allowNull: true,
338     },
339     cid_number: {
340       type: DataTypes.STRING,
341       allowNull: true,
342     },
343     callingpres: {
344       type: DataTypes.ENUM('ALLOWED_NOT_SCREENED',
345         'ALLOWED_PASSED_SCREEN', 'ALLOWED_FAILED_SCREEN', 'ALLOWED',
346         'PROHIB_NOT_SCREENED', 'PROHIB_PASSED_SCREEN',
347         'PROHIB_FAILED_SCREEN', 'PROHIB'),
348       allowNull: true,
349     },
350     mohinterpret: {
351       type: DataTypes.STRING,
352       allowNull: true,
353     },
354     mohsuggest: {
355       type: DataTypes.STRING,
356       allowNull: true,
357     },
358     parkinglot: {
359       type: DataTypes.STRING,
360       allowNull: true,
361     },
362     hasvoicemail: {
363       type: DataTypes.ENUM('yes', 'no'),
364       allowNull: true,
365     },
366     subscribemwi: {
367       type: DataTypes.ENUM('yes', 'no'),
368       allowNull: true,
369     },
370     vmexten: {
371       type: DataTypes.STRING,
372       allowNull: true,
373     },
374     description: {
375       type: DataTypes.STRING,
376       allowNull: true,
377     },
378     autoframing: {
379       type: DataTypes.ENUM('yes', 'no'),
380       allowNull: true,
381     },
382     limitonpeers: {
383       type: DataTypes.ENUM('yes', 'no'),
384       allowNull: true,
385       defaultValue: 'yes'
386     },
387     rtpkeepalive: {
388       type: DataTypes.INTEGER(11),
389       allowNull: true,
390     },
391     "call-limit": {
392       type: DataTypes.INTEGER(11),
393       allowNull: true,
394       defaultValue: null
395     },
396     g726nonstandard: {
397       type: DataTypes.ENUM('yes', 'no'),
398       allowNull: true,
399     },
400     ignoresdpversion: {
401       type: DataTypes.ENUM('yes', 'no'),
402       allowNull: true,
403     },
404     allowtransfer: {
405       type: DataTypes.ENUM('yes', 'no'),
406       allowNull: true,
407     },
408     dynamic: {
409       type: DataTypes.ENUM('yes', 'no'),
410       allowNull: true,
411     },
412     encryption: {
413       type: DataTypes.ENUM('yes', 'no'),
414       allowNull: true,
415       defaultValue: 'no'
416     },
417     registry: {
418       type: DataTypes.STRING,
419       allowNull: true,
420     }
421   }, {
422     tableName: 'users',
423     defaultScope: {
424       where: {
425         role: 'telephone'
426       }
427     },
428     instanceMethods: {
429       /**
430        * Authenticate - check if the passwords are the same
431        *
432        * @param {String} plainText
433        *        {function} callBack
434        * @api public
435        */
436       authenticate: function(plainText) {
437         return this.encryptPassword(plainText) === this.password;
438       },
439       /**
440        * Make salt
441        *
442        * @return {String}
443        * @api public
444        */
445       makeSalt: function() {
446         return crypto.randomBytes(16).toString('base64');
447       },
448       /**
449        * Encrypt password
450        *
451        * @param {String} password
452        * @return {String}
453        * @api public
454        */
455       encryptPassword: function(password) {
456         if (!password || !this.salt) return '';
457         var salt = new Buffer(this.salt, 'base64');
458         return crypto.pbkdf2Sync(password, salt, 10000, 64).toString(
459           'base64');
460       },
461       /**
462        * md5 password
463        *
464        * @param {String} password
465        * @return {String}
466        * @api public
467        */
468       md5Password: function(password) {
469         if (!password) return '';
470         return md5(password);
471       }
472     },
473     associate: function(models) {
474       // BELOGNS TO MANY
475       // Telephone.hasMany(models.ChatMessage);
476       // Telephone.hasMany(models.Contact);
477       // Telephone.belongsToMany(models.Module, {
478       //   through: 'user_has_modules'
479       // });
480       // Telephone.belongsToMany(models.Channel, {
481       //   through: 'user_has_channels'
482       // });
483       // Telephone.belongsToMany(models.Team, {
484       //   through: models.UserHasTeam
485       // });
486       // Telephone.belongsToMany(models.ChatRoom, {
487       //   through: models.UserHasChatRoom
488       // });
489       // Telephone.belongsToMany(models.MailQueue, {
490       //   through: models.UserHasMailQueue
491       // });
492       // Telephone.belongsToMany(models.ChatQueue, {
493       //   through: models.UserHasChatQueue
494       // });
495       // Telephone.belongsToMany(models.VoiceQueue, {
496       //   through: models.UserHasVoiceQueue
497       // });
498       // Telephone.hasMany(models.VoiceExtension, {
499       //   foreignKey: 'UserId',
500       //   as: 'UserExtensions',
501       //   onDelete: 'cascade'
502       // });
503     }
504   });
505
506   return Telephone;
507 }