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

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

397

398

399

400

401

402

403

404

405

406

407

408

409

# Copyright 2013-2015 The Distro Tracker Developers 

# See the COPYRIGHT file at the top-level directory of this distribution and 

# at https://deb.li/DTAuthors 

# 

# This file is part of Distro Tracker. It is subject to the license terms 

# in the LICENSE file found in the top-level directory of this 

# distribution and at https://deb.li/DTLicense. No part of Distro Tracker, 

# including this file, may be copied, modified, propagated, or distributed 

# except according to the terms contained in the LICENSE file. 

""" 

Implementation of miscellaneous commands. 

""" 

 

from django.conf import settings 

from django.core.exceptions import ValidationError 

 

from distro_tracker.core.models import ( 

BinaryPackageName, 

EmailSettings, 

PackageName, 

PseudoPackageName, 

SourcePackageName, 

Subscription, 

UserEmail 

) 

from distro_tracker.core.utils import ( 

distro_tracker_render_to_string, 

get_or_none 

) 

from distro_tracker.mail.control.commands.base import Command 

from distro_tracker.mail.control.commands.confirmation import needs_confirmation 

 

DISTRO_TRACKER_FQDN = settings.DISTRO_TRACKER_FQDN 

 

 

@needs_confirmation 

class SubscribeCommand(Command): 

""" 

A command which subscribes a user to a package so that they 

receive that package's email messages. 

 

.. note:: 

This command requires confirmation. 

""" 

META = { 

'description': """subscribe <srcpackage> [<email>] 

Subscribes <email> to all messages regarding <srcpackage>. If 

<email> is not given, it subscribes the From address. If the 

<srcpackage> is not a valid source package, you'll get a warning. 

If it's a valid binary package, the mapping will automatically be 

done for you.""", 

'name': 'subscribe', 

'position': 1, 

} 

 

REGEX_LIST = ( 

r'\s+(?P<package>\S+)(?:\s+(?P<email>\S+))?$', 

) 

 

def __init__(self, package, email): 

super(SubscribeCommand, self).__init__() 

self.package = package 

self.user_email = email 

 

def get_command_text(self): 

return super(SubscribeCommand, self).get_command_text( 

self.package, self.user_email) 

 

def pre_confirm(self): 

""" 

Implementation of a hook method which is executed instead of 

:py:meth:`handle` when the command is not confirmed. 

""" 

 

if not self.validate_email(self.user_email): 

self.warning('%s is not a valid email', self.user_email) 

return False 

 

settings = get_or_none(EmailSettings, 

user_email__email__iexact=self.user_email) 

if settings and settings.is_subscribed_to(self.package): 

self.warning('%s is already subscribed to %s', 

self.user_email, self.package) 

return False 

 

if not SourcePackageName.objects.exists_with_name(self.package): 

if BinaryPackageName.objects.exists_with_name(self.package): 

binary_package = \ 

BinaryPackageName.objects.get_by_name(self.package) 

self.warning('%s is not a source package.', self.package) 

self.reply('%s is the source package ' 

'for the %s binary package', 

binary_package.main_source_package_name, 

binary_package.name) 

self.package = binary_package.main_source_package_name.name 

else: 

self.warning('%s is neither a source package ' 

'nor a binary package.', self.package) 

if PseudoPackageName.objects.exists_with_name(self.package): 

self.warning('Package %s is a pseudo package.', 

self.package) 

else: 

self.warning('Package %s is not even a pseudo package.', 

self.package) 

 

try: 

Subscription.objects.create_for( 

email=self.user_email, 

package_name=self.package, 

active=False) 

except ValidationError as e: 

self.warning('%s', e.message) 

return False 

 

self.reply('A confirmation mail has been sent to %s', self.user_email) 

return True 

 

def handle(self): 

subscription = Subscription.objects.create_for( 

package_name=self.package, 

email=self.user_email, 

active=True) 

123 ↛ 127line 123 didn't jump to line 127, because the condition on line 123 was never false if subscription: 

self.reply('%s has been subscribed to %s', self.user_email, 

self.package) 

else: 

self.error('Could not subscribe %s to %s', self.user_email, 

self.package) 

 

def get_confirmation_message(self): 

""" 

:returns: A message giving additional information about subscribing to 

a package. 

:rtype: string 

""" 

return distro_tracker_render_to_string( 

'control/email-subscription-confirmation.txt', { 

'package': self.package, 

} 

) 

 

 

@needs_confirmation 

class UnsubscribeCommand(Command): 

""" 

Command which unsubscribes the user from a package so that they no 

longer receive any email messages regarding this package. 

 

.. note:: 

This command requires confirmation. 

""" 

META = { 

'description': """unsubscribe <srcpackage> [<email>] 

Unsubscribes <email> from <srcpackage>. Like the subscribe command, 

it will use the From address if <email> is not given.""", 

'name': 'unsubscribe', 

'position': 2, 

} 

 

REGEX_LIST = ( 

r'\s+(?P<package>\S+)(?:\s+(?P<email>\S+))?$', 

) 

 

def __init__(self, package, email): 

super(UnsubscribeCommand, self).__init__() 

self.package = package 

self.user_email = email 

 

def get_command_text(self): 

return super(UnsubscribeCommand, self).get_command_text( 

self.package, self.user_email) 

 

def pre_confirm(self): 

""" 

Implementation of a hook method which is executed instead of 

:py:meth:`handle` when the command is not confirmed. 

""" 

if not SourcePackageName.objects.exists_with_name(self.package): 

if BinaryPackageName.objects.exists_with_name(self.package): 

binary_package = \ 

BinaryPackageName.objects.get_by_name(self.package) 

self.warning('%s is not a source package.', self.package) 

self.reply('%s is the source package ' 

'for the %s binary package', 

binary_package.main_source_package_name, 

binary_package.name) 

self.package = binary_package.main_source_package_name.name 

else: 

self.warning('%s is neither a source package ' 

'nor a binary package.', self.package) 

settings = get_or_none(EmailSettings, 

user_email__email__iexact=self.user_email) 

if not settings or not settings.is_subscribed_to(self.package): 

self.error("%s is not subscribed, you can't unsubscribe.", 

self.user_email) 

return False 

 

self.reply('A confirmation mail has been sent to %s', self.user_email) 

return True 

 

def handle(self): 

success = Subscription.objects.unsubscribe(self.package, 

self.user_email) 

204 ↛ 208line 204 didn't jump to line 208, because the condition on line 204 was never false if success: 

self.reply('%s has been unsubscribed from %s', self.user_email, 

self.package) 

else: 

self.error('Could not unsubscribe %s from %s', self.user_email, 

self.package) 

 

def get_confirmation_message(self): 

""" 

:returns: A message giving additional information about unsubscribing 

from a package. 

:rtype: string 

""" 

return distro_tracker_render_to_string( 

'control/email-unsubscribe-confirmation.txt', { 

'package': self.package, 

} 

) 

 

 

class WhichCommand(Command): 

""" 

A command which returns a list of packages to which the given user is 

subscribed to. 

""" 

META = { 

'description': """which [<email>] 

Tells you which packages <email> is subscribed to.""", 

'name': 'which', 

'position': 4, 

} 

 

REGEX_LIST = ( 

r'(?:\s+(?P<email>\S+))?$', 

) 

 

def __init__(self, email): 

super(WhichCommand, self).__init__() 

self.user_email = email 

 

def get_command_text(self): 

return super(WhichCommand, self).get_command_text(self.user_email) 

 

def handle(self): 

user_subscriptions = Subscription.objects.get_for_email( 

self.user_email) 

if not user_subscriptions: 

self.reply('No subscriptions found') 

return 

self.list_reply(sub.package for sub in user_subscriptions) 

 

 

class WhoCommand(Command): 

""" 

A command which returns a list of users which are subscribed to the given 

package. 

""" 

META = { 

'description': """who <package> 

Outputs all the subscriber emails for the given package in 

an obfuscated form.""", 

'name': 'who', 

'position': 5, 

} 

 

REGEX_LIST = ( 

r'(?:\s+(?P<package>\S+))$', 

) 

 

def __init__(self, package): 

super(WhoCommand, self).__init__() 

self.package_name = package 

 

def get_command_text(self): 

return super(WhoCommand, self).get_command_text(self.package_name) 

 

def handle(self): 

package = get_or_none(PackageName, name=self.package_name) 

if not package: 

self.error('Package %s does not exist', self.package_name) 

return 

 

if package.subscriptions.count() == 0: 

self.reply('Package %s does not have any subscribers', package.name) 

return 

 

self.reply("Here's the list of subscribers to package %s:", 

self.package_name) 

self.list_reply( 

self.obfuscate(subscriber) 

for subscriber in package.subscriptions.all() 

) 

 

def obfuscate(self, user_email): 

""" 

Helper method which obfuscates the given email. 

 

:param user_email: The user whose email should be obfuscated. 

:type user_email: 

:py:class:`UserEmail <distro_tracker.core.models.UserEmail>` 

 

:returns: An obfuscated email address of the given user. 

:rtype: string 

""" 

email = user_email.email 

local_part, domain = email.rsplit('@', 1) 

domain_parts = domain.split('.') 

obfuscated_domain = '.'.join( 

part[0] + '.' * (len(part) - 1) 

for part in domain_parts 

) 

return local_part + '@' + obfuscated_domain 

 

 

class QuitCommand(Command): 

""" 

When this command is executed, the processing of further commands should 

stop. 

""" 

META = { 

'description': '''quit 

Stops processing commands''', 

'name': 'quit', 

'aliases': ['thanks', '--'], 

'position': 6 

} 

 

REGEX_LIST = ( 

r'$', 

) 

 

def handle(self): 

self.reply('Stopping processing here.') 

 

 

@needs_confirmation 

class UnsubscribeallCommand(Command): 

""" 

Command which unsubscribes the user from all packages so that they 

no longer receive any email messages regarding any packages. 

 

.. note:: 

This command requires confirmation. 

""" 

META = { 

'description': '''unsubscribeall [<email>] 

Cancel all subscriptions of <email>. Like the subscribe command, 

it will use the From address if <email> is not given.''', 

'name': 'unsubscribeall', 

'position': 7, 

} 

 

REGEX_LIST = ( 

r'(?:\s+(?P<email>\S+))?$', 

) 

 

def __init__(self, email): 

super(UnsubscribeallCommand, self).__init__() 

self.user_email = email 

 

def get_command_text(self): 

return super(UnsubscribeallCommand, self).get_command_text( 

self.user_email) 

 

def pre_confirm(self): 

""" 

Implementation of a hook method which is executed instead of 

:py:meth:`handle` when the command is not confirmed. 

""" 

settings = get_or_none(EmailSettings, 

user_email__email__iexact=self.user_email) 

if not settings or settings.subscription_set.count() == 0: 

self.warning('User %s is not subscribed to any packages', 

self.user_email) 

return False 

 

self.reply('A confirmation mail has been sent to %s', self.user_email) 

return True 

 

def handle(self): 

user = get_or_none(UserEmail, email__iexact=self.user_email) 

email_settings = get_or_none(EmailSettings, user_email=user) 

386 ↛ 387line 386 didn't jump to line 387, because the condition on line 386 was never true if user is None or email_settings is None: 

return 

packages = [ 

subscription.package.name 

for subscription in email_settings.subscription_set.all() 

] 

email_settings.unsubscribe_all() 

self.reply('All your subscriptions have been terminated:') 

self.list_reply( 

'{email} has been unsubscribed from {package}@{fqdn}'.format( 

email=self.user_email, 

package=package, 

fqdn=DISTRO_TRACKER_FQDN) 

for package in sorted(packages)) 

 

def get_confirmation_message(self): 

""" 

:returns: A message giving additional information about unsubscribing 

from all packages. 

:rtype: string 

""" 

return distro_tracker_render_to_string( 

'control/email-unsubscribeall-confirmation.txt' 

)