Signal-iOS/SignalUI/ViewControllers/SpamCaptchaViewController.swift
Evan Hahn 370ff654e7
Change license to AGPL
Change license to AGPL

This commit:

- Updates the `LICENSE` file

- Start every file with something like:

      // Copyright YEAR_FIRST_PUBLISHED Signal Messenger, LLC
      // SPDX-License-Identifier: AGPL-3.0-only

---

First, I removed existing license headers with this Ruby 3.1.2 script:

    require 'set'

    EXTENSIONS_TO_CHECK = Set['.h', '.hpp', '.cpp', '.m', '.mm', '.pch', '.swift']

    same = 0
    different = 0

    all_files = `git ls-files`.lines.map { |line| line.strip }
    all_files.each do |relative_path|
      if relative_path == 'Pods'
        next
      end

      unless EXTENSIONS_TO_CHECK.include? File.extname(relative_path)
        next
      end

      path = File.expand_path(relative_path)

      contents = File.read(path)
      new_contents = contents.sub(/\/\/\n\/\/  Copyright .*\n\/\/\n\n/, '')

      if contents == new_contents
        same += 1
      else
        different += 1
      end

      File.write(path, new_contents)
    end

    puts "updated #{different} file(s), left #{same} untouched"

I'm sure this script could be improved, but it worked well enough.

Then, I created `Scripts/lint/lint-license-headers` and ran it to auto-
fix a lot of files. This changed the mode of some files, but I think
that's actually desirable. For example,
`SignalServiceKit/src/Util/AppContext.m` previously had a mode of
`0755/-rwxr-xr-x`, and it's now `0644/-rw-r--r--`.

Then I fixed some stragglers and updated the precommit script.

See [a similar change in the Desktop app][0].

[0]: 8bfaf598af
2022-10-13 08:25:37 -05:00

168 lines
5.9 KiB
Swift

//
// Copyright 2019 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
//
import UIKit
@objc
public class SpamCaptchaViewController: UIViewController, CaptchaViewDelegate {
private var captchaView: CaptchaView?
var completionHandler: ((String?) -> Void)?
private init() {
super.init(nibName: nil, bundle: nil)
}
override public func loadView() {
let captchaView = CaptchaView(context: .challenge)
captchaView.delegate = self
let view = UIView()
view.addSubview(captchaView)
captchaView.autoPinEdgesToSuperviewEdges()
self.captchaView = captchaView
self.view = view
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override public func viewDidLoad() {
super.viewDidLoad()
captchaView?.loadCaptcha()
if #available(iOS 13.0, *) {
isModalInPresentation = true
}
navigationItem.title = OWSLocalizedString("SPAM_CAPTCHA_VIEW_CONTROLLER", comment: "Title for the captcha view controller")
navigationItem.leftBarButtonItem = UIBarButtonItem(
barButtonSystemItem: .stop,
target: self,
action: #selector(didTapCancel)
)
}
@objc
func didTapCancel() {
completionHandler?(nil)
completionHandler = nil
}
public func captchaView(_: CaptchaView, didCompleteCaptchaWithToken token: String) {
completionHandler?(token)
completionHandler = nil
}
public func captchaViewDidFailToCompleteCaptcha(_: CaptchaView) {
captchaView?.loadCaptcha()
}
public override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return UIDevice.current.isIPad ? .all : .portrait
}
}
extension SpamCaptchaViewController {
@objc
public static func presentActionSheet(from fromVC: UIViewController) {
let titleLabel = UILabel()
titleLabel.font = UIFont.ows_dynamicTypeTitle2Clamped.ows_semibold
titleLabel.textColor = Theme.primaryTextColor
titleLabel.numberOfLines = 0
titleLabel.lineBreakMode = .byWordWrapping
titleLabel.textAlignment = .center
titleLabel.text = OWSLocalizedString("SPAM_CAPTCHA_SHEET_TITLE", comment: "Title for action sheet explaining captcha requirement.")
let bodyLabel = UILabel()
bodyLabel.font = .ows_dynamicTypeBody2Clamped
bodyLabel.textColor = Theme.primaryTextColor
bodyLabel.numberOfLines = 0
bodyLabel.lineBreakMode = .byWordWrapping
bodyLabel.text = OWSLocalizedString("SPAM_CAPTCHA_SHEET_BODY", comment: "Body for action sheet explaining captcha requirement.")
let continueButton = OWSFlatButton()
continueButton.setTitle(
title: CommonStrings.continueButton,
font: UIFont.ows_dynamicTypeBodyClamped.ows_semibold,
titleColor: .white)
continueButton.setBackgroundColors(upColor: Theme.accentBlueColor)
continueButton.layer.cornerRadius = 8
continueButton.layer.masksToBounds = true
continueButton.contentEdgeInsets = UIEdgeInsets(hMargin: 4, vMargin: 14)
let stackView = UIStackView(arrangedSubviews: [
titleLabel,
bodyLabel,
UIView.spacer(withHeight: 72),
continueButton
])
stackView.axis = .vertical
stackView.alignment = .center
stackView.distribution = .fill
stackView.spacing = 16
let sheet = SheetViewController()
sheet.isHandleHidden = true
sheet.contentView.addSubview(stackView)
sheet.dismissHandler = { sheet in
sheet.dismiss(animated: true)
let confirmationSheet = ActionSheetController(
title: OWSLocalizedString(
"SPAM_CAPTCHA_DISMISS_CONFIRMATION_TITLE",
comment: "Title for confirmation dialog confirming to ignore verification."),
message: OWSLocalizedString(
"SPAM_CAPTCHA_DISMISS_CONFIRMATION_MESSAGE",
comment: "Message for confirmation dialog confirming to ignore verification.")
)
confirmationSheet.addAction(
ActionSheetAction(
title: OWSLocalizedString("SPAM_CAPTCHA_SKIP_VERIFICATION_ACTION", comment: "Action to skip verification"),
style: .destructive
))
confirmationSheet.addAction(
ActionSheetAction(
title: CommonStrings.cancelButton,
style: .cancel,
handler: { _ in
presentActionSheet(from: fromVC)
}
))
fromVC.present(confirmationSheet, animated: true, completion: nil)
}
continueButton.setPressedBlock {
sheet.dismiss(animated: true)
presentCaptchaVC(from: fromVC)
}
stackView.autoPinEdgesToSuperviewMargins(
with: UIEdgeInsets(hMargin: 24, vMargin: 16))
continueButton.autoPinWidthToSuperviewMargins()
fromVC.present(sheet, animated: true, completion: nil)
}
@objc
static func presentCaptchaVC(from fromVC: UIViewController) {
let vc = SpamCaptchaViewController()
vc.completionHandler = { token in
if let token = token {
fromVC.presentToast(
text: OWSLocalizedString(
"SPAM_CAPTCHA_COMPLETED_TOAST",
comment: "Text for toast presented after spam verification has been completed"))
spamChallengeResolver.handleIncomingCaptchaChallengeToken(token)
}
vc.dismiss(animated: true)
}
let navVC = OWSNavigationController(rootViewController: vc)
fromVC.present(navVC, animated: true, completion: nil)
}
}