diff --git a/sydent/config/sms.py b/sydent/config/sms.py index 404a10a8..cde8b6fa 100644 --- a/sydent/config/sms.py +++ b/sydent/config/sms.py @@ -11,12 +11,14 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. - +import configparser from configparser import ConfigParser from typing import Dict, List +import sydent.sms.openmarket from sydent.config._base import BaseConfig from sydent.config.exceptions import ConfigError +from sydent.util.loader import load_class class SMSConfig(BaseConfig): @@ -33,6 +35,16 @@ def parse_config(self, cfg: "ConfigParser") -> bool: self.api_username = cfg.get("sms", "username").encode("UTF-8") self.api_password = cfg.get("sms", "password").encode("UTF-8") + self.provider_class = sydent.sms.openmarket.OpenMarketSMS + try: + sms_provider = cfg.get("sms", "provider") + except configparser.NoOptionError: + pass + else: + if sms_provider: + self.provider_class = load_class(sms_provider) + self.provider_config = cfg.get("sms", "provider_config", fallback={}) + self.originators: Dict[str, List[Dict[str, str]]] = {} self.smsRules = {} diff --git a/sydent/util/loader.py b/sydent/util/loader.py new file mode 100644 index 00000000..6defdb9a --- /dev/null +++ b/sydent/util/loader.py @@ -0,0 +1,13 @@ +import importlib +from typing import Type + +from sydent.config.exceptions import ConfigError + + +def load_class(full_path: str) -> Type: + try: + _module, class_name = full_path.rsplit(".", 1) + module = importlib.import_module(_module) + return getattr(module, class_name) + except (AttributeError, ModuleNotFoundError): + raise ConfigError("Cannot load: %s" % full_path) diff --git a/sydent/validators/msisdnvalidator.py b/sydent/validators/msisdnvalidator.py index 478298bd..70364009 100644 --- a/sydent/validators/msisdnvalidator.py +++ b/sydent/validators/msisdnvalidator.py @@ -19,7 +19,6 @@ import phonenumbers from sydent.db.valsession import ThreePidValSessionStore -from sydent.sms.openmarket import OpenMarketSMS from sydent.util import time_msec from sydent.validators import DestinationRejectedException, common @@ -32,7 +31,7 @@ class MsisdnValidator: def __init__(self, sydent: "Sydent") -> None: self.sydent = sydent - self.omSms = OpenMarketSMS(sydent) + self.Sms = sydent.config.sms.provider_class(sydent) # cache originators & sms rules from config file self.originators = self.sydent.config.sms.originators @@ -94,7 +93,7 @@ async def requestToken( smsBody = smsBodyTemplate.format(token=token_info.token) - await self.omSms.sendTextSMS(smsBody, msisdn, originator) + await self.Sms.sendTextSMS(smsBody, msisdn, originator) valSessionStore.setSendAttemptNumber(valSession.id, send_attempt)