diff --git a/.gitignore b/.gitignore index 67d57be..7baa130 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ # sensitive data config.yml +config.json cookies.pickle # venv bin/ @@ -10,5 +11,6 @@ lib64/ pyvenv.cfg # log files geckodriver.log +ivory.log # pycache *__pycache__* diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..0178b6b --- /dev/null +++ b/Dockerfile @@ -0,0 +1,11 @@ +FROM python:3.7.4 + +WORKDIR /app + +COPY . /app + +RUN pip install -r requirements.txt + +RUN python -m pytest -s tests/ + +CMD ["python", "."] diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..f288702 --- /dev/null +++ b/LICENSE @@ -0,0 +1,674 @@ + GNU GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The GNU General Public License is a free, copyleft license for +software and other kinds of works. + + The licenses for most software and other practical works are designed +to take away your freedom to share and change the works. By contrast, +the GNU General Public License is intended to guarantee your freedom to +share and change all versions of a program--to make sure it remains free +software for all its users. We, the Free Software Foundation, use the +GNU General Public License for most of our software; it applies also to +any other work released this way by its authors. You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +them if you wish), that you receive source code or can get it if you +want it, that you can change the software or use pieces of it in new +free programs, and that you know you can do these things. + + To protect your rights, we need to prevent others from denying you +these rights or asking you to surrender the rights. Therefore, you have +certain responsibilities if you distribute copies of the software, or if +you modify it: responsibilities to respect the freedom of others. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must pass on to the recipients the same +freedoms that you received. You must make sure that they, too, receive +or can get the source code. And you must show them these terms so they +know their rights. + + Developers that use the GNU GPL protect your rights with two steps: +(1) assert copyright on the software, and (2) offer you this License +giving you legal permission to copy, distribute and/or modify it. + + For the developers' and authors' protection, the GPL clearly explains +that there is no warranty for this free software. For both users' and +authors' sake, the GPL requires that modified versions be marked as +changed, so that their problems will not be attributed erroneously to +authors of previous versions. + + Some devices are designed to deny users access to install or run +modified versions of the software inside them, although the manufacturer +can do so. This is fundamentally incompatible with the aim of +protecting users' freedom to change the software. The systematic +pattern of such abuse occurs in the area of products for individuals to +use, which is precisely where it is most unacceptable. Therefore, we +have designed this version of the GPL to prohibit the practice for those +products. If such problems arise substantially in other domains, we +stand ready to extend this provision to those domains in future versions +of the GPL, as needed to protect the freedom of users. + + Finally, every program is threatened constantly by software patents. +States should not allow patents to restrict development and use of +software on general-purpose computers, but in those that do, we wish to +avoid the special danger that patents applied to a free program could +make it effectively proprietary. To prevent this, the GPL assures that +patents cannot be used to render the program non-free. + + The precise terms and conditions for copying, distribution and +modification follow. + + TERMS AND CONDITIONS + + 0. Definitions. + + "This License" refers to version 3 of the GNU General Public License. + + "Copyright" also means copyright-like laws that apply to other kinds of +works, such as semiconductor masks. + + "The Program" refers to any copyrightable work licensed under this +License. Each licensee is addressed as "you". "Licensees" and +"recipients" may be individuals or organizations. + + To "modify" a work means to copy from or adapt all or part of the work +in a fashion requiring copyright permission, other than the making of an +exact copy. The resulting work is called a "modified version" of the +earlier work or a work "based on" the earlier work. + + A "covered work" means either the unmodified Program or a work based +on the Program. + + To "propagate" a work means to do anything with it that, without +permission, would make you directly or secondarily liable for +infringement under applicable copyright law, except executing it on a +computer or modifying a private copy. Propagation includes copying, +distribution (with or without modification), making available to the +public, and in some countries other activities as well. + + To "convey" a work means any kind of propagation that enables other +parties to make or receive copies. Mere interaction with a user through +a computer network, with no transfer of a copy, is not conveying. + + An interactive user interface displays "Appropriate Legal Notices" +to the extent that it includes a convenient and prominently visible +feature that (1) displays an appropriate copyright notice, and (2) +tells the user that there is no warranty for the work (except to the +extent that warranties are provided), that licensees may convey the +work under this License, and how to view a copy of this License. If +the interface presents a list of user commands or options, such as a +menu, a prominent item in the list meets this criterion. + + 1. Source Code. + + The "source code" for a work means the preferred form of the work +for making modifications to it. "Object code" means any non-source +form of a work. + + A "Standard Interface" means an interface that either is an official +standard defined by a recognized standards body, or, in the case of +interfaces specified for a particular programming language, one that +is widely used among developers working in that language. + + The "System Libraries" of an executable work include anything, other +than the work as a whole, that (a) is included in the normal form of +packaging a Major Component, but which is not part of that Major +Component, and (b) serves only to enable use of the work with that +Major Component, or to implement a Standard Interface for which an +implementation is available to the public in source code form. A +"Major Component", in this context, means a major essential component +(kernel, window system, and so on) of the specific operating system +(if any) on which the executable work runs, or a compiler used to +produce the work, or an object code interpreter used to run it. + + The "Corresponding Source" for a work in object code form means all +the source code needed to generate, install, and (for an executable +work) run the object code and to modify the work, including scripts to +control those activities. However, it does not include the work's +System Libraries, or general-purpose tools or generally available free +programs which are used unmodified in performing those activities but +which are not part of the work. For example, Corresponding Source +includes interface definition files associated with source files for +the work, and the source code for shared libraries and dynamically +linked subprograms that the work is specifically designed to require, +such as by intimate data communication or control flow between those +subprograms and other parts of the work. + + The Corresponding Source need not include anything that users +can regenerate automatically from other parts of the Corresponding +Source. + + The Corresponding Source for a work in source code form is that +same work. + + 2. Basic Permissions. + + All rights granted under this License are granted for the term of +copyright on the Program, and are irrevocable provided the stated +conditions are met. This License explicitly affirms your unlimited +permission to run the unmodified Program. The output from running a +covered work is covered by this License only if the output, given its +content, constitutes a covered work. This License acknowledges your +rights of fair use or other equivalent, as provided by copyright law. + + You may make, run and propagate covered works that you do not +convey, without conditions so long as your license otherwise remains +in force. You may convey covered works to others for the sole purpose +of having them make modifications exclusively for you, or provide you +with facilities for running those works, provided that you comply with +the terms of this License in conveying all material for which you do +not control copyright. Those thus making or running the covered works +for you must do so exclusively on your behalf, under your direction +and control, on terms that prohibit them from making any copies of +your copyrighted material outside their relationship with you. + + Conveying under any other circumstances is permitted solely under +the conditions stated below. Sublicensing is not allowed; section 10 +makes it unnecessary. + + 3. Protecting Users' Legal Rights From Anti-Circumvention Law. + + No covered work shall be deemed part of an effective technological +measure under any applicable law fulfilling obligations under article +11 of the WIPO copyright treaty adopted on 20 December 1996, or +similar laws prohibiting or restricting circumvention of such +measures. + + When you convey a covered work, you waive any legal power to forbid +circumvention of technological measures to the extent such circumvention +is effected by exercising rights under this License with respect to +the covered work, and you disclaim any intention to limit operation or +modification of the work as a means of enforcing, against the work's +users, your or third parties' legal rights to forbid circumvention of +technological measures. + + 4. Conveying Verbatim Copies. + + You may convey verbatim copies of the Program's source code as you +receive it, in any medium, provided that you conspicuously and +appropriately publish on each copy an appropriate copyright notice; +keep intact all notices stating that this License and any +non-permissive terms added in accord with section 7 apply to the code; +keep intact all notices of the absence of any warranty; and give all +recipients a copy of this License along with the Program. + + You may charge any price or no price for each copy that you convey, +and you may offer support or warranty protection for a fee. + + 5. Conveying Modified Source Versions. + + You may convey a work based on the Program, or the modifications to +produce it from the Program, in the form of source code under the +terms of section 4, provided that you also meet all of these conditions: + + a) The work must carry prominent notices stating that you modified + it, and giving a relevant date. + + b) The work must carry prominent notices stating that it is + released under this License and any conditions added under section + 7. This requirement modifies the requirement in section 4 to + "keep intact all notices". + + c) You must license the entire work, as a whole, under this + License to anyone who comes into possession of a copy. This + License will therefore apply, along with any applicable section 7 + additional terms, to the whole of the work, and all its parts, + regardless of how they are packaged. This License gives no + permission to license the work in any other way, but it does not + invalidate such permission if you have separately received it. + + d) If the work has interactive user interfaces, each must display + Appropriate Legal Notices; however, if the Program has interactive + interfaces that do not display Appropriate Legal Notices, your + work need not make them do so. + + A compilation of a covered work with other separate and independent +works, which are not by their nature extensions of the covered work, +and which are not combined with it such as to form a larger program, +in or on a volume of a storage or distribution medium, is called an +"aggregate" if the compilation and its resulting copyright are not +used to limit the access or legal rights of the compilation's users +beyond what the individual works permit. Inclusion of a covered work +in an aggregate does not cause this License to apply to the other +parts of the aggregate. + + 6. Conveying Non-Source Forms. + + You may convey a covered work in object code form under the terms +of sections 4 and 5, provided that you also convey the +machine-readable Corresponding Source under the terms of this License, +in one of these ways: + + a) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by the + Corresponding Source fixed on a durable physical medium + customarily used for software interchange. + + b) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by a + written offer, valid for at least three years and valid for as + long as you offer spare parts or customer support for that product + model, to give anyone who possesses the object code either (1) a + copy of the Corresponding Source for all the software in the + product that is covered by this License, on a durable physical + medium customarily used for software interchange, for a price no + more than your reasonable cost of physically performing this + conveying of source, or (2) access to copy the + Corresponding Source from a network server at no charge. + + c) Convey individual copies of the object code with a copy of the + written offer to provide the Corresponding Source. This + alternative is allowed only occasionally and noncommercially, and + only if you received the object code with such an offer, in accord + with subsection 6b. + + d) Convey the object code by offering access from a designated + place (gratis or for a charge), and offer equivalent access to the + Corresponding Source in the same way through the same place at no + further charge. You need not require recipients to copy the + Corresponding Source along with the object code. If the place to + copy the object code is a network server, the Corresponding Source + may be on a different server (operated by you or a third party) + that supports equivalent copying facilities, provided you maintain + clear directions next to the object code saying where to find the + Corresponding Source. Regardless of what server hosts the + Corresponding Source, you remain obligated to ensure that it is + available for as long as needed to satisfy these requirements. + + e) Convey the object code using peer-to-peer transmission, provided + you inform other peers where the object code and Corresponding + Source of the work are being offered to the general public at no + charge under subsection 6d. + + A separable portion of the object code, whose source code is excluded +from the Corresponding Source as a System Library, need not be +included in conveying the object code work. + + A "User Product" is either (1) a "consumer product", which means any +tangible personal property which is normally used for personal, family, +or household purposes, or (2) anything designed or sold for incorporation +into a dwelling. In determining whether a product is a consumer product, +doubtful cases shall be resolved in favor of coverage. For a particular +product received by a particular user, "normally used" refers to a +typical or common use of that class of product, regardless of the status +of the particular user or of the way in which the particular user +actually uses, or expects or is expected to use, the product. A product +is a consumer product regardless of whether the product has substantial +commercial, industrial or non-consumer uses, unless such uses represent +the only significant mode of use of the product. + + "Installation Information" for a User Product means any methods, +procedures, authorization keys, or other information required to install +and execute modified versions of a covered work in that User Product from +a modified version of its Corresponding Source. The information must +suffice to ensure that the continued functioning of the modified object +code is in no case prevented or interfered with solely because +modification has been made. + + If you convey an object code work under this section in, or with, or +specifically for use in, a User Product, and the conveying occurs as +part of a transaction in which the right of possession and use of the +User Product is transferred to the recipient in perpetuity or for a +fixed term (regardless of how the transaction is characterized), the +Corresponding Source conveyed under this section must be accompanied +by the Installation Information. But this requirement does not apply +if neither you nor any third party retains the ability to install +modified object code on the User Product (for example, the work has +been installed in ROM). + + The requirement to provide Installation Information does not include a +requirement to continue to provide support service, warranty, or updates +for a work that has been modified or installed by the recipient, or for +the User Product in which it has been modified or installed. Access to a +network may be denied when the modification itself materially and +adversely affects the operation of the network or violates the rules and +protocols for communication across the network. + + Corresponding Source conveyed, and Installation Information provided, +in accord with this section must be in a format that is publicly +documented (and with an implementation available to the public in +source code form), and must require no special password or key for +unpacking, reading or copying. + + 7. Additional Terms. + + "Additional permissions" are terms that supplement the terms of this +License by making exceptions from one or more of its conditions. +Additional permissions that are applicable to the entire Program shall +be treated as though they were included in this License, to the extent +that they are valid under applicable law. If additional permissions +apply only to part of the Program, that part may be used separately +under those permissions, but the entire Program remains governed by +this License without regard to the additional permissions. + + When you convey a copy of a covered work, you may at your option +remove any additional permissions from that copy, or from any part of +it. (Additional permissions may be written to require their own +removal in certain cases when you modify the work.) You may place +additional permissions on material, added by you to a covered work, +for which you have or can give appropriate copyright permission. + + Notwithstanding any other provision of this License, for material you +add to a covered work, you may (if authorized by the copyright holders of +that material) supplement the terms of this License with terms: + + a) Disclaiming warranty or limiting liability differently from the + terms of sections 15 and 16 of this License; or + + b) Requiring preservation of specified reasonable legal notices or + author attributions in that material or in the Appropriate Legal + Notices displayed by works containing it; or + + c) Prohibiting misrepresentation of the origin of that material, or + requiring that modified versions of such material be marked in + reasonable ways as different from the original version; or + + d) Limiting the use for publicity purposes of names of licensors or + authors of the material; or + + e) Declining to grant rights under trademark law for use of some + trade names, trademarks, or service marks; or + + f) Requiring indemnification of licensors and authors of that + material by anyone who conveys the material (or modified versions of + it) with contractual assumptions of liability to the recipient, for + any liability that these contractual assumptions directly impose on + those licensors and authors. + + All other non-permissive additional terms are considered "further +restrictions" within the meaning of section 10. If the Program as you +received it, or any part of it, contains a notice stating that it is +governed by this License along with a term that is a further +restriction, you may remove that term. If a license document contains +a further restriction but permits relicensing or conveying under this +License, you may add to a covered work material governed by the terms +of that license document, provided that the further restriction does +not survive such relicensing or conveying. + + If you add terms to a covered work in accord with this section, you +must place, in the relevant source files, a statement of the +additional terms that apply to those files, or a notice indicating +where to find the applicable terms. + + Additional terms, permissive or non-permissive, may be stated in the +form of a separately written license, or stated as exceptions; +the above requirements apply either way. + + 8. Termination. + + You may not propagate or modify a covered work except as expressly +provided under this License. Any attempt otherwise to propagate or +modify it is void, and will automatically terminate your rights under +this License (including any patent licenses granted under the third +paragraph of section 11). + + However, if you cease all violation of this License, then your +license from a particular copyright holder is reinstated (a) +provisionally, unless and until the copyright holder explicitly and +finally terminates your license, and (b) permanently, if the copyright +holder fails to notify you of the violation by some reasonable means +prior to 60 days after the cessation. + + Moreover, your license from a particular copyright holder is +reinstated permanently if the copyright holder notifies you of the +violation by some reasonable means, this is the first time you have +received notice of violation of this License (for any work) from that +copyright holder, and you cure the violation prior to 30 days after +your receipt of the notice. + + Termination of your rights under this section does not terminate the +licenses of parties who have received copies or rights from you under +this License. If your rights have been terminated and not permanently +reinstated, you do not qualify to receive new licenses for the same +material under section 10. + + 9. Acceptance Not Required for Having Copies. + + You are not required to accept this License in order to receive or +run a copy of the Program. Ancillary propagation of a covered work +occurring solely as a consequence of using peer-to-peer transmission +to receive a copy likewise does not require acceptance. However, +nothing other than this License grants you permission to propagate or +modify any covered work. These actions infringe copyright if you do +not accept this License. Therefore, by modifying or propagating a +covered work, you indicate your acceptance of this License to do so. + + 10. Automatic Licensing of Downstream Recipients. + + Each time you convey a covered work, the recipient automatically +receives a license from the original licensors, to run, modify and +propagate that work, subject to this License. You are not responsible +for enforcing compliance by third parties with this License. + + An "entity transaction" is a transaction transferring control of an +organization, or substantially all assets of one, or subdividing an +organization, or merging organizations. If propagation of a covered +work results from an entity transaction, each party to that +transaction who receives a copy of the work also receives whatever +licenses to the work the party's predecessor in interest had or could +give under the previous paragraph, plus a right to possession of the +Corresponding Source of the work from the predecessor in interest, if +the predecessor has it or can get it with reasonable efforts. + + You may not impose any further restrictions on the exercise of the +rights granted or affirmed under this License. For example, you may +not impose a license fee, royalty, or other charge for exercise of +rights granted under this License, and you may not initiate litigation +(including a cross-claim or counterclaim in a lawsuit) alleging that +any patent claim is infringed by making, using, selling, offering for +sale, or importing the Program or any portion of it. + + 11. Patents. + + A "contributor" is a copyright holder who authorizes use under this +License of the Program or a work on which the Program is based. The +work thus licensed is called the contributor's "contributor version". + + A contributor's "essential patent claims" are all patent claims +owned or controlled by the contributor, whether already acquired or +hereafter acquired, that would be infringed by some manner, permitted +by this License, of making, using, or selling its contributor version, +but do not include claims that would be infringed only as a +consequence of further modification of the contributor version. For +purposes of this definition, "control" includes the right to grant +patent sublicenses in a manner consistent with the requirements of +this License. + + Each contributor grants you a non-exclusive, worldwide, royalty-free +patent license under the contributor's essential patent claims, to +make, use, sell, offer for sale, import and otherwise run, modify and +propagate the contents of its contributor version. + + In the following three paragraphs, a "patent license" is any express +agreement or commitment, however denominated, not to enforce a patent +(such as an express permission to practice a patent or covenant not to +sue for patent infringement). To "grant" such a patent license to a +party means to make such an agreement or commitment not to enforce a +patent against the party. + + If you convey a covered work, knowingly relying on a patent license, +and the Corresponding Source of the work is not available for anyone +to copy, free of charge and under the terms of this License, through a +publicly available network server or other readily accessible means, +then you must either (1) cause the Corresponding Source to be so +available, or (2) arrange to deprive yourself of the benefit of the +patent license for this particular work, or (3) arrange, in a manner +consistent with the requirements of this License, to extend the patent +license to downstream recipients. "Knowingly relying" means you have +actual knowledge that, but for the patent license, your conveying the +covered work in a country, or your recipient's use of the covered work +in a country, would infringe one or more identifiable patents in that +country that you have reason to believe are valid. + + If, pursuant to or in connection with a single transaction or +arrangement, you convey, or propagate by procuring conveyance of, a +covered work, and grant a patent license to some of the parties +receiving the covered work authorizing them to use, propagate, modify +or convey a specific copy of the covered work, then the patent license +you grant is automatically extended to all recipients of the covered +work and works based on it. + + A patent license is "discriminatory" if it does not include within +the scope of its coverage, prohibits the exercise of, or is +conditioned on the non-exercise of one or more of the rights that are +specifically granted under this License. You may not convey a covered +work if you are a party to an arrangement with a third party that is +in the business of distributing software, under which you make payment +to the third party based on the extent of your activity of conveying +the work, and under which the third party grants, to any of the +parties who would receive the covered work from you, a discriminatory +patent license (a) in connection with copies of the covered work +conveyed by you (or copies made from those copies), or (b) primarily +for and in connection with specific products or compilations that +contain the covered work, unless you entered into that arrangement, +or that patent license was granted, prior to 28 March 2007. + + Nothing in this License shall be construed as excluding or limiting +any implied license or other defenses to infringement that may +otherwise be available to you under applicable patent law. + + 12. No Surrender of Others' Freedom. + + If conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot convey a +covered work so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you may +not convey it at all. For example, if you agree to terms that obligate you +to collect a royalty for further conveying from those to whom you convey +the Program, the only way you could satisfy both those terms and this +License would be to refrain entirely from conveying the Program. + + 13. Use with the GNU Affero General Public License. + + Notwithstanding any other provision of this License, you have +permission to link or combine any covered work with a work licensed +under version 3 of the GNU Affero General Public License into a single +combined work, and to convey the resulting work. The terms of this +License will continue to apply to the part which is the covered work, +but the special requirements of the GNU Affero General Public License, +section 13, concerning interaction through a network will apply to the +combination as such. + + 14. Revised Versions of this License. + + The Free Software Foundation may publish revised and/or new versions of +the GNU General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + + Each version is given a distinguishing version number. If the +Program specifies that a certain numbered version of the GNU General +Public License "or any later version" applies to it, you have the +option of following the terms and conditions either of that numbered +version or of any later version published by the Free Software +Foundation. If the Program does not specify a version number of the +GNU General Public License, you may choose any version ever published +by the Free Software Foundation. + + If the Program specifies that a proxy can decide which future +versions of the GNU General Public License can be used, that proxy's +public statement of acceptance of a version permanently authorizes you +to choose that version for the Program. + + Later license versions may give you additional or different +permissions. However, no additional obligations are imposed on any +author or copyright holder as a result of your choosing to follow a +later version. + + 15. Disclaimer of Warranty. + + THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY +APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT +HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY +OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, +THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM +IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF +ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. Limitation of Liability. + + IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS +THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY +GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE +USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF +DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD +PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), +EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF +SUCH DAMAGES. + + 17. Interpretation of Sections 15 and 16. + + If the disclaimer of warranty and limitation of liability provided +above cannot be given local legal effect according to their terms, +reviewing courts shall apply local law that most closely approximates +an absolute waiver of all civil liability in connection with the +Program, unless a warranty or assumption of liability accompanies a +copy of the Program in return for a fee. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +state the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + +Also add information on how to contact you by electronic and paper mail. + + If the program does terminal interaction, make it output a short +notice like this when it starts in an interactive mode: + + Copyright (C) + This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, your program's commands +might be different; for a GUI interface, you would use an "about box". + + You should also get your employer (if you work as a programmer) or school, +if any, to sign a "copyright disclaimer" for the program, if necessary. +For more information on this, and how to apply and follow the GNU GPL, see +. + + The GNU General Public License does not permit incorporating your program +into proprietary programs. If your program is a subroutine library, you +may consider it more useful to permit linking proprietary applications with +the library. If this is what you want to do, use the GNU Lesser General +Public License instead of this License. But first, please read +. diff --git a/README.md b/README.md index 388bf06..1f962b3 100644 --- a/README.md +++ b/README.md @@ -1,118 +1,196 @@ -# Ivory, a crappy Mastodon automoderator +# Ivory -Ivory is an automoderation system for Mastodon which logs in as a normal user to -the Mastodon frontend, scans the reports page, and automatically deals with -common moderation tasks, particularly spam. It does this by reading over each -report it finds and running a number of configuration-declared *rules* which are -checked on each report. +Ivory is an automoderator and anti-spam tool for Mastodon admins. It automates +handling certain trivial reports and new user requests using *rules* - +configurable tests that check reports and pending users for bad usernames, +malicious links, and more. -Currently, Ivory is intended to function as a stopgap measure to curb spam while -we await the actual moderation API, though I have intentionally designed things -in a way that will allow me to convert it to use said API when (or if) it -releases. +## Installation Guide -## Installation and Usage +This installation guide assumes you know your way around a Linux terminal, +have Python and Git installed, and maybe a little bit about common tech like +Python and JSON formatting. -First, install Geckodriver and make sure it's in your terminal's PATH. If you're -running Linux, you may have a version in your distro's package manager. +### Installing -After doing that, snippet should work. You'll want Python 3 (preferably 3.7 or -above) for this: + +In a Linux terminal, the following commands will clone and install Ivory to +whichever folder you're in. Make sure you have Git and Python installed: ```bash git clone https://github.com/bclindner/ivory cd ivory python -m venv . source bin/activate -pip install -r requirements.txt +python -m pip install -r requirements.txt ``` -After that, create a config.yml in the project root as shown in the Configuring -section below and then run: +This repo also comes with a Dockerfile, so if you want to deploy with that, that +works too: ```bash -python . +git clone https://github.com/bclindner/ivory +cd ivory +docker build -t ivory . +docker run -v /srv/ivory/ivory_config.json:/app/config.json ivory ``` -You will be asked for a username and password, and optionally an OTP if your -account is set up for that. This should only happen once; after that, cookies -are stored in the project root as `cookies.pickle` and the app will log in with -those. (If at some point Ivory stops signing in correctly, delete this file and -try manually logging in again.) - -## Configuring - -Ivory is configured using a YAML file. An example configuration is below: -```yaml -# Time to wait in between checks (in seconds) -wait_time: 600 # 10min; lower numbers shouldn't stress your servers out -driver: - type: browser # browser is the only supported driver type at present - # Instance URL - instance_url: https://mastodon.technology -# Array of rules for Ivory to judge with -rules: - # This name is what Ivory mentions in the moderation notes when finishing a - # report. -- name: "No womenarestupid.site spam links" - # This rule parses over links in every post attached to a report. - # Also supports text phrases in reported posts with the 'content' type. - type: link_content - blocked: - # This list supports regexes! - - womenarestupid.site - - dontmarry.com - punishment: - # The highest severity punishment in a single judgement is the one used when - # punishing the user. - severity: 1000 - # Currently only suspend is supported. - type: suspend - # Not implemented, but the following are for local users. - delete_account_data: yes - local_suspend_message: "Your account has been suspended for spamming." -- name: "No womenarestupid.site shorturls" - # This rule type resolves shorturls! - type: link_resolver - blocked: - - dontmarry.com - - womenarestupid.site - punishment: - severity: 1000 - type: suspend - delete_account_data: yes - local_suspend_message: "Your account has been suspended for spamming." -- name: "No inflammatory usernames" - type: username_content - blocked: - # You can do case insensitive searches using regex, too! - - (?i)heck - punishment: - severity: 1000 - type: suspend - delete_account_data: yes - local_suspend_message: "Your account has been suspended for having an inflammatory username." +### Configuration + +Before starting Ivory, you need to create a new application in the Preferences +menu. Don't worry about setting redirect URIs or anything that isn't required - +just make sure you enable all of the `admin:` scopes. Once you've created the +application, you'll want to grab its access token to place in the configuration +file (example below). + +*Be* ***EXTREMELY*** *careful with handling the access token this generates - +this key has a lot of power and in the wrong hands, this could mean someone +completely obliterating your instance.* + +Once you've done that, it's time to set up your config file. Configuring Ivory +is done with JSON; a sample is below: + +```json +{ + "token": "", + "instanceURL": "", + "waitTime": 300, + "reports": { + "rules": [ + { + "name": "No spam links", + "type": "link_resolver", + "blocked": ["evilspam\\.website", "dontmarry\\.com"], + "severity": 2, + "punishment": { + "type": "suspend", + "message": "Your account has been suspended for spamming." + } + }, + { + "name": "No link-in-bio spammers", + "type": "bio_content", + "blocked": ["sexie.ru"], + "severity": 1, + "punishment": { + "type": "disable", + "message": "Your account has been disabled for spamming." + } + } + ] + }, + "pendingAccounts": { + "rules": [ + { + "name": "No tags", + "_comment": "Because honestly, you're definitely a bot if you're putting tags into the field", + "type": "message_content", + "blocked": [".*"], + "severity": 1, + "punishment": { + "type": "reject" + } + }, + { + "name": "StopForumSpam test", + "type": "stopforumspam", + "threshold": 95, + "severity": 1, + "punishment": { + "type": "reject" + } + } + ] + } +} ``` -## Caveats +A more [in-depth guide to Ivory configuration](https://github.com/bclindner/ivory/wiki/Getting-Started) +and the list of [rules](https://github.com/bclindner/ivory/wiki/Rules) and +[punishments](https://github.com/bclindner/ivory/wiki/Punishments) +can be found on the wiki. + +Ideally you only have to change this once in a blue moon, but if you do, you can +use the `"dryRun": true` option to prevent Ivory from taking action, so you can +test some rules on recent live moderation queues. -This code is using Selenium to drive its report handling system. Selenium can be -finicky. Stuff can break. +### Running -Take care when writing your rules. Ivory doesn't care if you get them wrong, and -Ivory will absolutely ban users with impunity if you do. Test them if you can. -Support for dry runs will come available when I get to it. +After you've set up a config file, run the following in a Linux terminal: -## Maintainers +``` +# if you're running in the same terminal session you installed from, you can +# skip this next line: +source bin/activate +python . +``` -This is currently solely maintained by me, -[@bclindner@mastodon.technology](http://mastodon.technology/@bclindner). +Hopefully, no errors will be thrown and Ivory will start up and begin its first +moderation pass, reading the first page of active reports and pending users and +applying your set rules. Ivory will handle these queues every 300 seconds, +or 5 minutes. (This is controlled by the `waitTime` part of the above config +file - if you wanted 10 minutes, you could set it to 600!) +If you'd rather run it on some other schedule via a proper task scheduler like +cron or a systemd .timer unit, you can use `python . oneshot` which will run +Ivory only once. This sample cron line will run Ivory every 5 minutes and output +to a log file: + +```cron +*/5 * * * * cd /absolute/path/to/ivory; ./bin/python . oneshot >> ivory.log +``` + +## Extending (custom rules) + +You'll notice the `rules/` folder is a flat folder of Python scripts, one per +Ivory rule. If you've got a little Python experience, you can easily create your +own rules by just dropping in a new Python file and using one of the other files +in the folder as a jumping-off point. + +The reports and pending accounts that Ivory rules receive are the same as what +Mastodon.py provides for +[reports](https://mastodonpy.readthedocs.io/en/stable/#report-dicts) and [admin +accounts](https://mastodonpy.readthedocs.io/en/stable/#admin-account-dicts), +respectively. + +**Don't forget to use `dryRun` in your config when testing your new rule!** + +Once you've finished writing up your custom rule, say as +`rules/filename_of_your_rule.py`, you can address it by its filename in your +config: + +```json +... +"pendingAccounts": { + "rules": [ + { + "name": "An instance of my cool new rule", + "type": "filename_of_your_rule", + "custom_option": true, + "severity": 5, + "punishment": { + "type": "reject" + } + }, + ] +} +... +``` + +If you come up with any useful rules and wouldn't mind writing a schema and some +tests for it, making a pull request to include it in Ivory's main release would +be highly appreciated! The more rules Ivory gets, the more tools are +collectively available to other admins for dealing with spammers and other +threats to the Fediverse at large. ## Bugs & Contributing -Contributions welcome. I'm a JS dev, not a Python one, so I may well need -them. +If you have any issues with Ivory not working as expected, please file a GitHub +issue. -Got bugs or feature request? File them as a GitHub issue and I'll address them -when I can. Same goes for PRs. +Contributions are also welcome - send in pull requests if you have anything new +to add. + +If you have any other questions, go ahead and [ping me on +Mastodon](https://mastodon.technology/@bclindner) and I might be able to answer +them. diff --git a/__main__.py b/__main__.py old mode 100644 new mode 100755 index ee58eaa..3bbd4af --- a/__main__.py +++ b/__main__.py @@ -1,4 +1,46 @@ +#!/usr/bin/env python3 +""" +Main "executable" for Ivory. + +Running this file will start Ivory in watch mode, using the config provided in +config.json. +""" + +import logging +import sys +import json +import argparse from ivory import Ivory +from constants import DEFAULT_CONFIG_PATH, COMMAND_WATCH, COMMAND_ONESHOT -if __name__ == '__main__': - Ivory().run() +if __name__ == "__main__": + logger = logging.getLogger(__name__) + argparser = argparse.ArgumentParser( + description="A Mastodon automoderator.") + argparser.add_argument("--config", + dest="configpath", + help="Path to the configuration file (default is config.json)", + default=DEFAULT_CONFIG_PATH) + argparser.add_argument('command', + help="Command to run (oneshot to run once, watch to run on a loop). Runs in watch mode by default.", + default=COMMAND_WATCH, + nargs='?', + choices=[COMMAND_WATCH, COMMAND_ONESHOT]) + args = argparser.parse_args() + try: + # set up logging + logging.basicConfig(stream=sys.stdout) + with open(args.configpath) as config_file: + config = json.load(config_file) + logging.getLogger().setLevel(config.get('logLevel', logging.INFO)) + # start up ivory in watch mode + if args.command == COMMAND_WATCH: + Ivory(config).watch() + elif args.command == COMMAND_ONESHOT: + Ivory(config).run() + except OSError as err: + logger.exception("failed to load config file") + exit(1) + except KeyboardInterrupt as err: + logger.info("interrupt signal detected, exiting") + exit(1) diff --git a/constants.py b/constants.py index 5422a23..92bf829 100644 --- a/constants.py +++ b/constants.py @@ -1 +1,23 @@ -VERSION = "0.2" +""" +Constants for Ivory. +""" + +# Ivory version number +VERSION = "v1.0.0" + +# Default amount of seconds to wait between report passes +DEFAULT_WAIT_TIME = 300 + +# Default configuration path to use when no path is manually specified +DEFAULT_CONFIG_PATH = "config.json" + +# Punishment types +PUNISH_WARN = "warn" +PUNISH_REJECT = "reject" +PUNISH_DISABLE = "disable" +PUNISH_SILENCE = "silence" +PUNISH_SUSPEND = "suspend" + +# Command types +COMMAND_WATCH = "watch" +COMMAND_ONESHOT = "oneshot" diff --git a/core.py b/core.py deleted file mode 100644 index f21db0a..0000000 --- a/core.py +++ /dev/null @@ -1,192 +0,0 @@ -""" -Base classes for the Ivory system. -""" -from typing import List - - -class User: - """ - A simplified class representation of a Mastodon user. - """ - - def __init__(self, user_id: str, username: str): - self.id = user_id - self.username = username - - def __repr__(self): - return "User %s (@%s)" % (self.id, self.username) - - def __str__(self): - return self.username - - -class Report: - """ - A class representation of a Mastodon report. - - Since reports from other instances won't have accounts attached, drivers - cannot create reporter users, so they should just set the reporter field to None. - """ - - def __init__(self, - report_id: str, - status: str, - reported: User, - reporter: User, - report_comment: str, - reported_posts: List[str], - reported_links: List[str]): - # ugh - self.report_id = report_id - self.status = status - self.reporter = reporter - self.reported = reported - self.comment = report_comment - self.posts = reported_posts - self.links = reported_links - - def __str__(self): - return "Report #%s (%s)" % (self.report_id, self.reported.username) - - def __repr__(self): - return self.__str__() # lol - - -class Punishment: - """ - A Punishment is Ivory's representation of a moderation action. - Punishments are of a specific type (usually suspend, silence, or warn), and - have a given severity. - Punishments also optionally come with a configuration dict that defines - extra options. - NOTE: Should this be a base class where we derive punishments? - """ - - def __init__(self, config): - self.type = config['type'] - self.severity = config['severity'] - self.config = config - - def __str__(self): - return "Punishment (%s)" % self.type - - def __repr__(self): - return "Punishment (%s, severity %s)" % (self.type, self.severity) - - -class Rule: - """ - Rules in Ivory are kind of like unit tests for reports. - Each one can be run against a report to determine if it passes or fails. - In this case, it differs in that a Rule comes with a Punishment. - """ - - def __init__(self, config): - self.name = config['name'] - self.punishment = Punishment(config['punishment']) - - def test(self, report: Report): - """ - Test the rule against a given report. - """ - - def __str__(self): - return self.name - - def __repr__(self): - return "Rule '%s' (%s)" % (self.name, type(self).__name__) - - -class Judge: - """ - Interface for judging reports based on rules. - - The Judge class is a dead-simple class that holds Rule objects, and allows - you to test each Rule on a single Report object with the make_judgement - method. - """ - rules = [] - - def add_rule(self, rule): - """ - Add a rule to the judge's list. - - Future judgements will use this rule. - """ - self.rules.append(rule) - - def clear_rules(self): - """ - Clear this judge's rules list. - """ - self.rules = [] - - def make_judgement(self, report: Report) -> (Punishment, List[Rule]): - """ - Judge a report. - - Returns: - final_verdict: Returns the Punishment object that should be used for - this report, or None if there is none. - rules_broken: The list of rules the judge determined were broken. - """ - most_severe_rule = None - rules_broken = set() - for rule in self.rules: - if rule.test(report): - rules_broken.add(rule) - if (most_severe_rule is None or - most_severe_rule.punishment.severity < rule.punishment.severity): - most_severe_rule = rule - if most_severe_rule is not None: - final_verdict = most_severe_rule.punishment - else: - final_verdict = None - return (final_verdict, rules_broken) - - -class Driver: - """ - A dummy Ivory driver to derive your own custom drivers from. - - For Ivory to work, all of the below base methods must be defined. - """ - - def __init__(self): - pass - - def get_unresolved_report_ids(self): - """ - Get a list of unresolved report IDs. - - Returns: - list: An array of report ID strings. - """ - raise NotImplementedError() - - def get_report(self, report_id: str): - """ - Get a report by ID. - - The report ID is currently a string type as it will almost certainly be - plugged into a URL or a REST API. This may change in future major versions. - """ - raise NotImplementedError() - - def punish(self, report: Report, punishment: Punishment): - """ - Apply a punishment to a report. - - Driver classes should implement Punishments of type "suspend", - "silence", and "warn", if possible. If it isn't possible to implement - them, raise a NotImplementedError. You can define custom punishments, - but be wary of extending too much. - """ - raise NotImplementedError() - - def add_note(self, report_id: str, message: str, resolve: bool = False): - """ - Add a note to a report by its ID. - """ - raise NotImplementedError() - diff --git a/drivers/__init__.py b/drivers/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/drivers/browser.py b/drivers/browser.py deleted file mode 100644 index fcd4740..0000000 --- a/drivers/browser.py +++ /dev/null @@ -1,226 +0,0 @@ -import pickle -# for (safely) parsing URLs -from urllib.parse import urljoin -# for getting passwords during initialization -from getpass import getpass -# The WebDriver itself -from selenium import webdriver -# WebDriverWait stuff -from selenium.webdriver.common.by import By -from selenium.webdriver.support.ui import WebDriverWait -from selenium.webdriver.support import expected_conditions as EC -# Selenium exceptions which we use to handle some potential fault areas -from selenium.common.exceptions import NoSuchElementException, TimeoutException -# Ivory imports -from core import Report, Driver, User -from exceptions import ConfigurationError, DriverError, DriverNetworkError - -# Default timeout for the Selenium driver to get a Web page. -DEFAULT_TIMEOUT = 30 - -class BrowserDriver(Driver): - """ - A Selenium-based browser driver for Ivory. - """ - - def __init__(self, config): - Driver.__init__(self) - try: - self.instance_url = config['instance_url'] - except KeyError: - raise ConfigurationError("instance_url not found") - self.__driver = webdriver.Firefox() - self.__wait = WebDriverWait(self.__driver, DEFAULT_TIMEOUT) - # Attempt log-in - cookies = [] - try: - with open('cookies.pickle', 'rb') as cookies_file: - cookies = pickle.load(cookies_file) - print('Found cookies; using those instead of asking you for login') - except Exception: - print("Failed to open cookies file; manual login required") - if cookies: - self.__login_with_cookies(cookies) - print('Looks like login was successful.') - else: - email = input('Enter email: ') - password = getpass(prompt='Enter password: ') - otp = input('If using OTP, enter OTP token: ') - cookies = self.__login_with_credentials(email, password, otp) - print('Looks like login was successful. Saving cookies...') - with open('cookies.pickle', 'wb') as cookies_file: - pickle.dump(cookies, cookies_file) - - def __url(self, path=''): - return urljoin(self.instance_url, path) - - def __login_with_cookies(self, cookies): - """ - Login to the given instance using stored cookies. - - Current implementation is pretty rough - needs more checking to see if the - cookies are actually working or not. - """ - self.__driver.get(self.__url()) - for cookie in cookies: - self.__driver.add_cookie(cookie) - return True - - def __login_with_credentials(self, email, password, otp=""): - """ - Login to the given instance using user credentials. - - Returns: - list: A list of cookies in Selenium-consumable form. - Save this and use it with login_with_cookies for future logins. - """ - try: - self.__driver.get(self.__url('/auth/sign_in')) - # TODO add wait here for safety - # Email + Password - self.__driver.find_element_by_id("user_email").send_keys(email) - pwfield = self.__driver.find_element_by_id("user_password") - pwfield.send_keys(password) - pwfield.submit() - except TimeoutException: - raise DriverError("failed logging in - OTP page could not be reached") - except NoSuchElementException: - raise DriverError("failed to input login details - has this instance's login page been modified?") - - # OTP - # TODO NON-OTP IS UNTESTED - if otp: - # Server needs a sec to catch up - try: - self.__wait.until(EC.presence_of_element_located((By.ID, 'user_otp_attempt'))) - otpfield = self.__driver.find_element_by_id('user_otp_attempt') - otpfield.send_keys(otp) - otpfield.submit() - except TimeoutException: - raise DriverError("failed logging in - OTP page could not be reached") - except NoSuchElementException: - raise DriverError("failed to input and submit OTP code") - # Server needs a sec to catch up - try: - self.__wait.until(EC.url_contains('getting-started')) - except TimeoutException: - raise DriverError("failed logging in - homepage could not be reached") - # Grab cookies - cookies = self.__driver.get_cookies() - return cookies - - def get_unresolved_report_ids(self): - """ - Scrapes the page to get unresolved reports. - - This does not get all reports! Currently it's just designed to get what - it can. If you end up with more than a page of reports at a time, this - driver might not work for you for now. - """ - try: - self.__driver.get(self.__url('/admin/reports')) - self.__wait.until(EC.title_contains('Reports')) - link_elements = self.__driver.find_elements_by_xpath( - '//div[@class="report-card__summary__item__content"]/a') - links = [link.get_attribute('href').split('/')[-1] for link in link_elements] - return links - except TimeoutException: - raise DriverNetworkError("timed out navigating to reports page") - - def get_report(self, report_id: str): - """ - Scrape the report page into a Report object. - """ - # Parse the report from the page itself - self.__driver.get(self.__url('/admin/reports/') + report_id) - try: - self.__wait.until(EC.title_contains('Report #'+report_id)) - except TimeoutException: - raise DriverNetworkError("timed out getting report #"+report_id) - # Get report status - report_status = self.__driver.find_element_by_xpath( - '//table[1]//tr[5]/td[1]').text - # Get reported user - reported_row = self.__driver.find_element_by_xpath( - '//table[1]//tr[1]/td[1]/a') - reported_username = reported_row.get_attribute('title') - reported_id = reported_row.get_attribute('href').split('/')[-1] - reported_user = User(reported_id, reported_username) - # Get reporter data - try: - reporter_row = self.__driver.find_element_by_xpath( - '//table[1]//tr[2]/td[1]/a') - reporter_username = reporter_row.get_attribute('title') - reporter_id = reporter_row.get_attribute('href').split('/')[-1] - reporter_user = User(reporter_id, reporter_username) - except NoSuchElementException: - # If that block failed, then this was probably a federated report - # forwarded to us - set reporter_user to None - reporter_user = None - # Get reporter's comment - reporter_comment = self.__driver.find_element_by_class_name( - 'speech-bubble__bubble').text - # Get reported posts - # Un-CW all posts for deserialization - cwposts = self.__driver.find_elements_by_tag_name('details') - for post in cwposts: - post.click() - posts = self.__driver.find_elements_by_class_name('status__content') - reported_posts = [post.text for post in posts] - # Get links in reported posts - links = self.__driver.find_elements_by_xpath( - '//div[@class="status__content"]//a') - reported_links = [link.get_attribute( - 'href') for link in links] # lmfao - # Turn it all into a Report object and send it back - report = Report(report_id, report_status, reported_user, - reporter_user, reporter_comment, reported_posts, reported_links) - return report - - def add_note(self, report_id: str, message: str, resolve: bool = False): - """ - Adds a note through the reports page directly. - """ - # Parse the report from the page itself - self.__driver.get(self.__url('/admin/reports/') + report_id) - self.__wait.until(EC.title_contains('Report #'+report_id)) - self.__driver.find_element_by_id( - 'report_note_content').send_keys(message) - buttons = self.__driver.find_elements_by_class_name('btn') - if resolve: - buttons[0].click() - else: - buttons[1].click() - self.__wait.until(EC.presence_of_element_located( - (By.CLASS_NAME, 'notice'))) - - def punish(self, report, punishment): - """ - Punish a user. - """ - if punishment.type == 'suspend': - self.suspend(report.report_id) - else: - raise NotImplementedError() - - def suspend(self, report_id): - """ - Suspends a user through the reports page directly. - """ - try: - self.__driver.get(self.__url('/admin/reports/') + report_id) - self.__wait.until(EC.title_contains('Report #'+report_id)) - except TimeoutException: - raise DriverNetworkError("failed to get report #"+report_id) - try: - self.__driver.find_element_by_xpath( - '//div[@class="content"]/div[1]//a[text() = "Suspend"]').click() - self.__wait.until(EC.title_contains('Perform moderation')) - self.__driver.find_element_by_class_name('btn').click() - self.__wait.until(EC.title_contains('Reports')) - except TimeoutException: - raise DriverNetworkError("timed out attempting to suspend user") - except NoSuchElementException: - raise DriverError("malformed page") - -driver = BrowserDriver diff --git a/exceptions.py b/exceptions.py deleted file mode 100644 index 02be62d..0000000 --- a/exceptions.py +++ /dev/null @@ -1,34 +0,0 @@ -class ConfigurationError(Exception): - """ - Generic exception for config problems. - - No derivative of this should be necessary - throw a ConfigurationError in - any case and Ivory should exit after printing the message provided in the - error. - In the future I may extend this by having it provide the name of the key - it failed on. - """ - -class DriverError(Exception): - """ - Generic base exception for Driver problems. - """ - -class DriverNetworkError(DriverError): - """ - Exception for network errors a Driver might encounter. - - Ivory currently assumes these network errors are temporary, and will - attempt to retry when this error is raised. - """ - -class DriverAuthorizationError(DriverError): - """ - Exception raised if the driver does not have access to something it needs - using the provided credentials. - """ - -class RuleError(Exception): - """ - Generic base exception for Rule problems. - """ \ No newline at end of file diff --git a/ivory.py b/ivory.py index b3ea730..fc6da74 100644 --- a/ivory.py +++ b/ivory.py @@ -1,148 +1,186 @@ -from importlib import import_module -import traceback -import time -from typing import List +""" +Ivory core class file. +Contains everything you need to run Ivory programmatically. +""" +import logging +import time # for Ivory.watch() -import yaml +from mastodon import Mastodon, MastodonError, MastodonGatewayTimeoutError # API wrapper lib -from core import Judge -from exceptions import ConfigurationError, DriverError, DriverAuthorizationError, DriverNetworkError +import constants # Ivory constants +from judge import ReportJudge, PendingAccountJudge, Punishment # Judge to integrate into Ivory +from schemas import IvoryConfig -MAX_RETRIES = 3 -class Ivory: +class Ivory(): """ - The core class for the Ivory automoderation system. - In practice, you really only need to import this and a driver to get it - running. + The main Ivory class, which programmatically handles reports pulled from + the Mastodon API. """ - handled_reports: List[str] = [] - def __init__(self): - # get config file - try: - with open('config.yml') as config_file: - config = yaml.load(config_file, Loader=yaml.Loader) - except OSError: - print("Failed to open config.yml!") - exit(1) - self.debug_mode = config.get('debug_mode', False) - self.wait_time = config.get('wait_time', 300) - # parse rules first; fail early and all that - self.judge = Judge() - try: - rules_config = config['rules'] - except KeyError: - print("ERROR: Couldn't find any rules in config.yml!") - exit(1) - rulecount = 1 - for rule_config in rules_config: - try: - # programmatically load rule based on type in config - rule_type = rule_config['type'] - Rule = import_module('rules.' + rule_type).rule - self.judge.add_rule(Rule(rule_config)) - rulecount += 1 - except ModuleNotFoundError: - print("ERROR: Rule #%d not found!" % rulecount) - exit(1) - except Exception as err: - print("Failed to initialize rule #%d!" % rulecount) - raise err - try: - driver_config = config['driver'] - # programmatically load driver based on type in config - module_name = 'drivers.' + driver_config['type'] - Driver = import_module(module_name).driver - self.driver = Driver(driver_config) - except KeyError: - print("ERROR: Driver configuration not found in config.yml!") - exit(1) - except ModuleNotFoundError as err: - if err.name == module_name: - print("ERROR: Driver not found!") - exit(1) - else: - raise err - except ConfigurationError as err: - print("ERROR: Bad configuration:", err) - exit(1) - except DriverError as err: - print("ERROR while initializing driver:", err) + def __init__(self, raw_config): + """ + Runs Ivory. + """ + # **Validate the configuration** + config = IvoryConfig(raw_config) + + # **Set up logger** + self._logger = logging.getLogger(__name__) + + self._logger.info("Ivory version %s starting", constants.VERSION) + + # **Load Judge and Rules** + self._logger.info("parsing rules") + if 'reports' in config: + self.report_judge = ReportJudge(config['reports'].get("rules")) + else: + self._logger.debug("no report rules detected") + self.report_judge = None + if 'pendingAccounts' in config: + self.pending_account_judge = PendingAccountJudge(config['pendingAccounts'].get("rules")) + else: + self._logger.debug("no pending account rules detected") + self.pending_account_judge = None + + + # **Initialize and verify API connectivity** + self._api = Mastodon( + access_token=config['token'], + api_base_url=config['instanceURL'] + ) + self._logger.debug("mastodon API wrapper initialized") + # 2.9.1 required for moderation API + if not self._api.verify_minimum_version("2.9.1"): + self._logger.error("This instance is not updated to 2.9.1 - this version is required for the Moderation API %s", self._api.users_moderated) exit(1) - except Exception as err: - print("ERROR: Uncaught error while initializing driver!!") - raise err + self._logger.debug("minimum version verified; should be ok") + # grab some info which could be helpful here + self.instance = self._api.instance() + self.user = self._api.account_verify_credentials() + # log a bunch of shit + self._logger.info("logged into %s as %s", + self.instance['uri'], self.user['username']) + self._logger.debug("instance info: %s", self.instance) + self._logger.debug("user info: %s", self.user) + + # **Set some variables from config** + if 'waitTime' not in config: + self._logger.info( + "no waittime specified, defaulting to %d seconds", constants.DEFAULT_WAIT_TIME) + self.wait_time = config.get("waitTime", constants.DEFAULT_WAIT_TIME) + self.dry_run = config.get('dryRun', False) - def handle_reports(self): + + def handle_unresolved_reports(self): + """ + Handles all unresolved reports. + """ + reports = self._api.admin_reports() + for report in reports: + self.handle_report(report) + + def handle_report(self, report: dict): + """ + Handles a single report. """ - Get reports from the driver, and judge and punish each one accordingly. + self._logger.info("handling report #%d", report['id']) + (punishment, rules_broken) = self.report_judge.make_judgement(report) + if rules_broken: + self._logger.info("report breaks these rules: %s", rules_broken) + if punishment is not None: + self._logger.info("handling report with punishment %s", punishment) + self.punish(report['target_account']['id'], punishment, report['id']) + + def handle_pending_accounts(self): + """ + Handle all accounts in the pending account queue. + """ + accounts = self._api.admin_accounts(status="pending") + for account in accounts: + self.handle_pending_account(account) + + def handle_pending_account(self, account: dict): """ - retries = 0 + Handle a single pending account. + """ + self._logger.info("handling pending user %s", account['username']) + (punishment, rules_broken) = self.pending_account_judge.make_judgement(account) + if rules_broken: + self._logger.info("pending account breaks these rules: %s", rules_broken) + if punishment is not None: + self._logger.info("handling report with punishment %s", punishment) + self._logger.debug("punishment cfg: %s", punishment.config) + self.punish(account['id'], punishment) + + def punish(self, account_id, punishment: Punishment, report_id=None): + if self.dry_run: + self._logger.info("ignoring punishment; in dry mode") + return + maxtries = 3 + tries = 0 while True: try: - reports_to_check = self.driver.get_unresolved_report_ids() - break - except DriverNetworkError as err: - retries += 1 - if retries < MAX_RETRIES: - print("Failed to check reports - retrying...") - continue + if punishment.type == constants.PUNISH_REJECT: + self._api.admin_account_reject(account_id) + elif punishment.type == constants.PUNISH_WARN: + self._api.admin_account_moderate( + account_id, + None, + report_id, + text=punishment.config.get('message') + ) + elif punishment.type == constants.PUNISH_DISABLE: + self._api.admin_account_moderate( + account_id, + "disable", + report_id, + text=punishment.config.get('message') + ) + elif punishment.type == constants.PUNISH_SILENCE: + self._api.admin_account_moderate( + account_id, + "silence", + report_id, + text=punishment.config.get('message') + ) + elif punishment.type == constants.PUNISH_SUSPEND: + self._api.admin_account_moderate( + account_id, + "suspend", + report_id, + text=punishment.config.get('message') + ) else: - print("Failed to get reports:",err) - break - for report_id in reports_to_check: - if report_id in self.handled_reports: - print("Report #%s skipped" % report_id) - continue - retries = 0 - while True: - try: - print("Handling report #%s..." % report_id) - report = self.driver.get_report(report_id) - (final_verdict, rules_broken) = self.judge.make_judgement(report) - if final_verdict: - self.driver.punish(report, final_verdict) - rules_broken_str = ', '.join( - [str(rule) for rule in rules_broken]) # lol - note = "Ivory has suspended this user for breaking rules: "+rules_broken_str - else: - note = "Ivory has scanned this report and found no infractions." - self.driver.add_note(report.report_id, note) - self.handled_reports.append(report_id) - # network error handling - except DriverNetworkError as err: - retries += 1 - print("Encountered network error:", err) - if retries < MAX_RETRIES: - print("Retrying (attempt %d)..." % retries) - continue - else: - print("Max retries hit; skipping...") - # driver error handling - except DriverAuthorizationError as err: - print("Fatal authorization error:",err) - print("Exiting...") - exit(1) - except DriverError as err: - print("Driver error handling report #"+report_id+":",err) - print("Skipping...") - # general exception catch - except Exception as err: - print("Error handling report #"+report_id+":",err) - print("Skipping...") + # whoops + raise NotImplementedError() + break + except MastodonGatewayTimeoutError as err: + self._logger.warn("gateway timed out. ignoring for now, if that didn't do it we'll get it next pass...") break def run(self): + self._logger.info("starting moderation pass") + try: + if self.report_judge: + self.handle_unresolved_reports() + if self.pending_account_judge: + self.handle_pending_accounts() + self._logger.info("moderation pass complete") + except MastodonError: + self._logger.exception( + "enountered an API error. waiting %d seconds to try again", self.wait_time) + + def watch(self): """ - Runs the Ivory automoderator main loop. + Runs handle_unresolved_reports() on a loop, with a delay specified in + the "waittime" field of the config. """ while True: - print('Running report pass...') - try: - self.handle_reports() - except Exception as err: - print("Unexpected error handling reports:",err) - if self.debug_mode: - raise err - print('Report pass complete.') - time.sleep(self.wait_time) + starttime = time.time() + self.run() + time_to_wait = self.wait_time - (time.time() - starttime) + if time_to_wait > 0: + self._logger.debug("waiting for %.4f seconds", time_to_wait) + time.sleep(time_to_wait) + else: + self._logger.warn("moderation pass took longer than waitTime - this will cause significant drift. you may want to increase waitTime") diff --git a/judge.py b/judge.py new file mode 100644 index 0000000..6498866 --- /dev/null +++ b/judge.py @@ -0,0 +1,151 @@ +""" +The Judge system, which tests the reports and pending accounts Ivory feeds into +it. +""" +import logging # for logging in Judge +from typing import List # type hinting for List +from importlib import import_module # for dynamic rule imports + +class Punishment: + """ + A Punishment is Ivory's representation of a moderation action. + + Currently this is a simple proxy to the Mastodon.admin_account_moderate() + function's kwargs, with an extra field for the punishment's severity. + """ + + def __init__(self, severity: int, **config): + self.type = config['type'] + self.severity = severity + self.config = config + + def __str__(self): + return "Punishment (%s)" % self.type + + def __repr__(self): + return "Punishment (%s, severity %s)" % (self.type, self.severity) + + +class Rule: + """ + Rules in Ivory are kind of like unit tests for the data the Judge screens. + Each one can be run against a data structure to determine if it passes or + fails. + In this case, it differs in that a Rule comes with a Punishment. + """ + + def __init__(self, **config): + self.name = config['name'] + self.punishment = Punishment(config['severity'], **config['punishment']) + self._logger = logging.getLogger(__name__) + + def test_report(self, report: dict): + """ + Test a report. + """ + raise NotImplementedError() + + def test_pending_account(self, account: dict): + """ + Test a pending account. + """ + raise NotImplementedError() + + def __str__(self): + return self.name + + def __repr__(self): + return "Rule '%s' (%s)" % (self.name, type(self).__name__) + + +class Judge: + """ + Interface for judging data based on rules. + + The Judge class is a dead-simple class that holds Rule objects, and allows + you to test each Rule on a single dict with the make_judgement + method. + """ + + def __init__(self, rule_configs: List[dict] = None): + self.rules = [] + if rule_configs is not None: + self.load_rules(rule_configs) + + def load_rules(self, rule_configs: List[dict]): + """ + Load rules from a list of rule configuration dicts. + """ + rulecount = 1 + logger = logging.getLogger(__name__) + for rule_config in rule_configs: + try: + # programmatically load rule based on type in config + rule_type = rule_config['type'] + logger.debug( + "loading rule #%d of type %s", rulecount, rule_type) + Rule = import_module('rules.{}'.format(rule_type)).rule + new_rule = Rule(rule_config) + self.add_rule(new_rule) + rulecount += 1 + except ModuleNotFoundError as err: + logger.exception("rule #%d not found", rulecount) + logger.critical("could not parse rules") + raise err + except Exception as err: + logger.exception( + "failed to initialize rule #%d", rulecount) + logger.critical("could not parse rules") + raise err + logger.info("%s judge loaded %d rules (%d total)", type(self).__name__, rulecount - 1, len(self.rules)) + + def add_rule(self, rule): + """ + Add a rule to the judge's list. + + Future judgements will use this rule. + """ + self.rules.append(rule) + + def clear_rules(self): + """ + Clear this judge's rules list. + """ + self.rules = [] + + def test_rule(self, rule: Rule, data: dict): + raise NotImplementedError() + + + def make_judgement(self, data: dict) -> (Punishment, List[Rule]): + """ + Judge some data. + + Returns: + final_verdict: Returns the Punishment object that should be used for + this data, or None if there is none. + rules_broken: The list of rules the judge determined were broken. + """ + logger = logging.getLogger(__name__) + most_severe_rule = None + rules_broken = set() + for rule in self.rules: + logger.debug("running rule %s", rule) + rule_was_broken = self.test_rule(rule, data) + if rule_was_broken: + rules_broken.add(rule) + if (most_severe_rule is None or + most_severe_rule.punishment.severity < rule.punishment.severity): + most_severe_rule = rule + if most_severe_rule is not None: + final_verdict = most_severe_rule.punishment + else: + final_verdict = None + return (final_verdict, rules_broken) + +class ReportJudge(Judge): + def test_rule(self, rule: Rule, data: dict): + return rule.test_report(data) +class PendingAccountJudge(Judge): + def test_rule(self, rule: Rule, data: dict): + return rule.test_pending_account(data) diff --git a/requirements.txt b/requirements.txt index c11ee28..e155ee6 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,7 +1,27 @@ -certifi==2019.3.9 +atomicwrites==1.3.0 +attrs==19.3.0 +autopep8==1.4.4 +beautifulsoup4==4.8.0 +certifi==2019.9.11 chardet==3.0.4 +decorator==4.4.0 idna==2.8 -PyYAML==5.1.1 +importlib-metadata==0.23 +Mastodon.py==1.4.6 +more-itertools==7.2.0 +packaging==19.2 +pluggy==0.13.0 +py==1.8.0 +pycodestyle==2.5.0 +pyparsing==2.4.2 +pytest==5.2.2 +python-dateutil==2.8.0 +python-magic==0.4.15 +pytz==2019.2 requests==2.22.0 -selenium==3.141.0 -urllib3==1.25.3 +six==1.12.0 +soupsieve==1.9.4 +urllib3==1.25.6 +voluptuous==0.11.7 +wcwidth==0.1.7 +zipp==0.6.0 diff --git a/rules/bio_content.py b/rules/bio_content.py new file mode 100644 index 0000000..4b8d486 --- /dev/null +++ b/rules/bio_content.py @@ -0,0 +1,26 @@ +import re + +from judge import Rule + +from schemas import RegexBlockingRule + +class BioContentRule(Rule): + def __init__(self, raw_config): + config = RegexBlockingRule(raw_config) + Rule.__init__(self, **config) + self.blocked = config['blocked'] + def test_report(self, report: dict): + """ + Test if the target account's bio text or fields matches any of the given blocked regexes. + """ + acct = report['target_account']['account'] + for regex in self.blocked: + if re.search(regex, acct.get('note')): + return True + for field in acct.get('fields'): + if re.search(regex, field.get('value')): + return True + + return False + +rule = BioContentRule diff --git a/rules/link_content.py b/rules/link_content.py index cb5b190..d6a77bb 100644 --- a/rules/link_content.py +++ b/rules/link_content.py @@ -1,20 +1,26 @@ import re -from core import Rule, Report +from judge import Rule +from util import parse_links_from_statuses + +from schemas import RegexBlockingRule class LinkContentRule(Rule): """ A rule which checks for banned link content. """ - def __init__(self, config): - Rule.__init__(self, config) - self.blocked = config['blocked'] - def test(self, report: Report): + def __init__(self, raw_config): + config = RegexBlockingRule(raw_config) + Rule.__init__(self, **config) + self.blocked = [] + for regex in config['blocked']: + self.blocked.append(re.compile(regex)) + def test_report(self, report: dict): """ Test if a post's links matches any of the given blocked regexes. """ - for link in report.links: + for link in parse_links_from_statuses(report['statuses']): for regex in self.blocked: - if re.search(regex, link): + if regex.search(link): return True return False diff --git a/rules/link_resolver.py b/rules/link_resolver.py index 3f8b1e6..1b3702f 100644 --- a/rules/link_resolver.py +++ b/rules/link_resolver.py @@ -1,10 +1,14 @@ import re import requests -from core import Rule, Report +from judge import Rule from constants import VERSION +from util import parse_links_from_statuses + +from schemas import RegexBlockingRule # HTTP headers for the LinkResolverRule. # Certain URL shorteners require us to set a valid non-generic user agent. +# Also, it's just good practice. HEADERS = { "User-Agent": "Mozilla/5.0 IvoryAutomod/" + VERSION } @@ -14,11 +18,12 @@ class LinkResolverRule(Rule): A rule which checks for banned links, resolving links to prevent shorturl mitigation. """ - def __init__(self, config): - Rule.__init__(self, config) + def __init__(self, raw_config): + config = RegexBlockingRule(raw_config) + Rule.__init__(self, **config) self.blocked = config['blocked'] - def test(self, report: Report): - for link in report.links: + def test_report(self, report: dict): + for link in parse_links_from_statuses(report['statuses']): response = requests.head(link, allow_redirects=True, headers=HEADERS) resolved_url = response.url for regex in self.blocked: diff --git a/rules/message_content.py b/rules/message_content.py index ae944a4..8982367 100644 --- a/rules/message_content.py +++ b/rules/message_content.py @@ -1,19 +1,33 @@ import re -from core import Rule, Report +from judge import Rule + +from schemas import RegexBlockingRule class MessageContentRule(Rule): - def __init__(self, config): - Rule.__init__(self, config) + def __init__(self, raw_config): + config = RegexBlockingRule(raw_config) + Rule.__init__(self, **config) self.blocked = config['blocked'] - def test(self, report: Report): + def test_report(self, report: dict): """ - Test if a post matches any of the given blocked regexes. + Test if a status matches any of the given blocked regexes. """ - for post in report.posts: + for status in report.get('statuses', []): for regex in self.blocked: - if re.search(regex, post): + if re.search(regex, status.get('content', '')): return True return False + def test_pending_account(self, account: dict): + """ + Test if a pending account's join reason matches any of the blocked + regexes. + """ + if 'invite_request' not in account: + return False # can't violate this rule if you don't have a pending blurb :rollsafe: + for regex in self.blocked: + if re.search(regex, str(account.get('invite_request'))): + return True + return False rule = MessageContentRule diff --git a/rules/stopforumspam.py b/rules/stopforumspam.py new file mode 100644 index 0000000..0d20198 --- /dev/null +++ b/rules/stopforumspam.py @@ -0,0 +1,77 @@ +from judge import Rule +import requests +import logging + +import schemas +from voluptuous import Required, Range + +Config = schemas.Rule.extend({ + Required("threshold"): Range(min=0, max=100) +}) + +class StopForumSpamRule(Rule): + """ + A rule which pings StopForumSpam's API to see if a user is a reported + spammer. + """ + def __init__(self, raw_config): + # Validate configuration + config = Config(raw_config) + Rule.__init__(self, **config) + # Caching for known emails/IPs + self.email_confidences = {} + self.tested_emails = set() + self.ip_confidences = {} + self.tested_ips = set() + self.threshold = config['threshold'] + + def calc_confidence(self, ip_confidence, email_confidence): + if not email_confidence and not ip_confidence: + return False + elif email_confidence and not ip_confidence: + return email_confidence >= self.threshold + elif ip_confidence and not email_confidence: + return ip_confidence >= self.threshold + else: + return max([email_confidence, ip_confidence]) >= self.threshold + + def test_pending_account(self, account: dict): + """ + Test if the reported user's email is listed in StopForumSpam's + database. + + See https://www.stopforumspam.com/usage for how we're interfacing with + the API here. + """ + email = account['email'] + ip = account['ip'] + if email in self.tested_emails and ip in self.tested_ips: + self._logger.debug("looks like we've already tested for this user and ip; recalculating to be sure") + judgement = self.calc_confidence(self.ip_confidences.get(ip), self.email_confidences.get(email)) + return judgement + + params = { + "email": email, + "ip": ip, + "json": '' + } + resp = requests.get("https://api.stopforumspam.org/api", params=params) + results = resp.json() + + ip_confidence = results.get('ip').get('confidence') + email_confidence = results.get('email').get('confidence') + if ip_confidence: + self.ip_confidences[ip] = ip_confidence + if email_confidence: + self.email_confidences[email] = email_confidence + self.tested_emails.add(email) + self.tested_ips.add(ip) + + judgement = self.calc_confidence(ip_confidence, email_confidence) + self._logger.debug("ip confidence {}".format(ip_confidence)) + self._logger.debug("email confidence {}".format(email_confidence)) + self._logger.debug("judgement: {}".format(judgement)) + + return judgement + +rule = StopForumSpamRule diff --git a/rules/username_content.py b/rules/username_content.py index 251199a..01e05f9 100644 --- a/rules/username_content.py +++ b/rules/username_content.py @@ -1,16 +1,27 @@ import re +from judge import Rule -from core import Rule, Report +from schemas import RegexBlockingRule class UsernameContentRule(Rule): - def __init__(self, config): - Rule.__init__(self, config) + def __init__(self, raw_config): + config = RegexBlockingRule(raw_config) + Rule.__init__(self, **config) self.blocked = config['blocked'] - def test(self, report: Report): + def test_report(self, report: dict): """ - Test if the reported user matches any of the blocked regexes." + Test if the reported user matches any of the blocked regexes. """ - username = report.reported.username + username = report['target_account']['account']['username'] + for regex in self.blocked: + if re.search(regex, username): + return True + return False + def test_pending_account(self, account: dict): + """ + Test if the pending user's username matches any of the blocked regexes. + """ + username = account['username'] for regex in self.blocked: if re.search(regex, username): return True diff --git a/schemas.py b/schemas.py new file mode 100644 index 0000000..f6760a3 --- /dev/null +++ b/schemas.py @@ -0,0 +1,61 @@ +""" +Config schemas used in Ivory and its rules. +""" +from voluptuous import Schema, Required, Any, Url, ALLOW_EXTRA +import constants + +ReportPunishment = Schema({ + Required("type"): Any( + constants.PUNISH_WARN, + constants.PUNISH_REJECT, + constants.PUNISH_DISABLE, + constants.PUNISH_SILENCE, + constants.PUNISH_SUSPEND + ) +}, extra=ALLOW_EXTRA) + +PendingAcctPunishment = Schema({ + Required("type"): Any( + constants.PUNISH_REJECT + ) +}, extra=ALLOW_EXTRA) + +Rule = Schema({ + Required("name"): str, + Required("type"): str, + Required("severity"): int, +}, extra=ALLOW_EXTRA) + +ReportRule = Rule.extend({ + Required("punishment"): ReportPunishment +}) + +PendingAcctRule = Rule.extend({ + Required("punishment"): PendingAcctPunishment +}) + +Reports = Schema({ + Required("rules"): [ReportRule] +}) + +PendingAccounts = Schema({ + Required("rules"): [PendingAcctRule] +}) + +IvoryConfig = Schema({ + Required("token"): str, + # I know I should be using Url() here but it didn't work and I'm tired + Required("instanceURL"): str, + "waitTime": int, + "dryRun": bool, + "logLevel": Any("CRITICAL", "ERROR", "WARNING", "INFO", "DEBUG"), + "reports": Reports, + "pendingAccounts": PendingAccounts +}) + +# Schemas used by several rules + +RegexBlockingRule = Rule.extend({ + Required("blocked"): [str] +}) + diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 0000000..b7924a2 --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,186 @@ +import pytest +import requests + +@pytest.fixture(autouse=True) +def disable_requests(monkeypatch): + """ + Prevent Requests from making any actual network requests. + (This also prevents Mastodon.py from doing anything, as it uses requests as + well.) + """ + monkeypatch.delattr("requests.sessions.Session.request") + +@pytest.fixture +def MockResponse(): + """ + Returns an extendable class that resembles the Requests response class. + """ + class _MockResponse: + def __init__(self, **kwargs): + self.jsonresponse = kwargs.get("json") + self.url = kwargs.get("url") + def json(self): + return self.jsonresponse + return _MockResponse + +@pytest.fixture +def account_field(): + def _account_field(**kwargs): + field = { + "name": kwargs.get("name", "name"), + "value": kwargs.get("value", "value"), + } + if kwargs.get('verified') is True: + field["verified_at"] = "2019-01-01T00:00:00.000Z" + return field + return _account_field + + +@pytest.fixture +def account(account_field): + def _account(**kwargs): + fields = [] + for cfg in kwargs.get("fields", []): + fields.append(account_field(**cfg)) + return { + "id": kwargs.get("account_id", "1"), + "username": kwargs.get("username", "fakeuser"), + "acct": kwargs.get("username", "fakeuser"), + "display_name": kwargs.get("username", "fakeuser"), + "locked": False, + "bot": False, + "created_at": "2019-01-01T00:00:00.000Z", + "note": kwargs.get("note", ""), + "url": "https://example.com/@user", + "avatar": "https://cdn.example.com/testavatar.png", + "avatar_static": "https://cdn.example.com/testheader.png", + "header": "https://cdn.example.com/testheader.png", + "header_static": "https://cdn.example.com/testheader.png", + "followers_count": 100, + "following_count": 100, + "statuses_count": 100, + "last_status_at": "2019-01-01T00:00:00.000Z", + "emojis": [], + "fields": fields + } + return _account + + +@pytest.fixture +def pending_account(account): + def _pending_account(**kwargs): + return { + "id": kwargs.get("account_id", "1"), + "username": kwargs.get("username", "fakeuser"), + "domain": None, + "created_at": "2019-01-01T00:00:00.000Z", + "email": kwargs.get("email", "testuser@example.com"), + "ip": kwargs.get("ip", "127.0.0.1"), + "role": "user", + "confirmed": True, + "suspended": False, + "silenced": False, + "disabled": False, + "approved": False, + "locale": "en", + "invite_request": kwargs.get("message", "Test message."), + "account": account(**kwargs.get("account", {})) + } + return _pending_account + + +@pytest.fixture +def admin_account(account): + def _admin_account(**kwargs): + return { + "id": kwargs.get("account_id", "1"), + "username": kwargs.get("username", "fakeuser"), + "domain": kwargs.get("domain"), + "created_at": "2019-01-01T00:00:00.000Z", + "email": kwargs.get("email", "testuser@example.com"), + "ip": kwargs.get("ip", "127.0.0.1"), + "role": "user", + "confirmed": True, + "suspended": False, + "silenced": False, + "disabled": False, + "approved": True, + "locale": "en", + "invite_request": None, + "account": account(**kwargs.get("account", {})) + } + return _admin_account + + +@pytest.fixture +def report(account, admin_account, status): + def _report(**kwargs): + reporter = admin_account(**kwargs.get("reporter", {})) + reported = admin_account(**kwargs.get("reported", {})) + statuses = [] + for cfg in kwargs.get("statuses", []): + statuses.append(status(**cfg)) + return { + "id": kwargs.get("report_id", "1"), + "action_taken": False, + "comment": kwargs.get("comment", ""), + "created_at": "2019-01-01T00:00:00.000Z", + "updated_at": "2019-01-01T00:00:00.000Z", + "account": reporter, + "target_account": reported, + "assigned_account": None, + "action_taken_by_account": None, + "statuses": statuses + } + return _report + +@pytest.fixture +def status_tag(): + def _status_tag(name): + return { + "name": name, + "url": "https://example.com/tags/{}".format(name) + } + return _status_tag + +@pytest.fixture +def status(account, status_tag): + def _status(**kwargs): + tags = [] + for tagname in kwargs.get("tags", []): + tags.append(status_tag(tagname)) + return { + "id": kwargs.get("status_id", "1"), + "created_at": "2019-01-01T00:00:00.000Z", + "in_reply_to_id": kwargs.get("reply_id", "1"), + "in_reply_to_account_id": kwargs.get("replying_to_id", "1"), + "sensitive": bool(kwargs.get("spoiler_text", None)), + "spoiler_text": kwargs.get("spoiler_text", None), + "visibility": "public", + "language": "en", + "uri": "https://example.com/users/testuser/statuses/1", + "url": "https://example.com/@testuser/1", + "replies_count": 1, + "reblogs_count": 0, + "favourites_count": 0, + "favourited": False, + "reblogged": False, + "muted": False, + "content": kwargs.get("content", "

Test post.

"), + "reblog": None, + "account": account(**kwargs.get("author", {})), + "media_attachments": [], + "mentions": [ + { + "id": "1", + "username": "fakeuser", + "url": "https://example.com/@testuser", + "acct": "fakeuser@example.com" + } + ], + "tags": [{"name": "hashtag", "url": "https://example.com/tags/hashtag"}], + "emojis": [], + "card": None, + "poll": None + } + return _status diff --git a/tests/test_bio_content.py b/tests/test_bio_content.py new file mode 100644 index 0000000..2ff7f61 --- /dev/null +++ b/tests/test_bio_content.py @@ -0,0 +1,75 @@ +import pytest +import voluptuous +from copy import deepcopy + +from rules.bio_content import rule as Rule + +ruleconfig = { + "name": "Test rule", + "type": "test_type", + # this second one will block "slur" but not "slurp". + # just gotta be sure regex is working + "blocked": ["badword", "slur[^p]"], + "severity": 1, + "punishment": { + "type": "suspend" + } +} + + +@pytest.fixture +def rule(): + return Rule(ruleconfig) + + +def test_requires_blocked(): + bad_config = deepcopy(ruleconfig) + bad_config.pop("blocked") + # bad config no have block. no worky :( + with pytest.raises(voluptuous.error.MultipleInvalid): + Rule(bad_config) + + +def test_evil_report(rule, report): + rpt1 = report( + reported={ + "account": { + "note": "slur!" # needs the extra char on the end for the regex hehe + } + } + ) + rpt2 = report( + reported={ + "account": { + "fields": [ + { + "name": "normal field", + "value": "just passing through here,,,,," + }, { + "name": "bad field", + "value": "badword" + } + ] + } + } + ) + # test that it's checking the notes + assert rule.test_report(rpt1) + # test that it's checking the fields + assert rule.test_report(rpt2) + + +def test_good_report(rule, report): + rpt = report( + reported={ + "username": "babword", + "note": "slurpy boi", + "fields": [ + { + "name": "website", + "value": "slurpzo.ne" + } + ] + } + ) + assert not rule.test_report(rpt) diff --git a/tests/test_ivory.py b/tests/test_ivory.py new file mode 100644 index 0000000..a610012 --- /dev/null +++ b/tests/test_ivory.py @@ -0,0 +1,142 @@ +import pytest +import ivory +import time + +ivoryconfig = { + "token": "testtoken", + "instanceURL": "https://testinstance.local", + "waitTime": 300, + "logLevel": "DEBUG", + "reports": { + "rules": [ + { + "name": "No bad usernames", + "type": "username_content", + "blocked": ["badword"], + "severity": 1, + "punishment": { + "type": "suspend", + "message": "Your account has been suspended for spamming." + } + }, + { + "name": "No badwords", + "type": "message_content", + "blocked": ["badword"], + "severity": 1, + "punishment": { + "type": "disable", + "message": "Your account has been disabled for having a badword in a message." + } + } + ] + }, + "pendingAccounts": { + "rules": [ + { + "name": "No probbox spammers", + "type": "message_content", + "blocked": ["badword", "slur[^p]"], + "severity": 1, + "punishment": { + "type": "reject" + } + }, + { + "name": "No mewkid spammers", + "type": "username_content", + "blocked": ["badword", "slur[^p]"], + "severity": 1, + "punishment": { + "type": "reject" + } + } + ] + } +} + +@pytest.fixture +def generate_mockstodon(monkeypatch): + """ + Replace Mastodon.py completely with a custom copy that replicates the functions Ivory uses. + + I hate having to do this but I don't know if there's really a better way to + test without doing this... + """ + def _generate_mockstodon(**kwargs): + class Mockstodon(): + # Static values we check in the tests + # these are 4-tuples in the form of: + # (account id, action, report id, message) + moderation_actions = [] + accounts = [] + reports = [] + def __init__(self, **kwargs): + # We don't actually use these values internally, we just want to make sure Mastodon.py is getting passed the right stuff + assert kwargs.get("access_token") == ivoryconfig['token'] + assert kwargs.get("api_base_url") == ivoryconfig['instanceURL'] + def verify_minimum_version(self, version): + return True + def instance(self): + return { + "uri": ivoryconfig['instanceURL'], + } + def account_verify_credentials(self): + return { + "username": "testuser" + } + def admin_reports(self): + return self.reports + def admin_accounts(self, **kwargs): + if kwargs.get("status") == "pending": + return self.accounts + else: + assert kwargs.get("status") == "pending" + def admin_account_reject(self, acct_id): + self.moderation_actions.append((acct_id, "reject", None, None)) + return + def admin_account_moderate(self, acct_id, action, report_id, **kwargs): + self.moderation_actions.append((acct_id,action,report_id, kwargs.get("message"))) + return + Mockstodon.accounts = kwargs.get("accounts") + Mockstodon.reports = kwargs.get("reports") + monkeypatch.setattr(ivory, "Mastodon", Mockstodon) + return Mockstodon + return _generate_mockstodon + +def test_oneshot(generate_mockstodon, report, pending_account): + """ + Verify that Ivory runs. + """ + Mockstodon = generate_mockstodon(reports=[ + # suspend + report(reported={ + "username": "badword", + "account": { + "username": "badword" + } + }), + # disable + report(statuses=[ + { + "content": "badword" + } + ]), + # clean + report() + ], accounts=[ + # clean + pending_account(), + # reject + pending_account(username="badword"), + # reject + pending_account(message="badword") + ]) + i = ivory.Ivory(ivoryconfig) + i.run() + assert Mockstodon.moderation_actions == [ + ('1', 'suspend', '1', None), + ('1', 'disable', '1', None), + ('1', 'reject', None, None), + ('1', 'reject', None, None) + ] diff --git a/tests/test_judge.py b/tests/test_judge.py new file mode 100644 index 0000000..8a84d97 --- /dev/null +++ b/tests/test_judge.py @@ -0,0 +1,164 @@ +import pytest +from judge import ReportJudge, PendingAccountJudge +from rules.message_content import Rule as MessageContentRule +from rules.username_content import Rule as UsernameContentRule + +@pytest.fixture +def pendingjudge(): + return PendingAccountJudge([ + { + "name": "Rule 1", + "type": "username_content", + "blocked": ["evilusername", "malicious", "asshole"], + "severity": 1, + "punishment": { + "type": "silence" + } + }, + { + "name": "Rule 2", + "type": "message_content", + "blocked": ["heck", "badword", "slur[^p]"], + "severity": 5, + "punishment": { + "type": "suspend" + } + } + ]) + + +@pytest.fixture +def reportjudge(): + return ReportJudge([ + { + "name": "Rule 1", + "type": "username_content", + "blocked": ["evilusername", "malicious", "asshole"], + "severity": 1, + "punishment": { + "type": "silence" + } + }, + { + "name": "Rule 2", + "type": "message_content", + "blocked": ["heck", "badword", "slur[^p]"], + "severity": 5, + "punishment": { + "type": "suspend" + } + } + ]) + + +def test_report_empty(reportjudge, report): + # dummy report (always passes) + rpt = report() + (punishment, rules_broken) = reportjudge.make_judgement(rpt) + assert len(rules_broken) == 0 + assert punishment == None + +def test_report_rule1(reportjudge, report): + # bad username (silence) + rpt = report( + reported={ + "username": "evilusername", + "account": { + "username": "evilusername" + } + } + ) + (punishment, rules_broken) = reportjudge.make_judgement(rpt) + assert len(rules_broken) == 1 + for first_item in rules_broken: + assert first_item.name == "Rule 1" + assert isinstance(first_item, UsernameContentRule) + assert punishment.type == "silence" + +def test_report_rule2(reportjudge, report): + # bad message (suspend) + rpt = report( + statuses=[ + { + "content": + "

this post doesnt have anything objectionable in it haha

" + }, + { + "content": + "

this post contains a badword ban me pls

" + } + ] + ) + (punishment, rules_broken) = reportjudge.make_judgement(rpt) + assert len(rules_broken) == 1 + for first_item in rules_broken: + assert first_item.name == "Rule 2" + assert isinstance(first_item, MessageContentRule) + assert punishment.type == "suspend" + +def test_report_multirule(reportjudge, report): + # multiple rule breaks (highest severity takes precendence) + rpt = report( + reported={ + "username": "evilusername", + "account": { + "username": "evilusername" + } + }, + statuses=[ + { + "content": + "

this post doesnt have anything objectionable in it haha

" + }, + { + "content": + "

this post contains a badword ban me pls

" + } + ] + ) + (punishment, rules_broken) = reportjudge.make_judgement(rpt) + assert len(rules_broken) == 2 + for rule in rules_broken: + assert rule.name in ["Rule 1", "Rule 2"] + assert isinstance(rule, (MessageContentRule, UsernameContentRule)) + assert punishment.type == "suspend" + +def test_pending_empty(pendingjudge, pending_account): + # dummy pending account (always passes) + acct = pending_account() + (punishment, rules_broken) = pendingjudge.make_judgement(acct) + assert len(rules_broken) == 0 + assert punishment == None + +def test_pending_rule1(pendingjudge, pending_account): + # bad username (silence) + acct = pending_account(username="evilusername") + (punishment, rules_broken) = pendingjudge.make_judgement(acct) + assert len(rules_broken) == 1 + for first_item in rules_broken: + assert first_item.name == "Rule 1" + assert isinstance(first_item, UsernameContentRule) + assert punishment.type == "silence" + +def test_pending_rule2(pendingjudge, pending_account): + # bad message (suspend) + acct = pending_account(message="

this post contains a badword ban me pls

") + (punishment, rules_broken) = pendingjudge.make_judgement(acct) + assert len(rules_broken) == 1 + for first_item in rules_broken: + assert first_item.name == "Rule 2" + assert isinstance(first_item, MessageContentRule) + assert punishment.type == "suspend" + +def test_pending_multirule(pendingjudge, pending_account): + # multiple rule breaks (highest severity takes precendence) + acct = pending_account( + username="evilusername", + message="

this post contains a badword ban me pls

" + ) + (punishment, rules_broken) = pendingjudge.make_judgement(acct) + assert len(rules_broken) == 2 + for rule in rules_broken: + assert rule.name in ["Rule 1", "Rule 2"] + assert isinstance(rule, (MessageContentRule, UsernameContentRule)) + assert punishment.type == "suspend" diff --git a/tests/test_link_content.py b/tests/test_link_content.py new file mode 100644 index 0000000..91673f2 --- /dev/null +++ b/tests/test_link_content.py @@ -0,0 +1,73 @@ +import pytest +import voluptuous +from copy import deepcopy + +from rules.link_content import rule as Rule + +ruleconfig = { + "name": "Test rule", + "type": "test_type", + "blocked": ["evilsi\\.te", "heresa\\.porn\\.domain"], + "severity": 1, + "punishment": { + "type": "suspend" + } +} + + +@pytest.fixture +def rule(): + return Rule(ruleconfig) + + +def test_requires_blocked(): + bad_config = deepcopy(ruleconfig) + bad_config.pop("blocked") + # bad config no have block. no worky :( + with pytest.raises(voluptuous.error.MultipleInvalid): + Rule(bad_config) + + +def test_evil_report(rule, report): + rpt1 = report( + statuses=[ + { + "content": + '

this post doesnt have any objectionable links in it haha

' + }, + { + "content": + '

this post contains an bad link ban me pls

' + } + ] + ) + rpt2 = report( + statuses=[ + { + "content": + "

this post doesnt have any links at all in it haha

" + }, + { + "content": + '

gottem

' + } + ] + ) + assert rule.test_report(rpt1) + assert rule.test_report(rpt2) + + +def test_good_report(rule, report): + rpt = report( + statuses=[ + { + "content": + "

blah blah blah asdjhlg

" + }, + { + "content": + '

check my mixtape yeet

' + } + ] + ) + assert not rule.test_report(rpt) diff --git a/tests/test_link_resolver.py b/tests/test_link_resolver.py new file mode 100644 index 0000000..e565384 --- /dev/null +++ b/tests/test_link_resolver.py @@ -0,0 +1,81 @@ +import pytest +import voluptuous +import requests +from copy import deepcopy + +from rules.link_resolver import rule as Rule + +ruleconfig = { + "name": "Test rule", + "type": "test_type", + "blocked": ["evilsi\\.te", "heresa\\.porn\\.domain"], + "severity": 1, + "punishment": { + "type": "suspend" + } +} + + +@pytest.fixture +def rule(): + return Rule(ruleconfig) + +@pytest.fixture +def head_mock(MockResponse, monkeypatch): + def handler(url, *args, **kwargs): + respmap = { + "https://example.com/archive/notmaliciousatall/": "https://evilsi.te", # malicious site + "https://example.com/archive/definitelynotporn/": "https://heresa.porn.domain", # malicious site + "https://example.com/archive/actuallynotmalicious/": "https://example.com" # non-malicious site + } + return MockResponse(url=respmap[url]) + monkeypatch.setattr(requests, "head", handler) + + +def test_requires_blocked(): + bad_config = deepcopy(ruleconfig) + bad_config.pop("blocked") + # bad config no have block. no worky :( + with pytest.raises(voluptuous.error.MultipleInvalid): + Rule(bad_config) + +def test_evil_report(head_mock, rule, report): + # multiple statuses with one malicious + rpt1 = report( + statuses=[ + { + "content": + "

blah blah blah asdjhlg

" + }, + { + "content": + '

heres an inconspicuous archive link

' + } + ] + ) + assert rule.test_report(rpt1) + rpt2 = report( + statuses=[ + { + "content": + '

yeah uhh im up to no good lol

' + } + ] + ) + assert rule.test_report(rpt2) + +def test_good_report(head_mock, rule, report): + # multiple statuses with one malicious + rpt = report( + statuses=[ + { + "content": + "

blah blah blah asdjhlg

" + }, + { + "content": + '

heres an actually ok archive link

' + } + ] + ) + assert not rule.test_report(rpt) diff --git a/tests/test_message_content.py b/tests/test_message_content.py new file mode 100644 index 0000000..9a176c5 --- /dev/null +++ b/tests/test_message_content.py @@ -0,0 +1,85 @@ +import pytest +import voluptuous +from copy import deepcopy + +from rules.message_content import rule as Rule + +ruleconfig = { + "name": "Test rule", + "type": "test_type", + # this second one will block "slur" but not "slurp". + # just gotta be sure regex is working + "blocked": ["badword", "slur[^p]"], + "severity": 1, + "punishment": { + "type": "suspend" + } +} + + +@pytest.fixture +def rule(): + return Rule(ruleconfig) + + +def test_requires_blocked(): + bad_config = deepcopy(ruleconfig) + bad_config.pop("blocked") + # bad config no have block. no worky :( + with pytest.raises(voluptuous.error.MultipleInvalid): + Rule(bad_config) + + +def test_evil_report(rule, report): + rpt1 = report( + statuses=[ + { + "content": + "

this post doesnt have anything objectionable in it haha

" + }, + { + "content": + "

this post contains a badword ban me pls

" + } + ] + ) + rpt2 = report( + statuses=[ + { + "content": + "

this post doesnt have anything objectionable in it haha

" + }, + { + "content": + "

slurslurslurslurslur

" + } + ] + ) + assert rule.test_report(rpt1) + assert rule.test_report(rpt2) + +def test_good_report(rule, report): + rpt = report( + statuses=[ + { + "content": + "

blah blah slurp asdjhlg

" + }, + { + "content": + "

im just fuckibg POSTING,,,

" + } + ] + ) + assert not rule.test_report(rpt) + +def test_evil_pending_account(rule, pending_account): + acct1 = pending_account(message="lol slur slur slur you'll still let me in do it u wont") + assert rule.test_pending_account(acct1) + acct2 = pending_account(message="buy badword penis pills :joylmfao:") + assert rule.test_pending_account(acct2) + +def test_good_pending_account(rule, pending_account): + acct = pending_account(message="slurpeedrinker26") # of no relation to slurpeedrinker28 + assert not rule.test_pending_account(acct) + diff --git a/tests/test_stopforumspam.py b/tests/test_stopforumspam.py new file mode 100644 index 0000000..1a070e1 --- /dev/null +++ b/tests/test_stopforumspam.py @@ -0,0 +1,94 @@ +import pytest +import voluptuous +import requests # for mocking +from copy import deepcopy + +from rules.stopforumspam import rule as Rule + +ruleconfig = { + "name": "StopForumSpam test", + "type": "stopforumspam", + "threshold": 90, + "severity": 1, + "punishment": { + "type": "reject" + } + } + + +@pytest.fixture +def rule(): + return Rule(ruleconfig) + +@pytest.fixture +def sfs_response(): + def _sfs_response(ip_conf=None, email_conf=None): + # The rule currently only uses confidence internally, but we + # simulate a full response just in case + resp = { + "success": 1, + "ip": { + "frequency": 10, + "appears": 1 + }, + "email": { + "frequency": 10, + "appears": 1 + } + } + if ip_conf: + resp['ip']['confidence'] = ip_conf + if email_conf: + resp['email']['confidence'] = email_conf + return resp + return _sfs_response + +@pytest.fixture +def sfs_mock(monkeypatch, MockResponse, sfs_response): + """ + Expose a way to mock the "requests" library so that we can test inputs. + """ + def _sfs_mock(ip=None, email=None): + def handler(*args, **kwargs): + return MockResponse(json=sfs_response(ip, email)) + monkeypatch.setattr(requests, "get", handler) + return _sfs_mock + +def test_requires_threshold(): + bad_config = deepcopy(ruleconfig) + bad_config.pop("threshold") + # bad config no have threshold. no worky :( + with pytest.raises(voluptuous.error.MultipleInvalid): + Rule(bad_config) + +def test_evil_pending_account_iponly(rule, pending_account, sfs_mock): + acct = pending_account() + sfs_mock(ip=90) + assert rule.test_pending_account(acct) + +def test_good_pending_account_iponly(rule, pending_account, sfs_mock): + acct = pending_account() + # probably not *actually* good but not confident enough for us to act on, + # of course + sfs_mock(ip=89) + assert not rule.test_pending_account(acct) + +def test_evil_pending_account_emailonly(rule, pending_account, sfs_mock): + acct = pending_account() + sfs_mock(email=90) + assert rule.test_pending_account(acct) + +def test_good_pending_account_emailonly(rule, pending_account, sfs_mock): + acct = pending_account() + sfs_mock(email=89) + assert not rule.test_pending_account(acct) + +def test_evil_pending_account(rule, pending_account, sfs_mock): + acct = pending_account() + sfs_mock(ip=85, email=99) + assert rule.test_pending_account(acct) + +def test_good_pending_account(rule, pending_account, sfs_mock): + acct = pending_account() + sfs_mock(ip=80, email=89) + assert not rule.test_pending_account(acct) diff --git a/tests/test_username_content.py b/tests/test_username_content.py new file mode 100644 index 0000000..d82ec98 --- /dev/null +++ b/tests/test_username_content.py @@ -0,0 +1,77 @@ +import pytest +import voluptuous +from copy import deepcopy + +from rules.username_content import rule as Rule + +ruleconfig = { + "name": "Test rule", + "type": "test_type", + # this second one will block "slur" but not "slurp". + # just gotta be sure regex is working + "blocked": ["badword", "slur[^p]"], + "severity": 1, + "punishment": { + "type": "suspend" + } +} + + +@pytest.fixture +def rule(): + return Rule(ruleconfig) + + +def test_requires_blocked(): + bad_config = deepcopy(ruleconfig) + bad_config.pop("blocked") + # bad config no have block. no worky :( + with pytest.raises(voluptuous.error.MultipleInvalid): + Rule(bad_config) + + +def test_evil_report(rule, report): + # In the rule itself we're actually using the one in the nested account + # object (not the admin account) but we set both here for completion's + # sake. + rpt1 = report( + reported={ + "username": "slurmaster9000", + "account": { + "username": "slurmaster9000" + } + } + ) + rpt2 = report( + reported={ + "username": "xX_badword_Xx", + "account": { + "username": "xX_badword_Xx" + } + } + ) + assert rule.test_report(rpt1) + assert rule.test_report(rpt2) + +def test_good_report(rule, report): + rpt = report( + reported={ + # ffs corporate shill + "username": "slurpeedrinker28", + "account": { + "username": "slurpeedrinker28" + } + } + ) + assert not rule.test_report(rpt) + +def test_evil_pending_account(rule, pending_account): + acct1 = pending_account(username="slurtroll") + assert rule.test_pending_account(acct1) + acct2 = pending_account(username="badwordyfiend") + assert rule.test_pending_account(acct2) + +def test_good_pending_account(rule, pending_account): + acct = pending_account(message="slurpeedrinker26") # of no relation to slurpeedrinker28 + assert not rule.test_pending_account(acct) + diff --git a/util.py b/util.py new file mode 100644 index 0000000..340bf0d --- /dev/null +++ b/util.py @@ -0,0 +1,27 @@ +""" +Utilities for Ivory operations. +""" +from bs4 import BeautifulSoup +from typing import List + +def parse_links(text: str): + """ + Parse all links out of an HTML string. + Currently using BeautifulSoup for this. + + Used primarily when getting links out of a status or bio. + + TODO: Make parsing read non- links? + TODO: Exclude mentions? The "mention" class is not a good way to filter that... + """ + return [a.get('href') for a in BeautifulSoup(text, "html.parser").find_all('a')] + +def parse_links_from_statuses(statuses: List[dict]): + """ + Get a list of all links out of an array of Mastodon statuses. + """ + links = [] + for status in statuses: + for link in parse_links(status['content']): + links.append(link) + return links