aboutsummaryrefslogtreecommitdiffstats
AgeCommit message (Collapse)Author
2016-09-12Change name of users.list cached Storable fileTeddy Wing
Prefix the filename with the name of our script for better uniqueness and a clearer connection to this script.
2016-09-12Add TODOTeddy Wing
2016-09-12Only check for server if it's neededTeddy Wing
We only need the `$server` object if we're doing a WHOIS for the current nick. That's because if there's no server, there's no current nick. Otherwise, as long as the Slack API token is configured, a server isn't needed and shouldn't necessarily be required in order to request the information.
2016-09-12help: Return early instead of using an `if` statementTeddy Wing
Removes a level of indentation and looks cleaner.
2016-09-12Add help for `/swhois`Teddy Wing
Include a help document that can be accessed from inside Irssi showing a description of the `swhois` command and some examples. Leaned how to do this and format the output from: - https://github.com/irssi/scripts.irssi.org/blob/master/scripts/trackbar.pl - https://github.com/irssi/scripts.irssi.org/blob/22060fc4669627a9859c92efa2d6651e3673d494/scripts/spotify.pl - https://github.com/irssi/irssi/blob/b1ffd5f6472584aa3966746da9728c5afefcc4ce/docs/help/in/rehash.in - https://github.com/irssi/scripts.irssi.org/blob/22060fc4669627a9859c92efa2d6651e3673d494/scripts/clearable.pl
2016-09-12Add READMETeddy Wing
Include a short description, Slack API key configuration settings, usage information, sample output, and license information.
2016-09-12Add license & copyright informationTeddy Wing
2016-09-12Remove Data::Dumper importTeddy Wing
No longer needed. Was using this for debugging purposes.
2016-09-12Add script information as a comment headerTeddy Wing
Include a short description, a note about needing a Slack API key, and usage information.
2016-09-12Display an error and exit if Slack API token isn't setTeddy Wing
Since we can't do anything in the script if the user hasn't configured their Slack API token, die if it isn't set. Display a message to users telling them to set their API token along with the URL where they can obtain one. Also don't print the "Fetching users list..." message until we check for the Slack token because if we die then that message makes no sense.
2016-09-12Print message before fetching users listTeddy Wing
Let users know that we're requesting the users list from Slack and that it could take a while to get.
2016-09-12Rename slack-profile.pl -> slack_profile.plTeddy Wing
Underscores seem to be the convention in filenames so why not follow that.
2016-09-12Print whois info for current nick when `/swhois` is called without argTeddy Wing
Use the current nick from `$server->{nick}` as the default argument to `/swhois`. Extracted the whois printing functionality to its own subroutine so that it can be more easily executed whether or not an argument is passed to the command.
2016-09-12Ensure we're connected to a server before running /swhoisTeddy Wing
This is to allow us to run `/swhois` without an argument to get information about our current nick.
2016-09-11Switch from LWP to HTTP::Tiny packageTeddy Wing
Since I couldn't install LWP on my Raspberry Pi, I'm switching to HTTP::Tiny, which did install successfully.
2016-09-11swhois: Fix spacing of 'name' outputTeddy Wing
Add a space before the colons so that it lines up with the colons of the other fields.
2016-09-11If the users list is not available, fetch it from the Slack APITeddy Wing
In `find_user`, we check whether the Storable file is there (and non-zero). If it isn't, we fetch the users list from the Slack API. Renamed the subroutine containing the HTTP code to `fetch_users_list` from `users_list` to better convey the fact that it has to reach out to the web in order to get it. Store the Storable object in the Irssi scripts directory (instead of our local development directory as it was previously). Use the correct Slack API URL in our HTTP request, and also include our Slack API token from the Irssi settings. Add better error handling and messaging when the API request fails for whatever reason. Die on error so we don't end up executing the `retrieve` line (otherwise this line will cause an error complaining that the `users.list.plstore` file doesn't exist if it hasn't yet been created). If the API call succeeds and Slack gives us an 'ok' response, then `store` the JSON-decoded users list array in our serialized cache file and assign it to the `@users_list` global variable so we additionally have it in memory..
2016-09-10Add a setting for the Slack API tokenTeddy Wing
Users can set this with `/set slack_profile_token`. It's required in order to query the Slack API.
2016-09-10find_user: Remove redundant lineTeddy Wing
This break statement doesn't even get executed any more because we're returning the user right above it.
2016-09-10swhois: Add timezone to outputTeddy Wing
2016-09-10swhois: Rename `$data` to `$username`Teddy Wing
Because this name is so much clearer. I had used data because that's what's used in the Irssi command example.
2016-09-10swhois: Don't print fields if their value is emptyTeddy Wing
Certain fields that we weren't checking against could be empty (title and email). If that's the case, I don't think we should be printing a line with a field label and empty value. Add a new subroutine that prints the field if it's not blank. This eliminates all the repetitive `if` statements.
2016-09-10swhois: If user is a bot, identify them as such in swhois outputTeddy Wing
2016-09-10swhois: Print additional profile informationTeddy Wing
Include full name, title, email, phone number, and skype name.
2016-09-10swhois: Remove commented `$user` assignmentTeddy Wing
Wasn't sure whether I could make assignments in an if condition in Perl so I had this line originally, but when I tried the other syntax it worked, so I'm getting rid of this one.
2016-09-10Move the users list to a global variableTeddy Wing
This way we can actively store the users list in memory instead of always unpickling it from Storage and the filesystem. We'll need to work out exactly where this file should be stored, too. Ideally that would be configurable.
2016-09-10Add 'swhois' Irssi commandTeddy Wing
Create a command that will retrieve 'whois' information about a given user. This command takes a username argument and searches our users.list database for a match. It then prints out a field from the user hash. We'll want to print out more information in a more structured display, but this is just for testing within Irssi. The server conditional check is commented out so I can test this without connecting to a server. Running `/swhois` without an argument should print whois information for the current user/nick. Will have to look up how to get that information.
2016-09-10Clean up code that finds a user in the users listTeddy Wing
Put this in a subroutine called `find_user` that takes a username as input. This username will be searched for in the list and the corresponding user hash will be returned if found.
2016-09-10Add Irssi metadataTeddy Wing
2016-09-10Remove binary search in favour of linear searchTeddy Wing
In my very unscientific performance "benchmarks", there appeared to be no significant difference between binary search and linear search. In light of that, I've decided to go with linear search instead to reduce complexity in the code and also account for the fact that Slack makes the claim that the `users.list` is returned "in no particular order" (https://api.slack.com/methods/users.list). My "benchmarks", run on a late 2012 Core i5 MacBook Air: Binary Search (JSON decode): $ time perl slack-profile.pl real 0m2.039s user 0m2.019s sys 0m0.017s $ time perl slack-profile.pl real 0m1.981s user 0m1.961s sys 0m0.017s $ time perl slack-profile.pl real 0m2.065s user 0m2.045s sys 0m0.016s $ time perl slack-profile.pl real 0m2.039s user 0m2.017s sys 0m0.018s $ time perl slack-profile.pl real 0m1.997s user 0m1.978s sys 0m0.017s Binary Search (Storable): $ time perl slack-profile.pl real 0m0.118s user 0m0.102s sys 0m0.013s $ time perl slack-profile.pl real 0m0.117s user 0m0.100s sys 0m0.014s $ time perl slack-profile.pl real 0m0.123s user 0m0.105s sys 0m0.016s $ time perl slack-profile.pl real 0m0.123s user 0m0.107s sys 0m0.014s $ time perl slack-profile.pl real 0m0.127s user 0m0.109s sys 0m0.016s $ time perl slack-profile.pl real 0m0.125s user 0m0.108s sys 0m0.015s Linear Search (JSON decode): $ time perl slack-profile.pl real 0m2.017s user 0m1.998s sys 0m0.015s $ time perl slack-profile.pl real 0m2.025s user 0m2.005s sys 0m0.016s $ time perl slack-profile.pl real 0m2.011s user 0m1.992s sys 0m0.016s $ time perl slack-profile.pl real 0m2.064s user 0m2.044s sys 0m0.017s $ time perl slack-profile.pl real 0m2.166s user 0m2.147s sys 0m0.016s Linear Search (Storable): $ time perl slack-profile.pl real 0m0.120s user 0m0.103s sys 0m0.014s $ time perl slack-profile.pl real 0m0.125s user 0m0.107s sys 0m0.015s $ time perl slack-profile.pl real 0m0.129s user 0m0.110s sys 0m0.016s $ time perl slack-profile.pl real 0m0.121s user 0m0.106s sys 0m0.013s $ time perl slack-profile.pl real 0m0.118s user 0m0.101s sys 0m0.014s
2016-09-10Use `Storable` to serialize the users list dataTeddy Wing
This is _much_ faster than decoding the JSON string. The difference is between ~2 seconds and ~0.120 milliseconds. Also messed around with rough testing the performance of binary search vs. linear search again.
2016-09-10Implement binary search to find users by usernameTeddy Wing
Create a `binary_search` subroutine that will allow us to search through an array. Used Wikipedia and Rosetta Code as a reference: - https://en.wikipedia.org/wiki/Binary_search_algorithm#Algorithm - http://rosettacode.org/wiki/Binary_search#Perl In order to test matches, `binary_search` expects an anonymous function that will return 0, 1, or -1 depending on the result of the comparison. The anonymous function we use compares usernames alphabetically to the one we want to find. Finally, we have a section commented out that performs a linear search for a user in the members array. We will time this and the binary search version to see how they compare. Slack attests that the `users.list` API method provides users "in no particular order" (https://api.slack.com/methods/users.list), however most users appear to be ordered by username. The one exception appears to be 'slackbot', who appears at the end of the list.
2016-09-10Read and decode a JSON users list from a fileTeddy Wing
We have the output from the Slack API's `users.list` method stored in a file. Read that file and decode the JSON into Perl scalars so that we can filter the list for a specific user.
2016-09-10Move HTTP code to a subroutineTeddy Wing
File this code away into its own container so we can work on some JSON decoding.
2016-09-09Initial commit. Make an HTTP request.Teddy Wing
Figured out how to make an HTTPS request in Perl. We'll need this structure in order to interact with the Slack Web API.