|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# |
| 3 | +# ongoing.py |
| 4 | +# Weechat script for automatized downloading of the new releases on XDCC bots. |
| 5 | +# |
| 6 | +# To enable it copy the file "ongoing.py" to ~/.weechat/python/ directiry and |
| 7 | +# execute the command "/python load python/ongoing.py" |
| 8 | +# |
| 9 | +# The comprehensive information about script usage can be found using |
| 10 | +# the command "/help ongoing". |
| 11 | +# |
| 12 | +# Licensed under MIT, see LICENSE file content for the details. |
| 13 | +# |
| 14 | +# |
| 15 | +# HERE BE DRAGONS |
| 16 | +# |
| 17 | + |
| 18 | +import weechat |
| 19 | +import re, os, pickle |
| 20 | + |
| 21 | + |
| 22 | +SCRIPT_NAME = "ongoing" |
| 23 | +SCRIPT_AUTHOR = "Mayoi Hachikuji <[email protected]>" |
| 24 | +SCRIPT_VERSION = "0.1" |
| 25 | +SCRIPT_LICENSE = "MIT" |
| 26 | +SCRIPT_DESC = "Automatically downloads new files on mask from XDCC bots" |
| 27 | +SCRIPT_COMMAND = "ongoing" |
| 28 | + |
| 29 | +SCRIPT_CMDS = "channel [name] | add_bot name regex | del_bot name | " + \ |
| 30 | + "list_bots | add_filter regex | del_filter id | list_filters" |
| 31 | +SCRIPT_HELP = """Available commands: |
| 32 | + {0}/{2} channel{1} - get the name of the channel for monitoring |
| 33 | + {0}/{2} channel #nibl{1} - monitor the channel #nibl for the updates |
| 34 | + {0}/{2} add_bot KareRaisu .*SEND\s([0-9]+).*{1} - look for the messages from |
| 35 | + the bot KareRaisu matching the listed regular expression (the only |
| 36 | + mentioned group in regex should be pack ID! Well, this regex should work |
| 37 | + with the most of Eggdrop installations and masquerading ones) |
| 38 | + {0}/{2} list_bots{1} - list of the bots are watched |
| 39 | + {0}/{2} del_bot KareRaisu{1} - stop the monitoring of messages from this bot |
| 40 | + {0}/{2} add_filter HorribleSubs.*Kantai.*720p{1} - add filter for the files |
| 41 | + with the names matching with this regular expression |
| 42 | + {0}/{2} list_filters{1} - list of the enabled file filters with their IDs |
| 43 | + {0}/{2} del_filter 1{1} - delete the filter with ID 1 |
| 44 | +
|
| 45 | +""".format(weechat.color("yellow"), weechat.color("chat"), SCRIPT_COMMAND) |
| 46 | + |
| 47 | + |
| 48 | +# register new weechat script |
| 49 | +weechat.register(SCRIPT_NAME, SCRIPT_AUTHOR, SCRIPT_VERSION, SCRIPT_LICENSE, |
| 50 | + SCRIPT_DESC, "", "") |
| 51 | + |
| 52 | +# the only default configuration option for the script |
| 53 | +DEFAULT_OPTIONS = { |
| 54 | + "channel": "#news" |
| 55 | +} |
| 56 | + |
| 57 | +CONFIG_DIR = weechat.info_get("weechat_dir", "") + "/ongoing/" |
| 58 | +FILE_BOTS = CONFIG_DIR + "bots.db" |
| 59 | +FILE_FILTERS = CONFIG_DIR + "filters.db" |
| 60 | + |
| 61 | + |
| 62 | +# load script configuration |
| 63 | +for option, default_value in DEFAULT_OPTIONS.items(): |
| 64 | + if not weechat.config_is_set_plugin(option): |
| 65 | + weechat.config_set_plugin(option, default_value) |
| 66 | + |
| 67 | + |
| 68 | +# -------------------------------------- |
| 69 | +# File manipulation helpers |
| 70 | +def file_check(f): |
| 71 | + try: |
| 72 | + with open(f): |
| 73 | + pass |
| 74 | + except IOError: |
| 75 | + d = os.path.dirname(f) |
| 76 | + if not os.path.exists(d): |
| 77 | + os.makedirs(d) |
| 78 | + open(f, 'w').close() |
| 79 | + |
| 80 | +def file_read(f): |
| 81 | + file_check(f) |
| 82 | + with open(f, "rb") as ff: |
| 83 | + try: |
| 84 | + return pickle.load(ff) |
| 85 | + except EOFError: |
| 86 | + return {} |
| 87 | + |
| 88 | +def file_write(f, data): |
| 89 | + with open(f, "wb") as ff: |
| 90 | + pickle.dump(data, ff) |
| 91 | + pass |
| 92 | + |
| 93 | + |
| 94 | +# -------------------------------------- |
| 95 | +# script command handlers |
| 96 | +def stats(): |
| 97 | + weechat.prnt("", "stats goes here") |
| 98 | + return weechat.WEECHAT_RC_OK |
| 99 | + |
| 100 | +def get_channel(): |
| 101 | + channel_name = weechat.config_get_plugin("channel") |
| 102 | + weechat.prnt("", "The current channel is %s%s" % |
| 103 | + (weechat.color("green"), channel_name)) |
| 104 | + return weechat.WEECHAT_RC_OK |
| 105 | + |
| 106 | +def set_channel(channel_name): |
| 107 | + weechat.config_set_plugin("channel", channel_name.lower()) |
| 108 | + weechat.prnt("", "The channel set to %s%s" % |
| 109 | + (weechat.color("green"), channel_name)) |
| 110 | + return weechat.WEECHAT_RC_OK |
| 111 | + |
| 112 | +def add_bot(bot_name, regex): |
| 113 | + bots = file_read(FILE_BOTS) |
| 114 | + bots[bot_name] = regex |
| 115 | + file_write(FILE_BOTS, bots) |
| 116 | + weechat.prnt("", "Added %s%s%s to XDCC providers list." % |
| 117 | + (weechat.color("green"), bot_name, weechat.color("chat"))) |
| 118 | + return weechat.WEECHAT_RC_OK |
| 119 | + |
| 120 | +def list_bots(): |
| 121 | + bots = file_read(FILE_BOTS) |
| 122 | + if bots == {}: |
| 123 | + weechat.prnt("", "%sThere are no added bots to watch for updates on." % |
| 124 | + (weechat.color("red"))) |
| 125 | + else: |
| 126 | + weechat.prnt("", "-- %sList of the watched bots %s--------" % |
| 127 | + (weechat.color("yellow"), weechat.color("chat"))) |
| 128 | + for bot_name, regex in bots.iteritems(): |
| 129 | + weechat.prnt("", " %s%-24s %s%s" % (weechat.color("green"), |
| 130 | + bot_name, weechat.color("chat"), regex)) |
| 131 | + weechat.prnt("", "------------------------------------") |
| 132 | + return weechat.WEECHAT_RC_OK |
| 133 | + |
| 134 | +def del_bot(bot_name): |
| 135 | + bots = file_read(FILE_BOTS) |
| 136 | + try: |
| 137 | + bots.pop(bot_name) |
| 138 | + weechat.prnt("", "%s%s%s has been removed from the list." % |
| 139 | + (weechat.color("green"), bot_name, weechat.color("chat"))) |
| 140 | + except KeyError: |
| 141 | + weechat.prnt("", "There is no bot named %s%s%s in list to delete him." % |
| 142 | + (weechat.color("red"), bot_name, weechat.color("chat"))) |
| 143 | + file_write(FILE_BOTS, bots) |
| 144 | + return weechat.WEECHAT_RC_OK |
| 145 | + |
| 146 | +def add_filter(fltr): |
| 147 | + filters_h = file_read(FILE_FILTERS) |
| 148 | + filters = filters_h.get('filters', []) |
| 149 | + filters.append(fltr) |
| 150 | + file_write(FILE_FILTERS, {"filters": filters}) |
| 151 | + weechat.prnt("", "Added %s%s%s to file filters list." % |
| 152 | + (weechat.color("green"), fltr, weechat.color("chat"))) |
| 153 | + return weechat.WEECHAT_RC_OK |
| 154 | + |
| 155 | +def list_filters(): |
| 156 | + filters_h = file_read(FILE_FILTERS) |
| 157 | + if filters_h == {} or filters_h == {"filters": []}: |
| 158 | + weechat.prnt("", "%sThere are no added file filters." % |
| 159 | + (weechat.color("red"))) |
| 160 | + else: |
| 161 | + weechat.prnt("", "-- %sList of the file filters %s--------" % |
| 162 | + (weechat.color("yellow"), weechat.color("chat"))) |
| 163 | + i = 0 |
| 164 | + for fltr in filters_h["filters"]: |
| 165 | + i += 1 |
| 166 | + weechat.prnt("", "%4s %s%s%s" % (str(i), weechat.color("green"), |
| 167 | + fltr, (weechat.color("chat")))) |
| 168 | + weechat.prnt("", "------------------------------------") |
| 169 | + return weechat.WEECHAT_RC_OK |
| 170 | + |
| 171 | +def del_filter(fltr_id): |
| 172 | + fid = int(fltr_id) |
| 173 | + filters_h = file_read(FILE_FILTERS) |
| 174 | + if filters_h == {} or filters_h == {"filters": []}: |
| 175 | + weechat.prnt("", "%sThere are no added file filters." % |
| 176 | + (weechat.color("red"))) |
| 177 | + return weechat.WEECHAT_RC_OK |
| 178 | + filters = filters_h['filters'] |
| 179 | + try: |
| 180 | + fltr_data = filters[fid - 1] |
| 181 | + filters.pop(fid - 1) |
| 182 | + weechat.prnt("", "%s%s%s has been removed from the list." % |
| 183 | + (weechat.color("green"), fltr_data, weechat.color("chat"))) |
| 184 | + except IndexError: |
| 185 | + weechat.prnt("", "There is no filter ID %s%s%s in list to delete it." % |
| 186 | + (weechat.color("red"), str(fid), weechat.color("chat"))) |
| 187 | + file_write(FILE_FILTERS, {"filters": filters}) |
| 188 | + return weechat.WEECHAT_RC_OK |
| 189 | + |
| 190 | + |
| 191 | +# -------------------------------------- |
| 192 | +# handler for the hook for script commands |
| 193 | +def ongoing_hook(data, buffer, args): |
| 194 | + a = args.split(" ", 1) |
| 195 | + command = a[0] |
| 196 | + try: |
| 197 | + retval = weechat.WEECHAT_RC_OK |
| 198 | + if command == "stats": |
| 199 | + retval = stats() |
| 200 | + elif command == "channel": |
| 201 | + if len(a) == 1: |
| 202 | + retval = get_channel() |
| 203 | + else: |
| 204 | + retval = set_channel(a[1]) |
| 205 | + elif command == "add_bot": |
| 206 | + # weechat.prnt("", "%s" % a[1]) |
| 207 | + [bot_name, regex] = a[1].split(" ", 1) |
| 208 | + retval = add_bot(bot_name, regex) |
| 209 | + elif command == "list_bots": |
| 210 | + retval = list_bots() |
| 211 | + elif command == "del_bot": |
| 212 | + retval = del_bot(a[1]) |
| 213 | + elif command == "add_filter": |
| 214 | + retval = add_filter(a[1]) |
| 215 | + elif command == "list_filters": |
| 216 | + retval = list_filters() |
| 217 | + elif command == "del_filter": |
| 218 | + retval = del_filter(a[1]) |
| 219 | + except IndexError: |
| 220 | + retval = weechat.WEECHAT_RC_ERROR |
| 221 | + return retval |
| 222 | + |
| 223 | + |
| 224 | +# -------------------------------------- |
| 225 | +# handle for the hook for parsing channel messages |
| 226 | +def parse_messages(data, signal, signal_data): |
| 227 | + srv = signal.split(',', 2)[0] |
| 228 | + msghash = weechat.info_get_hashtable("irc_message_parse", |
| 229 | + {"message": signal_data}) |
| 230 | + if msghash['channel'].lower() == weechat.config_get_plugin("channel"): |
| 231 | + bots = file_read(FILE_BOTS) |
| 232 | + if msghash['nick'] in bots: |
| 233 | + regex = bots[msghash['nick']] |
| 234 | + filters = file_read(FILE_FILTERS)['filters'] |
| 235 | + for fltr in filters: |
| 236 | + if re.search(fltr, msghash['arguments']): |
| 237 | + g = re.search(regex, msghash['arguments']) |
| 238 | + if g: |
| 239 | + file_id = g.group(1) |
| 240 | + weechat.command("", "/msg -server %s %s xdcc send %s" % |
| 241 | + (srv, msghash['nick'], file_id)) |
| 242 | + break |
| 243 | + # weechat.prnt("", "%s" % signal) |
| 244 | + return weechat.WEECHAT_RC_OK |
| 245 | + |
| 246 | + |
| 247 | +# -------------------------------------- |
| 248 | +# register the hook for script commands |
| 249 | +weechat.hook_command(SCRIPT_COMMAND, SCRIPT_DESC, SCRIPT_CMDS, SCRIPT_HELP, |
| 250 | + "channel %(channel_name)" |
| 251 | + " || add_bot %(bot_name) %(regex)" |
| 252 | + " || list_bots" |
| 253 | + " || del_bot %(bot_name)" |
| 254 | + " || add_filter %(filter)" |
| 255 | + " || list_filters" |
| 256 | + " || del_filter %(filter_id)", |
| 257 | + "ongoing_hook", "") |
| 258 | + |
| 259 | +# register the hooks for message parser |
| 260 | +weechat.hook_signal("*,irc_in2_privmsg", "parse_messages", "") |
| 261 | + |
| 262 | + |
0 commit comments