[f3af614] | 1 | #!/usr/bin/python |
---|
| 2 | |
---|
| 3 | import sys |
---|
| 4 | import bjsonrpc |
---|
| 5 | from bjsonrpc.handlers import BaseHandler |
---|
| 6 | |
---|
[e9face7] | 7 | import operator |
---|
[f3af614] | 8 | import random |
---|
| 9 | import re |
---|
| 10 | import socket |
---|
| 11 | import time |
---|
| 12 | |
---|
[e9face7] | 13 | import requests |
---|
[2533d10] | 14 | |
---|
[f3af614] | 15 | # List of functions an IM plugin can export. This library will indicate to |
---|
| 16 | # BitlBee which functions are actually implemented so omitted features |
---|
| 17 | # will be disabled, but note that some/many functions are simply mandatory. |
---|
[f8feb8a] | 18 | # (Currently login/-out, buddy_msg.) |
---|
[f3af614] | 19 | SUPPORTED_FUNCTIONS = [ |
---|
| 20 | 'login', 'keepalive', 'logout', 'buddy_msg', 'set_away', |
---|
| 21 | 'send_typing', 'add_buddy', 'remove_buddy', 'add_permit', |
---|
| 22 | 'add_deny', 'rem_permit', 'rem_deny', 'get_info', 'chat_invite', |
---|
| 23 | 'chat_kick', 'chat_leave', 'chat_msg', 'chat_with', 'chat_join', |
---|
| 24 | 'chat_topic' |
---|
| 25 | ] |
---|
| 26 | |
---|
| 27 | class RpcForwarder(object): |
---|
| 28 | """Tiny object that forwards RPCs from local Python code to BitlBee |
---|
| 29 | with a marginally nicer syntax. This layer could eventually be |
---|
| 30 | used to add basic parameter checking though I don't think that should |
---|
| 31 | be done here.""" |
---|
| 32 | |
---|
| 33 | def __init__(self, methods, target): |
---|
| 34 | for m in methods: |
---|
| 35 | # imc(b)_ prefix is not useful here, chop it. |
---|
| 36 | # (Maybe do this in BitlBee already as well.) |
---|
| 37 | newname = re.sub("^imcb?_", "", m) |
---|
| 38 | self.__setattr__(newname, target.__getattr__(m)) |
---|
| 39 | |
---|
| 40 | class BitlBeeIMPlugin(BaseHandler): |
---|
[c5a7b8d] | 41 | # Protocol name to be used in the BitlBee CLI, etc. |
---|
[f3af614] | 42 | NAME = "rpc-test" |
---|
[c5a7b8d] | 43 | |
---|
| 44 | # See account.h (TODO: Add constants.) |
---|
[e9face7] | 45 | ACCOUNT_FLAGS = 0 |
---|
[c5a7b8d] | 46 | |
---|
| 47 | # Supported away states. If your protocol supports a specific set of |
---|
| 48 | # away states, put them in a list in this variable. |
---|
[2533d10] | 49 | AWAY_STATES = None |
---|
[f3af614] | 50 | |
---|
| 51 | # Filled in during initialisation: |
---|
| 52 | # Version code in hex. So if you need to do comparisions, for example |
---|
| 53 | # check "self.bitlbee_version >= 0x030202" for versions 3.2.2+ |
---|
| 54 | bitlbee_version = None |
---|
| 55 | # Full version string |
---|
| 56 | bitlbee_version_str = None |
---|
[c5a7b8d] | 57 | # Will become an RpcForwarder object to call into BitlBee |
---|
| 58 | bee = None |
---|
[f3af614] | 59 | |
---|
[e9face7] | 60 | BASE_URL = "https://newsblur.com" |
---|
| 61 | |
---|
[f3af614] | 62 | @classmethod |
---|
| 63 | def _factory(cls, *args, **kwargs): |
---|
| 64 | def handler_factory(connection): |
---|
| 65 | handler = cls(connection, *args, **kwargs) |
---|
| 66 | return handler |
---|
| 67 | return handler_factory |
---|
| 68 | |
---|
| 69 | #def __init__(self, connection, *args, **kwargs): |
---|
| 70 | # BaseHandler.__init__(self,connection) |
---|
| 71 | |
---|
[e9face7] | 72 | def url(self, path): |
---|
| 73 | return (self.BASE_URL + path) |
---|
| 74 | |
---|
[f3af614] | 75 | def init(self, bee): |
---|
| 76 | self.bee = RpcForwarder(bee["method_list"], self._conn.call) |
---|
| 77 | self.bitlbee_version = bee["version"] |
---|
| 78 | self.bitlbee_version_str = bee["version_str"] |
---|
[2533d10] | 79 | |
---|
[f3af614] | 80 | # TODO: See how to call into the module here. |
---|
| 81 | return { |
---|
| 82 | "name": self.NAME, |
---|
| 83 | "method_list": list(set(dir(self)) & set(SUPPORTED_FUNCTIONS)), |
---|
[c5a7b8d] | 84 | "account_flags": self.ACCOUNT_FLAGS, |
---|
| 85 | "away_state_list": self.AWAY_STATES, |
---|
[f3af614] | 86 | "settings": { |
---|
| 87 | "oauth": { |
---|
| 88 | "default": "off", |
---|
[f15553d] | 89 | "type": "bool", |
---|
[f3af614] | 90 | }, |
---|
| 91 | "test": { |
---|
| 92 | "default": "123", |
---|
[f15553d] | 93 | "type": "int", |
---|
[f3af614] | 94 | }, |
---|
[f15553d] | 95 | "stringetje": { |
---|
| 96 | "default": "testje", |
---|
| 97 | "flags": 0x04, |
---|
| 98 | } |
---|
[f3af614] | 99 | }, |
---|
| 100 | } |
---|
| 101 | |
---|
| 102 | def login(self, account): |
---|
[e9face7] | 103 | self.ua = requests.Session() |
---|
| 104 | creds = {"username": account["user"], "password": account["pass"]} |
---|
| 105 | r = self.ua.post(self.url("/api/login"), creds) |
---|
| 106 | if r.status_code != 200: |
---|
| 107 | self.bee.error("HTTP error %d" % r.status_code) |
---|
| 108 | self.bee.logout(True) |
---|
| 109 | elif r.json()["errors"]: |
---|
| 110 | self.bee.error("Authentication error") |
---|
| 111 | self.bee.logout(False) |
---|
| 112 | else: |
---|
| 113 | self.bee.add_buddy("rss", None) |
---|
| 114 | self.bee.connected() |
---|
| 115 | self.seen_hashes = set() |
---|
| 116 | self.keepalive() |
---|
[f3af614] | 117 | |
---|
| 118 | def logout(self): |
---|
| 119 | self.bee.error("Ok bye!") |
---|
| 120 | |
---|
[2533d10] | 121 | def buddy_msg(self, handle, msg, flags): |
---|
| 122 | feed = self.feeds[handle] |
---|
| 123 | cmd = re.split(r"\s+", msg) |
---|
[f15553d] | 124 | |
---|
| 125 | def set_set(self, setting, value): |
---|
| 126 | print "Setting %s changed to %r" % (setting, value) |
---|
[e9face7] | 127 | |
---|
| 128 | # BitlBee will call us here every minute which is actually a neat way |
---|
| 129 | # to get periodic work (like RSS polling) scheduled. :-D |
---|
| 130 | def keepalive(self): |
---|
| 131 | r = self.ua.post( |
---|
| 132 | self.url("/reader/unread_story_hashes"), |
---|
| 133 | {"include_timestamps": True}) |
---|
| 134 | if r.status_code != 200: |
---|
| 135 | self.bee.error("HTTP error %d" % r.status_code) |
---|
| 136 | return |
---|
| 137 | |
---|
| 138 | # Throw all unread-post hashes in a long list and sort it by posting time. |
---|
| 139 | #feed_hashes = r.json()["unread_feed_story_hashes"] |
---|
| 140 | wtf = r.json() |
---|
| 141 | feed_hashes = wtf["unread_feed_story_hashes"] |
---|
| 142 | all_hashes = [] |
---|
| 143 | for feed, hashes in feed_hashes.iteritems(): |
---|
| 144 | all_hashes += [tuple(h) for h in hashes] |
---|
| 145 | all_hashes.sort(key=operator.itemgetter(1)) |
---|
| 146 | |
---|
| 147 | # Look at the most recent 20, grab the ones we haven't shown yet. |
---|
| 148 | req_hashes = [] |
---|
| 149 | for hash, _ in all_hashes[-20:]: |
---|
| 150 | if hash not in self.seen_hashes: |
---|
| 151 | req_hashes.append(hash) |
---|
| 152 | |
---|
| 153 | if not req_hashes: |
---|
| 154 | return |
---|
| 155 | print req_hashes |
---|
| 156 | |
---|
| 157 | # Grab post details. |
---|
| 158 | r = self.ua.post(self.url("/reader/river_stories"), {"h": req_hashes}) |
---|
| 159 | if r.status_code != 200: |
---|
| 160 | self.bee.error("HTTP error %d" % r.status_code) |
---|
| 161 | return |
---|
| 162 | |
---|
| 163 | # Response is not in the order we requested. :-( |
---|
| 164 | wtf = r.json() |
---|
| 165 | stories = {} |
---|
| 166 | for s in wtf["stories"]: |
---|
| 167 | stories[s["story_hash"]] = s |
---|
| 168 | |
---|
| 169 | for s in (stories[hash] for hash in req_hashes): |
---|
| 170 | line = "%(story_title)s <%(story_permalink)s>" % s |
---|
| 171 | ts = int(s.get("story_timestamp", "0")) |
---|
| 172 | self.bee.buddy_msg("rss", line, 0, ts) |
---|
| 173 | self.seen_hashes.add(s["story_hash"]) |
---|
| 174 | print s["story_hash"] |
---|
| 175 | |
---|
[f3af614] | 176 | |
---|
| 177 | def RunPlugin(plugin, debug=True): |
---|
| 178 | sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) |
---|
| 179 | sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) |
---|
| 180 | sock.bind("/tmp/rpcplugins/test2") |
---|
| 181 | sock.listen(3) |
---|
| 182 | |
---|
| 183 | srv = bjsonrpc.server.Server(sock, plugin._factory()) |
---|
| 184 | |
---|
| 185 | srv.debug_socket(debug) |
---|
| 186 | srv.serve() |
---|
| 187 | |
---|
| 188 | RunPlugin(BitlBeeIMPlugin) |
---|