Share Google Drive Files with Apps Script

I wrote a little Google Script that saves Gmail attachments to Drive and shares the saved files with users based on rules defined in the message body itself. Here’s a sample rule:

#, v #
#,, c#
#,, e #

In the above case, the file(s) would be shared with all these users but the sharing permissions would be different. The user view@example can only view (v) the document, the users edit@example have editing permissions while others have commenting permissions.